Search in sources :

Example 1 with BaseNode

use of org.drools.core.common.BaseNode in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeNodeMemories.

private static void writeNodeMemories(MarshallerWriteContext context, ProtobufMessages.RuleData.Builder _ksb) throws IOException {
    InternalWorkingMemory wm = context.wm;
    NodeMemories memories = wm.getNodeMemories();
    // so we iterate over all of them and process only those that require it
    for (BaseNode baseNode : context.sinks.values()) {
        Memory memory = memories.peekNodeMemory(baseNode);
        if (memory != null) {
            ProtobufMessages.NodeMemory _node = null;
            switch(memory.getNodeType()) {
                case NodeTypeEnums.AccumulateNode:
                    {
                        _node = writeAccumulateNodeMemory(baseNode.getId(), memory);
                        break;
                    }
                case NodeTypeEnums.RightInputAdaterNode:
                    {
                        _node = writeRIANodeMemory(baseNode.getId(), baseNode, memories);
                        break;
                    }
                case NodeTypeEnums.FromNode:
                case NodeTypeEnums.ReactiveFromNode:
                    {
                        _node = writeFromNodeMemory(baseNode.getId(), memory);
                        break;
                    }
                case NodeTypeEnums.QueryElementNode:
                    {
                        _node = writeQueryElementNodeMemory(baseNode.getId(), memory, wm);
                        break;
                    }
            }
            if (_node != null) {
                // not all node memories require serialization
                _ksb.addNodeMemory(_node);
            }
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Memory(org.drools.core.common.Memory) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) BaseNode(org.drools.core.common.BaseNode) NodeMemories(org.drools.core.common.NodeMemories)

Example 2 with BaseNode

use of org.drools.core.common.BaseNode in project drools by kiegroup.

the class MarshallingTest method testSingleRuleSingleJoinNodePattern.

@Test
public void testSingleRuleSingleJoinNodePattern() throws Exception {
    String rule = "package org.drools.compiler.test;\n";
    rule += "import org.drools.compiler.Person\n";
    rule += "import org.drools.compiler.Cheese\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Rule 1\"\n";
    rule += "when\n";
    rule += "    $c : Cheese( ) \n";
    rule += "    $p : Person( cheese == $c ) \n";
    rule += "then\n";
    rule += "    list.add( $p );\n";
    rule += "end";
    KieBase kBase = loadKnowledgeBaseFromString(rule);
    // Make sure the rete node map is created correctly
    Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
    assertEquals(5, nodes.size());
    assertEquals("Cheese", ((ClassObjectType) ((ObjectTypeNode) nodes.get(3)).getObjectType()).getClassType().getSimpleName());
    assertEquals("Person", ((ClassObjectType) ((ObjectTypeNode) nodes.get(5)).getObjectType()).getClassType().getSimpleName());
    assertTrue("Should end with JoinNode", nodes.get(6).getClass().getSimpleName().endsWith("JoinNode"));
    assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(7)).getRule().getName());
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    Cheese stilton = new Cheese("stilton", 25);
    Cheese brie = new Cheese("brie", 49);
    Person bobba = new Person("bobba fet", 32);
    bobba.setCheese(stilton);
    Person vadar = new Person("darth vadar", 32);
    session.insert(stilton);
    session.insert(bobba);
    session.insert(vadar);
    session.insert(brie);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(1, ((List) session.getGlobal("list")).size());
    assertEquals(bobba, ((List) session.getGlobal("list")).get(0));
    Person c3po = new Person("c3p0", 32);
    c3po.setCheese(stilton);
    session.insert(c3po);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(2, ((List) session.getGlobal("list")).size());
    assertEquals(c3po, ((List) session.getGlobal("list")).get(1));
    Person r2d2 = new Person("r2d2", 32);
    r2d2.setCheese(brie);
    session.insert(r2d2);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(3, ((List) session.getGlobal("list")).size());
    assertEquals(r2d2, ((List) session.getGlobal("list")).get(2));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 3 with BaseNode

use of org.drools.core.common.BaseNode in project drools by kiegroup.

the class MarshallingTest method testEmptyRule.

@Test
public void testEmptyRule() throws Exception {
    String rule = "package org.drools.compiler.test;\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Rule 1\"\n";
    rule += "when\n";
    rule += "then\n";
    rule += "    list.add( \"fired\" );\n";
    rule += "end";
    KieBase kBase = loadKnowledgeBaseFromString(rule);
    // Make sure the rete node map is created correctly
    Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
    assertEquals(2, nodes.size());
    assertEquals("InitialFactImpl", ((ClassObjectType) ((ObjectTypeNode) nodes.get(2)).getObjectType()).getClassType().getSimpleName());
    assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(4)).getRule().getName());
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    assertEquals(1, ((List) session.getGlobal("list")).size());
    assertEquals("fired", ((List) session.getGlobal("list")).get(0));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 4 with BaseNode

