Search in sources :

Example 11 with AlphaNode

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

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

the class IndexingTest method testFullFastIteratorResume.

@Test(timeout = 10000)
public void testFullFastIteratorResume() 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) {
        }

        @Override
        public void rowDeleted(Row row) {
        }

        @Override
        public void rowUpdated(Row row) {
        }
    });
    Map<String, InternalFactHandle> peeps = new HashMap<String, InternalFactHandle>();
    Person p = new Person("x0", 100);
    InternalFactHandle fh = (InternalFactHandle) wm.insert(p);
    peeps.put(p.getName(), fh);
    for (int i = 1; i < 100; i++) {
        p = new Person("x" + i, 101);
        fh = (InternalFactHandle) wm.insert(p);
        wm.fireAllRules();
        peeps.put(p.getName(), fh);
    }
    List<RightTuple> list = new ArrayList<RightTuple>(100);
    FastIterator it = n.getRightIterator(bm.getRightTupleMemory());
    for (RightTuple rt = n.getFirstRightTuple(null, bm.getRightTupleMemory(), null, it); rt != null; rt = (RightTuple) it.next(rt)) {
        list.add(rt);
    }
    assertEquals(100, list.size());
    // check we can resume from each entry in the list above.
    for (int i = 0; i < 100; i++) {
        RightTuple rightTuple = list.get(i);
        // resumes from the current rightTuple
        it = n.getRightIterator(bm.getRightTupleMemory(), rightTuple);
        int j = i + 1;
        for (RightTuple rt = (RightTuple) it.next(rightTuple); rt != null; rt = (RightTuple) it.next(rt)) {
            assertSame(list.get(j), rt);
            j++;
        }
    }
}
Also used : NotNode(org.drools.core.reteoo.NotNode) HashMap(java.util.HashMap) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) DoubleNonIndexSkipBetaConstraints(org.drools.core.common.DoubleNonIndexSkipBetaConstraints) KieBase(org.kie.api.KieBase) FastIterator(org.drools.core.util.FastIterator) InternalFactHandle(org.drools.core.common.InternalFactHandle) BetaMemory(org.drools.core.reteoo.BetaMemory) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) AlphaNode(org.drools.core.reteoo.AlphaNode) RightTuple(org.drools.core.reteoo.RightTuple) IndexableConstraint(org.drools.core.rule.IndexableConstraint) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Row(org.kie.api.runtime.rule.Row) Person(org.drools.compiler.Person) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 13 with AlphaNode

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

the class IndexingTest method testIndexingOnQueryUnification.

@Test(timeout = 10000)
public void testIndexingOnQueryUnification() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test  \n";
    str += "import org.drools.compiler.Person \n";
    str += "query peeps( String $name, String $likes, String $street) \n";
    str += "    $p : Person( $name := name, $likes := likes, $street := address.street ) \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;
        }
    }
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    // $p2
    JoinNode j = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
    TripleNonIndexSkipBetaConstraints c = (TripleNonIndexSkipBetaConstraints) j.getRawConstraints();
    // assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
    assertTrue(c.isIndexed());
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(j);
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) TripleNonIndexSkipBetaConstraints(org.drools.core.common.TripleNonIndexSkipBetaConstraints) KieBase(org.kie.api.KieBase) JoinNode(org.drools.core.reteoo.JoinNode) 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) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 14 with AlphaNode

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

the class IndexingTest method testBuildsIndexedAlphaNodes.

@Test(timeout = 10000)
public void testBuildsIndexedAlphaNodes() {
    String drl = "";
    drl += "package org.drools.compiler.test\n";
    drl += "import org.drools.compiler.Person\n";
    drl += "rule test1\n";
    drl += "when\n";
    drl += "   Person(name == \"Mark\", age == 37)\n";
    drl += "   Person(name == \"Mark\", happy == true)\n";
    drl += "then\n";
    drl += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(drl);
    ObjectTypeNode otn = getObjectTypeNode(kbase, Person.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    AlphaNode alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
    CompositeObjectSinkAdapter sinkAdapter = (CompositeObjectSinkAdapter) alphaNode1.getObjectSinkPropagator();
    ObjectSinkNodeList hashableSinks = sinkAdapter.getHashableSinks();
    assertNotNull(hashableSinks);
    assertEquals(2, hashableSinks.size());
    AlphaNode alphaNode2 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
    assertSame(hashableSinks.getFirst(), alphaNode2);
    AlphaNode alphaNode3 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[1];
    assertSame(hashableSinks.getLast(), alphaNode3);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) CompositeObjectSinkAdapter(org.drools.core.reteoo.CompositeObjectSinkAdapter) AlphaNode(org.drools.core.reteoo.AlphaNode) ObjectSinkNodeList(org.drools.core.reteoo.ObjectSinkNodeList) Test(org.junit.Test)

Example 15 with AlphaNode

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

the class MVELTest method testMapAccessorWithStaticFieldAccess.

@Test
public void testMapAccessorWithStaticFieldAccess() {
    final String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + TestEnum.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when  \n" + "    $m : Person( namedAddresses[TestEnum.ONE] == new Address('s1'), namedAddresses[TestEnum.ONE].street == new Address('s1').street ) \n" + "then \n" + "    list.add('r1'); \n" + "end \n";
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final KieSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Person p = new Person("yoda");
    p.getNamedAddresses().put(TestEnum.ONE, new Address("s1"));
    ksession.insert(p);
    ksession.fireAllRules();
    assertEquals("r1", list.get(0));
    // Check it was built with MVELReturnValueExpression constraint
    final List<ObjectTypeNode> nodes = ((InternalKnowledgeBase) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (final ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
            node = n;
            break;
        }
    }
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        assertTrue(((MvelConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
        assertEquals(new Address("s1"), ((MvelConstraint) alphanode.getConstraint()).getField().getValue());
    }
    alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        assertTrue(((MvelConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
        assertEquals(new Address("s1").getStreet(), ((MvelConstraint) alphanode.getConstraint()).getField().getValue());
    }
}
Also used : Address(org.drools.compiler.Address) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) TestEnum(org.drools.compiler.TestEnum) MVELObjectClassFieldReader(org.drools.core.base.extractors.MVELObjectClassFieldReader) AlphaNode(org.drools.core.reteoo.AlphaNode) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

AlphaNode (org.drools.core.reteoo.AlphaNode)30 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)29 Test (org.junit.Test)29 KieBase (org.kie.api.KieBase)24 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)19 ClassObjectType (org.drools.core.base.ClassObjectType)15 BetaNode (org.drools.core.reteoo.BetaNode)13 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)11 Person (org.drools.compiler.Person)10 ArrayList (java.util.ArrayList)5 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)5 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)5 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)5 KieSession (org.kie.api.runtime.KieSession)5 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)5 List (java.util.List)4 Address (org.drools.compiler.Address)4 MVELObjectClassFieldReader (org.drools.core.base.extractors.MVELObjectClassFieldReader)4 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)4