Search in sources :

Example 1 with And

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

the class DefaultBeanClassBuilder method buildTraitMap.

/**
 * A traitable class is a special class with support for dynamic properties and types.
 *
 * This method builds the trait map, containing the references to the proxies
 * for each trait carried by an object at a given time.
 *
 * @param cw
 * @param classDef
 */
protected void buildTraitMap(ClassWriter cw, ClassDefinition classDef) {
    FieldVisitor fv = cw.visitField(ACC_PRIVATE, TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class), "Ljava/util/Map<Ljava/lang/String;Lorg/drools/core/factmodel/traits/Thing;>;", null);
    fv.visitEnd();
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}), "()Ljava/util/Map<Ljava/lang/String;Lorg/drools/factmodel/traits/Thing;>;", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "_setTraitMap", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(Map.class) }), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "addTrait", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(String.class), Type.getType(Thing.class) }), "(Ljava/lang/String;Lorg/drools/core/factmodel/traits/Thing;)V", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "put", Type.getMethodDescriptor(Type.getType(Object.class), new Type[] { Type.getType(Object.class), Type.getType(Object.class) }));
    mv.visitInsn(POP);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "getTrait", Type.getMethodDescriptor(Type.getType(Thing.class), new Type[] { Type.getType(String.class) }), Type.getMethodDescriptor(Type.getType(Thing.class), new Type[] { Type.getType(String.class) }), null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "get", Type.getMethodDescriptor(Type.getType(Object.class), new Type[] { Type.getType(Object.class) }));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Thing.class));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "hasTrait", Type.getMethodDescriptor(Type.getType(boolean.class), new Type[] { Type.getType(String.class) }), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    Label l0 = new Label();
    mv.visitJumpInsn(IFNONNULL, l0);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "containsKey", Type.getMethodDescriptor(Type.getType(boolean.class), new Type[] { Type.getType(Object.class) }));
    mv.visitInsn(IRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "hasTraits", Type.getMethodDescriptor(Type.getType(boolean.class), new Type[] {}), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    Label l5 = new Label();
    mv.visitJumpInsn(IFNULL, l5);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "isEmpty", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] {}));
    mv.visitJumpInsn(IFNE, l5);
    mv.visitInsn(ICONST_1);
    Label l4 = new Label();
    mv.visitJumpInsn(GOTO, l4);
    mv.visitLabel(l5);
    mv.visitInsn(ICONST_0);
    mv.visitLabel(l4);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "removeTrait", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(String.class) }), Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(String.class) }), null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitTypeMap.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TraitTypeMap.class), "removeCascade", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(String.class) }));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "removeTrait", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(BitSet.class) }), Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(BitSet.class) }), null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitTypeMap.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TraitTypeMap.class), "removeCascade", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(BitSet.class) }));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "getTraits", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] {}), "()Ljava/util/Collection<Ljava/lang/String;>;", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "keySet", Type.getMethodDescriptor(Type.getType(Set.class), new Type[] {}));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "_setBottomTypeCode", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(BitSet.class) }), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitTypeMap.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TraitTypeMap.class), "setBottomCode", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(BitSet.class) }));
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "getMostSpecificTraits", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] {}), "()Ljava/util/Collection<Lorg/drools/core/factmodel/traits/Thing;>;", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    Label l99 = new Label();
    mv.visitJumpInsn(IFNULL, l99);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitTypeMap.class));
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TraitTypeMap.class), "getMostSpecificTraits", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] {}));
    mv.visitInsn(ARETURN);
    mv.visitLabel(l99);
    mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Collections.class), "emptySet", Type.getMethodDescriptor(Type.getType(Set.class), new Type[] {}));
    mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Collections.class), "unmodifiableCollection", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(Collection.class) }));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "getCurrentTypeCode", Type.getMethodDescriptor(Type.getType(BitSet.class), new Type[] {}), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    Label l3 = new Label();
    mv.visitJumpInsn(IFNONNULL, l3);
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l3);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitTypeMap.class));
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TraitTypeMap.class), "getCurrentTypeCode", Type.getMethodDescriptor(Type.getType(BitSet.class), new Type[] {}));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Type(org.mvel2.asm.Type) Label(org.mvel2.asm.Label) BitSet(java.util.BitSet) Collection(java.util.Collection) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap) Collections(java.util.Collections) FieldVisitor(org.mvel2.asm.FieldVisitor) Map(java.util.Map) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap) Thing(org.drools.core.factmodel.traits.Thing) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 2 with And

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

