Search in sources :

Example 21 with BetaNode

use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.

the class PropertySpecificTest method test2DifferentAlphaWatchBeforeSameBeta.

@Test
public void test2DifferentAlphaWatchBeforeSameBeta() {
    String rule1 = "B(a == 15) @watch(b) C()";
    String rule2 = "B(a == 15) @watch(c) C()";
    KieBase kbase = getKnowledgeBase(rule1, rule2);
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode otnB = getObjectTypeNode(kbase, "B");
    List<String> sp = getSettableProperties(wm, otnB);
    Class classType = ((ClassObjectType) otnB.getObjectType()).getClassType();
    AlphaNode alphaNode = (AlphaNode) otnB.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), alphaNode.getInferredMask());
    ObjectTypeNode otnC = getObjectTypeNode(kbase, "C");
    BetaNode betaNodeC1 = (BetaNode) otnC.getObjectSinkPropagator().getSinks()[0];
    BetaNode betaNodeC2 = (BetaNode) otnC.getObjectSinkPropagator().getSinks()[1];
    LeftInputAdapterNode lia1 = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
    assertSame(betaNodeC1, lia1.getSinkPropagator().getSinks()[0]);
    LeftInputAdapterNode lia2 = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[1];
    assertSame(betaNodeC2, lia2.getSinkPropagator().getSinks()[0]);
    assertEquals(EmptyBitMask.get(), betaNodeC1.getRightDeclaredMask());
    assertEquals(EmptyBitMask.get(), betaNodeC1.getRightInferredMask());
    assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNodeC1.getLeftDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b"), sp), betaNodeC1.getLeftInferredMask());
    assertEquals(EmptyBitMask.get(), betaNodeC2.getRightDeclaredMask());
    assertEquals(EmptyBitMask.get(), betaNodeC2.getRightInferredMask());
    assertEquals(calculatePositiveMask(classType, list("c"), sp), betaNodeC2.getLeftDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "c"), sp), betaNodeC2.getLeftInferredMask());
    kbase.removeRule("org.drools.compiler.integrationtests", "r0");
    assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "c"), sp), alphaNode.getInferredMask());
    assertEquals(1, lia2.getSinkPropagator().getSinks().length);
    BetaNode betaNodeC = (BetaNode) lia2.getSinkPropagator().getSinks()[0];
    assertEquals(EmptyBitMask.get(), betaNodeC2.getRightDeclaredMask());
    assertEquals(EmptyBitMask.get(), betaNodeC2.getRightInferredMask());
    assertEquals(calculatePositiveMask(classType, list("c"), sp), betaNodeC2.getLeftDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "c"), sp), betaNodeC2.getLeftInferredMask());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 22 with BetaNode

use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.

the class BetaNodeVisitor method doVisit.

@Override
protected void doVisit(NetworkNode node, Stack<NetworkNode> nodeStack, StatefulKnowledgeSessionInfo info) {
    BetaNode bn = (BetaNode) node;
    DefaultNodeInfo ni = (DefaultNodeInfo) info.getNodeInfo(node);
    final BetaMemory memory = (BetaMemory) info.getSession().getNodeMemory(bn);
    ni.setMemoryEnabled(true);
    if (bn.isObjectMemoryEnabled()) {
        ni.setFactMemorySize(memory.getRightTupleMemory().size());
    }
    if (bn.isLeftTupleMemoryEnabled()) {
        ni.setTupleMemorySize(memory.getLeftTupleMemory().size());
    }
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) BetaMemory(org.drools.core.reteoo.BetaMemory)

Example 23 with BetaNode

use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.

the class RightInputAdapterNodeVisitor method doVisit.

