use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.
the class LinkingTest method testNestedSubNetwork.
@Test
public void testNestedSubNetwork() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "import " + F.class.getCanonicalName() + "\n";
str += "import " + G.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " A() \n";
str += " B() \n";
str += " exists( C() and D() and exists( E() and F() ) ) \n";
str += " G() \n";
str += "then \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
assertEquals(1, liaNode.getSinkPropagator().size());
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
assertEquals(2, bNode.getSinkPropagator().size());
ExistsNode exists1n = (ExistsNode) bNode.getSinkPropagator().getSinks()[1];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
JoinNode dNode = (JoinNode) cNode.getSinkPropagator().getSinks()[0];
assertEquals(2, dNode.getSinkPropagator().size());
ExistsNode exists2n = (ExistsNode) dNode.getSinkPropagator().getSinks()[1];
JoinNode eNode = (JoinNode) dNode.getSinkPropagator().getSinks()[0];
JoinNode fNode = (JoinNode) eNode.getSinkPropagator().getSinks()[0];
RightInputAdapterNode riaNode2 = (RightInputAdapterNode) fNode.getSinkPropagator().getSinks()[0];
assertEquals(exists2n, riaNode2.getObjectSinkPropagator().getSinks()[0]);
RightInputAdapterNode riaNode1 = (RightInputAdapterNode) exists2n.getSinkPropagator().getSinks()[0];
assertEquals(exists1n, riaNode1.getObjectSinkPropagator().getSinks()[0]);
JoinNode gNode = (JoinNode) exists1n.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) gNode.getSinkPropagator().getSinks()[0];
wm.insert(new A());
wm.insert(new B());
wm.insert(new C());
wm.insert(new D());
wm.insert(new F());
wm.insert(new G());
PathMemory rs = (PathMemory) wm.getNodeMemory(rtn);
assertFalse(rs.isRuleLinked());
FactHandle fhE1 = wm.insert(new E());
FactHandle fhE2 = wm.insert(new E());
wm.flushPropagations();
assertTrue(rs.isRuleLinked());
wm.retract(fhE1);
wm.flushPropagations();
assertTrue(rs.isRuleLinked());
wm.retract(fhE2);
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
}
use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.
the class LinkingTest method testSubNetworkRiaLinking.
@Test
public void testSubNetworkRiaLinking() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " A() \n";
str += " B() \n";
str += " exists( C() and D() ) \n";
str += " E() \n";
str += "then \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
assertEquals(1, liaNode.getSinkPropagator().size());
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
assertEquals(2, bNode.getSinkPropagator().size());
ExistsNode exists1n = (ExistsNode) bNode.getSinkPropagator().getSinks()[1];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
JoinNode dNode = (JoinNode) cNode.getSinkPropagator().getSinks()[0];
assertEquals(1, dNode.getSinkPropagator().size());
RightInputAdapterNode riaNode1 = (RightInputAdapterNode) dNode.getSinkPropagator().getSinks()[0];
JoinNode eNode = (JoinNode) exists1n.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) eNode.getSinkPropagator().getSinks()[0];
SegmentUtilities.createSegmentMemory(exists1n, wm);
BetaMemory existsBm = (BetaMemory) wm.getNodeMemory(exists1n);
assertEquals(0, existsBm.getSegmentMemory().getLinkedNodeMask());
FactHandle fhc = wm.insert(new C());
FactHandle fhd = wm.insert(new D());
wm.flushPropagations();
// exists is start of new segment
assertEquals(1, existsBm.getSegmentMemory().getLinkedNodeMask());
wm.retract(fhd);
wm.flushPropagations();
assertEquals(0, existsBm.getSegmentMemory().getLinkedNodeMask());
PathMemory rs = (PathMemory) wm.getNodeMemory(rtn);
assertFalse(rs.isRuleLinked());
wm.insert(new A());
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
wm.insert(new B());
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
wm.insert(new E());
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
wm.insert(new D());
wm.flushPropagations();
assertTrue(rs.isRuleLinked());
wm.retract(fhc);
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
wm.insert(new C());
wm.flushPropagations();
assertTrue(rs.isRuleLinked());
}
use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.
the class ASMConsequenceBuilder method createConsequenceBytecode.
protected byte[] createConsequenceBytecode(RuleBuildContext ruleContext, final Map<String, Object> consequenceContext) {
final InvokerDataProvider data = new InvokerContext(consequenceContext);
final String name = (String) consequenceContext.get("consequenceName");
final Declaration[] declarations = (Declaration[]) consequenceContext.get("declarations");
final ClassGenerator generator = InvokerGenerator.createInvokerClassGenerator(data, ruleContext).setInterfaces(Consequence.class, CompiledInvoker.class);
generator.addMethod(ACC_PUBLIC, "getName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
push(name);
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(null, KnowledgeHelper.class, WorkingMemory.class), new String[] { "java/lang/Exception" }, new GeneratorHelper.EvaluateMethod() {
public void body(MethodVisitor mv) {
// Tuple tuple = knowledgeHelper.getTuple();
mv.visitVarInsn(ALOAD, 1);
invokeInterface(KnowledgeHelper.class, "getTuple", Tuple.class);
mv.visitVarInsn(ASTORE, 3);
// Declaration[] declarations = ((RuleTerminalNode)knowledgeHelper.getMatch().getTuple().getTupleSink()).getDeclarations();
mv.visitVarInsn(ALOAD, 1);
invokeInterface(KnowledgeHelper.class, "getMatch", Activation.class);
invokeInterface(Activation.class, "getTuple", Tuple.class);
invokeInterface(Tuple.class, "getTupleSink", Sink.class);
cast(RuleTerminalNode.class);
invokeVirtual(RuleTerminalNode.class, "getRequiredDeclarations", Declaration[].class);
mv.visitVarInsn(ASTORE, 4);
final String[] globals = data.getGlobals();
final String[] globalTypes = data.getGlobalTypes();
final Boolean[] notPatterns = (Boolean[]) consequenceContext.get("notPatterns");
int[] paramsPos = new int[declarations.length];
int offset = 5;
for (int i = 0; i < declarations.length; i++) {
int factPos = offset;
int objPos = ++offset;
paramsPos[i] = factPos;
// Object obj[i] = tuple.get(declarations[i]);
// org.kie.spi.Tuple
mv.visitVarInsn(ALOAD, 3);
// org.kie.rule.Declaration[]
mv.visitVarInsn(ALOAD, 4);
// i
push(i);
// declarations[i]
mv.visitInsn(AALOAD);
invokeInterface(Tuple.class, "get", InternalFactHandle.class, Declaration.class);
// fact[i]
mv.visitVarInsn(ASTORE, factPos);
// declarations[i].getValue((org.kie.common.InternalWorkingMemory)workingMemory, obj[i] );
// org.kie.rule.Declaration[]
mv.visitVarInsn(ALOAD, 4);
// i
push(i);
// declarations[i]
mv.visitInsn(AALOAD);
// WorkingMemory
mv.visitVarInsn(ALOAD, 2);
cast(InternalWorkingMemory.class);
// fact[i]
mv.visitVarInsn(ALOAD, factPos);
invokeInterface(InternalFactHandle.class, "getObject", Object.class);
String readMethod = declarations[i].getNativeReadMethodName();
boolean isObject = readMethod.equals("getValue");
String returnedType = isObject ? "Ljava/lang/Object;" : typeDescr(declarations[i].getTypeName());
mv.visitMethodInsn(INVOKEVIRTUAL, Declaration.class.getName().replace('.', '/'), readMethod, "(L" + InternalWorkingMemory.class.getName().replace('.', '/') + ";Ljava/lang/Object;)" + returnedType);
if (isObject)
mv.visitTypeInsn(CHECKCAST, internalName(declarations[i].getTypeName()));
// obj[i]
offset += store(objPos, declarations[i].getTypeName());
if (notPatterns[i]) {
mv.visitVarInsn(ALOAD, 1);
invokeInterface(KnowledgeHelper.class, "getWorkingMemory", WorkingMemory.class);
loadAsObject(objPos);
invokeInterface(WorkingMemory.class, "getFactHandle", FactHandle.class, Object.class);
cast(InternalFactHandle.class);
mv.visitVarInsn(ASTORE, factPos);
}
}
// @{ruleClassName}.@{methodName}(KnowledgeHelper, @foreach{declr : declarations} Object, FactHandle @end)
StringBuilder consequenceMethodDescr = new StringBuilder("(L" + KnowledgeHelper.class.getName().replace('.', '/') + ";");
// KnowledgeHelper
mv.visitVarInsn(ALOAD, 1);
for (int i = 0; i < declarations.length; i++) {
// obj[i]
load(paramsPos[i] + 1);
// fact[i]
mv.visitVarInsn(ALOAD, paramsPos[i]);
consequenceMethodDescr.append(typeDescr(declarations[i].getTypeName())).append("L" + FactHandle.class.getName().replace('.', '/') + ";");
}
// @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
for (int i = 0; i < globals.length; i++) {
// WorkingMemory
mv.visitVarInsn(ALOAD, 2);
push(globals[i]);
invokeInterface(WorkingMemory.class, "getGlobal", Object.class, String.class);
mv.visitTypeInsn(CHECKCAST, internalName(globalTypes[i]));
consequenceMethodDescr.append(typeDescr(globalTypes[i]));
}
consequenceMethodDescr.append(")V");
mv.visitMethodInsn(INVOKESTATIC, data.getInternalRuleClassName(), data.getMethodName(), consequenceMethodDescr.toString());
mv.visitInsn(RETURN);
}
});
return generator.generateBytecode();
}
use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.
the class TerminalNodeIteratorTest method testTerminalNodeListener.
@Test
public void testTerminalNodeListener() {
String str = "package org.kie.test \n" + "\n" + "rule rule1 when\n" + "then\n" + "end\n" + "rule rule2 when\n" + "then\n" + "end\n" + "rule rule3 when\n" + " Object()" + "then\n" + "end\n" + "rule rule4 when\n" + " Object()" + "then\n" + "end\n" + // this will result in two terminal nodes
"rule rule5 when\n" + " Object() or\n" + " Object()\n" + "then\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
List<String> nodes = new ArrayList<String>();
Iterator it = TerminalNodeIterator.iterator(kbase);
for (TerminalNode node = (TerminalNode) it.next(); node != null; node = (TerminalNode) it.next()) {
nodes.add(((RuleTerminalNode) node).getRule().getName());
}
assertEquals(6, nodes.size());
assertTrue(nodes.contains("rule1"));
assertTrue(nodes.contains("rule2"));
assertTrue(nodes.contains("rule3"));
assertTrue(nodes.contains("rule4"));
assertTrue(nodes.contains("rule5"));
int first = nodes.indexOf("rule5");
int second = nodes.lastIndexOf("rule5");
assertTrue(first != second);
}
use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.
the class ReteooRuleBuilderTest method testAddRuleWithPatterns.
@Test
public void testAddRuleWithPatterns() {
final RuleImpl rule = new RuleImpl("only patterns");
final Pattern c1 = new Pattern(0, new ClassObjectType(String.class));
final Pattern c2 = new Pattern(1, new ClassObjectType(String.class));
final Pattern c3 = new Pattern(2, new ClassObjectType(String.class));
final GroupElement lhsroot = GroupElementFactory.newAndInstance();
lhsroot.addChild(c1);
lhsroot.addChild(c2);
lhsroot.addChild(c3);
rule.setLhs(lhsroot);
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
System.out.println("Consequence!");
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
};
rule.setConsequence(consequence);
final List terminals = this.builder.addRule(rule, this.rulebase);
assertEquals("Rule must have a single terminal node", 1, terminals.size());
final RuleTerminalNode terminal = (RuleTerminalNode) terminals.get(0);
}
Aggregations