the class DialectUtil method setContainerBlockInputs.

/**
 * This code is not currently used, it's commented out in method caller. This is because we couldn't
 * get this to work and will have to wait until MVEL supports genercs (mdp).
 */
public static void setContainerBlockInputs(RuleBuildContext context, List<JavaBlockDescr> descrs, JavaContainerBlockDescr parentBlock, String originalCode, BoundIdentifiers bindings, Map<String, Class<?>> parentVars, int offset) {
    StringBuilder consequence = new StringBuilder();
    int lastAdded = 0;
    // strip blocks, so we can analyse this block with MVEL
    for (JavaBlockDescr block : parentBlock.getJavaBlockDescrs()) {
        if (block.getEnd() == 0) {
            // do nothing, it was incorrectly parsed, but this error should be picked up else where
            continue;
        }
        if (block.getType() == JavaBlockDescr.BlockType.TRY) {
            // adding previous chunk up to the start of this block
            consequence.append(originalCode.substring(lastAdded, block.getStart() - 1 - offset));
            JavaTryBlockDescr tryDescr = (JavaTryBlockDescr) block;
            if (tryDescr.getFinal() != null) {
                lastAdded = tryDescr.getFinal().getEnd() - offset;
            } else {
                lastAdded = tryDescr.getCatches().get(tryDescr.getCatches().size() - 1).getEnd() - offset;
            }
            stripTryDescr(originalCode, consequence, (JavaTryBlockDescr) block, offset);
        } else if (block.getType() == JavaBlockDescr.BlockType.THROW) {
            // adding previous chunk up to the start of this block
            consequence.append(originalCode.substring(lastAdded, block.getStart() - 1 - offset));
            JavaThrowBlockDescr throwBlock = (JavaThrowBlockDescr) block;
            addWhiteSpaces(originalCode, consequence, throwBlock.getStart() - offset, throwBlock.getTextStart() - offset);
            consequence.append(originalCode.substring(throwBlock.getTextStart() - offset - 1, throwBlock.getEnd() - 1 - offset)).append(";");
            lastAdded = throwBlock.getEnd() - offset;
        } else if (block.getType() == JavaBlockDescr.BlockType.IF) {
            // adding previous chunk up to the start of this block
            consequence.append(originalCode.substring(lastAdded, block.getStart() - 1 - offset));
            JavaIfBlockDescr ifDescr = (JavaIfBlockDescr) block;
            lastAdded = ifDescr.getEnd() - offset;
            stripBlockDescr(originalCode, consequence, ifDescr, offset);
        } else if (block.getType() == JavaBlockDescr.BlockType.ELSE) {
            // adding previous chunk up to the start of this block
            consequence.append(originalCode.substring(lastAdded, block.getStart() - 1 - offset));
            JavaElseBlockDescr elseDescr = (JavaElseBlockDescr) block;
            lastAdded = elseDescr.getEnd() - offset;
            stripBlockDescr(originalCode, consequence, elseDescr, offset);
        } else if (block.getType() == JavaBlockDescr.BlockType.WHILE) {
            // adding previous chunk up to the start of this block
            consequence.append(originalCode.substring(lastAdded, block.getStart() - 1 - offset));
            JavaWhileBlockDescr whileDescr = (JavaWhileBlockDescr) block;
            lastAdded = whileDescr.getEnd() - offset;
            stripBlockDescr(originalCode, consequence, whileDescr, offset);
        } else if (block.getType() == JavaBlockDescr.BlockType.FOR) {
            // adding previous chunk up to the start of this block
            consequence.append(originalCode.substring(lastAdded, block.getStart() - 1 - offset));
            JavaForBlockDescr forDescr = (JavaForBlockDescr) block;
            lastAdded = forDescr.getEnd() - offset;
            stripBlockDescr(originalCode, consequence, forDescr, offset);
        }
    }
    consequence.append(originalCode.substring(lastAdded));
    // We need to do this as MVEL doesn't recognise "modify"
    MacroProcessor macroProcessor = new MacroProcessor();
    Map<String, Macro> macros = new HashMap<String, Macro>(MVELConsequenceBuilder.macros);
    macros.put("modify", new Macro() {

        public String doMacro() {
            return "with  ";
        }
    });
    macroProcessor.setMacros(macros);
    String mvelCode = macroProcessor.parse(consequence.toString());
    Map<String, Class<?>> inputs = getInputs(context, mvelCode, bindings, parentVars);
    inputs.putAll(parentVars);
    parentBlock.setInputs(inputs);
    // set inputs for current container blocks to be rewritten
    for (JavaBlockDescr block : parentBlock.getJavaBlockDescrs()) {
        if (block.getType() == JavaBlockDescr.BlockType.TRY) {
            JavaTryBlockDescr tryBlock = (JavaTryBlockDescr) block;
            setContainerBlockInputs(context, descrs, tryBlock, originalCode.substring(tryBlock.getTextStart() - offset, tryBlock.getEnd() - 1 - offset), bindings, inputs, tryBlock.getTextStart());
            for (JavaCatchBlockDescr catchBlock : tryBlock.getCatches()) {
                setContainerBlockInputs(context, descrs, catchBlock, catchBlock.getClause() + "=null;" + originalCode.substring(catchBlock.getTextStart() - offset, catchBlock.getEnd() - 1 - offset), bindings, inputs, tryBlock.getTextStart());
            }
            if (tryBlock.getFinal() != null) {
                JavaFinalBlockDescr finalBlock = tryBlock.getFinal();
                setContainerBlockInputs(context, descrs, finalBlock, originalCode.substring(finalBlock.getTextStart() - offset, finalBlock.getEnd() - 1 - offset), bindings, inputs, tryBlock.getTextStart());
            }
        } else if (block.getType() == JavaBlockDescr.BlockType.IF) {
            JavaIfBlockDescr ifBlock = (JavaIfBlockDescr) block;
            int adjustBlock = (originalCode.charAt(ifBlock.getTextStart() - offset - 1) == '{') ? 0 : 1;
            setContainerBlockInputs(context, descrs, ifBlock, originalCode.substring(ifBlock.getTextStart() - offset + adjustBlock, ifBlock.getEnd() - 1 - offset - adjustBlock), bindings, inputs, ifBlock.getTextStart());
        } else if (block.getType() == JavaBlockDescr.BlockType.ELSE) {
            JavaElseBlockDescr elseBlock = (JavaElseBlockDescr) block;
            int adjustBlock = (originalCode.charAt(elseBlock.getTextStart() - offset - 1) == '{') ? 0 : 1;
            setContainerBlockInputs(context, descrs, elseBlock, originalCode.substring(elseBlock.getTextStart() - offset + adjustBlock, elseBlock.getEnd() - 1 - offset - adjustBlock), bindings, inputs, elseBlock.getTextStart());
        } else if (block.getType() == JavaBlockDescr.BlockType.WHILE) {
            JavaWhileBlockDescr whileBlock = (JavaWhileBlockDescr) block;
            int adjustBlock = (originalCode.charAt(whileBlock.getTextStart() - offset - 1) == '{') ? 0 : 1;
            setContainerBlockInputs(context, descrs, whileBlock, originalCode.substring(whileBlock.getTextStart() - offset + adjustBlock, whileBlock.getEnd() - 1 - offset - adjustBlock), bindings, inputs, whileBlock.getTextStart());
        } else if (block.getType() == JavaBlockDescr.BlockType.FOR) {
            JavaForBlockDescr forBlock = (JavaForBlockDescr) block;
            int adjustBlock = (originalCode.charAt(forBlock.getTextStart() - offset - 1) == '{') ? 0 : 1;
            setContainerBlockInputs(context, descrs, forBlock, originalCode.substring(forBlock.getStartParen() - offset, forBlock.getInitEnd() - offset) + originalCode.substring(forBlock.getTextStart() - offset + adjustBlock, forBlock.getEnd() - 1 - offset - adjustBlock), bindings, inputs, forBlock.getTextStart() - (forBlock.getInitEnd() - forBlock.getStartParen()));
        } else {
            // each block to be rewritten now knows it's own variables
            block.setInputs(inputs);
            descrs.add(block);
        }
    }
}
Also used : JavaForBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaForBlockDescr) JavaIfBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaIfBlockDescr) JavaThrowBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaThrowBlockDescr) HashMap(java.util.HashMap) Macro(org.mvel2.Macro) JavaWhileBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaWhileBlockDescr) MacroProcessor(org.mvel2.MacroProcessor) JavaCatchBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaCatchBlockDescr) JavaFinalBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaFinalBlockDescr) JavaElseBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaElseBlockDescr) JavaTryBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaTryBlockDescr) JavaBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr)

