use of org.drools.core.reteoo.ObjectSink in project drools by kiegroup.
the class AccumulateTest method testAccumulateWithSameSubnetwork.
// (timeout = 10000)
@Test
public void testAccumulateWithSameSubnetwork() throws Exception {
String rule = "package org.drools.compiler.test;\n" + "import org.drools.compiler.Cheese;\n" + "import org.drools.compiler.Person;\n" + "global java.util.List list; \n" + "rule r1 salience 100 \n" + " when\n" + " $person : Person( name == 'Alice', $likes : likes )\n" + " $total : Number() from accumulate( $p : Person(likes != $likes, $l : likes) and $c : Cheese( type == $l ),\n" + " min($c.getPrice()) )\n" + " then\n" + " list.add( 'r1' + ':' + $total);\n" + "end\n" + "rule r2 \n" + " when\n" + " $person : Person( name == 'Alice', $likes : likes )\n" + " $total : Number() from accumulate( $p : Person(likes != $likes, $l : likes) and $c : Cheese( type == $l ),\n" + " max($c.getPrice()) )\n" + " then\n" + " list.add( 'r2' + ':' + $total);\n" + "end\n" + "";
// read in the source
KieBase kbase = loadKnowledgeBaseFromString(rule);
KieSession wm = createKnowledgeSession(kbase);
List list = new ArrayList();
wm.setGlobal("list", list);
// Check the network formation, to ensure the RiaNode is shared.
ObjectTypeNode cheeseOtn = LinkingTest.getObjectTypeNode(kbase, Cheese.class);
ObjectSink[] oSinks = cheeseOtn.getObjectSinkPropagator().getSinks();
assertEquals(1, oSinks.length);
JoinNode cheeseJoin = (JoinNode) oSinks[0];
LeftTupleSink[] ltSinks = cheeseJoin.getSinkPropagator().getSinks();
assertEquals(1, ltSinks.length);
RightInputAdapterNode rian = (RightInputAdapterNode) ltSinks[0];
// RiaNode is shared, if this has two outputs
assertEquals(2, rian.getObjectSinkPropagator().size());
wm.insert(new Cheese("stilton", 10));
wm.insert(new Person("Alice", "brie"));
wm.insert(new Person("Bob", "stilton"));
wm.fireAllRules();
assertEquals(2, list.size());
assertEquals("r1:10", list.get(0));
assertEquals("r2:10", list.get(1));
}
use of org.drools.core.reteoo.ObjectSink in project drools by kiegroup.
the class RemoveRuleTest method testRemoveBigRule.
@Test
public void testRemoveBigRule() throws Exception {
// JBRULES-3496
final String str = "package org.drools.compiler.test\n" + "\n" + "declare SimpleFact\n" + " patientSpaceId : String\n" + " block : int\n" + "end\n" + "\n" + "declare SimpleMembership\n" + " patientSpaceId : String\n" + " listId : String\n" + "end\n" + "\n" + "declare SimplePatient\n" + " spaceId : String\n" + "end\n" + "\n" + "rule \"RTR - 47146 retract\"\n" + "agenda-group \"list membership\"\n" + "when\n" + " $listMembership0 : SimpleMembership( $listMembershipPatientSpaceIdRoot : patientSpaceId, ( listId != null && listId == \"47146\" ) )\n" + " not ( $patient0 : SimplePatient( $patientSpaceIdRoot : spaceId, spaceId != null && spaceId == $listMembershipPatientSpaceIdRoot ) \n" + " and ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 1 )\n" + " ) or ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 2 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 3 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 4 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 5 )\n" + " ) ) and ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 6 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 7 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 8 )\n" + " ) ) and ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 9 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 10 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 11 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 12 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 13 )\n" + " ) or ( (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 14 )\n" + " ) and (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 15 )\n" + " ) ) or ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 16 )\n" + " ) and ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 17 )\n" + " ) ) or ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 18 )\n" + " ) and (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 19 )\n" + " ) ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 20 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 21 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 22 )\n" + " ) or ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 23 )\n" + " ) and (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 24 )\n" + " ) ) ) and ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 25 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 26 )\n" + " ) ) )\n" + "then\n" + "end\n";
final Collection<KiePackage> kpgs = loadKnowledgePackagesFromString(str);
assertEquals(1, kpgs.size());
final InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
kbase.addPackages(kpgs);
kbase.removeKiePackage(kpgs.iterator().next().getName());
final EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
for (final ObjectTypeNode otn : epn.getObjectTypeNodes().values()) {
final ObjectSink[] sinks = otn.getObjectSinkPropagator().getSinks();
if (sinks.length > 0) {
fail(otn + " has sinks " + Arrays.toString(sinks));
}
}
}
use of org.drools.core.reteoo.ObjectSink in project drools by kiegroup.
the class NodesPartitioningTest method checkPartitionedSinks.
private void checkPartitionedSinks(ObjectTypeNode otn) {
if (InitialFact.class.isAssignableFrom(((ClassObjectType) otn.getObjectType()).getClassType())) {
return;
}
ObjectSinkPropagator sinkPropagator = otn.getObjectSinkPropagator();
ObjectSinkPropagator[] propagators = sinkPropagator instanceof CompositePartitionAwareObjectSinkAdapter ? ((CompositePartitionAwareObjectSinkAdapter) sinkPropagator).getPartitionedPropagators() : new ObjectSinkPropagator[] { sinkPropagator };
for (int i = 0; i < propagators.length; i++) {
for (ObjectSink sink : propagators[i].getSinks()) {
assertEquals(sink + " on " + sink.getPartitionId() + " is expcted to be on propagator " + i, i, sink.getPartitionId().getId() % propagators.length);
}
}
}
use of org.drools.core.reteoo.ObjectSink in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeRIANodeMemory.
private static ProtobufMessages.NodeMemory writeRIANodeMemory(final int nodeId, final BaseNode node, final NodeMemories memories) {
RightInputAdapterNode riaNode = (RightInputAdapterNode) node;
ObjectSink[] sinks = riaNode.getObjectSinkPropagator().getSinks();
BetaNode betaNode = (BetaNode) sinks[0];
Memory betaMemory = memories.peekNodeMemory(betaNode);
if (betaMemory == null) {
return null;
}
BetaMemory bm;
if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
bm = ((AccumulateMemory) betaMemory).getBetaMemory();
} else {
bm = (BetaMemory) betaMemory;
}
// for RIA nodes, we need to store the ID of the created handles
bm.getRightTupleMemory().iterator();
if (bm.getRightTupleMemory().size() > 0) {
ProtobufMessages.NodeMemory.RIANodeMemory.Builder _ria = ProtobufMessages.NodeMemory.RIANodeMemory.newBuilder();
final org.drools.core.util.Iterator it = bm.getRightTupleMemory().iterator();
// iterates over all propagated handles and assert them to the new sink
for (RightTuple entry = (RightTuple) it.next(); entry != null; entry = (RightTuple) it.next()) {
LeftTuple leftTuple = entry instanceof LeftTuple ? // with phreak the entry is always both a right and a left tuple
(LeftTuple) entry : // this is necessary only for reteoo
(LeftTuple) entry.getFactHandle().getObject();
InternalFactHandle handle = (InternalFactHandle) leftTuple.getFactHandle();
if (handle == null) {
continue;
}
FactHandle _handle = ProtobufMessages.FactHandle.newBuilder().setId(handle.getId()).setRecency(handle.getRecency()).build();
_ria.addContext(ProtobufMessages.NodeMemory.RIANodeMemory.RIAContext.newBuilder().setTuple(PersisterHelper.createTuple(leftTuple)).setResultHandle(_handle).build());
}
return ProtobufMessages.NodeMemory.newBuilder().setNodeId(nodeId).setNodeType(ProtobufMessages.NodeMemory.NodeType.RIA).setRia(_ria.build()).build();
}
return null;
}
use of org.drools.core.reteoo.ObjectSink in project drools by kiegroup.
the class ReteooBuilderTest method nodesEquals.
private void nodesEquals(final Object object1, final Object object2) {
assertEquals(object1 + " is not of the same type as " + object2, object1.getClass(), object2.getClass());
assertEquals(object1 + " is not equal to " + object2, object1, object2);
if (object1 instanceof ObjectSource) {
final ObjectSource source1 = (ObjectSource) object1;
final ObjectSource source2 = (ObjectSource) object2;
final ObjectSink[] list1 = source1.getObjectSinkPropagator().getSinks();
final ObjectSink[] list2 = source2.getObjectSinkPropagator().getSinks();
assertEquals(object1.getClass() + " nodes have different number of sinks", list1.length, list2.length);
for (int i = 0, size = list1.length; i < size; i++) {
nodesEquals(list1[i], list2[i]);
}
} else if (object1 instanceof LeftTupleSource) {
final LeftTupleSource source1 = (LeftTupleSource) object1;
final LeftTupleSource source2 = (LeftTupleSource) object2;
final LeftTupleSink[] list1 = source1.getSinkPropagator().getSinks();
final LeftTupleSink[] list2 = source2.getSinkPropagator().getSinks();
assertEquals(object1.getClass() + " nodes have different number of sinks", list1.length, list2.length);
for (int i = 0, size = list1.length; i < size; i++) {
nodesEquals(list1[i], list2[i]);
}
}
}
Aggregations