Search in sources :

Example 6 with BaseDescr

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

the class GroupElementBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    final ConditionalElementDescr cedescr = (ConditionalElementDescr) descr;
    final GroupElement ge = this.newGroupElementFor(descr);
    context.getDeclarationResolver().pushOnBuildStack(ge);
    if (prefixPattern != null) {
        ge.addChild(prefixPattern);
    }
    // iterate over child descriptors
    for (final BaseDescr child : cedescr.getDescrs()) {
        // gets child to build
        // child.setResource(..) does not seem to be necessary (since builderImpls have already set the resource for all children)
        // but leaving it in here to be save
        child.setResource(context.getRuleDescr().getResource());
        child.setNamespace(context.getRuleDescr().getNamespace());
        // gets corresponding builder
        final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(child.getClass());
        if (builder != null) {
            final RuleConditionElement element = builder.build(context, child);
            // builder will return null. Ex: ClassNotFound for the pattern type
            if (element != null) {
                ge.addChild(element);
            }
        } else {
            throw new RuntimeException("BUG: no builder found for descriptor class " + child.getClass());
        }
    }
    context.getDeclarationResolver().popBuildStack();
    return ge;
}
Also used : GroupElement(org.drools.core.rule.GroupElement) BaseDescr(org.drools.drl.ast.descr.BaseDescr) RuleConditionElement(org.drools.core.rule.RuleConditionElement) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Example 7 with BaseDescr

use of org.drools.drl.ast.descr.BaseDescr 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)

Example 8 with BaseDescr

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

the class PatternBuilder method rewriteCompositeExpressions.

private String rewriteCompositeExpressions(RuleBuildContext context, Pattern pattern, ConstraintConnectiveDescr d) {
    int i = 0;
    StringBuilder sb = new StringBuilder();
    for (BaseDescr subDescr : d.getDescrs()) {
        if (subDescr instanceof BindingDescr) {
            continue;
        }
        if (i++ > 0) {
            sb.append(" ").append(d.getConnective().getConnective()).append(" ");
        }
        String normalizedExpr;
        if (subDescr instanceof RelationalExprDescr && isSimpleExpr((RelationalExprDescr) subDescr)) {
            RelationalExprDescr relDescr = (RelationalExprDescr) subDescr;
            if (relDescr.getExpression() != null) {
                normalizedExpr = normalizeExpression(context, pattern, relDescr, relDescr.getExpression());
            } else {
                i--;
                normalizedExpr = "";
            }
        } else if (subDescr instanceof ConstraintConnectiveDescr) {
            String rewrittenExpr = rewriteCompositeExpressions(context, pattern, (ConstraintConnectiveDescr) subDescr);
            if (rewrittenExpr == null) {
                return null;
            }
            normalizedExpr = "(" + rewrittenExpr + ")";
        } else if (subDescr instanceof AtomicExprDescr) {
            normalizedExpr = ((AtomicExprDescr) subDescr).getRewrittenExpression();
        } else {
            return null;
        }
        sb.append(normalizedExpr);
    }
    return sb.toString();
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) AtomicExprDescr(org.drools.drl.ast.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) 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) RelationalExprDescr(org.drools.drl.ast.descr.RelationalExprDescr)

Example 9 with BaseDescr

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

the class PatternBuilder method collectProps.

private static Collection<String> collectProps(PatternDescr pattern) {
    Collection<String> props = new HashSet<>();
    for (BaseDescr expr : pattern.getDescrs()) {
        if (expr instanceof ExprConstraintDescr) {
            String text = expr.getText();
            String prop = StringUtils.extractFirstIdentifier(text, 0);
            String propFromGetter = ClassUtils.getter2property(prop);
            props.add(propFromGetter != null ? propFromGetter : StringUtils.lcFirst(prop));
        }
    }
    return props;
}
Also used : BaseDescr(org.drools.drl.ast.descr.BaseDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) HashSet(java.util.HashSet)

Example 10 with BaseDescr

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

the class PatternBuilder method processConstraintsAndBinds.

/**
 * Process all constraints and bindings on this pattern
 */
private void processConstraintsAndBinds(RuleBuildContext context, PatternDescr patternDescr, Declaration xpathStartDeclaration, Pattern pattern) {
    DumperContext mvelCtx = new DumperContext().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;
        }
        result.setNegated(b.isNegated());
        isPositional &= !(result.getDescrs().size() == 1 && result.getDescrs().get(0) instanceof BindingDescr);
        if (isPositional) {
            processPositional(context, patternDescr, xpathStartDeclaration, pattern, (ExprConstraintDescr) b);
        } else {
            // need to build the actual constraint
            List<Constraint> constraints = build(context, patternDescr, xpathStartDeclaration, pattern, result, mvelCtx);
            pattern.addConstraints(constraints);
        }
    }
    TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
    if (typeDeclaration != null && typeDeclaration.isPropertyReactive()) {
        for (String field : lookAheadFieldsOfIdentifier(context.getRuleDescr(), patternDescr)) {
            addFieldToPatternWatchlist(pattern, typeDeclaration, field);
        }
    }
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) 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) BaseDescr(org.drools.drl.ast.descr.BaseDescr) DumperContext(org.drools.compiler.lang.DumperContext) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr)

Aggregations

BaseDescr (org.drools.drl.ast.descr.BaseDescr)117 ConstraintConnectiveDescr (org.drools.drl.ast.descr.ConstraintConnectiveDescr)23 AndDescr (org.drools.drl.ast.descr.AndDescr)18 AtomicExprDescr (org.drools.drl.ast.descr.AtomicExprDescr)17 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)16 PatternDescr (org.drools.drl.ast.descr.PatternDescr)15 AnnotatedBaseDescr (org.drools.drl.ast.descr.AnnotatedBaseDescr)14 BindingDescr (org.drools.drl.ast.descr.BindingDescr)13 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)11 Test (org.junit.Test)10 RelationalExprDescr (org.drools.drl.ast.descr.RelationalExprDescr)9 DeclareDescrBuilder (org.drools.drl.ast.dsl.DeclareDescrBuilder)9 AnnotationDescr (org.drools.drl.ast.descr.AnnotationDescr)8 CEDescrBuilder (org.drools.drl.ast.dsl.CEDescrBuilder)7 OrDescr (org.drools.drl.ast.descr.OrDescr)6 AccumulateDescrBuilder (org.drools.drl.ast.dsl.AccumulateDescrBuilder)6 AnnotatedDescrBuilder (org.drools.drl.ast.dsl.AnnotatedDescrBuilder)6 AnnotationDescrBuilder (org.drools.drl.ast.dsl.AnnotationDescrBuilder)6 AttributeDescrBuilder (org.drools.drl.ast.dsl.AttributeDescrBuilder)6 BehaviorDescrBuilder (org.drools.drl.ast.dsl.BehaviorDescrBuilder)6