Example 3 with And

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

the class PatternBuilder method buildPattern.

private Pattern buildPattern(RuleBuildContext context, PatternDescr patternDescr, ObjectType objectType) {
    String patternIdentifier = patternDescr.getIdentifier();
    boolean duplicateBindings = patternIdentifier != null && objectType instanceof ClassObjectType && context.getDeclarationResolver().isDuplicated(context.getRule(), patternIdentifier, objectType.getClassName());
    Pattern pattern;
    if (!StringUtils.isEmpty(patternIdentifier) && !duplicateBindings) {
        pattern = new Pattern(context.getNextPatternId(), // offset is 0 by default
        0, objectType, patternIdentifier, patternDescr.isInternalFact(context));
        if (objectType instanceof ClassObjectType) {
            // make sure PatternExtractor is wired up to correct ClassObjectType and set as a target for rewiring
            context.getPkg().getClassFieldAccessorStore().wireObjectType(objectType, (AcceptsClassObjectType) pattern.getDeclaration().getExtractor());
        }
    } else {
        pattern = new Pattern(context.getNextPatternId(), // offset is 0 by default
        0, objectType, null);
    }
    pattern.setPassive(patternDescr.isPassive(context));
    if (ClassObjectType.Match_ObjectType.isAssignableFrom(pattern.getObjectType())) {
        PropertyHandler handler = PropertyHandlerFactory.getPropertyHandler(RuleTerminalNodeLeftTuple.class);
        if (handler == null) {
            PropertyHandlerFactoryFixer.getPropertyHandlerClass().put(RuleTerminalNodeLeftTuple.class, new ActivationPropertyHandler());
        }
    }
    // adding the newly created pattern to the build stack this is necessary in case of local declaration usage
    context.getDeclarationResolver().pushOnBuildStack(pattern);
    if (duplicateBindings) {
        processDuplicateBindings(patternDescr.isUnification(), patternDescr, pattern, patternDescr, "this", patternDescr.getIdentifier(), context);
    }
    return pattern;
}
Also used : Pattern(org.drools.core.rule.Pattern) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ActivationPropertyHandler(org.drools.core.base.mvel.ActivationPropertyHandler) PropertyHandler(org.mvel2.integration.PropertyHandler) ActivationPropertyHandler(org.drools.core.base.mvel.ActivationPropertyHandler)

