Search in sources :

Example 1 with ClassGenerator

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());
}
Also used : ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator)

Example 2 with ClassGenerator

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"));
}
Also used : ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) InvokerStub(org.drools.core.rule.builder.dialect.asm.InvokerStub)

Example 3 with ClassGenerator

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();
}
Also used : ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) InvokerDataProvider(org.drools.core.rule.builder.dialect.asm.InvokerDataProvider)

Example 4 with ClassGenerator

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);
        }
    });
}
Also used : ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) PredicateStub(org.drools.core.rule.builder.dialect.asm.PredicateStub) Label(org.mvel2.asm.Label) Declaration(org.drools.core.rule.Declaration) PredicateExpression(org.drools.core.spi.PredicateExpression) CompiledInvoker(org.drools.core.spi.CompiledInvoker) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 5 with ClassGenerator

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();
}
Also used : ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) InvokerDataProvider(org.drools.core.rule.builder.dialect.asm.InvokerDataProvider)

Aggregations

ClassGenerator (org.drools.core.rule.builder.dialect.asm.ClassGenerator)14 MethodVisitor (org.mvel2.asm.MethodVisitor)7 Declaration (org.drools.core.rule.Declaration)6 InvokerDataProvider (org.drools.core.rule.builder.dialect.asm.InvokerDataProvider)5 CompiledInvoker (org.drools.core.spi.CompiledInvoker)4 Label (org.mvel2.asm.Label)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2 GeneratorHelper (org.drools.core.rule.builder.dialect.asm.GeneratorHelper)2 Tuple (org.drools.core.spi.Tuple)2 ArrayList (java.util.ArrayList)1 WorkingMemory (org.drools.core.WorkingMemory)1 EvaluatorWrapper (org.drools.core.base.EvaluatorWrapper)1 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)1 Sink (org.drools.core.reteoo.Sink)1 ConsequenceStub (org.drools.core.rule.builder.dialect.asm.ConsequenceStub)1 EvalStub (org.drools.core.rule.builder.dialect.asm.EvalStub)1 GeneratorHelper.matchDeclarationsToTuple (org.drools.core.rule.builder.dialect.asm.GeneratorHelper.matchDeclarationsToTuple)1 InvokerStub (org.drools.core.rule.builder.dialect.asm.InvokerStub)1 PredicateStub (org.drools.core.rule.builder.dialect.asm.PredicateStub)1