use of org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess 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.SingleDrlxParseSuccess in project drools by kiegroup.
the class AccumulateVisitor method binaryExprParameter.
private Optional<NewBinding> binaryExprParameter(PatternDescr basePattern, AccumulateDescr.AccumulateFunctionCallDescr function, MethodCallExpr functionDSL, String bindingId, String accumulateFunctionParameterStr) {
final DrlxParseResult parseResult = ConstraintParser.defaultConstraintParser(context, packageModel).drlxParse(Object.class, bindingId, accumulateFunctionParameterStr);
Optional<NewBinding> optNewBinding = parseResult.acceptWithReturnValue(new ParseResultVisitor<Optional<NewBinding>>() {
@Override
public Optional<NewBinding> onSuccess(DrlxParseSuccess drlxParseResult) {
SingleDrlxParseSuccess singleResult = (SingleDrlxParseSuccess) drlxParseResult;
Class<?> exprRawClass = singleResult.getExprRawClass();
AccumulateFunction accumulateFunction = getAccumulateFunction(function, exprRawClass);
validateAccFunctionTypeAgainstPatternType(context, basePattern, accumulateFunction);
final String bindExpressionVariable = context.getExprId(accumulateFunction.getResultType(), singleResult.getLeft().toString());
singleResult.setExprBinding(bindExpressionVariable);
context.addDeclarationReplacing(new DeclarationSpec(singleResult.getPatternBinding(), exprRawClass));
context.getExpressions().forEach(expression -> replaceTypeInExprLambda(bindingId, exprRawClass, expression));
functionDSL.addArgument(createAccSupplierExpr(accumulateFunction));
final MethodCallExpr newBindingFromBinary = AccumulateVisitor.this.buildBinding(bindExpressionVariable, singleResult.getUsedDeclarations(), singleResult.getExpr());
context.addDeclarationReplacing(new DeclarationSpec(bindExpressionVariable, exprRawClass));
functionDSL.addArgument(context.getVarExpr(bindExpressionVariable));
return Optional.of(new NewBinding(Collections.emptyList(), newBindingFromBinary));
}
@Override
public Optional<NewBinding> onFail(DrlxParseFail failure) {
return Optional.empty();
}
});
return optNewBinding;
}
use of org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess in project drools by kiegroup.
the class FromVisitor method createArg.
private Expression createArg(String expression, String bindingId, MethodCallExpr fromCall) {
if (bindingId != null) {
DeclarationSpec declarationSpec = context.getDeclarationById(bindingId).orElseThrow(RuntimeException::new);
Class<?> clazz = declarationSpec.getDeclarationClass();
DrlxParseResult drlxParseResult = ConstraintParser.withoutVariableValidationConstraintParser(context, packageModel).drlxParse(clazz, bindingId, expression);
return drlxParseResult.acceptWithReturnValue(drlxParseSuccess -> {
SingleDrlxParseSuccess singleResult = (SingleDrlxParseSuccess) drlxParseResult;
if (!isCompatibleWithFromReturnType(patternType, singleResult.getExprRawClass())) {
context.addCompilationError(new InvalidExpressionErrorResult("Pattern of type: '" + patternType.getCanonicalName() + "' on rule '" + context.getRuleName() + "' is not compatible with type " + singleResult.getExprRawClass().getCanonicalName() + " returned by source"));
}
Expression parsedExpression = drlxParseSuccess.getExpr();
Expression newExpr = generateLambdaWithoutParameters(singleResult.getUsedDeclarations(), parsedExpression, singleResult.isSkipThisAsParam(), ofNullable(singleResult.getPatternType()), context);
if (newExpr instanceof LambdaExpr) {
context.getPackageModel().registerLambdaReturnType((LambdaExpr) newExpr, singleResult.getExprType());
}
addArgumentWithPreexistingCheck(fromCall, singleResult.getUsedDeclarations());
return newExpr;
});
}
return null;
}
use of org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess in project drools by kiegroup.
the class PatternExpressionBuilder method buildExpressionWithIndexing.
@Override
public MethodCallExpr buildExpressionWithIndexing(DrlxParseSuccess drlxParseResult) {
if (drlxParseResult instanceof MultipleDrlxParseSuccess) {
MultipleDrlxParseSuccess multi = (MultipleDrlxParseSuccess) drlxParseResult;
MethodCallExpr exprDSL = new MethodCallExpr(null, multi.getOperator() == BinaryExpr.Operator.OR ? EXPR_OR_CALL : EXPR_AND_CALL);
for (DrlxParseSuccess child : multi.getResults()) {
MethodCallExpr childExpr = buildExpressionWithIndexing(child);
childExpr.setScope(exprDSL);
exprDSL = childExpr;
if (child instanceof SingleDrlxParseSuccess && child.getExprBinding() != null) {
SingleDrlxParseSuccess singleDrlxChild = (SingleDrlxParseSuccess) child;
context.addDeclaration(child.getExprBinding(), singleDrlxChild.getLeftExprRawClass());
Expression dslExpr = buildBinding(singleDrlxChild);
context.addExpression(dslExpr);
}
}
return new MethodCallExpr(exprDSL, multi.getOperator() == BinaryExpr.Operator.OR ? EXPR_END_OR_CALL : EXPR_END_AND_CALL);
}
return buildSingleExpressionWithIndexing((SingleDrlxParseSuccess) drlxParseResult);
}
use of org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess 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