Search in sources :

Example 1 with KieResources

use of org.kie.api.io.KieResources in project drools by kiegroup.

the class GuidedDecisionTableTest method initKieSession.

private void initKieSession(String... resourceNames) {
    KieResources kieResources = KieServices.get().getResources();
    final Resource[] resources = Stream.of(resourceNames).map(resource -> kieResources.newClassPathResource(resource, GuidedDecisionTableTest.class)).toArray(Resource[]::new);
    final KieBase kBase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resources);
    kSession = kBase.newKieSession();
    rulesFired = new TrackingAgendaEventListener();
    kSession.addEventListener(rulesFired);
    rulesFired.clear();
}
Also used : Address(org.drools.testcoverage.common.model.Address) Collection(java.util.Collection) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) KieResources(org.kie.api.io.KieResources) FactHandle(org.kie.api.runtime.rule.FactHandle) Resource(org.kie.api.io.Resource) Stream(java.util.stream.Stream) Person(org.drools.testcoverage.common.model.Person) KieServices(org.kie.api.KieServices) Assertions(org.assertj.core.api.Assertions) KieBaseUtil(org.drools.testcoverage.common.util.KieBaseUtil) TestParametersUtil(org.drools.testcoverage.common.util.TestParametersUtil) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) KieResources(org.kie.api.io.KieResources)

Example 2 with KieResources

use of org.kie.api.io.KieResources in project drools by kiegroup.

the class RuleTemplateTest method testGuidedRuleTemplate.

@Test
public void testGuidedRuleTemplate() throws Exception {
    final String resourceName = "cheese.template";
    final KieResources kieResources = KieServices.get().getResources();
    final Resource resource = kieResources.newClassPathResource(resourceName, RuleTemplateTest.class);
    resource.setResourceType(ResourceType.TEMPLATE);
    final KieBase kBase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resource);
    final KieSession kSession = kBase.newKieSession();
    final Cheese cheese = new Cheese();
    cheese.setPrice(90);
    final Customer petr = new Customer(0, "Peter");
    final Customer john = new Customer(1, "John");
    kSession.insert(cheese);
    kSession.insert(petr);
    kSession.insert(john);
    Assertions.assertThat(kSession.fireAllRules()).as("One rule should be fired").isEqualTo(1);
    final Collection messages = kSession.getObjects(object -> object instanceof Message);
    Assertions.assertThat(messages).hasSize(1);
    Assertions.assertThat(messages).hasOnlyOneElementSatisfying(message -> ((Message) message).getMessage().compareTo("Peter satisfied"));
}
Also used : Message(org.drools.testcoverage.common.model.Message) Customer(org.drools.testcoverage.common.model.Customer) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) KieResources(org.kie.api.io.KieResources) Test(org.junit.Test)

Example 3 with KieResources

use of org.kie.api.io.KieResources in project drools by kiegroup.

the class TypeDeclarationTest method testCrossPackageDeclares.

@Test
public void testCrossPackageDeclares() {
    String pkg1 = "package org.drools.compiler.test1; " + "import org.drools.compiler.test2.GrandChild; " + "import org.drools.compiler.test2.Child; " + "import org.drools.compiler.test2.BarFuu; " + "declare FuBaz foo : String end " + "declare Parent " + "   unknown : BarFuu " + "end " + "declare GreatChild extends GrandChild " + "   father : Child " + "end ";
    String pkg2 = "package org.drools.compiler.test2; " + "import org.drools.compiler.test1.Parent; " + "import org.drools.compiler.test1.FuBaz; " + "declare BarFuu " + "   baz : FuBaz " + "end " + "declare Child extends Parent " + "end " + "declare GrandChild extends Child " + "   notknown : FuBaz " + "end ";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.generateAndWritePomXML(ks.newReleaseId("test", "foo", "1.0"));
    KieModuleModel km = ks.newKieModuleModel();
    km.newKieBaseModel("rules").addPackage("org.drools.compiler.test2").addPackage("org.drools.compiler.test1");
    kfs.writeKModuleXML(km.toXML());
    KieResources kr = ks.getResources();
    Resource r1 = kr.newByteArrayResource(pkg1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("org/drools/compiler/test1/p1.drl");
    Resource r2 = kr.newByteArrayResource(pkg2.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("org/drools/compiler/test2/p2.drl");
    kfs.write(r1);
    kfs.write(r2);
    KieBuilder builder = ks.newKieBuilder(kfs);
    builder.buildAll();
    assertEquals(Collections.emptyList(), builder.getResults().getMessages(Message.Level.ERROR));
    KieContainer kc = ks.newKieContainer(builder.getKieModule().getReleaseId());
    FactType ft = kc.getKieBase("rules").getFactType("org.drools.compiler.test2", "Child");
    assertNotNull(ft);
    assertNotNull(ft.getFactClass());
    assertEquals("org.drools.compiler.test1.Parent", ft.getFactClass().getSuperclass().getName());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieResources(org.kie.api.io.KieResources) KieContainer(org.kie.api.runtime.KieContainer) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 KieResources (org.kie.api.io.KieResources)3 Resource (org.kie.api.io.Resource)3 Collection (java.util.Collection)2 KieBase (org.kie.api.KieBase)2 KieServices (org.kie.api.KieServices)2 KieSession (org.kie.api.runtime.KieSession)2 Stream (java.util.stream.Stream)1 Assertions (org.assertj.core.api.Assertions)1 TrackingAgendaEventListener (org.drools.testcoverage.common.listener.TrackingAgendaEventListener)1 Address (org.drools.testcoverage.common.model.Address)1 Cheese (org.drools.testcoverage.common.model.Cheese)1 Customer (org.drools.testcoverage.common.model.Customer)1 Message (org.drools.testcoverage.common.model.Message)1 Person (org.drools.testcoverage.common.model.Person)1 KieBaseTestConfiguration (org.drools.testcoverage.common.util.KieBaseTestConfiguration)1 KieBaseUtil (org.drools.testcoverage.common.util.KieBaseUtil)1 TestParametersUtil (org.drools.testcoverage.common.util.TestParametersUtil)1 Before (org.junit.Before)1 RunWith (org.junit.runner.RunWith)1