Search in sources :

Example 21 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DescrBuilderTest method testConditionalBranch.

@Test
public void testConditionalBranch() {
    String expected = "packageorg.drools.compiler\n" + "rule \"test\"\n" + "when\n" + "    Cheese( type == \"stilton\" )  \n" + "    if ( price < 10 ) do[c1] \n" + "    Cheese( type == \"cheddar\" )  \n" + "then\n" + "// do something\n" + "then[c1]\n" + "// do something else\n" + "end";
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.compiler").newRule().name("test").lhs().pattern("Cheese").constraint("type == \"stilton\"").end().conditionalBranch().condition().constraint("price < 10").end().consequence().name("c1").end().end().pattern("Cheese").constraint("type == \"cheddar\"").end().end().rhs("// do something").namedRhs("c1", "// do something else").end().getDescr();
    assertEquals(1, pkg.getRules().size());
    String drl = new DrlDumper().dump(pkg);
    Assertions.assertThat(expected).isEqualToIgnoringWhitespace(drl);
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = createKnowledgeSession(kbase);
    Cheese stilton = new Cheese("stilton", 5);
    Cheese cheddar = new Cheese("cheddar", 7);
    Cheese brie = new Cheese("brie", 5);
    ksession.insert(stilton);
    ksession.insert(cheddar);
    ksession.insert(brie);
    int rules = ksession.fireAllRules();
    assertEquals(2, rules);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 22 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DescrBuilderTest method testNamedConsequence.

@Test
public void testNamedConsequence() {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.compiler").newRule().name("test").lhs().pattern("Cheese").constraint("type == \"stilton\"").end().namedConsequence().name("c1").end().pattern("Cheese").constraint("type == \"cheddar\"").end().end().rhs("// do something").namedRhs("c1", "// do something else").end().getDescr();
    assertEquals(1, pkg.getRules().size());
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = createKnowledgeSession(kbase);
    Cheese stilton = new Cheese("stilton", 5);
    Cheese cheddar = new Cheese("cheddar", 7);
    Cheese brie = new Cheese("brie", 5);
    ksession.insert(stilton);
    ksession.insert(cheddar);
    ksession.insert(brie);
    int rules = ksession.fireAllRules();
    assertEquals(2, rules);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 23 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DescrBuilderTest method testRuleRHSComment.

@Test
public void testRuleRHSComment() throws InstantiationException, IllegalAccessException {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.compiler").newRule().name("r1").lhs().pattern("StockTick").constraint("company == \"RHT\"").end().end().rhs("// some comment").end().getDescr();
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new StockTick(1, "RHT", 80, 1));
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : StockTick(org.drools.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 24 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DescrBuilderTest method testRuleRHSOptional.

@Test
public void testRuleRHSOptional() throws InstantiationException, IllegalAccessException {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.compiler").newRule().name("r1").lhs().pattern("StockTick").constraint("company == \"RHT\"").end().end().end().getDescr();
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new StockTick(1, "RHT", 80, 1));
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : StockTick(org.drools.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 25 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DescrBuilderTest method testPackageAttributes.

@Test
public void testPackageAttributes() {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools").attribute("dialect").value("mvel").end().attribute("salience", "10").attribute("lock-on-active", "true", AttributeDescr.Type.BOOLEAN).getDescr();
    assertEquals("org.drools", pkg.getName());
    assertEquals(3, pkg.getAttributes().size());
    assertEquals("mvel", pkg.getAttribute("dialect").getValue());
    assertEquals("10", pkg.getAttribute("salience").getValue());
    assertEquals("true", pkg.getAttribute("lock-on-active").getValue());
    assertEquals(AttributeDescr.Type.BOOLEAN, pkg.getAttribute("lock-on-active").getType());
    assertNull(pkg.getAttribute("no-loop"));
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools", kpkg.getName());
}
Also used : KiePackage(org.kie.api.definition.KiePackage) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Aggregations

KiePackage (org.kie.api.definition.KiePackage)124 Test (org.junit.Test)93 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)80 KieSession (org.kie.api.runtime.KieSession)70 ArrayList (java.util.ArrayList)44 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)39 List (java.util.List)25 Cheese (org.drools.compiler.Cheese)22 Person (org.drools.compiler.Person)18 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)17 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)11 EntryPoint (org.kie.api.runtime.rule.EntryPoint)10 Resource (org.kie.api.io.Resource)9 StringReader (java.io.StringReader)8 HashMap (java.util.HashMap)8 FactHandle (org.kie.api.runtime.rule.FactHandle)8 Map (java.util.Map)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 KieBase (org.kie.api.KieBase)7