use of org.mvel2.tests.core.res.WorkingMemory 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.mvel2.tests.core.res.WorkingMemory in project drools by kiegroup.
the class MvelConditionEvaluator method evaluateIfNecessary.
private void evaluateIfNecessary(InternalFactHandle handle, InternalWorkingMemory workingMemory, Tuple tuple, ASTNode node) {
if (!isEvaluated(node)) {
ASTNode next = node.nextASTNode;
node.nextASTNode = null;
evaluate(asCompiledExpression(node), handle, workingMemory, tuple);
node.nextASTNode = next;
}
}
use of org.mvel2.tests.core.res.WorkingMemory in project drools by kiegroup.
the class MvelConstraint method createMvelConditionEvaluator.
protected ConditionEvaluator createMvelConditionEvaluator(InternalWorkingMemory workingMemory) {
if (compilationUnit != null) {
MVELDialectRuntimeData data = getMVELDialectRuntimeData(workingMemory);
ExecutableStatement statement = (ExecutableStatement) compilationUnit.getCompiledExpression(data, evaluationContext);
ParserConfiguration configuration = statement instanceof CompiledExpression ? ((CompiledExpression) statement).getParserConfiguration() : data.getParserConfiguration();
return new MvelConditionEvaluator(compilationUnit, configuration, statement, declarations, operators, getAccessedClass());
} else {
return new MvelConditionEvaluator(getParserConfiguration(workingMemory), expression, declarations, operators, getAccessedClass());
}
}
use of org.mvel2.tests.core.res.WorkingMemory in project mvel by mvel.
the class CoreConfidenceTests method testsequentialAccessorsThenMethodCall.
public void testsequentialAccessorsThenMethodCall() {
String expr = "System.out.println(drools.workingMemory); " + "drools.workingMemory.ruleBase.removeRule(\"org.drools.examples\", \"some rule\"); ";
ParserContext context = new ParserContext();
context.setStrictTypeEnforcement(true);
context.addInput("drools", KnowledgeHelper.class);
RuleBase ruleBase = new RuleBaseImpl();
WorkingMemory wm = new WorkingMemoryImpl(ruleBase);
KnowledgeHelper drools = new DefaultKnowledgeHelper(wm);
Map vars = new HashMap();
vars.put("drools", drools);
ExpressionCompiler compiler = new ExpressionCompiler(expr, context);
executeExpression(compiler.compile(), vars);
}
use of org.mvel2.tests.core.res.WorkingMemory in project drools by kiegroup.
the class ASMConsequenceStubBuilder method createStubConsequence.
private void createStubConsequence(final ClassGenerator generator, final InvokerDataProvider data, final Map<String, Object> vars) {
generator.setInterfaces(ConsequenceStub.class, CompiledInvoker.class).addField(ACC_PRIVATE + ACC_VOLATILE, "consequence", Consequence.class);
generator.addMethod(ACC_PUBLIC, "getName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
push((String) vars.get("consequenceName"));
// return the first object on the stack
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "getNotPatterns", generator.methodDescr(Boolean[].class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
returnAsArray((Boolean[]) vars.get("notPatterns"));
}
}).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(null, KnowledgeHelper.class, WorkingMemory.class), new String[] { "java/lang/Exception" }, new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
Label syncStart = new Label();
Label syncEnd = new Label();
Label l1 = new Label();
Label l2 = new Label();
mv.visitTryCatchBlock(syncStart, l1, l2, null);
Label l3 = new Label();
mv.visitTryCatchBlock(l2, l3, l2, null);
getFieldFromThis("consequence", Consequence.class);
mv.visitJumpInsn(IFNONNULL, syncEnd);
mv.visitVarInsn(ALOAD, 0);
mv.visitInsn(DUP);
mv.visitVarInsn(ASTORE, 3);
// synchronized(this) {
mv.visitInsn(MONITORENTER);
mv.visitLabel(syncStart);
getFieldFromThis("consequence", Consequence.class);
// if (consequence == null) ...
Label ifNotInitialized = new Label();
mv.visitJumpInsn(IFNONNULL, ifNotInitialized);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 2);
// ... ConsequenceGenerator.generate(this, knowledgeHelper, workingMemory)
invokeStatic(ConsequenceGenerator.class, "generate", null, ConsequenceStub.class, KnowledgeHelper.class, WorkingMemory.class);
mv.visitLabel(ifNotInitialized);
mv.visitVarInsn(ALOAD, 3);
mv.visitInsn(MONITOREXIT);
mv.visitLabel(l1);
mv.visitJumpInsn(GOTO, syncEnd);
mv.visitLabel(l2);
mv.visitVarInsn(ASTORE, 4);
mv.visitVarInsn(ALOAD, 3);
mv.visitInsn(MONITOREXIT);
mv.visitLabel(l3);
mv.visitVarInsn(ALOAD, 4);
mv.visitInsn(ATHROW);
mv.visitLabel(syncEnd);
// } end of synchronized
getFieldFromThis("consequence", Consequence.class);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 2);
invokeInterface(Consequence.class, "evaluate", null, KnowledgeHelper.class, WorkingMemory.class);
mv.visitInsn(RETURN);
}
}).addMethod(ACC_PUBLIC, "setConsequence", generator.methodDescr(null, Consequence.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
putFieldInThisFromRegistry("consequence", Consequence.class, 1);
mv.visitInsn(RETURN);
}
});
}
Aggregations