Search in sources :

Example 11 with Constraint

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

the class PatternBuilder method buildRelationalExpression.

protected Constraint buildRelationalExpression(final RuleBuildContext context, final Pattern pattern, final RelationalExprDescr relDescr, final String expr, final Map<String, OperatorDescr> aliases) {
    String[] values = new String[2];
    boolean usesThisRef = findExpressionValues(relDescr, values);
    ExprBindings value1Expr = getExprBindings(context, pattern, values[0]);
    ExprBindings value2Expr = getExprBindings(context, pattern, values[1]);
    // or as a fallback when the building of a constraint fails
    if (!usesThisRef && value1Expr.isConstant()) {
        return createAndBuildPredicate(context, pattern, relDescr, expr, aliases);
    }
    Constraint constraint = buildConstraintForPattern(context, pattern, relDescr, expr, values[0], values[1], value2Expr.isConstant(), aliases);
    return constraint != null ? constraint : createAndBuildPredicate(context, pattern, relDescr, expr, aliases);
}
Also used : 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)

Example 12 with Constraint

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

the class PatternBuilder method createAndBuildPredicate.

protected Constraint createAndBuildPredicate(RuleBuildContext context, Pattern pattern, BaseDescr base, String expr, Map<String, OperatorDescr> aliases) {
    Dialect dialect = context.getDialect();
    MVELDialect mvelDialect = (MVELDialect) context.getDialect("mvel");
    context.setDialect(mvelDialect);
    PredicateDescr pdescr = new PredicateDescr(context.getRuleDescr().getResource(), expr);
    pdescr.copyParameters(base);
    pdescr.copyLocation(base);
    Constraint evalConstraint = buildEval(context, pattern, pdescr, aliases, expr, false);
    // fall back to original dialect
    context.setDialect(dialect);
    return evalConstraint;
}
Also used : 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) Dialect(org.drools.compiler.compiler.Dialect) MVELDialect(org.drools.compiler.rule.builder.dialect.mvel.MVELDialect) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) MVELDialect(org.drools.compiler.rule.builder.dialect.mvel.MVELDialect) PredicateDescr(org.drools.compiler.lang.descr.PredicateDescr)

Example 13 with Constraint

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

the class PatternBuilder method build.

protected List<Constraint> build(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, ConstraintConnectiveDescr descr, MVELDumper.MVELDumperContext mvelCtx) {
    List<Constraint> constraints = new ArrayList<Constraint>();
    List<BaseDescr> initialDescrs = new ArrayList<BaseDescr>(descr.getDescrs());
    for (BaseDescr d : initialDescrs) {
        boolean isXPath = isXPathDescr(d);
        if (isXPath && pattern.hasXPath()) {
            registerDescrBuildError(context, patternDescr, "More than a single oopath constraint is not allowed in the same pattern");
            return constraints;
        }
        Constraint constraint = isXPath ? buildXPathDescr(context, patternDescr, pattern, d, mvelCtx) : buildCcdDescr(context, patternDescr, pattern, d, descr, mvelCtx);
        if (constraint != null) {
            Declaration declCorrXpath = getDeclarationCorrespondingToXpath(pattern, isXPath, constraint);
            if (declCorrXpath == null) {
                constraints.add(constraint);
            } else {
                // A constraint is using a declration bound to an xpath in the same pattern
                // Move the constraint inside the last chunk of the xpath defining this declaration, rewriting it as 'this'
                Pattern modifiedPattern = pattern.clone();
                modifiedPattern.setObjectType(new ClassObjectType(declCorrXpath.getDeclarationClass()));
                constraint = buildCcdDescr(context, patternDescr, modifiedPattern, d.replaceVariable(declCorrXpath.getBindingName(), "this"), descr, mvelCtx);
                if (constraint != null) {
                    pattern.getXpathConstraint().getChunks().getLast().addConstraint(constraint);
                }
            }
        }
    }
    if (descr.getDescrs().size() > initialDescrs.size()) {
        // The initial build process may have generated other constraint descrs.
        // This happens when null-safe references or inline-casts are used
        // These additional constraints must be built, and added as
        List<BaseDescr> additionalDescrs = new ArrayList<BaseDescr>(descr.getDescrs());
        additionalDescrs.removeAll(initialDescrs);
        if (!additionalDescrs.isEmpty()) {
            List<Constraint> additionalConstraints = new ArrayList<Constraint>();
            for (BaseDescr d : additionalDescrs) {
                Constraint constraint = buildCcdDescr(context, patternDescr, pattern, d, descr, mvelCtx);
                if (constraint != null) {
                    additionalConstraints.add(constraint);
                }
            }
            constraints.addAll(0, additionalConstraints);
        }
    }
    return constraints;
}
Also used : Pattern(org.drools.core.rule.Pattern) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) 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) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 14 with Constraint

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

