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;
}
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;
}
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();
}
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;
}
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);
}
}
}
Aggregations