Search in sources :

Example 1 with ExpressionDescr

use of org.drools.drl.ast.descr.ExpressionDescr in project drools by kiegroup.

the class PatternBuilder method build.

private List<Constraint> build(RuleBuildContext context, PatternDescr patternDescr, Declaration xpathStartDeclaration, Pattern pattern, ConstraintConnectiveDescr descr, DumperContext mvelCtx) {
    List<Constraint> constraints = new ArrayList<>();
    List<BaseDescr> initialDescrs = new ArrayList<>(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;
        }
        context.setXpathOffsetadjustment(xpathStartDeclaration == null ? 0 : -1);
        Constraint constraint = isXPath ? buildXPathDescr(context, patternDescr, xpathStartDeclaration, pattern, (ExpressionDescr) d, mvelCtx) : buildCcdDescr(context, patternDescr, xpathStartDeclaration, 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, xpathStartDeclaration, 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<>(descr.getDescrs());
        additionalDescrs.removeAll(initialDescrs);
        if (!additionalDescrs.isEmpty()) {
            List<Constraint> additionalConstraints = new ArrayList<>();
            for (BaseDescr d : additionalDescrs) {
                Constraint constraint = buildCcdDescr(context, patternDescr, xpathStartDeclaration, 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) PredicateConstraint(org.drools.core.rule.PredicateConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) ArrayList(java.util.ArrayList) ExpressionDescr(org.drools.drl.ast.descr.ExpressionDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration)

Aggregations

ArrayList (java.util.ArrayList)1 ClassObjectType (org.drools.core.base.ClassObjectType)1 Declaration (org.drools.core.rule.Declaration)1 Pattern (org.drools.core.rule.Pattern)1 PredicateConstraint (org.drools.core.rule.PredicateConstraint)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 NegConstraint (org.drools.core.rule.constraint.NegConstraint)1 XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)1 AcceptsClassObjectType (org.drools.core.spi.AcceptsClassObjectType)1 Constraint (org.drools.core.spi.Constraint)1 BaseDescr (org.drools.drl.ast.descr.BaseDescr)1 ExpressionDescr (org.drools.drl.ast.descr.ExpressionDescr)1