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);
}
}
}
}
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));
}
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));
}
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));
}
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;
}
}
}
Aggregations