Search in sources :

Example 61 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class AsmGeneratorTest method testOr.

@Test
public void testOr() {
    String s = "package org.kie.test\n" + "import org.drools.compiler.Person\n" + "import org.drools.compiler.Cheese\n" + "global java.util.List list\n" + "rule r1 when\n" + "    s1 : String( this == 's1' )\n" + "    Cheese( $type : type == \"stilton\", $price : price ) or\n" + "    ( Cheese( $type : type == \"brie\", $price : price ) and Person( name == \"bob\", likes == $type ) )\n" + "then\n" + "    // *2 are missed out on purpose to make sure we only resolved required declarations\n" + "    list.add( \"test3\"+$type +\":\"+ new Integer( $price ) ); \n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(s.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert("s1");
    ksession.insert(new Person("bob", "brie"));
    ksession.insert(new Cheese("stilton"));
    ksession.insert(new Cheese("brie"));
    ksession.insert(new Person("s2"));
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.contains("test3brie:0"));
    assertTrue(list.contains("test3stilton:0"));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 62 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class DescrBuilderTest method testDeclare.

@Test
public void testDeclare() throws InstantiationException, IllegalAccessException {
    PackageDescr pkg = DescrFactory.newPackage().name("org.beans").newDeclare().type().name("StockTick").newAnnotation("role").value("event").end().newAnnotation("author").value("bob").end().newField("symbol").type("String").end().newField("price").type("double").end().end().newDeclare().enumerative().name("Planets").newAnnotation("kind").value("enum").end().newEnumLiteral("earth").constructorArg("6.0").constructorArg("10.0").end().newEnumLiteral("jupiter").constructorArg("44.0").constructorArg("50.0").end().newField("mass").type("double").end().newField("radius").type("double").end().end().getDescr();
    assertEquals(1, pkg.getTypeDeclarations().size());
    assertEquals(1, pkg.getEnumDeclarations().size());
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.beans", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    FactType stType = kbase.getFactType("org.beans", "StockTick");
    assertNotNull(stType);
    Object st = stType.newInstance();
    stType.set(st, "symbol", "RHT");
    stType.set(st, "price", 10);
    assertEquals("RHT", stType.get(st, "symbol"));
// stType.getAnnotation("author"); TODO: implement support for this
}
Also used : KiePackage(org.kie.api.definition.KiePackage) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 63 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase 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 64 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase 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 65 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase 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)

Aggregations

InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)394 Test (org.junit.Test)321 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)231 KieSession (org.kie.api.runtime.KieSession)221 ArrayList (java.util.ArrayList)173 List (java.util.List)108 KiePackage (org.kie.api.definition.KiePackage)80 FactHandle (org.kie.api.runtime.rule.FactHandle)46 InternalFactHandle (org.drools.core.common.InternalFactHandle)43 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)42 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)41 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)41 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)40 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)39 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)38 Person (org.drools.compiler.Person)37 Cheese (org.drools.compiler.Cheese)36 FactType (org.kie.api.definition.type.FactType)27 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)25 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)24