use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class AbstractASMConsequenceBuilder method consequenceContext.
private Map<String, Object> consequenceContext(RuleBuildContext context, String consequenceName) {
String className = consequenceName + "Consequence";
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule(), consequenceName);
JavaAnalysisResult analysis = JavaRuleBuilderHelper.createJavaAnalysisResult(context, consequenceName, decls);
if (analysis == null) {
// not possible to get the analysis results
return null;
}
// this will fix modify, retract, insert, update, entrypoints and channels
String fixedConsequence = KnowledgeHelperFixer.fix(DialectUtil.fixBlockDescr(context, analysis, decls));
return JavaRuleBuilderHelper.createConsequenceContext(context, consequenceName, className, fixedConsequence, decls, analysis.getBoundIdentifiers());
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class ASMEvalStubBuilder method createStubEval.
private void createStubEval(final ClassGenerator generator, final InvokerDataProvider data, final Map vars) {
generator.setInterfaces(EvalStub.class, CompiledInvoker.class).addField(ACC_PRIVATE + ACC_VOLATILE, "eval", EvalExpression.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, "clone", generator.methodDescr(EvalExpression.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
String internalClassName = generator.getClassName().replace('.', '/');
mv.visitTypeInsn(NEW, internalClassName);
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, internalClassName, "<init>", "()V", false);
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "replaceDeclaration", generator.methodDescr(null, Declaration.class, Declaration.class)).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(Boolean.TYPE, Tuple.class, Declaration[].class, WorkingMemory.class, Object.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("eval", EvalExpression.class);
mv.visitJumpInsn(IFNONNULL, syncEnd);
mv.visitVarInsn(ALOAD, 0);
mv.visitInsn(DUP);
mv.visitVarInsn(ASTORE, 5);
// synchronized(this) {
mv.visitInsn(MONITORENTER);
mv.visitLabel(syncStart);
getFieldFromThis("eval", EvalExpression.class);
// if (eval == null) ...
Label ifNotInitialized = new Label();
mv.visitJumpInsn(IFNONNULL, ifNotInitialized);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 2);
mv.visitVarInsn(ALOAD, 3);
// ... EvalGenerator.generate(this, tuple, declarations, workingMemory)
invokeStatic(EvalGenerator.class, "generate", null, EvalStub.class, Tuple.class, Declaration[].class, WorkingMemory.class);
mv.visitLabel(ifNotInitialized);
mv.visitVarInsn(ALOAD, 5);
mv.visitInsn(MONITOREXIT);
mv.visitLabel(l1);
mv.visitJumpInsn(GOTO, syncEnd);
mv.visitLabel(l2);
mv.visitVarInsn(ASTORE, 6);
mv.visitVarInsn(ALOAD, 5);
mv.visitInsn(MONITOREXIT);
mv.visitLabel(l3);
mv.visitVarInsn(ALOAD, 6);
mv.visitInsn(ATHROW);
mv.visitLabel(syncEnd);
// } end of synchronized
getFieldFromThis("eval", EvalExpression.class);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 2);
mv.visitVarInsn(ALOAD, 3);
mv.visitVarInsn(ALOAD, 4);
invokeInterface(EvalExpression.class, "evaluate", Boolean.TYPE, Tuple.class, Declaration[].class, WorkingMemory.class, Object.class);
mv.visitInsn(IRETURN);
}
}).addMethod(ACC_PUBLIC, "setEval", generator.methodDescr(null, EvalExpression.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
putFieldInThisFromRegistry("eval", EvalExpression.class, 1);
mv.visitInsn(RETURN);
}
});
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class AbstractASMEvalBuilder method getUsedDeclarations.
private Declaration[] getUsedDeclarations(RuleBuildContext context, Pattern pattern, AnalysisResult analysis) {
BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
final List<Declaration> declarations = new ArrayList<Declaration>();
for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
declarations.add(context.getDeclarationResolver().getDeclaration(id));
}
createImplicitBindings(context, pattern, analysis.getNotBoundedIdentifiers(), analysis.getBoundIdentifiers(), declarations);
return declarations.toArray(new Declaration[declarations.size()]);
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class AbstractASMEvalBuilder method build.
public RuleConditionElement build(RuleBuildContext context, BaseDescr descr, Pattern prefixPattern) {
if (prefixPattern == null) {
return build(context, descr);
}
EvalDescr evalDescr = (EvalDescr) descr;
PredicateDescr predicateDescr = new PredicateDescr(context.getRuleDescr().getResource(), evalDescr.getContent());
AnalysisResult analysis = buildAnalysis(context, prefixPattern, predicateDescr, null);
Declaration[] declarations = getUsedDeclarations(context, prefixPattern, analysis);
return buildEval(context, evalDescr, analysis, declarations);
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class AbstractASMEvalBuilder method build.
public RuleConditionElement build(RuleBuildContext context, BaseDescr descr) {
// it must be an EvalDescr
final EvalDescr evalDescr = (EvalDescr) descr;
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
AnalysisResult analysis = context.getDialect().analyzeExpression(context, evalDescr, evalDescr.getContent(), new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
List<Declaration> requiredDeclarations = new ArrayList<Declaration>();
for (String usedIdentifier : analysis.getIdentifiers()) {
Declaration usedDec = decls.get(usedIdentifier);
if (usedDec != null) {
requiredDeclarations.add(usedDec);
}
}
final Declaration[] declarations = requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]);
return buildEval(context, evalDescr, analysis, declarations);
}
Aggregations