Search in sources :

Example 51 with And

use of org.mvel2.ast.And in project mvel by mvel.

the class TemplateTests method testDRLTemplate.

public void testDRLTemplate() {
    String template = "@declare{\"drl\"}@includeNamed{\"ced\"; node=root }@end{}" + "" + "@declare{\"ced\"}" + "@if{ node.base==1 } @includeNamed{ \"cedX\"; connect=\"AND\"; args=node.list }" + "@elseif{ node.base ==2 }@includeNamed{ \"cedX\"; connect=\"OR\"; args=node.list }" + "@end{}" + "@end{}" + "" + "@declare{\"cedX\"}@{connect}@foreach{child : args}" + "@includeNamed{\"ced\"; node=child; }@end{} @{connect}@end{}";
    TemplateRegistry reportRegistry = new SimpleTemplateRegistry();
    reportRegistry.addNamedTemplate("drl", TemplateCompiler.compileTemplate(template));
    TemplateRuntime.execute(reportRegistry.getNamedTemplate("drl"), null, reportRegistry);
    Map<String, Object> context = new HashMap<String, Object>();
    context.put("root", new Node(2, Arrays.asList(new Node(1, Collections.EMPTY_LIST))));
    String result = (String) TemplateRuntime.execute(reportRegistry.getNamedTemplate("drl"), null, new MapVariableResolverFactory(context), reportRegistry);
    assertEquals("OR AND AND OR", result);
}
Also used : TemplateRegistry(org.mvel2.templates.TemplateRegistry) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) TestPluginNode(org.mvel2.tests.templates.tests.res.TestPluginNode) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Example 52 with And

use of org.mvel2.ast.And in project drools by kiegroup.

the class DefaultEnumClassBuilder method buildClass.

/**
 * Dynamically builds, defines and loads a class based on the given class definition
 *
 * @param classDef the class definition object structure
 *
 * @return the Class instance for the given class definition
 *
 * @throws java.io.IOException
 * @throws java.lang.reflect.InvocationTargetException
 * @throws IllegalAccessException
 * @throws NoSuchMethodException
 * @throws ClassNotFoundException
 * @throws IllegalArgumentException
 * @throws SecurityException
 * @throws NoSuchFieldException
 * @throws InstantiationException
 */
public byte[] buildClass(ClassDefinition classDef, ClassLoader classLoader) throws IOException, SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
    if (!(classDef instanceof EnumClassDefinition)) {
        throw new RuntimeException("FATAL : Trying to create an enum out of a bean class definition  " + classDef);
    }
    EnumClassDefinition edef = (EnumClassDefinition) classDef;
    ClassWriter cw = this.buildClassHeader(classLoader, edef);
    this.buildLiterals(cw, edef);
    this.buildFields(cw, edef);
    this.buildConstructors(cw, edef);
    this.buildGettersAndSetters(cw, edef);
    this.buildEqualityMethods(cw, edef);
    this.buildToString(cw, edef);
    cw.visitEnd();
    byte[] serializedClass = cw.toByteArray();
    return serializedClass;
}
Also used : ClassGenerator.createClassWriter(org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter) ClassWriter(org.mvel2.asm.ClassWriter)

Example 53 with And

use of org.mvel2.ast.And in project drools by kiegroup.

the class DefaultBeanClassBuilder method fieldConstructorStart.

protected void fieldConstructorStart(MethodVisitor mv, ClassDefinition classDef, Collection<FieldDefinition> fieldDefs) {
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    String sup = "";
    try {
        sup = Type.getInternalName(Class.forName(classDef.getSuperClass()));
    } catch (ClassNotFoundException e) {
        sup = BuildUtils.getInternalType(classDef.getSuperClass());
    }
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, sup, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
    // local vars start at 1, as 0 is "this"
    int index = 1;
    for (FieldDefinition field : fieldDefs) {
        if (this.debug) {
            Label l11 = new Label();
            mv.visitLabel(l11);
        }
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Type.getType(BuildUtils.getTypeDescriptor(field.getTypeName())).getOpcode(Opcodes.ILOAD), index++);
        if (field.getTypeName().equals("long") || field.getTypeName().equals("double")) {
            // long and double variables use 2 words on the variables table
            index++;
        }
        if (!field.isInherited()) {
            mv.visitFieldInsn(Opcodes.PUTFIELD, BuildUtils.getInternalType(classDef.getClassName()), field.getName(), BuildUtils.getTypeDescriptor(field.getTypeName()));
        } else {
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getClassName()), field.getWriteMethod(), Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(BuildUtils.getTypeDescriptor(field.getTypeName())) }));
        }
    }
    for (FieldDefinition field : classDef.getFieldsDefinitions()) {
        if (!fieldDefs.contains(field) && field.getInitExpr() != null && !"".equals(field.getInitExpr().trim())) {
            initFieldWithDefaultValue(mv, classDef, field);
        }
    }
    if (classDef.isTraitable()) {
        initializeDynamicTypeStructures(mv, classDef);
    }
}
Also used : Type(org.mvel2.asm.Type) Label(org.mvel2.asm.Label)

