public void execute(int repNr) { int i = 0; for (Bean bean : beans) { factHandles[i++] = ksession.insert(bean); } ksession.fireAllRules(); for (FactHandle factHandle : factHandles) { ksession.retract(factHandle); } ksession.fireAllRules(); }
@Test public void testDoubleNullSafe() { String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + " Person( address!.street!.length > 15 ) \n" + "then\n" + "end"; KnowledgeBase kbase = loadKnowledgeBaseFromString(str); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); ksession.insert(new Person("Mario", 38)); Person mark = new Person("Mark", 37); mark.setAddress(new Address("Main Street")); ksession.insert(mark); Person edson = new Person("Edson", 34); edson.setAddress(new Address(null)); ksession.insert(edson); Person alex = new Person("Alex", 34); alex.setAddress(new Address("The Main Very Big Street")); ksession.insert(alex); assertEquals(1, ksession.fireAllRules()); ksession.dispose(); }
@Test public void testNullSafeMemberOf() { // DROOLS-50 String str = "declare A\n" + " list : java.util.List\n" + "end\n" + "\n" + "rule Init when\n" + "then\n" + " insert( new A( java.util.Arrays.asList( \"test\" ) ) );" + " insert( \"test\" );" + "end\n" + "rule R when\n" + " $a : A()\n" + " $s : String( this memberOf $a!.list )\n" + "then\n" + "end"; KnowledgeBase kbase = loadKnowledgeBaseFromString(str); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); assertEquals(2, ksession.fireAllRules()); ksession.dispose(); }
@Test @SuppressWarnings("unchecked") public void test1() throws Exception { String rule = "package org.test;\n"; rule += "import org.jbpm.integrationtests.test.Person\n"; rule += "global java.util.List list\n"; rule += "rule \"Rule 1\"\n"; rule += " ruleflow-group \"hello\"\n"; rule += "when\n"; rule += " $p : Person( ) \n"; rule += "then\n"; rule += " list.add( $p );\n"; rule += "end"; String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"ruleflow\" id=\"org.test.ruleflow\" package-name=\"org.test\" >\n" + " <header>\n" + " </header>\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <ruleSet id=\"2\" name=\"Hello\" ruleFlowGroup=\"hello\" />\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\"/>\n" + " <connection from=\"2\" to=\"3\"/>\n" + " </connections>\n" + "</process>"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newReaderResource(new StringReader(rule)), ResourceType.DRL); kbuilder.add(ResourceFactory.newReaderResource(new StringReader(process)), ResourceType.DRF); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); List<Object> list = new ArrayList<Object>(); ksession.setGlobal("list", list); Person p = new Person("bobba fet", 32); ksession.insert(p); ksession.startProcess("org.test.ruleflow"); assertEquals(1, ksession.getProcessInstances().size()); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); assertEquals(1, ksession.getProcessInstances().size()); ksession.fireAllRules(); assertEquals(1, ((List<Object>) ksession.getGlobal("list")).size()); assertEquals(p, ((List<Object>) ksession.getGlobal("list")).get(0)); assertEquals(0, ksession.getProcessInstances().size()); }
@Test public void testNullSafeBinding() { String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + " Person( $streetName : address!.street ) \n" + "then\n" + "end"; KnowledgeBase kbase = loadKnowledgeBaseFromString(str); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); ksession.insert(new Person("Mario", 38)); Person mark = new Person("Mark", 37); mark.setAddress(new Address("Main Street")); ksession.insert(mark); Person edson = new Person("Edson", 34); edson.setAddress(new Address(null)); ksession.insert(edson); assertEquals(2, ksession.fireAllRules()); ksession.dispose(); }
// JBRULES-2526 @Test public void testOrWithVariableResolution2() throws Exception { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "test_OrCEFollowedByMultipleEval2.drl", FirstOrderLogicTest.class), ResourceType.DRL); assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors()); final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); final StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); final AgendaEventListener al = mock(AgendaEventListener.class); ksession.addEventListener(al); ksession.insert(new FactA("a")); ksession.insert(new FactB("b")); ksession.insert(new FactC("c")); ksession.fireAllRules(); verify(al, times(8)).afterMatchFired(any(AfterMatchFiredEvent.class)); }
@Test public void testNewConstructor() { String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $m : Person( address == new Address('s1')) \n" + "then \n" + " list.add('r1'); \n" + "end \n"; 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 = createKnowledgeSession(kbase); List list = new ArrayList(); ksession.setGlobal("list", list); Person p = new Person("yoda"); p.setAddress(new Address("s1")); ksession.insert(p); ksession.fireAllRules(); assertEquals("r1", list.get(0)); // Check it was built with MVELReturnValueExpression constraint List<ObjectTypeNode> nodes = ((InternalKnowledgeBase) kbase).getRete().getObjectTypeNodes(); ObjectTypeNode node = null; for (ObjectTypeNode n : nodes) { if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) { node = n; break; } } AlphaNode alphanode = (AlphaNode) node.getSinkPropagator().getSinks()[0]; AlphaNodeFieldConstraint constraint = alphanode.getConstraint(); if (constraint instanceof MvelConstraint) { assertTrue(((MvelConstraint) constraint).getFieldExtractor() instanceof ClassFieldReader); FieldValue r = ((MvelConstraint) constraint).getField(); assertEquals(p.getAddress(), r.getValue()); } }
@Test public void testPersistenceRuleSet() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(new ClassPathResource("RuleSetProcess.rf"), ResourceType.DRF); kbuilder.add(new ClassPathResource("RuleSetRules.drl"), ResourceType.DRL); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = InfinispanKnowledgeService.newStatefulKnowledgeSession(kbase, null, env); long id = ksession.getIdentifier(); ksession.insert(new ArrayList<Object>()); ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess"); ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNotNull(processInstance); ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ksession.fireAllRules(); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNull(processInstance); }
@Test public void testPackageImports() throws Exception { String str = ""; str += "package org.kie \n"; str += "dialect \"mvel\"\n"; str += "import org.acme.healthcare.* \n"; str += "import org.acme.insurance.* \n"; str += "import org.acme.sensors.SensorReading \n"; str += "rule rule1 \n"; str += " when \n"; str += " eval(true)\n"; str += " then \n"; str += " insert(new Claim()); // from org.acme.healthcare.* \n"; str += " insert(new Policy()); // from org.acme.insurance.* \n"; str += " insert(new SensorReading()); // from org.acme.sensor.SensorReading \n"; str += "end\n"; final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); if (kbuilder.hasErrors()) { throw new RuntimeException(kbuilder.getErrors().toString()); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); int result = ksession.fireAllRules(); assertEquals(1, result); Collection<? extends Object> insertedObjects = ksession.getObjects(); assertEquals(3, insertedObjects.size()); }
@Test public void testNestedEnum() { String str = "" + "package org.drools.compiler.test \n" + "import " + Triangle.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $t: Triangle(t == Triangle.Type.ACUTE) \n" + "then \n" + " list.add($t.getT()); \n" + "end \n"; 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 = createKnowledgeSession(kbase); List list = new ArrayList(); ksession.setGlobal("list", list); Triangle t = new Triangle(Triangle.Type.ACUTE); ksession.insert(t); ksession.fireAllRules(); assertEquals(Triangle.Type.ACUTE, list.get(0)); }
@Test public void testLocalTransactionPerStatement() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(ruleString.getBytes()), ResourceType.DRL); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = InfinispanKnowledgeService.newStatefulKnowledgeSession(kbase, null, env); List<?> list = new ArrayList<Object>(); ksession.setGlobal("list", list); ksession.insert(1); ksession.insert(2); ksession.insert(3); ksession.fireAllRules(); assertEquals(3, list.size()); }
@Test public void testIncrementOperator() throws Exception { String str = ""; str += "package org.kie \n"; str += "global java.util.List list \n"; str += "rule rule1 \n"; str += " dialect \"mvel\" \n"; str += "when \n"; str += " $I : Integer() \n"; str += "then \n"; str += " i = $I.intValue(); \n"; str += " i += 5; \n"; str += " list.add( i ); \n"; str += "end \n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); assertFalse(kbuilder.hasErrors()); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); List list = new ArrayList(); ksession.setGlobal("list", list); ksession.insert(5); ksession.fireAllRules(); assertEquals(1, list.size()); assertEquals(10, list.get(0)); }
@Test public void testFixedPattern() throws FileNotFoundException { DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration(); dtconf.setInputType(DecisionTableInputType.XLS); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource("fixedPattern.xls", getClass()), ResourceType.DTABLE, dtconf); if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); List<Long> list = new ArrayList<Long>(); ksession.setGlobal("list", list); ksession.insert(1L); ksession.insert(2); ksession.fireAllRules(); assertEquals(1, list.size()); assertEquals(1L, (long) list.get(0)); ksession.dispose(); }
public void execute(int repNr) { for (int i = 0; i < objectNr; i++) { ksession.insert(as[i]); ksession.insert(bs[i]); } ksession.fireAllRules(); }
@Test public void testNullSafeNullComparisonReverse() { // DROOLS-82 String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + " Person( \"Main Street\".equalsIgnoreCase(address!.street) )\n" + "then\n" + "end"; KnowledgeBase kbase = loadKnowledgeBaseFromString(str); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); ksession.insert(new Person("Mario", 38)); Person mark = new Person("Mark", 37); mark.setAddress(new Address("Main Street")); ksession.insert(mark); Person edson = new Person("Edson", 34); edson.setAddress(new Address(null)); ksession.insert(edson); assertEquals(1, ksession.fireAllRules()); ksession.dispose(); }
public void execute(Object object) { StatefulKnowledgeSession ksession = newWorkingMemory(); try { ksession.insert(object); ksession.fireAllRules(); } finally { dispose(ksession); } }
@Test public void testCollectAfterOrCE() throws Exception { // Set up facts final Cheesery bonFromage = new Cheesery(); bonFromage.addCheese(new Cheese("cheddar")); bonFromage.addCheese(new Cheese("cheddar")); // Test in memory compile of DRL KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource("test_OrCEFollowedByCollect.drl", getClass()), ResourceType.DRL); KnowledgeBuilderErrors errors = kbuilder.getErrors(); if (errors.size() > 0) { for (KnowledgeBuilderError error : errors) { logger.warn(error.toString()); } throw new IllegalArgumentException("Could not parse knowledge."); } assertFalse(kbuilder.hasErrors()); Collection<KnowledgePackage> knowledgePackages = kbuilder.getKnowledgePackages(); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(knowledgePackages); StatefulKnowledgeSession session = createKnowledgeSession(kbase); session.insert(bonFromage); int rules = session.fireAllRules(); assertEquals(2, rules); // Serialize and test again knowledgePackages = SerializationHelper.serializeObject(knowledgePackages); kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(knowledgePackages); session = createKnowledgeSession(kbase); session.insert(bonFromage); rules = session.fireAllRules(); assertEquals(2, rules); }
@Test public void testNegativeNumbers() throws Exception { KnowledgeBase kbase = readKnowledgeBase("/data/DT_WithNegativeNumbers.xls"); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); IntHolder i1 = new IntHolder(1); IntHolder i2 = new IntHolder(-1); ksession.insert(i1); ksession.insert(i2); ksession.fireAllRules(); }
@Test public void testOutOfMemory() throws Exception { KnowledgeBase kbase = loadKnowledgeBase("empty.drl"); for (int i = 0; i < 10000; i++) { StatefulKnowledgeSession session = createKnowledgeSession(kbase); WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger(session); session.fireAllRules(); session.dispose(); } }
@Test public void testForallSinglePattern2() throws Exception { final KnowledgeBase kbase = loadKnowledgeBase("test_ForallSinglePattern2.drl"); final StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); ksession.insert(new Triangle(3, 3, 3)); ksession.insert(new Triangle(3, 3, 3)); // no cheeses, so should fire int fired = ksession.fireAllRules(); assertEquals(1, fired); ksession.dispose(); }
@Test public void testForall2() throws Exception { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_Forall2.drl")), ResourceType.DRL); assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors()); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); final StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); final List<String> list = new ArrayList<String>(); ksession.setGlobal("results", list); final State state = new State("SP"); ksession.insert(state); final Person bob = new Person("Bob"); bob.setStatus(state.getState()); bob.setAlive(true); ksession.insert(bob); ksession.fireAllRules(); assertEquals(0, list.size()); final State qc = new State("QC"); ksession.insert(qc); final Person john = new Person("John"); john.setStatus(qc.getState()); john.setAlive(false); ksession.fireAllRules(); assertEquals(1, list.size()); }
@Test @Ignore("Added 30-APR-2011 -Rikkola-") public void testNestedEnumFromJar() { String str = "" + "package org.drools.compiler.test \n" + "import org.kie.examples.eventing.EventRequest \n" + "global java.util.List list \n" + "rule 'zaa'\n " + "when \n " + "request: EventRequest( status == EventRequest.Status.ACTIVE )\n " + "then \n " + "request.setStatus(EventRequest.Status.ACTIVE); \n " + "end"; JarInputStream jis = null; try { jis = new JarInputStream(this.getClass().getResourceAsStream("/eventing-example.jar")); } catch (IOException e) { fail("Failed to load the jar"); } MapBackedClassLoader loader = createClassLoader(jis); KnowledgeBuilderConfiguration knowledgeBuilderConfiguration = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(knowledgeBuilderConfiguration); kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } KieBaseConfiguration knowledgeBaseConfiguration = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, loader); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(knowledgeBaseConfiguration); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); List list = new ArrayList(); ksession.setGlobal("list", list); Triangle t = new Triangle(Triangle.Type.ACUTE); ksession.insert(t); ksession.fireAllRules(); assertEquals(Triangle.Type.ACUTE, list.get(0)); }
public List executeWithResults(Iterable objects, ObjectFilter filter) { List list = new ArrayList(); StatefulKnowledgeSession ksession = newWorkingMemory(); try { for (Object object : objects) { ksession.insert(object); } ksession.fireAllRules(); for (FactHandle fh : ksession.getFactHandles(filter)) { list.add(((InternalFactHandle) fh).getObject()); } } finally { dispose(ksession); } return list; }
@Test public void testExists3() throws Exception { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "test_Exists_JBRULES_2810.drl", FirstOrderLogicTest.class), ResourceType.DRL); assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors()); final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); final StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); WorkingMemoryConsoleLogger logger = new WorkingMemoryConsoleLogger(ksession); ksession.fireAllRules(); ksession.dispose(); }
@Test public void shouldFireAssertIfAllPresentFailedWithNull() { // Get Rule infomration for a Given Form val mapa = new LinkedHashMap<String, String>(); mapa.put("name", "myproject"); mapa.put("project_type", ""); mapa.put("start_date", ""); ruleObj.setData(mapa); kSession = kbase.newStatefulKnowledgeSession(); List<String> list = new ArrayList<String>(); Evaluator eval = (Evaluator) ruleObj; kSession.insert(eval); kSession.insert(list); int actualNumberOfRulesFired = kSession.fireAllRules(); assertEquals(list.size(), 0); }
@Test public void testNullSafeInnerConstraint() { String str = "declare Content\n" + " complexContent : Content\n" + " extension : Content\n" + "end\n" + "\n" + "declare Context\n" + " ctx : Content\n" + "end\n" + "\n" + "rule \"Complex Type Attribute\"\n" + "when\n" + " $con : Content()\n" + " Context( ctx == $con || == $con!.complexContent!.extension )\n" + "then\n" + " System.out.println( $con ); \n" + "end\n" + "\n" + "rule \"Init\"\n" + "when\n" + "then\n" + " Content ext = new Content();\n" + " Content complex = new Content( new Content( null, ext ), null );\n" + " Content complex2 = new Content( null, null );\n" + " Context ctx = new Context( ext );\n" + " Context ctx2 = new Context( complex2 );\n" + " insert( complex );\n" + " insert( complex2 );\n" + " insert( ctx );\n" + " insert( ctx2 );\n" + "end"; KnowledgeBase kbase = loadKnowledgeBaseFromString(str); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); assertEquals(3, ksession.fireAllRules()); ksession.dispose(); }
@Test public void testFromWithOr() throws Exception { KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(); builder.add( ResourceFactory.newInputStreamResource( getClass().getResourceAsStream("test_FromWithOr.drl")), ResourceType.DRL); if (builder.hasErrors()) { logger.info(builder.getErrors().toString()); } assertFalse(builder.hasErrors()); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(builder.getKnowledgePackages()); final StatefulKnowledgeSession session = createKnowledgeSession(kbase); final List<Address> results = new ArrayList<Address>(); session.setGlobal("results", results); Address a1 = new Address(); a1.setZipCode("12345"); Address a2 = new Address(); a2.setZipCode("54321"); Address a3 = new Address(); a3.setZipCode("99999"); Person p = new Person(); p.addAddress(a1); p.addAddress(a2); p.addAddress(a3); session.insert(p); session.fireAllRules(); assertEquals(2, results.size()); assertTrue(results.contains(a1)); assertTrue(results.contains(a2)); }
@Test public void testOrs() throws Exception { String str = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getName() + "\n" + "rule X\n" + " when\n" + " Message( message == 'test' )\n" + " Message( !fired ) or eval( !false )\n" + " then\n" + "end\n"; KnowledgeBase kbase = loadKnowledgeBaseFromString(str); StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); ksession.insert(new Message("test")); int rules = ksession.fireAllRules(); assertEquals(2, rules); ksession.dispose(); }
@Test public void testSetFocus() { String str = ""; str += "package org.drools.test\n"; str += "global java.util.List list\n"; str += "rule rule1\n"; str += "agenda-group \"badfocus\""; str += "when\n"; str += " Integer(intValue > 0)\n"; str += "then\n"; str += " list.add( 1 );\n"; str += "end\n"; str += "\n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = InfinispanKnowledgeService.newStatefulKnowledgeSession(kbase, null, env); List<?> list = new ArrayList<Object>(); ksession.setGlobal("list", list); ksession.insert(1); ksession.insert(2); ksession.insert(3); ksession.getAgenda().getAgendaGroup("badfocus").setFocus(); ksession.fireAllRules(); assertEquals(3, list.size()); }
public <T> T execute(Command<T> command) { StatefulKnowledgeSession ksession = newWorkingMemory(); FixedKnowledgeCommandContext context = new FixedKnowledgeCommandContext( new ContextImpl("ksession", null), null, null, ksession, new ExecutionResultImpl()); try { ((StatefulKnowledgeSessionImpl) ksession).startBatchExecution(new ExecutionResultImpl()); Object o = ((GenericCommand) command).execute(context); // did the user take control of fireAllRules, if not we will auto execute boolean autoFireAllRules = true; if (command instanceof FireAllRulesCommand) { autoFireAllRules = false; } else if (command instanceof BatchExecutionCommandImpl) { for (Command nestedCmd : ((BatchExecutionCommandImpl) command).getCommands()) { if (nestedCmd instanceof FireAllRulesCommand) { autoFireAllRules = false; break; } } } if (autoFireAllRules) { ksession.fireAllRules(); } if (command instanceof BatchExecutionCommandImpl) { ExecutionResults result = ((StatefulKnowledgeSessionImpl) ksession).getExecutionResult(); return (T) result; } else { return (T) o; } } finally { ((StatefulKnowledgeSessionImpl) ksession).endBatchExecution(); dispose(ksession); } }