Search in sources :

Example 6 with ObjectSink

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));
}
Also used : JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) Cheese(org.drools.compiler.Cheese) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) ObjectSink(org.drools.core.reteoo.ObjectSink) Person(org.drools.compiler.Person) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) Test(org.junit.Test)

Example 7 with ObjectSink

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));
        }
    }
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) KiePackage(org.kie.api.definition.KiePackage) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ObjectSink(org.drools.core.reteoo.ObjectSink) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 8 with ObjectSink

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);
        }
    }
}
Also used : CompositePartitionAwareObjectSinkAdapter(org.drools.core.reteoo.CompositePartitionAwareObjectSinkAdapter) ObjectSinkPropagator(org.drools.core.reteoo.ObjectSinkPropagator) ObjectSink(org.drools.core.reteoo.ObjectSink)

Example 9 with ObjectSink

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;
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) Memory(org.drools.core.common.Memory) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) ObjectSink(org.drools.core.reteoo.ObjectSink) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 10 with ObjectSink

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]);
        }
    }
}
Also used : LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) ObjectSource(org.drools.core.reteoo.ObjectSource) ObjectSink(org.drools.core.reteoo.ObjectSink)

Aggregations

ObjectSink (org.drools.core.reteoo.ObjectSink)10 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)5 BetaMemory (org.drools.core.reteoo.BetaMemory)4 BetaNode (org.drools.core.reteoo.BetaNode)4 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)3 Memory (org.drools.core.common.Memory)3 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)3 LeftTupleSource (org.drools.core.reteoo.LeftTupleSource)3 PathMemory (org.drools.core.reteoo.PathMemory)3 QueryElementNodeMemory (org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory)3 RightTuple (org.drools.core.reteoo.RightTuple)3 ConditionalBranchMemory (org.drools.core.reteoo.ConditionalBranchNode.ConditionalBranchMemory)2 EvalMemory (org.drools.core.reteoo.EvalConditionNode.EvalMemory)2 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)2 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)2 LeftTuple (org.drools.core.reteoo.LeftTuple)2 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)2 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)2 RiaNodeMemory (org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory)2 SegmentMemory (org.drools.core.reteoo.SegmentMemory)2