use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class DescrBuilderTest method testFunctions.
@Test
public void testFunctions() {
PackageDescr pkg = DescrFactory.newPackage().name("org.drools").newFunctionImport().target("java.lang.Math.max").end().newFunction().returnType("long").name("myMax").parameter("long", "v1").parameter("long", "v2").body("return max(v1, v2);").end().newRule().name("test").lhs().eval().constraint("myMax(5, 10) == 10").end().end().rhs("// do something").end().getDescr();
assertEquals(1, pkg.getFunctionImports().size());
assertEquals(1, pkg.getFunctions().size());
assertEquals(1, pkg.getRules().size());
KiePackage kpkg = compilePkgDescr(pkg);
assertEquals("org.drools", kpkg.getName());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Collections.singletonList(kpkg));
KieSession ksession = kbase.newKieSession();
int rules = ksession.fireAllRules();
assertEquals(1, rules);
}
use of org.drools.kiesession.rulebase.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.mvel.compiler").newRule().name("r1").lhs().pattern("StockTick").constraint("company == \"RHT\"").end().end().rhs("// some comment").end().getDescr();
KiePackage kpkg = compilePkgDescr(pkg);
assertEquals("org.drools.mvel.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.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class DescrBuilderTest method testDumperFromPkg.
@Test
public void testDumperFromPkg() {
// DROOLS-109
PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().and().or().pattern().id("$x", false).type("Integer").constraint("this > 10").end().pattern().id("$x", false).type("Integer").constraint("this < 20").end().end().pattern().type("Integer").constraint("this == $x").constraint("this == 42").end().end().end().rhs("").end().end().getDescr();
String drl = new DrlDumper().dump(pkg);
System.out.println(drl);
KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
System.err.println(knowledgeBuilder.getErrors());
assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
KieSession knowledgeSession = kbase.newKieSession();
KiePackage rebuiltPkg = knowledgeBuilder.getPackage("org.test");
org.kie.api.definition.rule.Rule rule = rebuiltPkg.getRules().iterator().next();
RuleImpl r = ((RuleImpl) rule);
assertEquals(2, r.getLhs().getChildren().size());
Iterator<RuleConditionElement> iter = r.getLhs().getChildren().iterator();
RuleConditionElement arg1 = iter.next();
assertTrue(arg1 instanceof GroupElement && ((GroupElement) arg1).getType() == GroupElement.Type.OR);
assertEquals(2, ((GroupElement) arg1).getChildren().size());
RuleConditionElement arg2 = iter.next();
assertTrue(arg2 instanceof Pattern);
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class DescrBuilderTest method testFromEntryPoint.
@Test
public void testFromEntryPoint() throws InstantiationException, IllegalAccessException {
PackageDescr pkg = DescrFactory.newPackage().name("org.drools").newRule().name("from rule").lhs().pattern("String").id("s", false).from().entryPoint("EventStream").end().end().rhs("//System.out.println(s);").end().getDescr();
KiePackage kpkg = compilePkgDescr(pkg, "org.drools");
assertEquals("org.drools", kpkg.getName());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Collections.singletonList(kpkg));
KieSession ksession = kbase.newKieSession();
EntryPoint ep = ksession.getEntryPoint("EventStream");
ep.insert("Hello World!");
int rules = ksession.fireAllRules();
assertEquals(1, rules);
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class DescrBuilderTest method testTopLevelAccumulate.
@Test
public void testTopLevelAccumulate() throws InstantiationException, IllegalAccessException {
PackageDescr pkg = DescrFactory.newPackage().name("org.drools.mvel.compiler").newRule().name("r1").lhs().accumulate().source().pattern("StockTick").constraint("company == \"RHT\"").bind("$p", "price", false).end().end().function("sum", "$sum", false, "$p").function("count", "$cnt", false, "$p").end().end().rhs("// some comment").end().getDescr();
KiePackage kpkg = compilePkgDescr(pkg, "org.drools.mvel.compiler");
assertEquals("org.drools.mvel.compiler", kpkg.getName());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Collections.singletonList(kpkg));
KieSession ksession = kbase.newKieSession();
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
ksession.insert(new StockTick(1, "RHT", 80, 1));
ksession.insert(new StockTick(2, "RHT", 100, 10));
int rules = ksession.fireAllRules();
assertEquals(1, rules);
ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael).afterMatchFired(cap.capture());
assertThat(((Number) cap.getValue().getMatch().getDeclarationValue("$sum")).intValue(), is(180));
assertThat(((Number) cap.getValue().getMatch().getDeclarationValue("$cnt")).intValue(), is(2));
}
Aggregations