Search in sources :

Example 6 with Constraint

use of org.drools.core.spi.Constraint in project drools by kiegroup.

the class PatternBuilder method buildXPathDescr.

private Constraint buildXPathDescr(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, BaseDescr descr, MVELDumper.MVELDumperContext mvelCtx) {
    String expression = ((ExpressionDescr) descr).getExpression();
    XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(expression);
    if (xpathAnalysis.hasError()) {
        registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': " + xpathAnalysis.getError());
        return null;
    }
    XpathConstraint xpathConstraint = new XpathConstraint();
    ObjectType objectType = pattern.getObjectType();
    Class<?> patternClass = objectType.getClassType();
    List<Class<?>> backReferenceClasses = new ArrayList<Class<?>>();
    backReferenceClasses.add(patternClass);
    XpathBackReference backRef = new XpathBackReference(pattern, backReferenceClasses);
    pattern.setBackRefDeclarations(backRef);
    ObjectType originalType = pattern.getObjectType();
    ObjectType currentObjectType = originalType;
    mvelCtx.setInXpath(true);
    try {
        for (XpathAnalysis.XpathPart part : xpathAnalysis) {
            XpathConstraint.XpathChunk xpathChunk = xpathConstraint.addChunck(patternClass, part.getField(), part.getIndex(), part.isIterate(), part.isLazy());
            // make sure the Pattern is wired up to correct ClassObjectType and set as a target for rewiring
            context.getPkg().getClassFieldAccessorStore().wireObjectType(currentObjectType, xpathChunk);
            if (xpathChunk == null) {
                registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': cannot access " + part.getField() + " on " + patternClass);
                pattern.setObjectType(originalType);
                return null;
            }
            if (part.getInlineCast() != null) {
                try {
                    patternClass = context.getDialect().getTypeResolver().resolveType(part.getInlineCast());
                } catch (ClassNotFoundException e) {
                    registerDescrBuildError(context, patternDescr, "Unknown class " + part.getInlineCast() + " in xpath expression '" + expression + "'");
                    return null;
                }
                part.addInlineCastConstraint(patternClass);
                currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
                xpathChunk.setReturnedType(currentObjectType);
            } else {
                patternClass = xpathChunk.getReturnedClass();
                currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
            }
            pattern.setObjectType(currentObjectType);
            backReferenceClasses.add(0, patternClass);
            backRef.reset();
            for (String constraint : part.getConstraints()) {
                ConstraintConnectiveDescr result = parseExpression(context, patternDescr, new ExprConstraintDescr(constraint), constraint);
                if (result == null) {
                    continue;
                }
                for (Constraint c : build(context, patternDescr, pattern, result, mvelCtx)) {
                    xpathChunk.addConstraint(c);
                }
            }
        }
    } finally {
        mvelCtx.setInXpath(false);
        pattern.setBackRefDeclarations(null);
        pattern.setObjectType(originalType);
    }
    xpathConstraint.setXpathStartDeclaration(patternDescr.getXpathStartDeclaration());
    if (descr instanceof BindingDescr) {
        xpathConstraint.setDeclaration(pattern.addDeclaration(((BindingDescr) descr).getVariable()));
    }
    return xpathConstraint;
}
Also used : XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) ArrayList(java.util.ArrayList) XpathBackReference(org.drools.core.rule.XpathBackReference) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ExpressionDescr(org.drools.compiler.lang.descr.ExpressionDescr) XpathPart(org.drools.compiler.rule.builder.XpathAnalysis.XpathPart) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 7 with Constraint

use of org.drools.core.spi.Constraint in project drools by kiegroup.

the class PatternBuilder method buildCcdDescr.