the class PatternBuilder method buildConstraintForPattern.

protected Constraint buildConstraintForPattern(final RuleBuildContext context, final Pattern pattern, final RelationalExprDescr relDescr, String expr, String value1, String value2, boolean isConstant, Map<String, OperatorDescr> aliases) {
    InternalReadAccessor extractor = getFieldReadAccessor(context, relDescr, pattern, value1, null, true);
    if (extractor == null) {
        return null;
    }
    int dotPos = value1.indexOf('.');
    if (dotPos > 0) {
        String part0 = value1.substring(0, dotPos).trim();
        if ("this".equals(part0.trim())) {
            value1 = value1.substring(dotPos + 1);
        } else if (pattern.getDeclaration() != null && part0.equals(pattern.getDeclaration().getIdentifier())) {
            value1 = value1.substring(dotPos + 1);
            expr = expr.substring(dotPos + 1);
        }
    }
    ValueType vtype = extractor.getValueType();
    String operator = relDescr.getOperator().trim();
    LiteralRestrictionDescr restrictionDescr = buildLiteralRestrictionDescr(context, relDescr, value2, operator, isConstant);
    if (restrictionDescr != null) {
        FieldValue field = getFieldValue(context, vtype, restrictionDescr.getText().trim());
        if (field != null) {
            Constraint constraint = getConstraintBuilder(context).buildLiteralConstraint(context, pattern, vtype, field, expr, value1, operator, value2, extractor, restrictionDescr, aliases);
            if (constraint != null) {
                return constraint;
            }
        }
    }
    value2 = context.getDeclarationResolver().normalizeValueForUnit(value2);
    Declaration declr = null;
    if (value2.indexOf('(') < 0 && value2.indexOf('.') < 0 && value2.indexOf('[') < 0) {
        declr = context.getDeclarationResolver().getDeclaration(value2);
        if (declr == null) {
            // trying to create implicit declaration
            final Pattern thisPattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
            declr = createDeclarationObject(context, value2, thisPattern);
        }
    }
    Declaration[] declarations = null;
    if (declr == null) {
        String[] parts = value2.split("\\.");
        if (parts.length == 2) {
            if ("this".equals(parts[0].trim())) {
                declr = createDeclarationObject(context, parts[1].trim(), (Pattern) context.getDeclarationResolver().peekBuildStack());
                value2 = parts[1].trim();
            } else {
                declr = context.getDeclarationResolver().getDeclaration(parts[0].trim());
                // if a declaration exists, then it may be a variable direct property access
                if (declr != null) {
                    if (declr.isPatternDeclaration()) {
                        // TODO: no need to extract inner declaration when using mvel constraint
                        declarations = new Declaration[] { declr };
                        declr = createDeclarationObject(context, parts[1].trim(), declr.getPattern());
                        value2 = parts[1].trim();
                    } else {
                        // we will later fallback to regular predicates, so don't raise error
                        return null;
                    }
                }
            }
        }
    }
    if (declarations == null) {
        if (declr != null) {
            declarations = new Declaration[] { declr };
        } else {
            declarations = getDeclarationsForReturnValue(context, relDescr, operator, value2);
            if (declarations == null) {
                return null;
            }
        }
    }
    return getConstraintBuilder(context).buildVariableConstraint(context, pattern, expr, declarations, value1, relDescr.getOperatorDescr(), value2, extractor, declr, relDescr, aliases);
}
Also used : Pattern(org.drools.core.rule.Pattern) ValueType(org.drools.core.base.ValueType) 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) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FieldValue(org.drools.core.spi.FieldValue) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) 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) LiteralRestrictionDescr(org.drools.compiler.lang.descr.LiteralRestrictionDescr)

Example 15 with Constraint

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

the class PatternBuilder method build.

protected void build(final RuleBuildContext context, final PatternDescr patternDescr, final Pattern pattern, final BaseDescr original, final String expr) {
    ConstraintConnectiveDescr result = parseExpression(context, patternDescr, original, expr);
    if (result == null) {
        return;
    }
    result.copyLocation(original);
    MVELDumper.MVELDumperContext mvelCtx = new MVELDumper.MVELDumperContext().setRuleContext(context);
    List<Constraint> constraints = build(context, patternDescr, pattern, result, mvelCtx);
    pattern.addConstraints(constraints);
}
Also used : 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) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) MVELDumper(org.drools.compiler.lang.MVELDumper)

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