use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class DescrBuilderTest method testRule.
@Test
public void testRule() throws InstantiationException, IllegalAccessException {
PackageDescrBuilder packBuilder = DescrFactory.newPackage().name("org.drools.mvel.compiler").newRule().name("r1").lhs().and().or().pattern("StockTick").constraint("price > 100").end().pattern("StockTick").constraint("price < 10").end().end().pattern("StockTick").constraint("company == \"RHT\"").end().end().end().rhs(" System.out.println(\"foo\");\n").end();
PackageDescr pkg = packBuilder.getDescr();
String drl = new DrlDumper().dump(packBuilder.getDescr());
System.out.println(drl);
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(0, rules);
ksession = kbase.newKieSession();
ksession.insert(new StockTick(2, "RHT", 150, 1));
rules = ksession.fireAllRules();
assertEquals(1, rules);
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class DslTest method testEmptyDSL.
@Test
@Ignore("antlr cannot parse correctly if the file ends with a comment without a further line break")
public void testEmptyDSL() throws Exception {
// FIXME etirelli / mic_hat not sure what to do with this?
// gives antlr <EOF> error
final String DSL = "# This is an empty dsl file.";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_expander.dsl", getClass()), ResourceType.DSL);
kbuilder.add(ResourceFactory.newReaderResource(new StringReader(DSL)), ResourceType.DSLR);
// trying to expand Cheese() pattern
assertFalse(kbuilder.hasErrors());
// Check errors
final String err = kbuilder.getErrors().toString();
assertEquals("", err);
assertEquals(0, kbuilder.getErrors().size());
// the compiled package
Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
assertEquals(0, pkgs.size());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
kbase = SerializationHelper.serializeObject(kbase);
KieSession session = kbase.newKieSession();
pkgs = SerializationHelper.serializeObject(pkgs);
assertNull(pkgs);
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class EnumTest method testQueryEnum.
@Test
public void testQueryEnum() {
final String str = "package org.kie.test;\n" + "\n" + "declare enum Ennumm\n" + " ONE, TWO;\n" + "end\n" + "\n" + "declare Bean\n" + " fld : Ennumm\n" + "end\n" + "\n" + "query seeWhat( Ennumm $e, Bean $b )\n" + " $b := Bean( $e == Ennumm.ONE )\n" + "end\n" + "\n" + "rule rool\n" + "when\n" + "then\n" + " insert( new Bean( Ennumm.ONE ) );\n" + "end\n" + "\n" + "\n" + "rule rool2\n" + "when\n" + " seeWhat( $ex, $bx ; )\n" + "then\n" + " System.out.println( $bx );\n" + "end";
final String str2 = "package org.drools.mvel.compiler.test2; \n" + "" + "declare Naeb \n" + " fld : String \n" + "end \n";
KieBuilder kieBuilder = KieUtil.getKieBuilderFromDrls(kieBaseTestConfiguration, false, str);
List<Message> errors = kieBuilder.getResults().getMessages(Message.Level.ERROR);
assertTrue(errors.toString(), errors.isEmpty());
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getDefaultKieBaseFromKieBuilder(kieBuilder);
Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromKieModuleFromDrl("tmp", kieBaseTestConfiguration, str2).getKiePackages();
kbase.addPackages(kpkgs);
final KieSession ksession = kbase.newKieSession();
ksession.fireAllRules();
ksession.dispose();
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class KnowledgeBuilderTest method testDifferentPackages.
@Test
public void testDifferentPackages() throws Exception {
String rule = "package org.drools.mvel.compiler.test.rule\n" + "import org.drools.mvel.compiler.testA.FactA\n" + "import org.drools.mvel.compiler.testB.FactB\n" + "rule R1 when\n" + " $fieldA : FactA( $fieldB : fieldB, bigint == 1 )\n" + " FactB( this == $fieldB, fieldA == $fieldA )\n" + "then\n" + " list.add(\"OK\");" + "end";
String declarationA = "package org.drools.mvel.compiler.testA\n" + "import org.drools.mvel.compiler.testB.FactB\n" + "import java.math.BigInteger\n" + "declare FactA\n" + " fieldB: FactB\n" + " bigint: BigInteger\n" + "end\n";
String declarationB = "package org.drools.mvel.compiler.testB\n" + "import org.drools.mvel.compiler.testA.FactA\n" + "global java.util.List list\n" + "declare FactB\n" + " fieldA: FactA\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.batch().type(ResourceType.DRL).add(ResourceFactory.newByteArrayResource(rule.getBytes())).add(ResourceFactory.newByteArrayResource(declarationA.getBytes())).add(ResourceFactory.newByteArrayResource(declarationB.getBytes())).build();
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
String declarationC = "package org.drools.mvel.compiler.testA\n" + "declare FactC\n" + " field : UnknownClass\n" + "end\n";
kbuilder.add(ResourceFactory.newByteArrayResource(declarationC.getBytes()), ResourceType.DRL);
assertTrue(kbuilder.hasErrors());
kbuilder.undo();
assertFalse(kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
FactType aType = kbase.getFactType("org.drools.mvel.compiler.testA", "FactA");
Object a = aType.newInstance();
FactType bType = kbase.getFactType("org.drools.mvel.compiler.testB", "FactB");
Object b = bType.newInstance();
aType.set(a, "fieldB", b);
aType.set(a, "bigint", new BigInteger("1"));
bType.set(b, "fieldA", a);
ksession.insert(a);
ksession.insert(b);
int rules = ksession.fireAllRules();
assertEquals(1, rules);
assertEquals("OK", list.get(0));
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class PathEndNodeTest method testSubNetworkSharing.
@Test
public void testSubNetworkSharing() throws Exception {
String str = "package org.test \n" + "rule xxx \n" + "when \n" + " $s : String()\n" + " not( Integer() and Long() )\n" + "then \n" + "end \n" + "rule yyy \n" + "when \n" + " $s : String()\n" + " not( Integer() and Long() )\n" + "then \n" + "end \n";
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
EntryPointNode epn = kbase.getRete().getEntryPointNode(EntryPointId.DEFAULT);
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Long.class));
BetaNode beta1 = (BetaNode) otn.getObjectSinkPropagator().getSinks()[0];
RightInputAdapterNode rian = (RightInputAdapterNode) beta1.getSinkPropagator().getSinks()[0];
BetaNode beta2 = (BetaNode) rian.getObjectSinkPropagator().getSinks()[0];
LeftTupleSink[] sinks = beta2.getSinkPropagator().getSinks();
RuleTerminalNode rtn1 = (RuleTerminalNode) sinks[0];
RuleTerminalNode rtn2 = (RuleTerminalNode) sinks[1];
assertEquals(3, rian.getPathEndNodes().length);
assertTrue(asList(rian.getPathEndNodes()).containsAll(asList(rtn1, rtn2, rian)));
kbase.removeRule("org.test", "xxx");
assertEquals(2, rian.getPathEndNodes().length);
RuleTerminalNode remainingRTN = rtn1.getRule().getName().equals("yyy") ? rtn1 : rtn2;
assertTrue(asList(rian.getPathEndNodes()).containsAll(asList(remainingRTN, rian)));
}
Aggregations