protected Constraint buildCcdDescr(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, BaseDescr d, ConstraintConnectiveDescr ccd, MVELDumper.MVELDumperContext mvelCtx) {
    d.copyLocation(patternDescr);
    mvelCtx.clear();
    String expr = context.getCompilerFactory().getExpressionProcessor().dump(d, ccd, mvelCtx);
    Map<String, OperatorDescr> aliases = mvelCtx.getAliases();
    // create bindings
    TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
    for (BindingDescr bind : mvelCtx.getBindings()) {
        buildRuleBindings(context, patternDescr, pattern, bind, typeDeclaration);
    }
    if (expr.length() == 0) {
        return null;
    }
    // check if it is an atomic expression
    Constraint constraint = processAtomicExpression(context, pattern, d, expr, aliases);
    // otherwise check if it is a simple expression
    return constraint != null ? constraint : buildExpression(context, pattern, d, expr, aliases);
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) OperatorDescr(org.drools.compiler.lang.descr.OperatorDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 8 with Constraint

use of org.drools.core.spi.Constraint in project drools by kiegroup.

the class PatternBuilder method buildEval.

@SuppressWarnings("unchecked")
protected Constraint buildEval(final RuleBuildContext context, final Pattern pattern, final PredicateDescr predicateDescr, final Map<String, OperatorDescr> aliases, final String expr, final boolean isEvalExpression) {
    AnalysisResult analysis = buildAnalysis(context, pattern, predicateDescr, aliases);
    if (analysis == null) {
        // something bad happened
        return null;
    }
    Declaration[][] usedDeclarations = getUsedDeclarations(context, pattern, analysis);
    Declaration[] previousDeclarations = usedDeclarations[0];
    Declaration[] localDeclarations = usedDeclarations[1];
    BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
    Arrays.sort(previousDeclarations, SortDeclarations.instance);
    Arrays.sort(localDeclarations, SortDeclarations.instance);
    boolean isJavaEval = isEvalExpression && context.getDialect() instanceof JavaDialect;
    if (isJavaEval) {
        final PredicateConstraint predicateConstraint = new PredicateConstraint(null, previousDeclarations, localDeclarations);
        final PredicateBuilder builder = context.getDialect().getPredicateBuilder();
        builder.build(context, usedIdentifiers, previousDeclarations, localDeclarations, predicateConstraint, predicateDescr, analysis);
        return predicateConstraint;
    }
    MVELCompilationUnit compilationUnit = getConstraintBuilder(context).buildCompilationUnit(context, previousDeclarations, localDeclarations, predicateDescr, analysis);
    String[] requiredGlobals = usedIdentifiers.getGlobals().keySet().toArray(new String[usedIdentifiers.getGlobals().size()]);
    Declaration[] mvelDeclarations = new Declaration[previousDeclarations.length + localDeclarations.length + requiredGlobals.length];
    int i = 0;
    for (Declaration d : previousDeclarations) {
        mvelDeclarations[i++] = d;
    }
    for (Declaration d : localDeclarations) {
        mvelDeclarations[i++] = d;
    }
    for (String global : requiredGlobals) {
        mvelDeclarations[i++] = context.getDeclarationResolver().getDeclaration(global);
    }
    boolean isDynamic = !pattern.getObjectType().getClassType().isArray() && !context.getKnowledgeBuilder().getTypeDeclaration(pattern.getObjectType().getClassType()).isTypesafe();
    return getConstraintBuilder(context).buildMvelConstraint(context.getPkg().getName(), expr, mvelDeclarations, getOperators(usedIdentifiers.getOperators()), compilationUnit, isDynamic);
}
Also used : MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) PredicateConstraint(org.drools.core.rule.PredicateConstraint)

Example 9 with Constraint

use of org.drools.core.spi.Constraint in project drools by kiegroup.

the class PatternBuilder method processListenedPropertiesAnnotation.

