private KnowledgeBase buildKnowledgeBase(String... rules) { String str = ""; str += "package org.kie \n"; str += "import " + A.class.getCanonicalName() + "\n"; str += "import " + B.class.getCanonicalName() + "\n"; str += "import " + C.class.getCanonicalName() + "\n"; str += "global java.util.List list \n"; int i = 0; for (String lhs : rules) { str += "rule rule" + (i++) + " when \n"; str += lhs; str += "then \n"; str += "end \n"; } KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors()); KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); kconf.setOption(LRUnlinkingOption.ENABLED); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); return kbase; }
@Test public void testUpdateWithNonEffectiveActivations() throws Exception { // JBRULES-3604 String str = "package inheritance\n" + "\n" + "import org.drools.Address\n" + "\n" + "rule \"Parent\"\n" + " enabled false\n" + " when \n" + " $a : Address(suburb == \"xyz\")\n" + " then \n" + " System.out.println( $a ); \n" + "end \n" + "rule \"Child\" extends \"Parent\" \n" + " when \n" + " $b : Address( this == $a, street == \"123\")\n" + " then \n" + " System.out.println( $b ); \n" + "end"; KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(); builder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); if (builder.hasErrors()) { throw new RuntimeException(builder.getErrors().toString()); } KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); kconf.setOption(LRUnlinkingOption.ENABLED); KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(kconf); knowledgeBase.addKnowledgePackages(builder.getKnowledgePackages()); StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession(); Address address = new Address(); address.setSuburb("xyz"); org.kie.runtime.rule.FactHandle addressHandle = ksession.insert(address); int rulesFired = ksession.fireAllRules(); assertEquals(0, rulesFired); address.setStreet("123"); ksession.update(addressHandle, address); rulesFired = ksession.fireAllRules(); System.out.println(rulesFired); assertEquals(1, rulesFired); ksession.dispose(); }
private static StatefulKnowledgeSession getSession(String drl) { KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kBuilder.add(new ClassPathResource("org/drools/examples/traits/" + drl), ResourceType.DRL); if (kBuilder.hasErrors()) { System.err.println(kBuilder.getErrors().toString()); } KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase(); kBase.addKnowledgePackages(kBuilder.getKnowledgePackages()); return kBase.newStatefulKnowledgeSession(); }
@Test public void testClassNotFoundAfterDeserialization() throws Exception { // JBRULES-3670 String drl = "package completely.other.deal;\n" + "\n" + "declare Person\n" + " firstName : String\n" + " lastName : String\n" + "end\n" + "\n" + "rule \"now use it B\"\n" + " when\n" + " Person( $christianName, $surname; )\n" + " then\n" + " insert( new Person( $christianName, null ) );\n" + "end"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL); if (kbuilder.hasErrors()) { throw new RuntimeException("" + kbuilder.getErrors()); } FileManager fileManager = new FileManager().setUp(); try { File root = fileManager.getRootDirectory(); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File(root, "test.drl.compiled"))); out.writeObject(kbuilder.getKnowledgePackages()); out.close(); KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); kconf.setOption(LRUnlinkingOption.ENABLED); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf); ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(root, "test.drl.compiled"))); kbase.addKnowledgePackages((Collection<KnowledgePackage>) in.readObject()); in.close(); } finally { fileManager.tearDown(); } }
public static void main(final String[] args) { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "WorkItemConsequence1.drl", WorkItemConsequenceExample1.class), ResourceType.DRL); final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); if (kbuilder.hasErrors()) { throw new RuntimeException("Compilation error.\n" + kbuilder.getErrors().toString()); } final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); ksession .getWorkItemManager() .registerWorkItemHandler("EmailWorkItemHandler", new EmailWorkItemHandler()); // KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, // "log/WorkItemConsequence.log"); // Brains does not cause concern PersonLocation brains = new PersonLocation("Brains", 5); ksession.insert(brains); ksession.fireAllRules(); // Gargamel is too far away to care PersonLocation gargamel = new PersonLocation("Gargamel", 10); FactHandle gargamelFactHandle = ksession.insert(gargamel); ksession.fireAllRules(); // Uh oh, Gargamel moves closer gargamel.setDistance(5); ksession.update(gargamelFactHandle, gargamel); ksession.fireAllRules(); // logger.close(); ksession.dispose(); // Stateful rule session must always be disposed when finished }
@Test public void testMVELForLoop() throws Exception { // JBRULES-3717 String str = "rule demo\n" + "dialect \"mvel\"\n" + "when\n" + "then\n" + " for ( int i = 1; i <= 3; i++ ) {\n" + " insert( \"foo\" + i );\n" + " }\n" + "end"; KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(); builder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); if (builder.hasErrors()) { throw new RuntimeException(builder.getErrors().toString()); } }
@Test public void testDynamicAddRule() { // DROOLS-17 String str = "import org.drools.integrationtests.MiscTest2.A\n" + "rule r1 when\n" + " $a : A( f1 == 1 )\n" + "then\n" + "end\n" + "\n" + "rule r2 when\n" + " $a : A( f2 == 1 )\n" + "then\n" + "end\n" + "\n" + "rule r3 when\n" + " $a : A( f3 == 1 )" + "then\n" + "end"; String str2 = "import org.drools.integrationtests.MiscTest2.A\n" + "rule r4 when\n" + " $a : A( f2 == 1, f4 == 1 )" + "then\n" + "end"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); FactHandle fh = ksession.insert(new A(1, 1, 1, 1)); ksession.fireAllRules(); kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL); if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); ksession.fireAllRules(); // this second insert forces the regeneration of the otnIds ksession.insert(new A(2, 2, 2, 2)); LeftTuple leftTuple = ((DefaultFactHandle) fh).getFirstLeftTuple(); ObjectTypeNode.Id letTupleOtnId = leftTuple.getLeftTupleSink().getLeftInputOtnId(); leftTuple = leftTuple.getLeftParentNext(); while (leftTuple != null) { assertTrue(letTupleOtnId.before(leftTuple.getLeftTupleSink().getLeftInputOtnId())); letTupleOtnId = leftTuple.getLeftTupleSink().getLeftInputOtnId(); leftTuple = leftTuple.getLeftParentNext(); } }