Search in sources :

Example 41 with LeftInputAdapterNode

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

the class LinkingTest method testSubNetworkRiaLinking.

@Test
public void testSubNetworkRiaLinking() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   A() \n";
    str += "   B() \n";
    str += "   exists( C() and D() ) \n";
    str += "   E() \n";
    str += "then \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    assertEquals(1, liaNode.getSinkPropagator().size());
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
    assertEquals(2, bNode.getSinkPropagator().size());
    ExistsNode exists1n = (ExistsNode) bNode.getSinkPropagator().getSinks()[1];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    JoinNode dNode = (JoinNode) cNode.getSinkPropagator().getSinks()[0];
    assertEquals(1, dNode.getSinkPropagator().size());
    RightInputAdapterNode riaNode1 = (RightInputAdapterNode) dNode.getSinkPropagator().getSinks()[0];
    JoinNode eNode = (JoinNode) exists1n.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) eNode.getSinkPropagator().getSinks()[0];
    SegmentUtilities.createSegmentMemory(exists1n, wm);
    BetaMemory existsBm = (BetaMemory) wm.getNodeMemory(exists1n);
    assertEquals(0, existsBm.getSegmentMemory().getLinkedNodeMask());
    FactHandle fhc = wm.insert(new C());
    FactHandle fhd = wm.insert(new D());
    wm.flushPropagations();
    // exists is start of new segment
    assertEquals(1, existsBm.getSegmentMemory().getLinkedNodeMask());
    wm.retract(fhd);
    wm.flushPropagations();
    assertEquals(0, existsBm.getSegmentMemory().getLinkedNodeMask());
    PathMemory rs = (PathMemory) wm.getNodeMemory(rtn);
    assertFalse(rs.isRuleLinked());
    wm.insert(new A());
    wm.flushPropagations();
    assertFalse(rs.isRuleLinked());
    wm.insert(new B());
    wm.flushPropagations();
    assertFalse(rs.isRuleLinked());
    wm.insert(new E());
    wm.flushPropagations();
    assertFalse(rs.isRuleLinked());
    wm.insert(new D());
    wm.flushPropagations();
    assertTrue(rs.isRuleLinked());
    wm.retract(fhc);
    wm.flushPropagations();
    assertFalse(rs.isRuleLinked());
    wm.insert(new C());
    wm.flushPropagations();
    assertTrue(rs.isRuleLinked());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) ExistsNode(org.drools.core.reteoo.ExistsNode) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 42 with LeftInputAdapterNode

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

the class LinkingTest method testSubNetworkSharing.

@Test
public void testSubNetworkSharing() throws Exception {
    // Checks the network is correctly formed, with sharing
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   A() \n";
    str += "   B() \n";
    str += "   C() \n";
    str += "   D() \n";
    str += "then \n";
    str += "end \n";
    str += "rule rule2 when \n";
    str += "   A() \n";
    str += "   exists( B() and C() ) \n";
    str += "   D() \n";
    str += "then \n";
    str += "end \n";
    str += "rule rule3 when \n";
    str += "   A() \n";
    str += "   exists( B() and C() and D() ) \n";
    str += "   E() \n";
    str += "then \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    assertEquals(3, liaNode.getSinkPropagator().size());
    ExistsNode existsNode2 = (ExistsNode) liaNode.getSinkPropagator().getSinks()[1];
    ExistsNode existsNode3 = (ExistsNode) liaNode.getSinkPropagator().getSinks()[2];
    JoinNode joinNodeB = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
    assertSame(joinNodeB.getRightInput(), getObjectTypeNode(kbase, B.class));
    JoinNode joinNodeC = (JoinNode) joinNodeB.getSinkPropagator().getSinks()[0];
    assertSame(joinNodeC.getRightInput(), getObjectTypeNode(kbase, C.class));
    assertEquals(2, joinNodeC.getSinkPropagator().size());
    JoinNode joinNodeD = (JoinNode) joinNodeC.getSinkPropagator().getSinks()[0];
    assertSame(joinNodeD.getRightInput(), getObjectTypeNode(kbase, D.class));
    assertEquals(2, joinNodeD.getSinkPropagator().size());
    assertSame(existsNode2, ((RightInputAdapterNode) joinNodeC.getSinkPropagator().getSinks()[1]).getObjectSinkPropagator().getSinks()[0]);
    assertSame(existsNode3, ((RightInputAdapterNode) joinNodeD.getSinkPropagator().getSinks()[1]).getObjectSinkPropagator().getSinks()[0]);
}
Also used : JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ExistsNode(org.drools.core.reteoo.ExistsNode) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 43 with LeftInputAdapterNode

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

