use of org.drools.core.rule.builder.dialect.asm.ClassGenerator in project drools by kiegroup.
the class ClassGeneratorTest method testGenerateBean.
@Test
public void testGenerateBean() {
final String MY_NAME = "myName";
ClassGenerator generator = new ClassGenerator("pkg.Bean", getClass().getClassLoader()).addField(ACC_PRIVATE | ACC_FINAL, "name", String.class);
generator.addDefaultConstructor(new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
// read local variable 0 (initialized to this) and push it on the stack
mv.visitVarInsn(ALOAD, 0);
// push the String MY_NAME on the stack
mv.visitLdcInsn(MY_NAME);
putFieldInThis("name", String.class);
mv.visitInsn(RETURN);
}
}).addMethod(ACC_PUBLIC, "toString", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
getFieldFromThis("name", String.class);
// return the first object on the stack
mv.visitInsn(ARETURN);
}
});
Object instance = generator.newInstance();
assertEquals(MY_NAME, instance.toString());
}
use of org.drools.core.rule.builder.dialect.asm.ClassGenerator in project drools by kiegroup.
the class InvokerGeneratorTest method testGenerate.
@Test
public void testGenerate() {
Map<String, Object> invokerContext = new HashMap<String, Object>();
invokerContext.put("package", "pkg");
invokerContext.put("invokerClassName", "TestInvoker");
invokerContext.put("ruleClassName", "TestRule");
invokerContext.put("methodName", "testMethod");
invokerContext.put("consequenceName", "TestConsequence");
invokerContext.put("hashCode", 111);
invokerContext.put("declarations", new Declaration[0]);
invokerContext.put("globals", new String[] { "globalList" });
invokerContext.put("globalTypes", new String[] { "java/util/List" });
Set<String> imports = new HashSet<String>();
imports.add("p1");
imports.add("p2");
final ClassGenerator generator = InvokerGenerator.createStubGenerator(new InvokerContext(invokerContext), getClass().getClassLoader(), null, imports);
generator.setInterfaces(InvokerStub.class);
InvokerStub stub = generator.newInstance();
assertEquals("pkg", stub.getPackageName());
assertEquals("TestRule", stub.getRuleClassName());
assertEquals("testMethod", stub.getMethodName());
assertEquals("TestInvokerGenerated", stub.getGeneratedInvokerClassName());
assertEquals(111, stub.hashCode());
assertTrue(Arrays.equals(new String[] { "globalList" }, stub.getGlobals()));
assertTrue(Arrays.equals(new String[] { "java/util/List" }, stub.getGlobalTypes()));
List<String> importList = Arrays.asList(stub.getPackageImports());
assertTrue(importList.contains("p1"));
assertTrue(importList.contains("p2"));
}
use of org.drools.core.rule.builder.dialect.asm.ClassGenerator in project drools by kiegroup.
the class ASMConsequenceStubBuilder method createConsequenceBytecode.
protected byte[] createConsequenceBytecode(final RuleBuildContext ruleContext, final Map<String, Object> consequenceContext) {
// If the LHS contains an OR we cannot use the optimized consequence invoker that traverses the tuple only once
if (isOr(ruleContext.getRule().getLhs())) {
return super.createConsequenceBytecode(ruleContext, consequenceContext);
}
final InvokerDataProvider data = new InvokerContext(consequenceContext);
final ClassGenerator generator = InvokerGenerator.createInvokerStubGenerator(data, ruleContext);
createStubConsequence(generator, data, consequenceContext);
return generator.generateBytecode();
}
use of org.drools.core.rule.builder.dialect.asm.ClassGenerator in project drools by kiegroup.
the class ASMPredicateStubBuilder method createStubPredicate.
private void createStubPredicate(final ClassGenerator generator, final InvokerDataProvider data, final Map vars) {
generator.setInterfaces(PredicateStub.class, CompiledInvoker.class).addField(ACC_PRIVATE + ACC_VOLATILE, "predicate", PredicateExpression.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 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("predicate", PredicateExpression.class);
mv.visitJumpInsn(IFNONNULL, syncEnd);
mv.visitVarInsn(ALOAD, 0);
mv.visitInsn(DUP);
mv.visitVarInsn(ASTORE, 7);
// synchronized(this) {
mv.visitInsn(MONITORENTER);
mv.visitLabel(syncStart);
getFieldFromThis("predicate", PredicateExpression.class);
// if (predicate == null) ...
Label ifNotInitialized = new Label();
mv.visitJumpInsn(IFNONNULL, ifNotInitialized);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 2);
mv.visitVarInsn(ALOAD, 3);
mv.visitVarInsn(ALOAD, 4);
mv.visitVarInsn(ALOAD, 5);
// ... PredicateGenerator.generate(this, tuple, declarations, declarations, workingMemory)
invokeStatic(PredicateGenerator.class, "generate", null, PredicateStub.class, Tuple.class, Declaration[].class, Declaration[].class, WorkingMemory.class);
mv.visitLabel(ifNotInitialized);
mv.visitVarInsn(ALOAD, 7);
mv.visitInsn(MONITOREXIT);
mv.visitLabel(l1);
mv.visitJumpInsn(GOTO, syncEnd);
mv.visitLabel(l2);
mv.visitVarInsn(ASTORE, 8);
mv.visitVarInsn(ALOAD, 7);
mv.visitInsn(MONITOREXIT);
mv.visitLabel(l3);
mv.visitVarInsn(ALOAD, 8);
mv.visitInsn(ATHROW);
mv.visitLabel(syncEnd);
// } end of synchronized
getFieldFromThis("predicate", PredicateExpression.class);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 2);
mv.visitVarInsn(ALOAD, 3);
mv.visitVarInsn(ALOAD, 4);
mv.visitVarInsn(ALOAD, 5);
mv.visitVarInsn(ALOAD, 6);
invokeInterface(PredicateExpression.class, "evaluate", Boolean.TYPE, InternalFactHandle.class, Tuple.class, Declaration[].class, Declaration[].class, WorkingMemory.class, Object.class);
mv.visitInsn(IRETURN);
}
}).addMethod(ACC_PUBLIC, "setPredicate", generator.methodDescr(null, PredicateExpression.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
putFieldInThisFromRegistry("predicate", PredicateExpression.class, 1);
mv.visitInsn(RETURN);
}
});
}
use of org.drools.core.rule.builder.dialect.asm.ClassGenerator in project drools by kiegroup.
the class ASMPredicateStubBuilder method createPredicateBytecode.
protected byte[] createPredicateBytecode(final RuleBuildContext ruleContext, final Map vars) {
final InvokerDataProvider data = new InvokerContext(vars);
final ClassGenerator generator = InvokerGenerator.createInvokerStubGenerator(data, ruleContext);
createStubPredicate(generator, data, vars);
return generator.generateBytecode();
}
Aggregations