protected void processListenedPropertiesAnnotation(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern) {
    String watchedValues = null;
    try {
        Watch watch = patternDescr.getTypedAnnotation(Watch.class);
        watchedValues = watch == null ? null : watch.value();
    } catch (Exception e) {
        registerDescrBuildError(context, patternDescr, e.getMessage());
    }
    if (watchedValues == null) {
        return;
    }
    List<String> settableProperties = getSettableProperties(context, patternDescr, pattern);
    List<String> listenedProperties = new ArrayList<String>();
    for (String propertyName : watchedValues.split(",")) {
        propertyName = propertyName.trim();
        if (propertyName.equals("*") || propertyName.equals("!*")) {
            if (listenedProperties.contains("*") || listenedProperties.contains("!*")) {
                registerDescrBuildError(context, patternDescr, "Duplicate usage of wildcard * in @" + Watch.class.getSimpleName() + " annotation");
            } else {
                listenedProperties.add(propertyName);
            }
            continue;
        }
        boolean isNegative = propertyName.startsWith("!");
        propertyName = isNegative ? propertyName.substring(1).trim() : propertyName;
        if (settableProperties != null && !settableProperties.contains(propertyName)) {
            registerDescrBuildError(context, patternDescr, "Unknown property " + propertyName + " in @" + Watch.class.getSimpleName() + " annotation");
        } else if (listenedProperties.contains(propertyName) || listenedProperties.contains("!" + propertyName)) {
            registerDescrBuildError(context, patternDescr, "Duplicate property " + propertyName + " in @" + Watch.class.getSimpleName() + " annotation");
        } else {
            listenedProperties.add(isNegative ? "!" + propertyName : propertyName);
        }
    }
    for (Constraint constr : pattern.getConstraints()) {
        if (constr instanceof EvaluatorConstraint && ((EvaluatorConstraint) constr).isSelf()) {
            EvaluatorConstraint ec = ((EvaluatorConstraint) constr);
            if (ec.getEvaluator().getOperator() == IsAEvaluatorDefinition.ISA || ec.getEvaluator().getOperator() == IsAEvaluatorDefinition.NOT_ISA) {
                listenedProperties.add(TraitableBean.TRAITSET_FIELD_NAME);
            }
        }
    }
    pattern.setListenedProperties(listenedProperties);
}
Also used : EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) Watch(org.kie.api.definition.rule.Watch) ArrayList(java.util.ArrayList) DroolsParserException(org.drools.compiler.compiler.DroolsParserException)

Example 10 with Constraint

use of org.drools.core.spi.Constraint in project drools by kiegroup.

the class PatternBuilder method processConstraintsAndBinds.

/**
 * Process all constraints and bindings on this pattern
 */
protected void processConstraintsAndBinds(final RuleBuildContext context, final PatternDescr patternDescr, final Pattern pattern) {
    MVELDumper.MVELDumperContext mvelCtx = new MVELDumper.MVELDumperContext().setRuleContext(context);
    for (BaseDescr b : patternDescr.getDescrs()) {
        String expression;
        boolean isPositional = false;
        if (b instanceof BindingDescr) {
            BindingDescr bind = (BindingDescr) b;
            expression = bind.getVariable() + (bind.isUnification() ? " := " : " : ") + bind.getExpression();
        } else if (b instanceof ExprConstraintDescr) {
            ExprConstraintDescr descr = (ExprConstraintDescr) b;
            expression = descr.getExpression();
            isPositional = descr.getType() == ExprConstraintDescr.Type.POSITIONAL;
        } else {
            expression = b.getText();
        }
        ConstraintConnectiveDescr result = parseExpression(context, patternDescr, b, expression);
        if (result == null) {
            return;
        }
        isPositional &= !(result.getDescrs().size() == 1 && result.getDescrs().get(0) instanceof BindingDescr);
        if (isPositional) {
            processPositional(context, patternDescr, pattern, (ExprConstraintDescr) b);
        } else {
            // need to build the actual constraint
            List<Constraint> constraints = build(context, patternDescr, pattern, result, mvelCtx);
            pattern.addConstraints(constraints);
        }
    }
    TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
    if (typeDeclaration != null && typeDeclaration.isPropertyReactive()) {
        for (String field : context.getRuleDescr().lookAheadFieldsOfIdentifier(patternDescr)) {
            addFieldToPatternWatchlist(pattern, typeDeclaration, field);
        }
    }
    combineConstraints(context, pattern, mvelCtx);
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) MVELDumper(org.drools.compiler.lang.MVELDumper) TypeDeclaration(org.drools.core.rule.TypeDeclaration) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Aggregations

Constraint (org.drools.core.spi.Constraint)21 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)16 XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)14 PredicateConstraint (org.drools.core.rule.PredicateConstraint)13 EvaluatorConstraint (org.drools.core.rule.constraint.EvaluatorConstraint)11 NegConstraint (org.drools.core.rule.constraint.NegConstraint)11 Declaration (org.drools.core.rule.Declaration)6 TypeDeclaration (org.drools.core.rule.TypeDeclaration)6 Pattern (org.drools.core.rule.Pattern)5 ArrayList (java.util.ArrayList)4 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)3 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)3 ArrayElementReader (org.drools.core.base.extractors.ArrayElementReader)3 Test (org.junit.Test)3 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)2 MVELDumper (org.drools.compiler.lang.MVELDumper)2 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)2 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)2 JavaDialect (org.drools.compiler.rule.builder.dialect.java.JavaDialect)2