Example 4 with And

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

the class ConditionAnalyzer method analyzeCondition.

private Condition analyzeCondition(ASTNode node) {
    boolean isNegated = false;
    if (node instanceof Negation) {
        isNegated = true;
        ExecutableStatement statement = ((Negation) node).getStatement();
        if (statement instanceof ExecutableLiteral) {
            return new FixedValueCondition(!(Boolean) ((ExecutableLiteral) statement).getLiteral());
        }
        node = ((ExecutableAccessor) statement).getNode();
    }
    node = analyzeSubstatement(node);
    if (node instanceof LiteralNode && node.getEgressType() == Boolean.class) {
        boolean literalValue = (Boolean) node.getLiteralValue();
        return new FixedValueCondition(isNegated ? !literalValue : literalValue);
    }
    if (node instanceof Negation) {
        isNegated = !isNegated;
        node = ((ExecutableAccessor) ((Negation) node).getStatement()).getNode();
        node = analyzeSubstatement(node);
    }
    if (node instanceof And || node instanceof Or) {
        return analyzeCombinedCondition((BooleanNode) node, isNegated);
    }
    return analyzeSingleCondition(node, isNegated);
}
Also used : LiteralNode(org.mvel2.ast.LiteralNode) ExecutableStatement(org.mvel2.compiler.ExecutableStatement) Or(org.mvel2.ast.Or) Negation(org.mvel2.ast.Negation) And(org.mvel2.ast.And) ExecutableLiteral(org.mvel2.compiler.ExecutableLiteral)