the class AlphaNetworkModifyTest method testModifyWithLiaToAcc.

@Test
public void testModifyWithLiaToAcc() {
    // 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 += "    Object() from accumulate( $p : Person() and Cheese(), collectList( $p ) )\n";
    str += "    Person() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule x2 \n";
    str += "when \n";
    str += "    Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n";
    str += "    Person() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule x3 \n";
    str += "when \n";
    str += "    Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n";
    str += "    Person() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule x4 \n";
    str += "when \n";
    str += "    Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n";
    str += "    Person() \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(0, sinks[0].getLeftInputOtnId().getId());
    assertEquals(1, sinks[1].getLeftInputOtnId().getId());
    assertEquals(2, sinks[2].getLeftInputOtnId().getId());
    ObjectTypeNode otnPerson = getObjectTypeNode(kbase, "Person");
    ObjectTypeNode otnCheese = getObjectTypeNode(kbase, "Cheese");
    assertEquals(0, otnPerson.getOtnIdCounter());
    assertEquals(0, otnCheese.getOtnIdCounter());
    wm.insert(new Person());
    wm.insert(new Cheese());
    wm.fireAllRules();
    assertEquals(3, otnPerson.getOtnIdCounter());
    assertEquals(2, otnCheese.getOtnIdCounter());
}
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 44 with LeftInputAdapterNode

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

the class LeftInputAdapterNodeVisitor method doVisit.

@Override
protected void doVisit(NetworkNode node, Stack<NetworkNode> nodeStack, StatefulKnowledgeSessionInfo info) {
    LeftInputAdapterNode an = (LeftInputAdapterNode) node;
    DefaultNodeInfo ni = (DefaultNodeInfo) info.getNodeInfo(node);
    ni.setMemoryEnabled(false);
}
Also used : LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 45 with LeftInputAdapterNode

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

the class IndexingTest method testIndexingOnQueryUnificationWithNot.

