use of org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess in project drools by kiegroup.
the class OOPathExprGenerator method toPatternExpr.
private void toPatternExpr(String bindingId, List<DrlxParseResult> list, DrlxParseSuccess patternParseResult, Class<?> fieldType) {
MethodCallExpr patternExpr = createDslTopLevelMethod(PATTERN_CALL);
patternExpr.addArgument(context.getVar(bindingId));
SingleDrlxParseSuccess oopathConstraint = null;
for (DrlxParseResult drlx : list) {
if (drlx.isSuccess()) {
SingleDrlxParseSuccess singleDrlx = (SingleDrlxParseSuccess) drlx;
if (singleDrlx.isOOPath()) {
if (oopathConstraint != null) {
throw new UnsupportedOperationException("An oopath chunk can only have a single oopath constraint");
}
oopathConstraint = singleDrlx;
continue;
}
if (singleDrlx.getExprBinding() != null) {
MethodCallExpr expr = expressionBuilder.buildBinding(singleDrlx);
expr.setScope(patternExpr);
patternExpr = expr;
}
if (singleDrlx.getExpr() != null && !(singleDrlx.getExpr() instanceof NameExpr)) {
MethodCallExpr expr = expressionBuilder.buildExpressionWithIndexing(singleDrlx);
expr.setScope(patternExpr);
patternExpr = expr;
}
}
}
context.addExpression(patternExpr);
if (bindingId.equals(patternParseResult.getExprBinding())) {
context.registerOOPathPatternExpr(bindingId, patternExpr);
}
if (oopathConstraint != null) {
new OOPathExprGenerator(context, packageModel).visit(fieldType, bindingId, oopathConstraint);
}
}
use of org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess in project drools by kiegroup.
the class NamedConsequenceVisitor method whenThenDSL.
private MethodCallExpr whenThenDSL(ConditionalBranchDescr desc, PatternDescr patternRelated, Class<?> patternType, String callMethod, MethodCallExpr parentExpression) {
MethodCallExpr when = parentExpression == null ? createDslTopLevelMethod(callMethod) : new MethodCallExpr(parentExpression, callMethod);
final String condition = desc.getCondition().toString();
if (!condition.equals("true")) {
// Default case
when.addArgument(new StringLiteralExpr(context.getConditionId(patternType, condition)));
String identifier = patternRelated.getIdentifier();
DrlxParseResult parseResult;
if (identifier == null) {
// The accumulate pattern doesn't have an identifier. Let's parse the consequence and use the acc functions
parseResult = ConstraintParser.defaultConstraintParser(context, packageModel).drlxParse(Object.class, "", condition);
parseResult.accept((DrlxParseSuccess parseSuccess) -> {
SingleDrlxParseSuccess parseSuccess1 = (SingleDrlxParseSuccess) parseSuccess;
AccumulateDescr source = (AccumulateDescr) patternRelated.getSource();
for (String usedDeclaration : parseSuccess1.getUsedDeclarations()) {
for (AccumulateDescr.AccumulateFunctionCallDescr functionCallDescr : source.getFunctions()) {
if (functionCallDescr.getBind().equals(usedDeclaration)) {
addVariable(patternRelated, when, functionCallDescr);
}
}
}
when.addArgument(generateLambdaWithoutParameters(parseSuccess1.getUsedDeclarations(), parseSuccess.getExpr(), true, Optional.empty()));
});
} else {
when.addArgument(context.getVarExpr(identifier));
parseResult = ConstraintParser.defaultConstraintParser(context, packageModel).drlxParse(patternType, identifier, condition);
Collection<String> usedDeclarations = ((SingleDrlxParseSuccess) parseResult).getUsedDeclarations();
if (usedDeclarations.isEmpty()) {
// _this
parseResult.accept(parseSuccess -> when.addArgument(generateLambdaWithoutParameters(Collections.emptySortedSet(), parseSuccess.getExpr())));
} else {
parseResult.accept(parseSuccess -> when.addArgument(generateLambdaWithoutParameters(usedDeclarations, parseSuccess.getExpr(), true, Optional.empty())));
}
}
}
MethodCallExpr then = new MethodCallExpr(when, THEN_CALL);
MethodCallExpr rhs = onDSL(desc.getConsequence());
then.addArgument(rhs);
return then;
}
use of org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess in project drools by kiegroup.
the class EvalVisitor method visit.
public void visit(EvalDescr descr) {
String expression = descr.getContent().toString();
DrlxParseResult drlxParseResult = ConstraintParser.withoutVariableValidationConstraintParser(context, packageModel).drlxParse(null, null, expression);
drlxParseResult.accept(drlxParseSuccess -> {
SingleDrlxParseSuccess singleResult = (SingleDrlxParseSuccess) drlxParseResult;
Expression rewriteExprAsLambdaWithoutThisParam = DrlxParseUtil.generateLambdaWithoutParameters(singleResult.getUsedDeclarations(), singleResult.getExpr(), true, Optional.empty(), context);
// rewrites the DrlxParserResult expr as directly the lambda to use
singleResult.setExpr(rewriteExprAsLambdaWithoutThisParam);
singleResult.setStatic(true);
new EvalExpressionBuilder(context).processExpression(singleResult);
});
}
use of org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess in project drools by kiegroup.
the class AccumulateVisitor method methodCallExprParameter.
private Optional<NewBinding> methodCallExprParameter(PatternDescr basePattern, BaseDescr input, AccumulateDescr.AccumulateFunctionCallDescr function, MethodCallExpr functionDSL, String bindingId, Expression accumulateFunctionParameter) {
final Expression parameterConverted = convertParameter(accumulateFunctionParameter);
final DrlxParseUtil.RemoveRootNodeResult methodCallWithoutRootNode = DrlxParseUtil.removeRootNode(parameterConverted);
String rootNodeName = getRootNodeName(methodCallWithoutRootNode);
Optional<DeclarationSpec> decl = context.getDeclarationById(rootNodeName);
Class<?> clazz = decl.map(DeclarationSpec::getDeclarationClass).orElseGet(() -> {
try {
return context.getTypeResolver().resolveType(rootNodeName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
});
final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
final ExpressionTyper expressionTyper = new ExpressionTyper(context, clazz, bindingId, false, expressionTyperContext);
TypedExpression typedExpression = expressionTyper.toTypedExpression(methodCallWithoutRootNode.getWithoutRootNode()).typedExpressionOrException();
final Class<?> methodCallExprType = typedExpression.getRawClass();
final AccumulateFunction accumulateFunction = getAccumulateFunction(function, methodCallExprType);
validateAccFunctionTypeAgainstPatternType(context, basePattern, accumulateFunction);
functionDSL.addArgument(createAccSupplierExpr(accumulateFunction));
// Every expression in an accumulate function gets transformed in a bind expression with a generated id
// Then the accumulate function will have that binding expression as a source
final Class accumulateFunctionResultType = accumulateFunction.getResultType();
final String bindExpressionVariable = context.getExprId(accumulateFunctionResultType, typedExpression.toString());
String paramExprBindingId = rootNodeName;
Class<?> patternType = clazz;
PatternDescr inputPattern = decl.isPresent() ? null : findInputPattern(input);
if (inputPattern != null) {
String inputId = inputPattern.getIdentifier();
Optional<DeclarationSpec> accumulateClassDeclOpt = context.getDeclarationById(inputId);
if (accumulateClassDeclOpt.isPresent()) {
// when static method is used in accumulate function, "_this" is a pattern input
// Note that DrlxParseUtil.generateLambdaWithoutParameters() takes the patternType as a class of "_this"
paramExprBindingId = inputId;
patternType = accumulateClassDeclOpt.get().getDeclarationClass();
}
}
SingleDrlxParseSuccess drlxParseResult = (SingleDrlxParseSuccess) ConstraintParser.defaultConstraintParser(context, context.getPackageModel()).drlxParse(patternType, paramExprBindingId, printNode(parameterConverted));
if (inputPattern != null) {
drlxParseResult.setAccumulateBinding(inputPattern.getIdentifier());
}
return drlxParseResult.acceptWithReturnValue(new ReplaceBindingVisitor(functionDSL, bindingId, methodCallExprType, accumulateFunctionResultType, bindExpressionVariable, drlxParseResult));
}
use of org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess in project drools by kiegroup.
the class ConstraintUtil method normalizeConstraint.
/**
* Swap left and right operands in a constraint when a fact property is located on the right side.
*
* e.g. Person(20 < age) should be normalized to Person(age > 20)
*
* @param drlxParseResult
* @return Normalized <code>DrlxParseResult</code>
*/
public static DrlxParseResult normalizeConstraint(DrlxParseResult drlxParseResult) {
if (!ENABLE_NORMALIZE) {
return drlxParseResult;
}
if (drlxParseResult instanceof SingleDrlxParseSuccess) {
// Create a copy
SingleDrlxParseSuccess drlx = new SingleDrlxParseSuccess((SingleDrlxParseSuccess) drlxParseResult);
Expression expr = drlx.getExpr();
if (expr == null) {
return drlxParseResult;
}
if (expr instanceof MethodCallExpr) {
processTopLevelExpression(drlx, (MethodCallExpr) expr);
} else if (expr instanceof EnclosedExpr) {
Expression inner = stripEnclosedExpr((EnclosedExpr) expr);
if (inner instanceof MethodCallExpr) {
processTopLevelExpression(drlx, (MethodCallExpr) inner);
} else {
processExpression(expr);
}
} else {
processExpression(expr);
}
return drlx;
}
return drlxParseResult;
}
Aggregations