use of org.drools.drl.ast.descr.BaseDescr 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.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class AccumulateVisitor method visit.
public void visit(AccumulateDescr descr, PatternDescr basePattern) {
final MethodCallExpr accumulateDSL = createDslTopLevelMethod(ACCUMULATE_CALL);
context.addExpression(accumulateDSL);
final MethodCallExpr accumulateExprs = createDslTopLevelMethod(AND_CALL);
accumulateDSL.addArgument(accumulateExprs);
this.context.pushScope(descr);
pushAccumulateContext(accumulateExprs);
try {
Set<String> externalDeclrs = new HashSet<>(context.getAvailableBindings());
BaseDescr input = descr.getInputPattern() == null ? descr.getInput() : descr.getInputPattern();
input.accept(modelGeneratorVisitor);
if (accumulateExprs.getArguments().isEmpty()) {
accumulateDSL.remove(accumulateExprs);
} else if (accumulateExprs.getArguments().size() == 1) {
accumulateDSL.setArgument(0, accumulateExprs.getArguments().get(0));
}
if (!descr.getFunctions().isEmpty()) {
if (validateBindings(descr)) {
return;
}
processAccumulateFunctions(descr, basePattern, input, accumulateDSL);
} else if (descr.getFunctions().isEmpty() && descr.getInitCode() != null) {
new AccumulateInlineVisitor(context, packageModel).inlineAccumulate(descr, basePattern, accumulateDSL, externalDeclrs, input);
} else {
throw new UnsupportedOperationException("Unknown type of Accumulate.");
}
} finally {
context.popExprPointer();
this.context.popScope();
}
}
use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class FromCollectVisitor method trasformFromCollectToCollectList.
public void trasformFromCollectToCollectList(PatternDescr pattern, CollectDescr collectDescr) {
// The inner pattern of the "from collect" needs to be processed to have the binding
final PatternDescr collectDescrInputPattern = collectDescr.getInputPattern();
if (!parentVisitor.initPattern(collectDescrInputPattern)) {
return;
}
final AccumulateDescr accumulateDescr = new AccumulateDescr();
accumulateDescr.setInputPattern(collectDescrInputPattern);
accumulateDescr.addFunction("collectList", null, false, new String[] { collectDescrInputPattern.getIdentifier() });
final PatternDescr transformedPatternDescr = new PatternDescr(pattern.getObjectType(), pattern.getIdentifier());
for (BaseDescr o : pattern.getConstraint().getDescrs()) {
transformedPatternDescr.addConstraint(o);
}
transformedPatternDescr.setSource(accumulateDescr);
transformedPatternDescr.accept(parentVisitor);
}
use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class OrVisitor method visit.
public void visit(ConditionalElementDescr descr) {
final MethodCallExpr ceDSL = createDslTopLevelMethod(OR_CALL);
context.addExpression(ceDSL);
for (BaseDescr subDescr : descr.getDescrs()) {
final MethodCallExpr andDSL = createDslTopLevelMethod(AND_CALL);
context.setNestedInsideOr(true);
context.pushExprPointer(andDSL::addArgument);
subDescr.accept(modelGeneratorVisitor);
context.popExprPointer();
ceDSL.addArgument(andDSL);
context.setNestedInsideOr(false);
}
for (String k : context.getBindingOr()) {
if (context.getBindingOr().sizeFor(k) != descr.getDescrs().size()) {
context.getUnusableOrBinding().add(k);
}
}
}
use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class MVELDialect method analyzeExpression.
public static AnalysisResult analyzeExpression(final PackageBuildContext context, final BaseDescr descr, final Object content, final BoundIdentifiers availableIdentifiers, final Map<String, Class<?>> localTypes) {
AnalysisResult result = null;
// the following is required for proper error handling
BaseDescr temp = context.getParentDescr();
context.setParentDescr(descr);
try {
result = MVELExprAnalyzer.analyzeExpression(context, (String) content, availableIdentifiers, localTypes, "drools", KnowledgeHelper.class);
} catch (final Exception e) {
AsmUtil.copyErrorLocation(e, descr);
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to determine the used declarations.\n" + e.getMessage()));
} finally {
// setting it back to original parent descr
context.setParentDescr(temp);
}
return result;
}
Aggregations