Example 54 with And

use of org.mvel2.ast.And in project drools by kiegroup.

the class DefaultBeanClassBuilder method buildFieldTMS.

/**
 * A traitable class is a special class with support for dynamic properties and types.
 *
 * A traitable class in logical mode provides additional control over the values
 * and type(s) of its fields.
 *
 * @param cw
 * @param def
 */
protected void buildFieldTMS(ClassWriter cw, ClassDefinition def) {
    FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class), null, null);
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "_getFieldTMS", Type.getMethodDescriptor(Type.getType(TraitFieldTMS.class), new Type[] {}), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "_setFieldTMS", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(TraitFieldTMS.class) }), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Type(org.mvel2.asm.Type) TraitFieldTMS(org.drools.core.factmodel.traits.TraitFieldTMS) FieldVisitor(org.mvel2.asm.FieldVisitor) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 55 with And

use of org.mvel2.ast.And in project drools by kiegroup.

the class DefaultBeanClassBuilder method buildConstructorWithFields.

/**
 * Creates a constructor that takes and assigns values to all
 * fields in the order they are declared.
 *
 * @param cw
 * @param classDef
 */
protected void buildConstructorWithFields(ClassVisitor cw, ClassDefinition classDef, Collection<FieldDefinition> fieldDefs) {
    Type[] params = new Type[fieldDefs.size()];
    int index = 0;
    for (FieldDefinition field : fieldDefs) {
        params[index++] = Type.getType(BuildUtils.getTypeDescriptor(field.getTypeName()));
    }
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, params), null, null);
    mv.visitCode();
    Label l0 = null;
    if (this.debug) {
        l0 = new Label();
        mv.visitLabel(l0);
    }
    fieldConstructorStart(mv, classDef, fieldDefs);
    mv.visitInsn(Opcodes.RETURN);
    Label l1 = null;
    if (this.debug) {
        l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", BuildUtils.getTypeDescriptor(classDef.getClassName()), null, l0, l1, 0);
        for (FieldDefinition field : classDef.getFieldsDefinitions()) {
            Label l11 = new Label();
            mv.visitLabel(l11);
            mv.visitLocalVariable(field.getName(), BuildUtils.getTypeDescriptor(field.getTypeName()), null, l0, l1, 0);
        }
    }
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Type(org.mvel2.asm.Type) Label(org.mvel2.asm.Label) MethodVisitor(org.mvel2.asm.MethodVisitor)

Aggregations

CompileException (org.mvel2.CompileException)22 HashMap (java.util.HashMap)13 Map (java.util.Map)12 ParserContext (org.mvel2.ParserContext)12 ASTNode (org.mvel2.ast.ASTNode)9 Type (org.mvel2.asm.Type)8 Serializable (java.io.Serializable)7 Method (java.lang.reflect.Method)7 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)7 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)7 StringAppender (org.mvel2.util.StringAppender)6 EndOfStatement (org.mvel2.ast.EndOfStatement)5 IOException (java.io.IOException)4 BitSet (java.util.BitSet)4 List (java.util.List)4 LabelNode (org.mvel2.asm.tree.LabelNode)4 LiteralNode (org.mvel2.ast.LiteralNode)4 Proto (org.mvel2.ast.Proto)4 ArrayList (java.util.ArrayList)3 Money (org.broadleafcommerce.common.money.Money)3