use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class AccumulateNode method addAccFunctionDeclarationsToLeftMask.
private void addAccFunctionDeclarationsToLeftMask(InternalKnowledgeBase kbase, LeftTupleSource leftInput, Accumulate accumulate) {
BitMask leftMask = getLeftInferredMask();
ObjectType leftObjectType = leftInput.getObjectType();
if (leftObjectType instanceof ClassObjectType) {
TypeDeclaration typeDeclaration = kbase.getExactTypeDeclaration(((ClassObjectType) leftObjectType).getClassType());
if (typeDeclaration != null && typeDeclaration.isPropertyReactive()) {
List<String> accessibleProperties = typeDeclaration.getAccessibleProperties();
for (Declaration decl : accumulate.getRequiredDeclarations()) {
if (leftObjectType.equals(decl.getPattern().getObjectType())) {
leftMask = leftMask.setAll(calculatePositiveMask(typeDeclaration.getTypeClass(), decl.getPattern().getListenedProperties(), accessibleProperties));
}
}
}
}
setLeftInferredMask(leftMask);
}
use of org.drools.core.base.ClassObjectType 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.base.ClassObjectType 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.base.ClassObjectType 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.base.ClassObjectType in project drools by kiegroup.
the class FromTest method testFromSharing.
@Test
public void testFromSharing() {
// Keeping original test as non-property reactive by default, just allowed.
String drl = "import " + ListsContainer.class.getCanonicalName() + "\n" + "global java.util.List output1;\n" + "global java.util.List output2;\n" + "rule R1 when\n" + " ListsContainer( $list : list1 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output1.add($s);\n" + "end\n" + "rule R2 when\n" + " ListsContainer( $list : list2 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output2.add($s);\n" + "end\n" + "rule R3 when\n" + " ListsContainer( $list : list2 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output2.add($s);\n" + "end\n";
KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(drl, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
ReteDumper.dumpRete(kbase);
List<String> output1 = new ArrayList<String>();
ksession.setGlobal("output1", output1);
List<String> output2 = new ArrayList<String>();
ksession.setGlobal("output2", output2);
FactHandle fh = ksession.insert(new ListsContainer());
ksession.fireAllRules();
assertEquals("bb", output1.get(0));
assertEquals("22", output2.get(0));
assertEquals("22", output2.get(1));
EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(ListsContainer.class));
// There is only 1 LIA
assertEquals(1, otn.getObjectSinkPropagator().size());
LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
// There are only 2 FromNodes since R2 and R3 are sharing the second From
LeftTupleSink[] sinks = lian.getSinkPropagator().getSinks();
assertEquals(2, sinks.length);
// The first from has R1 has sink
assertEquals(1, sinks[0].getSinkPropagator().size());
// The second from has both R2 and R3 as sinks
assertEquals(2, sinks[1].getSinkPropagator().size());
}
Aggregations