@Override
protected void doVisit(NetworkNode node, Stack<NetworkNode> nodeStack, StatefulKnowledgeSessionInfo info) {
    RightInputAdapterNode an = (RightInputAdapterNode) node;
    DefaultNodeInfo ni = info.getNodeInfo(node);
    BetaNode betaNode = (BetaNode) an.getObjectSinkPropagator().getSinks()[0];
    Memory childMemory = info.getSession().getNodeMemory(betaNode);
    BetaMemory bm;
    if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
        bm = ((AccumulateMemory) childMemory).getBetaMemory();
    } else {
        bm = (BetaMemory) childMemory;
    }
    ni.setMemoryEnabled(true);
    ni.setTupleMemorySize(bm.getRightTupleMemory().size());
    ni.setCreatedFactHandles(bm.getRightTupleMemory().size());
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) Memory(org.drools.core.common.Memory) BetaMemory(org.drools.core.reteoo.BetaMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 24 with BetaNode

use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.

the class BackwardChainingTest method testSubNetworksAndQueries.

@Test(timeout = 10000)
public void testSubNetworksAndQueries() throws Exception {
    String str = "" + "package org.drools.compiler.test  \n" + "import java.util.List\n" + "import java.util.ArrayList\n" + "import java.util.Map\n" + "import java.util.HashMap\n" + "global List list\n" + "dialect \"mvel\"\n" + "\n" + "declare Location\n" + "    thing : String \n" + "    location : String \n" + "end" + "\n" + "declare Edible\n" + "   thing : String\n" + "end" + "\n" + "query whereFood( String x, String y ) \n" + "    Location(x, y;) Edible(x;) \n" + "end\n" + "\n" + "query look(String place, List food ) \n" + "    $s : String() // just here to give a OTN lookup point\n" + "    food := List() from accumulate( whereFood(thing, place;) ," + "                                    collectList( thing ) )\n" + "    exists( whereFood(thing, place;) )\n" + "    not( whereFood(thing, place;) and\n " + "         String( this == $s ) from thing )\n" + "end\n" + "\n" + "rule init when\n" + "then\n" + "        \n" + "        insert( new Location(\"apple\", \"kitchen\") );\n" + "        insert( new Location(\"crackers\", \"kitchen\") );\n" + "        insert( new Location(\"broccoli\", \"kitchen\") );\n" + "        insert( new Location(\"computer\", \"office\") );\n" + "        insert( new Edible(\"apple\") );\n" + "        insert( new Edible(\"crackers\") );\n" + "end\n" + "";
    logger.debug(str);
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
    // Get the accumulate node, so we can test it's memory later
    // now check beta memory was correctly cleared
    List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == String.class) {
            node = n;
            break;
        }
    }
    BetaNode stringBetaNode = (BetaNode) node.getObjectSinkPropagator().getSinks()[0];
    QueryElementNode queryElementNode1 = (QueryElementNode) stringBetaNode.getSinkPropagator().getSinks()[0];
    RightInputAdapterNode riaNode1 = (RightInputAdapterNode) queryElementNode1.getSinkPropagator().getSinks()[0];
    AccumulateNode accNode = (AccumulateNode) riaNode1.getObjectSinkPropagator().getSinks()[0];
    QueryElementNode queryElementNode2 = (QueryElementNode) accNode.getSinkPropagator().getSinks()[0];
    RightInputAdapterNode riaNode2 = (RightInputAdapterNode) queryElementNode2.getSinkPropagator().getSinks()[0];
    ExistsNode existsNode = (ExistsNode) riaNode2.getObjectSinkPropagator().getSinks()[0];
    QueryElementNode queryElementNode3 = (QueryElementNode) existsNode.getSinkPropagator().getSinks()[0];
    FromNode fromNode = (FromNode) queryElementNode3.getSinkPropagator().getSinks()[0];
    RightInputAdapterNode riaNode3 = (RightInputAdapterNode) fromNode.getSinkPropagator().getSinks()[0];
    NotNode notNode = (NotNode) riaNode3.getObjectSinkPropagator().getSinks()[0];
    KieSession ksession = createKnowledgeSession(kbase);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) ksession);
    AccumulateMemory accMemory = (AccumulateMemory) wm.getNodeMemory(accNode);
    BetaMemory existsMemory = (BetaMemory) wm.getNodeMemory(existsNode);
    FromMemory fromMemory = (FromMemory) wm.getNodeMemory(fromNode);
    BetaMemory notMemory = (BetaMemory) wm.getNodeMemory(notNode);
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    ksession.setGlobal("list", list);
    FactHandle fh = ksession.insert("bread");
    ksession.fireAllRules();
    final List food = new ArrayList();
    QueryResults results = null;
    // Execute normal query and check no subnetwork tuples are left behind
    results = ksession.getQueryResults("look", new Object[] { "kitchen", Variable.v });
    assertEquals(1, results.size());
    for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
        food.addAll((Collection) row.get("food"));
        logger.debug(row.get("food").toString());
    }
    assertEquals(2, food.size());
    assertContains(new String[] { "crackers", "apple" }, food);
    assertEquals(0, accMemory.getBetaMemory().getRightTupleMemory().size());
    assertEquals(0, existsMemory.getRightTupleMemory().size());
    assertEquals(0, fromMemory.getBetaMemory().getLeftTupleMemory().size());
    assertEquals(0, notMemory.getRightTupleMemory().size());
    // Now execute an open query and ensure the memory is left populated
    food.clear();
    final List foodUpdated = new ArrayList();
    LiveQuery query = ksession.openLiveQuery("look", new Object[] { "kitchen", Variable.v }, new ViewChangedEventListener() {

        public void rowUpdated(Row row) {
            foodUpdated.addAll((Collection) row.get("food"));
        }

        public void rowDeleted(Row row) {
        }

        public void rowInserted(Row row) {
            food.addAll((Collection) row.get("food"));
        }
    });
    assertEquals(2, food.size());
    assertContains(new String[] { "crackers", "apple" }, food);
    assertEquals(2, accMemory.getBetaMemory().getRightTupleMemory().size());
    assertEquals(2, existsMemory.getRightTupleMemory().size());
    assertEquals(2, fromMemory.getBetaMemory().getLeftTupleMemory().size());
    assertEquals(0, notMemory.getRightTupleMemory().size());
    food.clear();
    // Now try again, make sure it only delete's it's own tuples
    results = ksession.getQueryResults("look", new Object[] { "kitchen", Variable.v });
    assertEquals(1, results.size());
    for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
        food.addAll((Collection) row.get("food"));
        logger.debug(row.get("food").toString());
    }
    assertEquals(2, food.size());
    assertContains(new String[] { "crackers", "apple" }, food);
    assertEquals(2, accMemory.getBetaMemory().getRightTupleMemory().size());
    assertEquals(2, existsMemory.getRightTupleMemory().size());
    assertEquals(2, fromMemory.getBetaMemory().getLeftTupleMemory().size());
    assertEquals(0, notMemory.getRightTupleMemory().size());
    food.clear();
    // do an update and check it's  still memory size 2
    // however this time the food should be empty, as 'crackers' now blocks the not.
    ksession.update(fh, "crackers");
    ksession.fireAllRules();
    assertEquals(2, accMemory.getBetaMemory().getRightTupleMemory().size());
    assertEquals(2, existsMemory.getRightTupleMemory().size());
    assertEquals(2, fromMemory.getBetaMemory().getLeftTupleMemory().size());
    assertEquals(1, notMemory.getRightTupleMemory().size());
    assertEquals(0, foodUpdated.size());
    // do an update and check it's  still memory size 2
    // this time
    ksession.update(fh, "oranges");
    ksession.fireAllRules();
    assertEquals(2, accMemory.getBetaMemory().getRightTupleMemory().size());
    assertEquals(2, existsMemory.getRightTupleMemory().size());
    assertEquals(2, fromMemory.getBetaMemory().getLeftTupleMemory().size());
    assertEquals(0, notMemory.getRightTupleMemory().size());
    assertEquals(2, food.size());
    assertContains(new String[] { "crackers", "apple" }, food);
    // Close the open
    query.close();
    assertEquals(0, accMemory.getBetaMemory().getRightTupleMemory().size());
    assertEquals(0, existsMemory.getRightTupleMemory().size());
    assertEquals(0, fromMemory.getBetaMemory().getLeftTupleMemory().size());
    assertEquals(0, notMemory.getRightTupleMemory().size());
}
Also used : AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) NotNode(org.drools.core.reteoo.NotNode) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) AccumulateNode(org.drools.core.reteoo.AccumulateNode) QueryElementNode(org.drools.core.reteoo.QueryElementNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) FromNode(org.drools.core.reteoo.FromNode) ExistsNode(org.drools.core.reteoo.ExistsNode) LiveQuery(org.kie.api.runtime.rule.LiveQuery) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) BetaNode(org.drools.core.reteoo.BetaNode) BetaMemory(org.drools.core.reteoo.BetaMemory) QueryResults(org.kie.api.runtime.rule.QueryResults) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) Collection(java.util.Collection) Row(org.kie.api.runtime.rule.Row) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 25 with BetaNode

