Search in sources :

Example 76 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode 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 77 with ObjectTypeNode

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

the class CepEspTest method testEventExpiration3.

@Test(timeout = 10000)
public void testEventExpiration3() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_EventExpiration3.drl");
    Map<ObjectType, ObjectTypeNode> objectTypeNodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes(EntryPointId.DEFAULT);
    ObjectTypeNode node = objectTypeNodes.get(new ClassObjectType(StockTick.class));
    assertNotNull(node);
    // the expiration policy @expires(10m) should override the temporal operator usage
    assertEquals(TimeIntervalParser.parse("10m")[0] + 1, node.getExpirationOffset());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Test(org.junit.Test)

Example 78 with ObjectTypeNode

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

the class AlphaNetworkModifyTest method testModifyWithLiaToFrom.

@Test
public void testModifyWithLiaToFrom() {
    // technically you can't have a modify with InitialFactImpl
    // But added test for completeness
    String str = "";
    str += "package org.simple \n";
    str += "import " + Person.class.getCanonicalName() + "\n";
    str += "import " + Cheese.class.getCanonicalName() + "\n";
    str += "import " + Cat.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule x1 \n";
    str += "when \n";
    str += "    $pe : Person() from list\n";
    str += "then \n";
    str += "end  \n";
    str += "rule x2 \n";
    str += "when \n";
    str += "    $ch : Cheese() from list\n";
    str += "then \n";
    str += "end  \n";
    str += "rule x3 \n";
    str += "when \n";
    str += "    $ch : Cheese() from list\n";
    str += "then \n";
    str += "end  \n";
    str += "rule x4 \n";
    str += "when \n";
    str += "    $ch : Cheese() from list\n";
    str += "    eval( $ch != null ) \n";
    str += "then \n";
    str += "end  \n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession wm = kbase.newKieSession();
    wm.fireAllRules();
    ObjectTypeNode otnInit = getObjectTypeNode(kbase, "InitialFactImpl");
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) otnInit.getObjectSinkPropagator().getSinks()[0];
    LeftTupleSink[] sinks = liaNode.getSinkPropagator().getSinks();
    assertEquals(2, sinks.length);
    assertEquals(0, sinks[0].getLeftInputOtnId().getId());
    assertEquals(1, sinks[1].getLeftInputOtnId().getId());
}
Also used : KieBase(org.kie.api.KieBase) Cat(org.drools.compiler.Cat) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) Cheese(org.drools.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 79 with ObjectTypeNode

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

the class AlphaNetworkModifyTest method testModifyWithLiaToEval.

@Test
public void testModifyWithLiaToEval() {
    String str = "";
    str += "package org.simple \n";
    str += "import " + Person.class.getCanonicalName() + "\n";
    str += "import " + Cheese.class.getCanonicalName() + "\n";
    str += "import " + Cat.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule x1 \n";
    str += "when \n";
    str += "    $pe : Person() \n";
    str += "    $ch : Cheese() \n";
    str += "    $ca : Cat() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule x2 \n";
    str += "when \n";
    str += "    $ch : Cheese() \n";
    str += "    $ca : Cat() \n";
    str += "    $pe : Person() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule x3 \n";
    str += "when \n";
    str += "    $ch : Cheese() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule x4 \n";
    str += "when \n";
    str += "    $ch : Cheese() \n";
    str += "    eval( $ch != null ) \n";
    str += "then \n";
    str += "end  \n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession wm = kbase.newKieSession();
    ObjectTypeNode otnPerson = getObjectTypeNode(kbase, "Person");
    ObjectTypeNode otnCheese = getObjectTypeNode(kbase, "Cheese");
    ObjectTypeNode otnCat = getObjectTypeNode(kbase, "Cat");
    assertEquals(0, otnPerson.getOtnIdCounter());
    assertEquals(0, otnCheese.getOtnIdCounter());
    assertEquals(0, otnCat.getOtnIdCounter());
    wm.insert(new Person());
    wm.insert(new Cat("yyy"));
    wm.insert(new Cheese());
    wm.fireAllRules();
    assertEquals(2, otnPerson.getOtnIdCounter());
    assertEquals(4, otnCheese.getOtnIdCounter());
    assertEquals(2, otnCat.getOtnIdCounter());
}
Also used : KieBase(org.kie.api.KieBase) Cat(org.drools.compiler.Cat) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Cheese(org.drools.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 80 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode 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)

Aggregations

ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)116 Test (org.junit.Test)100 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)69 KieBase (org.kie.api.KieBase)64 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)60 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)40 ClassObjectType (org.drools.core.base.ClassObjectType)34 JoinNode (org.drools.core.reteoo.JoinNode)31 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)30 AlphaNode (org.drools.core.reteoo.AlphaNode)29 ArrayList (java.util.ArrayList)28 BetaNode (org.drools.core.reteoo.BetaNode)27 BetaMemory (org.drools.core.reteoo.BetaMemory)24 List (java.util.List)23 SegmentMemory (org.drools.core.reteoo.SegmentMemory)23 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)23 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)22 KieSession (org.kie.api.runtime.KieSession)20 Person (org.drools.compiler.Person)17 EntryPointNode (org.drools.core.reteoo.EntryPointNode)16