use of org.drools.core.reteoo.Rete in project drools by kiegroup.
the class ReteTest method testAssertObject.
/**
* Test asserts correctly propagate
*
* @throws Exception
*/
@Test
public void testAssertObject() throws Exception {
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
// Create a Rete network with ObjectTypeNodes for List, Collection and ArrayList
final Rete rete = kBase.getRete();
final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(List.class), buildContext);
objectTypeNode.attach(buildContext);
final MockObjectSink sink1 = new MockObjectSink();
objectTypeNode.addObjectSink(sink1);
// There are no String ObjectTypeNodes, make sure its not propagated
final String string = "String";
final DefaultFactHandle h1 = new DefaultFactHandle(1, string);
rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
assertLength(0, sink1.getAsserted());
// There is a List ObjectTypeNode, make sure it was propagated
final List list = new ArrayList();
final DefaultFactHandle h2 = new DefaultFactHandle(1, list);
rete.assertObject(h2, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
ksession.fireAllRules();
final List asserted = sink1.getAsserted();
assertLength(1, asserted);
final Object[] results = (Object[]) asserted.get(0);
assertSame(list, ((DefaultFactHandle) results[0]).getObject());
}
use of org.drools.core.reteoo.Rete in project drools by kiegroup.
the class ReteooWorkingMemoryTest method testDifferentEntryPointsOnSameFact.
@Test
public void testDifferentEntryPointsOnSameFact() {
// JBRULES-2971
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
Rete rete = kBase.getRete();
NodeFactory nFacotry = new PhreakNodeFactory();
EntryPointNode epn = nFacotry.buildEntryPointNode(kBase.getReteooBuilder().getNodeIdsGenerator().getNextId(), RuleBasePartitionId.MAIN_PARTITION, kBase.getConfiguration().isMultithreadEvaluation(), rete, new EntryPointId("xxx"));
kBase.getRete().addObjectSink(epn);
KieSession ksession = kBase.newKieSession();
FactHandle f1 = ksession.insert("f1");
EntryPoint ep = ksession.getEntryPoint("xxx");
try {
ep.update(f1, "s1");
fail("Should throw an exception");
} catch (IllegalArgumentException e) {
}
try {
ep.retract(f1);
fail("Should throw an exception");
} catch (IllegalArgumentException e) {
}
ksession.update(f1, "s1");
assertNotNull(ksession.getObject(f1));
ksession.retract(f1);
ksession.retract(f1);
assertNull(ksession.getObject(f1));
}
use of org.drools.core.reteoo.Rete in project drools by kiegroup.
the class IncrementalCompilationTest method testRemoveRuleWithRia.
@Test
public void testRemoveRuleWithRia() {
// DROOLS-954
final String drl1 = "import " + List.class.getCanonicalName() + "\n" + "rule R when\n" + " $list : List()\n" + " exists Integer( this == 1 ) from $list\n" + " exists Integer( this == 2 ) from $list\n" + "then \n" + "end\n";
final KieServices ks = KieServices.Factory.get();
final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.1");
KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, drl1);
final KieContainer kc = ks.newKieContainer(releaseId1);
final KieSession ksession = kc.newKieSession();
final ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
KieUtil.getKieModuleFromDrls(releaseId2, kieBaseTestConfiguration);
kc.updateToVersion(releaseId2);
final Rete rete = ((InternalKnowledgeBase) ksession.getKieBase()).getRete();
final EntryPointNode entryPointNode = rete.getEntryPointNodes().values().iterator().next();
for (final ObjectTypeNode otns : entryPointNode.getObjectTypeNodes().values()) {
assertEquals(0, otns.getObjectSinkPropagator().getSinks().length);
}
}
use of org.drools.core.reteoo.Rete in project drools by kiegroup.
the class NodePositionInPathTest method test.
@Test
public void test() {
String drl = "rule R1 when\n" + " Integer()\n" + " exists( Integer() and Integer() )\n" + " String()\n" + "then\n" + "end\n" + "rule R2 when\n" + " Integer()\n" + " exists( Integer() and String() )\n" + "then\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
ReteDumper.dumpRete(((RuleBase) kbase));
Rete rete = ((RuleBase) kbase).getRete();
LeftInputAdapterNode liaNode = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
Class<?> otnType = ((ClassObjectType) otn.getObjectType()).getClassType();
if (Integer.class == otnType) {
liaNode = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
}
}
assertEquals(0, liaNode.getPathIndex());
LeftTupleSink[] liaSinks = liaNode.getSinkPropagator().getSinks();
BetaNode join1 = (BetaNode) liaSinks[0];
assertEquals(1, join1.getPathIndex());
ExistsNode ex1 = (ExistsNode) liaSinks[1];
assertEquals(1, ex1.getPathIndex());
BetaNode join2 = (BetaNode) ex1.getSinkPropagator().getSinks()[0];
assertEquals(2, join2.getPathIndex());
RuleTerminalNode rtn1 = (RuleTerminalNode) join2.getSinkPropagator().getSinks()[0];
assertEquals(3, rtn1.getPathIndex());
ExistsNode ex2 = (ExistsNode) liaSinks[2];
assertEquals(1, ex2.getPathIndex());
RuleTerminalNode rtn2 = (RuleTerminalNode) ex2.getSinkPropagator().getSinks()[0];
assertEquals(2, rtn2.getPathIndex());
BetaNode join3 = (BetaNode) join1.getSinkPropagator().getSinks()[0];
assertEquals(2, join3.getPathIndex());
RightInputAdapterNode ria1 = (RightInputAdapterNode) join3.getSinkPropagator().getSinks()[0];
assertEquals(3, ria1.getPathIndex());
BetaNode join4 = (BetaNode) join1.getSinkPropagator().getSinks()[1];
assertEquals(2, join4.getPathIndex());
RightInputAdapterNode ria2 = (RightInputAdapterNode) join4.getSinkPropagator().getSinks()[0];
assertEquals(3, ria2.getPathIndex());
LeftTupleNode[] rtn1PathNodes = rtn1.getPathNodes();
assertEquals(4, rtn1PathNodes.length);
checkNodePosition(rtn1PathNodes, liaNode);
checkNodePosition(rtn1PathNodes, ex1);
checkNodePosition(rtn1PathNodes, join2);
checkNodePosition(rtn1PathNodes, rtn1);
LeftTupleNode[] rtn2PathNodes = rtn2.getPathNodes();
assertEquals(3, rtn2PathNodes.length);
checkNodePosition(rtn2PathNodes, liaNode);
checkNodePosition(rtn2PathNodes, ex2);
checkNodePosition(rtn2PathNodes, rtn2);
LeftTupleNode[] ria1PathNodes = ria1.getPathNodes();
assertEquals(4, ria1PathNodes.length);
checkNodePosition(ria1PathNodes, liaNode);
checkNodePosition(ria1PathNodes, join1);
checkNodePosition(ria1PathNodes, join3);
checkNodePosition(ria1PathNodes, ria1);
LeftTupleNode[] ria2PathNodes = ria2.getPathNodes();
assertEquals(4, ria2PathNodes.length);
checkNodePosition(ria2PathNodes, liaNode);
checkNodePosition(ria2PathNodes, join1);
checkNodePosition(ria2PathNodes, join4);
checkNodePosition(ria2PathNodes, ria2);
}
use of org.drools.core.reteoo.Rete in project drools by kiegroup.
the class PassiveQueryTest method testPassiveQueryUsingSegmentPropagator.
@Test
public void testPassiveQueryUsingSegmentPropagator() throws Exception {
String str = "global java.util.List list\n" + "query Q (Integer i)\n" + " String( this == i.toString() )\n" + "end\n" + "rule R1a @Propagation(IMMEDIATE) when\n" + " $i : Integer()\n" + " ?Q( $i; )\n" + "then\n" + " list.add( \"R1a\" );\n" + "end\n" + "rule R1b @Propagation(IMMEDIATE) when\n" + " $i : Integer()\n" + " ?Q( $i; )\n" + " Long( intValue == $i )\n" + "then\n" + " list.add( \"R1b\" );\n" + "end\n" + "rule R2 when\n" + " $i : Integer()\n" + " ?Q( $i; )\n" + "then\n" + " list.add( \"R2\" );\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert(1L);
FactHandle fh = ksession.insert(1);
ksession.insert("1");
Rete rete = ((RuleBase) kbase).getRete();
LeftInputAdapterNode lia = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
if (Integer.class == otn.getObjectType().getValueType().getClassType()) {
lia = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
break;
}
}
LeftTupleSink[] sinks = lia.getSinkPropagator().getSinks();
QueryElementNode q1 = (QueryElementNode) sinks[0];
QueryElementNode q2 = (QueryElementNode) sinks[1];
InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
wm.flushPropagations();
Memory memory1 = wm.getNodeMemory(q1);
assertTrue(memory1.getSegmentMemory().getStagedLeftTuples().isEmpty());
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("R2", list.get(0));
list.clear();
ksession.delete(fh);
ksession.insert(1);
ksession.fireAllRules();
assertEquals(3, list.size());
}
Aggregations