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