use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class PredicateGenerator method generate.
public static void generate(final PredicateStub stub, final Tuple tuple, final Declaration[] previousDeclarations, final Declaration[] localDeclarations, final WorkingMemory workingMemory) {
final String[] globals = stub.getGlobals();
final String[] globalTypes = stub.getGlobalTypes();
// Sort declarations based on their offset, so it can ascend the tuple's parents stack only once
final List<DeclarationMatcher> declarationMatchers = matchDeclarationsToTuple(previousDeclarations);
final ClassGenerator generator = createInvokerClassGenerator(stub, workingMemory).setInterfaces(PredicateExpression.class, CompiledInvoker.class);
generator.addMethod(ACC_PUBLIC, "createContext", generator.methodDescr(Object.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(Boolean.TYPE, InternalFactHandle.class, Tuple.class, Declaration[].class, Declaration[].class, WorkingMemory.class, Object.class), new String[] { "java/lang/Exception" }, new GeneratorHelper.EvaluateMethod() {
public void body(MethodVisitor mv) {
objAstorePos = 9;
int[] previousDeclarationsParamsPos = new int[previousDeclarations.length];
mv.visitVarInsn(ALOAD, 1);
invokeInterface(InternalFactHandle.class, "getObject", Object.class);
mv.visitVarInsn(ASTORE, 1);
mv.visitVarInsn(ALOAD, 2);
cast(LeftTuple.class);
// LeftTuple
mv.visitVarInsn(ASTORE, 7);
Tuple currentTuple = tuple;
for (DeclarationMatcher matcher : declarationMatchers) {
int i = matcher.getOriginalIndex();
previousDeclarationsParamsPos[i] = objAstorePos;
currentTuple = traverseTuplesUntilDeclaration(currentTuple, matcher.getRootDistance(), 7);
mv.visitVarInsn(ALOAD, 3);
push(i);
// declarations[i]
mv.visitInsn(AALOAD);
// workingMemory
mv.visitVarInsn(ALOAD, 5);
mv.visitVarInsn(ALOAD, 7);
invokeInterface(LeftTuple.class, "getFactHandle", InternalFactHandle.class);
// tuple.getFactHandle().getObject()
invokeInterface(InternalFactHandle.class, "getObject", Object.class);
storeObjectFromDeclaration(previousDeclarations[i], previousDeclarations[i].getTypeName());
}
int[] localDeclarationsParamsPos = parseDeclarations(localDeclarations, 4, 2, 5, false);
// @{ruleClassName}.@{methodName}(@foreach{previousDeclarations}, @foreach{localDeclarations}, @foreach{globals})
StringBuilder predicateMethodDescr = new StringBuilder("(");
for (int i = 0; i < previousDeclarations.length; i++) {
// previousDeclarations[i]
load(previousDeclarationsParamsPos[i]);
predicateMethodDescr.append(typeDescr(previousDeclarations[i].getTypeName()));
}
for (int i = 0; i < localDeclarations.length; i++) {
// localDeclarations[i]
load(localDeclarationsParamsPos[i]);
predicateMethodDescr.append(typeDescr(localDeclarations[i].getTypeName()));
}
// @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
parseGlobals(globals, globalTypes, 5, predicateMethodDescr);
predicateMethodDescr.append(")Z");
mv.visitMethodInsn(INVOKESTATIC, stub.getInternalRuleClassName(), stub.getMethodName(), predicateMethodDescr.toString());
mv.visitInsn(IRETURN);
}
});
stub.setPredicate(generator.<PredicateExpression>newInstance());
}
use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class ReturnValueGenerator method generate.
public static void generate(final ReturnValueStub stub, final Tuple tuple, final Declaration[] previousDeclarations, final Declaration[] localDeclarations, final WorkingMemory workingMemory) {
final String[] globals = stub.getGlobals();
final String[] globalTypes = stub.getGlobalTypes();
// Sort declarations based on their offset, so it can ascend the tuple's parents stack only once
final List<DeclarationMatcher> declarationMatchers = matchDeclarationsToTuple(previousDeclarations);
final ClassGenerator generator = createInvokerClassGenerator(stub, workingMemory).setInterfaces(ReturnValueExpression.class, CompiledInvoker.class);
generator.addMethod(ACC_PUBLIC, "createContext", generator.methodDescr(Object.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "replaceDeclaration", generator.methodDescr(null, Declaration.class, Declaration.class)).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(FieldValue.class, Object.class, Tuple.class, Declaration[].class, Declaration[].class, WorkingMemory.class, Object.class), new String[] { "java/lang/Exception" }, new GeneratorHelper.EvaluateMethod() {
public void body(MethodVisitor mv) {
objAstorePos = 9;
int[] previousDeclarationsParamsPos = new int[previousDeclarations.length];
mv.visitVarInsn(ALOAD, 2);
cast(LeftTuple.class);
// LeftTuple
mv.visitVarInsn(ASTORE, 7);
Tuple currentTuple = tuple;
for (DeclarationMatcher matcher : declarationMatchers) {
int i = matcher.getOriginalIndex();
previousDeclarationsParamsPos[i] = objAstorePos;
currentTuple = traverseTuplesUntilDeclaration(currentTuple, matcher.getRootDistance(), 7);
mv.visitVarInsn(ALOAD, 3);
push(i);
// declarations[i]
mv.visitInsn(AALOAD);
// workingMemory
mv.visitVarInsn(ALOAD, 5);
mv.visitVarInsn(ALOAD, 7);
invokeInterface(LeftTuple.class, "getFactHandle", InternalFactHandle.class);
// tuple.getFactHandle().getObject()
invokeInterface(InternalFactHandle.class, "getObject", Object.class);
storeObjectFromDeclaration(previousDeclarations[i], previousDeclarations[i].getTypeName());
}
int[] localDeclarationsParamsPos = parseDeclarations(localDeclarations, 4, 2, 5, false);
// @{ruleClassName}.@{methodName}(@foreach{previousDeclarations}, @foreach{localDeclarations}, @foreach{globals})
StringBuilder returnValueMethodDescr = new StringBuilder("(");
for (int i = 0; i < previousDeclarations.length; i++) {
// previousDeclarations[i]
load(previousDeclarationsParamsPos[i]);
returnValueMethodDescr.append(typeDescr(previousDeclarations[i].getTypeName()));
}
for (int i = 0; i < localDeclarations.length; i++) {
// localDeclarations[i]
load(localDeclarationsParamsPos[i]);
returnValueMethodDescr.append(typeDescr(localDeclarations[i].getTypeName()));
}
// @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
parseGlobals(globals, globalTypes, 5, returnValueMethodDescr);
returnValueMethodDescr.append(")Lorg/drools/core/spi/FieldValue;");
mv.visitMethodInsn(INVOKESTATIC, stub.getInternalRuleClassName(), stub.getMethodName(), returnValueMethodDescr.toString());
mv.visitInsn(ARETURN);
}
});
stub.setReturnValue(generator.<ReturnValueExpression>newInstance());
}
use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class FromNodeVisitor method doVisit.
@Override
protected void doVisit(NetworkNode node, Stack<NetworkNode> nodeStack, StatefulKnowledgeSessionInfo info) {
FromNode fn = (FromNode) node;
DefaultNodeInfo ni = info.getNodeInfo(node);
final FromMemory memory = (FromMemory) info.getSession().getNodeMemory(fn);
ni.setMemoryEnabled(true);
if (fn.isLeftTupleMemoryEnabled()) {
ni.setTupleMemorySize(memory.getBetaMemory().getLeftTupleMemory().size());
long handles = 0;
org.drools.core.util.Iterator it = memory.getBetaMemory().getLeftTupleMemory().iterator();
for (LeftTuple leftTuple = (LeftTuple) it.next(); leftTuple != null; leftTuple = (LeftTuple) it.next()) {
LeftTuple child = leftTuple.getFirstChild();
while (child != null) {
handles++;
child = child.getHandleNext();
}
}
ni.setCreatedFactHandles(handles);
} else {
info.warn("The left memory for this node is disabled, making it impossible to calculate the number of created handles");
}
}
use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class BetaMemoryLeakOnDeleteTest method testBetaMemoryLeakOnFactDelete.
@Test
public void testBetaMemoryLeakOnFactDelete() {
final String drl = "rule R1 when\n" + " $a : Integer(this == 1)\n" + " $b : String()\n" + " $c : Integer(this == 2)\n" + "then \n" + "end\n" + "rule R2 when\n" + " $a : Integer(this == 1)\n" + " $b : String()\n" + "then \n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
final FactHandle fh1 = ksession.insert(1);
final FactHandle fh2 = ksession.insert("test");
ksession.fireAllRules();
ksession.delete(fh1);
ksession.delete(fh2);
ksession.fireAllRules();
final NodeMemories nodeMemories = ((InternalWorkingMemory) ksession).getNodeMemories();
for (int i = 0; i < nodeMemories.length(); i++) {
final Memory memory = nodeMemories.peekNodeMemory(i);
if (memory != null && memory.getSegmentMemory() != null) {
final LeftTuple deleteFirst = memory.getSegmentMemory().getStagedLeftTuples().getDeleteFirst();
assertThat(deleteFirst).isNull();
}
}
}
use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class TraitTest method testTupleIntegrityOnModification.
@Test
public void testTupleIntegrityOnModification() {
String drl = "package test " + "import " + Entity.class.getName() + ";" + "global java.util.List list; " + "declare trait A @propertyReactive value : int end " + "rule Trait when " + " $core: Entity( ) " + "then " + " A o = don( $core, A.class ); " + " System.out.println( 'Found ! ' + o ); " + "end " + "rule Test when " + " $x: A( value == 0 ) " + "then " + " list.add( 0 ); " + "end " + "rule Check when " + " $x: A( value == 42 ) " + "then " + " list.add( 42 ); " + "end " + "rule Mood when " + " $x : A( value != 42 ) " + "then " + " modify ( $x ) { setValue( 42 ); } " + "end ";
KieBase kbase = getKieBaseFromString(drl);
TraitFactory.setMode(mode, kbase);
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(new Entity());
ksession.fireAllRules();
for (final Object o : ksession.getObjects(new ObjectFilter() {
@Override
public boolean accept(Object object) {
return object.getClass().getName().contains("test.A");
}
})) {
InternalFactHandle handle = (InternalFactHandle) ksession.getFactHandle(o);
LeftTuple first = handle.getFirstLeftTuple();
assertTrue(first instanceof RuleTerminalNodeLeftTuple);
assertEquals("Check", ((RuleTerminalNodeLeftTuple) first).getRule().getName());
}
assertEquals(Arrays.asList(0, 42), list);
}
Aggregations