Example 5 with And

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

the class PatternBuilder method attachPattern.

private void attachPattern(final BuildContext context, final BuildUtils utils, final Pattern pattern) throws InvalidPatternException {
    // Set pattern offset to the appropriate value
    pattern.setOffset(context.getCurrentPatternOffset());
    // this is needed for Activation patterns, to allow declarations and annotations to be used like field constraints
    if (ClassObjectType.Match_ObjectType.isAssignableFrom(pattern.getObjectType())) {
        PropertyHandler handler = PropertyHandlerFactory.getPropertyHandler(AgendaItemImpl.class);
        if (handler == null) {
            PropertyHandlerFactoryFixer.getPropertyHandlerClass().put(AgendaItemImpl.class, new ActivationPropertyHandler());
        }
    }
    Constraints constraints = createConstraints(context, pattern);
    // Create BetaConstraints object
    context.setBetaconstraints(constraints.betaConstraints);
    if (pattern.getSource() != null) {
        context.setAlphaConstraints(constraints.alphaConstraints);
        final int currentOffset = context.getCurrentPatternOffset();
        PatternSource source = pattern.getSource();
        ReteooComponentBuilder builder = utils.getBuilderFor(source);
        if (builder == null) {
            throw new RuntimeException("Unknown pattern source type: " + source.getClass() + " for source " + source + " on pattern " + pattern);
        }
        builder.build(context, utils, source);
        // restoring offset
        context.setCurrentPatternOffset(currentOffset);
    } else {
        // default entry point
        PatternSource source = EntryPointId.DEFAULT;
        ReteooComponentBuilder builder = utils.getBuilderFor(source);
        builder.build(context, utils, source);
    }
    buildBehaviors(context, utils, pattern, constraints);
    if (context.getObjectSource() != null) {
        attachAlphaNodes(context, utils, pattern, constraints.alphaConstraints);
    }
    buildXpathConstraints(context, utils, pattern, constraints);
    // last thing to do is increment the offset, since if the pattern has a source,
    // offset must be overriden
    context.incrementCurrentPatternOffset();
}
Also used : ActivationPropertyHandler(org.drools.core.base.mvel.ActivationPropertyHandler) PropertyHandler(org.mvel2.integration.PropertyHandler) PatternSource(org.drools.core.rule.PatternSource) ActivationPropertyHandler(org.drools.core.base.mvel.ActivationPropertyHandler) InstanceNotEqualsConstraint(org.drools.core.common.InstanceNotEqualsConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) IntervalProviderConstraint(org.drools.core.rule.IntervalProviderConstraint) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) Constraint(org.drools.core.spi.Constraint)

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