use of org.drools.core.common.BaseNode in project drools by kiegroup.

the class MarshallingTest method testSinglePattern.

@Test
public void testSinglePattern() throws Exception {
    String rule = "package org.drools.compiler.test;\n";
    rule += "import org.drools.compiler.Person\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Rule 1\"\n";
    rule += "when\n";
    rule += "    $p : Person( ) \n";
    rule += "then\n";
    rule += "    list.add( $p );\n";
    rule += "end";
    KieBase kBase = loadKnowledgeBaseFromString(rule);
    // Make sure the rete node map is created correctly
    Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
    assertEquals(3, nodes.size());
    assertEquals("Person", ((ClassObjectType) ((ObjectTypeNode) nodes.get(3)).getObjectType()).getClassType().getSimpleName());
    assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(5)).getRule().getName());
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    Person p = new Person("bobba fet", 32);
    session.insert(p);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(1, ((List) session.getGlobal("list")).size());
    assertEquals(p, ((List) session.getGlobal("list")).get(0));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Person(org.drools.compiler.Person) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 5 with BaseNode

use of org.drools.core.common.BaseNode in project drools by kiegroup.

the class ReteooBuilder method removePath.

/**
 * Path's must be removed starting from the outer most path, iterating towards the inner most path.
 * Each time it reaches a subnetwork beta node, the current path evaluation ends, and instead the subnetwork
 * path continues.
 */
private void removePath(InternalWorkingMemory[] wms, RuleRemovalContext context, Map<Integer, BaseNode> stillInUse, Collection<ObjectSource> alphas, PathEndNode endNode) {
    LeftTupleNode[] nodes = endNode.getPathNodes();
    for (int i = endNode.getPositionInPath(); i >= 0; i--) {
        BaseNode node = (BaseNode) nodes[i];
        boolean removed = false;
        if (NodeTypeEnums.isLeftTupleNode(node)) {
            removed = removeLeftTupleNode(wms, context, stillInUse, node);
        }
        if (removed) {
            // reteoo requires to call remove on the OTN for tuples cleanup
            if (NodeTypeEnums.isBetaNode(node) && !((BetaNode) node).isRightInputIsRiaNode()) {
                alphas.add(((BetaNode) node).getRightInput());
            } else if (node.getType() == NodeTypeEnums.LeftInputAdapterNode) {
                alphas.add(((LeftInputAdapterNode) node).getObjectSource());
            }
        }
        if (NodeTypeEnums.isBetaNode(node) && ((BetaNode) node).isRightInputIsRiaNode()) {
            endNode = (PathEndNode) ((BetaNode) node).getRightInput();
            removePath(wms, context, stillInUse, alphas, endNode);
            return;
        }
    }
}
Also used : BaseNode(org.drools.core.common.BaseNode)

Aggregations

BaseNode (org.drools.core.common.BaseNode)14 ArrayList (java.util.ArrayList)5 List (java.util.List)4 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)4 ClassObjectType (org.drools.core.base.ClassObjectType)4 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)4 Test (org.junit.Test)4 KieSession (org.kie.api.runtime.KieSession)4 KieBase (org.kie.api.KieBase)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Person (org.drools.compiler.Person)2 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2 Sink (org.drools.core.reteoo.Sink)2 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Cheese (org.drools.compiler.Cheese)1 ActivationListenerFactory (org.drools.core.ActivationListenerFactory)1 QueryResultsImpl (org.drools.core.QueryResultsImpl)1 DroolsQuery (org.drools.core.base.DroolsQuery)1