use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.

the class PathEndNodeTest method testSubNetworkSharing.

@Test
public void testSubNetworkSharing() throws Exception {
    String str = "package org.test \n" + "rule xxx \n" + "when \n" + "  $s : String()\n" + "  not( Integer() and Long() )\n" + "then \n" + "end  \n" + "rule yyy \n" + "when \n" + "  $s : String()\n" + "  not( Integer() and Long() )\n" + "then \n" + "end  \n";
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) new KieHelper().addContent(str, ResourceType.DRL).build();
    EntryPointNode epn = kbase.getRete().getEntryPointNode(EntryPointId.DEFAULT);
    ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Long.class));
    BetaNode beta1 = (BetaNode) otn.getObjectSinkPropagator().getSinks()[0];
    RightInputAdapterNode rian = (RightInputAdapterNode) beta1.getSinkPropagator().getSinks()[0];
    BetaNode beta2 = (BetaNode) rian.getObjectSinkPropagator().getSinks()[0];
    LeftTupleSink[] sinks = beta2.getSinkPropagator().getSinks();
    RuleTerminalNode rtn1 = (RuleTerminalNode) sinks[0];
    RuleTerminalNode rtn2 = (RuleTerminalNode) sinks[1];
    assertEquals(3, rian.getPathEndNodes().length);
    assertTrue(asList(rian.getPathEndNodes()).containsAll(asList(rtn1, rtn2, rian)));
    kbase.removeRule("org.test", "xxx");
    assertEquals(2, rian.getPathEndNodes().length);
    RuleTerminalNode remainingRTN = rtn1.getRule().getName().equals("yyy") ? rtn1 : rtn2;
    assertTrue(asList(rian.getPathEndNodes()).containsAll(asList(remainingRTN, rian)));
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Aggregations

BetaNode (org.drools.core.reteoo.BetaNode)38 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)26 Test (org.junit.Test)25 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 KieBase (org.kie.api.KieBase)23 ClassObjectType (org.drools.core.base.ClassObjectType)17 AlphaNode (org.drools.core.reteoo.AlphaNode)13 BetaMemory (org.drools.core.reteoo.BetaMemory)11 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)7 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)6 KieHelper (org.kie.internal.utils.KieHelper)6 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)5 LeftTuple (org.drools.core.reteoo.LeftTuple)5 RightTuple (org.drools.core.reteoo.RightTuple)5 Person (org.drools.compiler.Person)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 Memory (org.drools.core.common.Memory)4 ArrayList (java.util.ArrayList)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)3