Search in sources :

Example 61 with ObjectTypeNode

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

the class ReteDumper method getSinks.

public static Sink[] getSinks(BaseNode node) {
    Sink[] sinks = null;
    if (node instanceof EntryPointNode) {
        EntryPointNode source = (EntryPointNode) node;
        Collection<ObjectTypeNode> otns = source.getObjectTypeNodes().values();
        sinks = otns.toArray(new Sink[otns.size()]);
    } else if (node instanceof ObjectSource) {
        ObjectSource source = (ObjectSource) node;
        sinks = source.getObjectSinkPropagator().getSinks();
    } else if (node instanceof LeftTupleSource) {
        LeftTupleSource source = (LeftTupleSource) node;
        sinks = source.getSinkPropagator().getSinks();
    }
    return sinks;
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) Sink(org.drools.core.reteoo.Sink) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ObjectSource(org.drools.core.reteoo.ObjectSource)

Example 62 with ObjectTypeNode

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

Example 63 with ObjectTypeNode

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

the class IndexingTest method testAlphaNodeSharing.

@Test()
public void testAlphaNodeSharing() {
    String drl = "";
    drl += "package org.drools.compiler.test\n";
    drl += "import " + Person.class.getCanonicalName() + "\n";
    drl += "rule r1\n";
    drl += "when\n";
    drl += "   Person(name == \"Mark\")\n";
    drl += "then\n";
    drl += "end\n";
    drl += "rule r2\n";
    drl += "when\n";
    drl += "   Person(name == \"Mark\", age == 40)\n";
    drl += "then\n";
    drl += "end\n";
    drl += "rule r3\n";
    drl += "when\n";
    drl += "   Person(name == \"Mark\", age == 50)\n";
    drl += "then\n";
    drl += "end\n";
    drl += "rule r4\n";
    drl += "when\n";
    drl += "   Person(name == \"John\", age == 60)\n";
    drl += "then\n";
    drl += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(drl);
    Map<String, Rule> rules = rulestoMap(kbase);
    ObjectTypeNode otn = getObjectTypeNode(kbase, Person.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    assertEquals(2, otn.getObjectSinkPropagator().size());
    AlphaNode a1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
    assertEquals(3, a1.getObjectSinkPropagator().size());
    assertEquals(3, a1.getAssociationsSize());
    assertTrue(a1.isAssociatedWith(rules.get("r1")));
    assertTrue(a1.isAssociatedWith(rules.get("r2")));
    assertTrue(a1.isAssociatedWith(rules.get("r3")));
    AlphaNode a2 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[1];
    assertEquals(1, a2.getAssociationsSize());
    assertEquals(1, a2.getObjectSinkPropagator().size());
    assertTrue(a2.isAssociatedWith(rules.get("r4")));
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Rule(org.kie.api.definition.rule.Rule) Person(org.drools.compiler.Person) AlphaNode(org.drools.core.reteoo.AlphaNode) Test(org.junit.Test)

Example 64 with ObjectTypeNode

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

the class TraitTest method testAlphaNodeSharing.

@Test
public void testAlphaNodeSharing() {
    String drl = "package test; " + "import " + Entity.class.getName() + " " + "declare trait Person\n" + "    name : String\n" + "end\n" + "rule Init " + "when " + "then " + "    don( new Entity(), Person.class ); " + "end\n" + "rule One when" + "    $core: Entity( this isA Person ) " + "then " + "end " + "rule Two when" + "    $core: Entity( this isA Person ) " + "then " + "end " + "\n";
    final KieBase kbase = getKieBaseFromString(drl);
    TraitFactory.setMode(mode, kbase);
    KieSession kSession = kbase.newKieSession();
    assertEquals(3, kSession.fireAllRules());
    NamedEntryPoint nep = ((NamedEntryPoint) kSession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId()));
    ObjectTypeNode otn = nep.getEntryPointNode().getObjectTypeNodes().get(new ClassObjectType(Entity.class));
    assertNotNull(otn);
    assertEquals(1, otn.getObjectSinkPropagator().getSinks().length);
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 65 with ObjectTypeNode

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

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