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"));
}
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
}
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);
}
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);
}
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);
}
Aggregations