use of org.drools.modelcompiler.builder.generator.drlxparse.ConstraintExpression in project drools by kiegroup.
the class PatternDSL method findAllConstraint.
private List<PatternConstraintParseResult> findAllConstraint(PatternDescr pattern, List<? extends BaseDescr> constraintDescrs, Class<?> patternType) {
ConstraintParser constraintParser = ConstraintParser.defaultConstraintParser(context, packageModel);
List<PatternConstraintParseResult> patternConstraintParseResults = new ArrayList<>();
for (BaseDescr constraint : constraintDescrs) {
List<PatternConstraintParseResult> patternConstraintParseResultsPerConstraintDescr = new ArrayList<>();
String patternIdentifier = pattern.getIdentifier();
boolean isPositional = isPositional(constraint);
ConstraintExpression constraintExpression = ConstraintExpression.createConstraintExpression(context, patternType, constraint, isPositional);
if (constraintExpression == null) {
continue;
}
DrlxParseResult drlxParseResult;
try {
context.setCurrentConstraintDescr(Optional.of(constraint));
drlxParseResult = constraintParser.drlxParse(patternType, patternIdentifier, constraintExpression, isPositional);
} finally {
context.resetCurrentConstraintDescr();
}
String expression = constraintExpression.getExpression();
if (drlxParseResult.isSuccess() && ((DrlxParseSuccess) drlxParseResult).isRequiresSplit() && ((DrlxParseSuccess) drlxParseResult).getExpr().isBinaryExpr()) {
BinaryExpr expr = ((DrlxParseSuccess) drlxParseResult).getExpr().asBinaryExpr();
String leftExpression = printNode(((SingleDrlxParseSuccess) drlxParseResult).getLeft().getExpression());
DrlxParseResult leftExpressionReparsed = constraintParser.drlxParse(patternType, patternIdentifier, leftExpression, isPositional);
patternConstraintParseResultsPerConstraintDescr.add(new PatternConstraintParseResult(leftExpression, patternIdentifier, leftExpressionReparsed));
String rightExpression = printNode(((SingleDrlxParseSuccess) drlxParseResult).getRight().getExpression());
DrlxParseResult rightExpressionReparsed = constraintParser.drlxParse(patternType, patternIdentifier, rightExpression, isPositional);
DrlxParseResult normalizedParseResult = ConstraintUtil.normalizeConstraint(rightExpressionReparsed);
patternConstraintParseResultsPerConstraintDescr.add(new PatternConstraintParseResult(rightExpression, patternIdentifier, normalizedParseResult));
} else {
DrlxParseResult normalizedParseResult = ConstraintUtil.normalizeConstraint(drlxParseResult);
patternConstraintParseResultsPerConstraintDescr.add(new PatternConstraintParseResult(expression, patternIdentifier, normalizedParseResult));
}
// Cast-check should be placed earlier than Null-check (calling the add method later means pushing the constraint earlier)
addNullSafeExpr(constraintParser, pattern.getIdentifier(), patternConstraintParseResultsPerConstraintDescr);
addImplicitCastExpr(constraintParser, pattern.getIdentifier(), patternConstraintParseResultsPerConstraintDescr);
patternConstraintParseResults.addAll(patternConstraintParseResultsPerConstraintDescr);
}
return patternConstraintParseResults;
}
use of org.drools.modelcompiler.builder.generator.drlxparse.ConstraintExpression in project drools by kiegroup.
the class LhsParser method parseConstraint.
private void parseConstraint(RuleContext context, PatternDescr patternDescr, Pattern pattern, ConstraintParser constraintParser, BaseDescr constraintDescr) {
ConstraintExpression constraintExpression = ConstraintExpression.createConstraintExpression(context, pattern.getPatternClass(), constraintDescr, false);
DrlxParseResult drlxParseResult = constraintParser.drlxParse(pattern.getPatternClass(), patternDescr.getIdentifier(), constraintExpression, false);
if (drlxParseResult.isSuccess()) {
SingleDrlxParseSuccess result = (SingleDrlxParseSuccess) drlxParseResult;
if (!result.getReactOnProperties().isEmpty()) {
result.getReactOnProperties().forEach(pattern::addReactOn);
} else {
pattern.setClassReactive(true);
}
if (result.getRight() != null) {
Constraint constraint = new Constraint();
constraint = parseExpressionInConstraint(context, constraint, result.getLeft());
boolean valueOnLeft = constraint.getValue() != null;
constraint = parseExpressionInConstraint(context, constraint, result.getRight());
if (constraint.getValue() != null) {
// the constraint is relevant for impact analysis only if it checks a fixed value
constraint.setType(decode(result.getDecodeConstraintType(), valueOnLeft));
pattern.addConstraint(constraint);
}
} else if (result.getExprBinding() != null && result.getExpr() != null && result.getExpr().isLiteralExpr()) {
((ImpactAnalysisRuleContext) context).getBindVariableLiteralMap().put(result.getExprBinding(), ParserUtil.literalToValue(result.getExpr().asLiteralExpr()));
} else if (isPredicateMethodCall(result)) {
MethodCallExpr mce = result.getExpr().asMethodCallExpr();
addConstraintIfBooleanProperty(pattern, mce, true);
} else if (isNegatedPredicateMethodCall(result)) {
MethodCallExpr mce = result.getExpr().asUnaryExpr().getExpression().asMethodCallExpr();
addConstraintIfBooleanProperty(pattern, mce, false);
}
}
}
Aggregations