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