@Test(timeout = 10000)
public void testIndexingOnQueryUnificationWithNot() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test  \n";
    str += "import org.drools.compiler.Person \n";
    str += "query peeps( String $name, int $age ) \n";
    str += "    not $p2 : Person( $name := name, age > $age ) \n";
    str += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
            node = n;
            break;
        }
    }
    StatefulKnowledgeSessionImpl wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    NotNode n = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
    DoubleNonIndexSkipBetaConstraints c = (DoubleNonIndexSkipBetaConstraints) n.getRawConstraints();
    // assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
    assertTrue(c.isIndexed());
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n);
    System.out.println(bm.getLeftTupleMemory().getClass());
    System.out.println(bm.getRightTupleMemory().getClass());
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
    final Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("inserted", new Integer(0));
    map.put("deleted", new Integer(0));
    map.put("updated", new Integer(0));
    wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {

        @Override
        public void rowInserted(Row row) {
            System.out.println("inserted");
            Integer integer = map.get("inserted");
            map.put("inserted", integer.intValue() + 1);
        }

        @Override
        public void rowDeleted(Row row) {
            System.out.println("deleted");
            Integer integer = map.get("deleted");
            map.put("deleted", integer.intValue() + 1);
        }

        @Override
        public void rowUpdated(Row row) {
            System.out.println("updated");
            Integer integer = map.get("updated");
            map.put("updated", integer.intValue() + 1);
        }
    });
    System.out.println("inserted: " + map.get("inserted"));
    System.out.println("deleted: " + map.get("deleted"));
    System.out.println("updated: " + map.get("updated"));
    Map<String, InternalFactHandle> peeps = new HashMap<String, InternalFactHandle>();
    Person p = null;
    InternalFactHandle fh = null;
    int max = 3;
    // 1 matched, prior to any insertions
    assertEquals(1, map.get("inserted").intValue());
    assertEquals(0, map.get("deleted").intValue());
    assertEquals(0, map.get("updated").intValue());
    // x0 is the blocker
    for (int i = 0; i < max; i++) {
        p = new Person("x" + i, 100);
        fh = (InternalFactHandle) wm.insert(p);
        wm.fireAllRules();
        peeps.put(p.getName(), fh);
    }
    // insertions case 1 deletion
    assertEquals(1, map.get("inserted").intValue());
    assertEquals(1, map.get("deleted").intValue());
    assertEquals(0, map.get("updated").intValue());
    // each x is blocker in turn up to x99
    for (int i = 0; i < (max - 1); i++) {
        fh = peeps.get("x" + i);
        p = (Person) fh.getObject();
        p.setAge(90);
        wm.update(fh, p);
        wm.fireAllRules();
        // make sure this doesn't change
        assertEquals("i=" + i, 1, map.get("inserted").intValue());
    }
    // no change
    assertEquals(1, map.get("inserted").intValue());
    assertEquals(1, map.get("deleted").intValue());
    assertEquals(0, map.get("updated").intValue());
    // x99 is still the blocker, everything else is just added
    for (int i = 0; i < (max - 1); i++) {
        fh = peeps.get("x" + i);
        p = (Person) fh.getObject();
        p.setAge(102);
        wm.update(fh, p);
        wm.fireAllRules();
        // make sure this doesn't change
        assertEquals("i=" + i, 1, map.get("inserted").intValue());
    }
    // no change
    assertEquals(1, map.get("inserted").intValue());
    assertEquals(1, map.get("deleted").intValue());
    assertEquals(0, map.get("updated").intValue());
    // x99 is still the blocker
    for (int i = (max - 2); i >= 0; i--) {
        fh = peeps.get("x" + i);
        p = (Person) fh.getObject();
        p.setAge(90);
        wm.update(fh, p);
        wm.fireAllRules();
        // make sure this doesn't change
        assertEquals("i=" + i, 1, map.get("inserted").intValue());
    }
    // move x99, should no longer be a blocker, now it can increase
    fh = peeps.get("x" + (max - 1));
    p = (Person) fh.getObject();
    p.setAge(90);
    wm.update(fh, p);
    wm.fireAllRules();
    assertEquals(2, map.get("inserted").intValue());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) HashMap(java.util.HashMap) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) AlphaNode(org.drools.core.reteoo.AlphaNode) IndexableConstraint(org.drools.core.rule.IndexableConstraint) DoubleNonIndexSkipBetaConstraints(org.drools.core.common.DoubleNonIndexSkipBetaConstraints) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Row(org.kie.api.runtime.rule.Row) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Aggregations

LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)72 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)60 Test (org.junit.Test)56 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)48 KieBase (org.kie.api.KieBase)35 JoinNode (org.drools.core.reteoo.JoinNode)28 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)28 SegmentMemory (org.drools.core.reteoo.SegmentMemory)28 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)27 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)25 BetaMemory (org.drools.core.reteoo.BetaMemory)25 ArrayList (java.util.ArrayList)17 PathMemory (org.drools.core.reteoo.PathMemory)15 List (java.util.List)12 FactHandle (org.kie.api.runtime.rule.FactHandle)12 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)12 ClassObjectType (org.drools.core.base.ClassObjectType)11 AlphaNode (org.drools.core.reteoo.AlphaNode)11 NotNode (org.drools.core.reteoo.NotNode)11 ExistsNode (org.drools.core.reteoo.ExistsNode)10