use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class MVELDumper method processInlineCast.
private String[] processInlineCast(String expr, AtomicExprDescr atomicExpr, ConstraintConnectiveDescr ccd, MVELDumperContext context, int sharpPos, int parentIdx, int childIdx) {
// convert "field1#Class.field2" in ["field1 instanceof Class && ", "((Class)field1).field2"]
String field1 = expr.substring(0, sharpPos).trim();
int sharpPos2 = expr.indexOf('#', sharpPos + 1);
String part2 = sharpPos2 < 0 ? expr.substring(sharpPos + 1).trim() : expr.substring(sharpPos + 1, sharpPos2).trim();
String[] classAndField = splitInClassAndField(part2, context);
BaseDescr desc = parentIdx >= 0 ? ccd.getDescrs().get(parentIdx) : null;
if (classAndField == null) {
return new String[] { "", expr };
} else if (desc instanceof AtomicExprDescr && classAndField.length == 1) {
return new String[] { "", field1 + " instanceof " + classAndField[0] };
}
String className = classAndField[0];
String castedExpression = classAndField.length == 1 ? "((" + className + ")" + field1 + ")" : "((" + className + ")" + field1 + ")." + classAndField[1] + (sharpPos2 > 0 ? expr.substring(sharpPos2) : "");
RelationalExprDescr check = new RelationalExprDescr("instanceof", false, null, new AtomicExprDescr(field1), new AtomicExprDescr(className));
if (ccd.getConnective() == ConnectiveType.AND || ccd.getConnective() == ConnectiveType.INC_AND) {
ccd.getDescrs().add(childIdx, check);
} else {
if (desc instanceof ConstraintConnectiveDescr) {
((ConstraintConnectiveDescr) desc).getDescrs().add(childIdx, check);
} else {
ConstraintConnectiveDescr localAnd = new ConstraintConnectiveDescr(ConnectiveType.AND);
BaseDescr original = ccd.getDescrs().remove(parentIdx);
localAnd.getDescrs().add(check);
localAnd.getDescrs().add(original);
ccd.getDescrs().add(parentIdx, localAnd);
}
}
atomicExpr.setRewrittenExpression(castedExpression);
String innerCheck = check.toString() + " && ";
return new String[] { innerCheck, castedExpression };
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class PackageDescrResourceVisitor method visit.
protected void visit(final AccumulateDescr descr) {
checkResource(descr);
visit(descr.getInputPattern());
for (BaseDescr d : descr.getDescrs()) {
visit(d);
}
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class PackageDescrResourceVisitor method visit.
protected void visit(final PatternDescr descr) {
checkResource(descr);
visit(descr.getConstraint());
for (BaseDescr behaDescr : descr.getBehaviors()) {
visit(behaDescr);
}
}
use of org.drools.compiler.lang.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();
collectDescrInputPattern.accept(parentVisitor);
final AccumulateDescr accumulateDescr = new AccumulateDescr();
accumulateDescr.setInputPattern(collectDescrInputPattern);
accumulateDescr.addFunction("collectList", null, false, new String[] { collectDescrInputPattern.getIdentifier() });
final PatternDescr transformedPatternDescr = new PatternDescr(pattern.getObjectType());
for (BaseDescr o : pattern.getConstraint().getDescrs()) {
transformedPatternDescr.addConstraint(o);
}
transformedPatternDescr.setSource(accumulateDescr);
transformedPatternDescr.accept(parentVisitor);
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class AccumulateVisitor method visit.
public void visit(AccumulateDescr descr, PatternDescr basePattern) {
final MethodCallExpr accumulateDSL = new MethodCallExpr(null, "accumulate");
context.addExpression(accumulateDSL);
final MethodCallExpr accumulateExprs = new MethodCallExpr(null, "and");
accumulateDSL.addArgument(accumulateExprs);
context.pushExprPointer(accumulateExprs::addArgument);
BaseDescr input = descr.getInputPattern() == null ? descr.getInput() : descr.getInputPattern();
boolean inputPatternHasConstraints = (input instanceof PatternDescr) && (!((PatternDescr) input).getConstraint().getDescrs().isEmpty());
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()) {
for (AccumulateDescr.AccumulateFunctionCallDescr function : descr.getFunctions()) {
final Optional<NewBinding> optNewBinding = visit(context, function, accumulateDSL, basePattern, inputPatternHasConstraints);
processNewBinding(optNewBinding);
}
} else if (descr.getFunctions().isEmpty() && descr.getInitCode() != null) {
// LEGACY: Accumulate with inline custom code
if (input instanceof PatternDescr) {
visitAccInlineCustomCode(context, descr, accumulateDSL, basePattern, (PatternDescr) input);
} else {
throw new UnsupportedOperationException("I was expecting input to be of type PatternDescr. " + input);
}
} else {
throw new UnsupportedOperationException("Unknown type of Accumulate.");
}
context.popExprPointer();
postVisit();
}
Aggregations