use of org.drools.drl.ast.descr.AccumulateDescr in project drools by kiegroup.
the class AccumulateHelperHandler method end.
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final Element element = parser.endElementBuilder();
final String expression = ((org.w3c.dom.Text) element.getChildNodes().item(0)).getWholeText();
final Object parent = parser.getParent();
final AccumulateDescr accumulate = (AccumulateDescr) parent;
if (localName.equals("init")) {
emptyContentCheck(localName, expression, parser);
accumulate.setInitCode(expression.trim());
} else if (localName.equals("action")) {
emptyContentCheck(localName, expression, parser);
accumulate.setActionCode(expression.trim());
} else if (localName.equals("result")) {
emptyContentCheck(localName, expression, parser);
accumulate.setResultCode(expression.trim());
} else if (localName.equals("reverse")) {
emptyContentCheck(localName, expression, parser);
accumulate.setReverseCode(expression.trim());
} else if (localName.equals("external-function")) {
accumulate.addFunction(element.getAttribute("evaluator"), // no support to bindings yet?
null, false, new String[] { element.getAttribute("expression") });
}
return null;
}
use of org.drools.drl.ast.descr.AccumulateDescr in project drools by kiegroup.
the class AccumulateHandler method start.
public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
parser.startElementBuilder(localName, attrs);
final AccumulateDescr accumulateDesrc = new AccumulateDescr();
return accumulateDesrc;
}
use of org.drools.drl.ast.descr.AccumulateDescr in project drools by kiegroup.
the class AccumulateHandler method end.
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final Element element = parser.endElementBuilder();
final AccumulateDescr accumulateDescr = (AccumulateDescr) parser.getCurrent();
final Object parent = parser.getParent();
if (parent.getClass().getName().equals(FromDescr.class.getName())) {
final PatternDescr result = (PatternDescr) parser.getParent(1);
result.setSource(accumulateDescr);
} else if (parent instanceof ConditionalElementDescr) {
final ConditionalElementDescr parentDescr = (ConditionalElementDescr) parent;
parentDescr.addDescr(accumulateDescr);
}
return accumulateDescr;
}
use of org.drools.drl.ast.descr.AccumulateDescr 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.drl.ast.descr.AccumulateDescr in project drools by kiegroup.
the class JavaAccumulateBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
final AccumulateDescr accumDescr = (AccumulateDescr) descr;
if (!accumDescr.hasValidInput()) {
return null;
}
// build source
BaseDescr input = accumDescr.getInput();
if (input instanceof AndDescr && ((AndDescr) input).getDescrs().size() == 1) {
input = ((AndDescr) input).getDescrs().get(0);
}
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(input.getClass());
final RuleConditionElement source = builder.build(context, input);
if (source == null) {
return null;
}
final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
Map<String, Declaration> declsInScope = context.getDeclarationResolver().getDeclarations(context.getRule());
if (prefixPattern != null && prefixPattern.getDeclaration() != null) {
declsInScope.remove(prefixPattern.getDeclaration().getIdentifier());
}
Map<String, Class<?>> declCls = DeclarationScopeResolver.getDeclarationClasses(declsInScope);
Accumulate accumulate;
if (accumDescr.isExternalFunction()) {
// if it uses 1+ external function, build methods for them
accumulate = buildExternalFunctionCall(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
} else {
// if it uses inline code, build the class for it
accumulate = buildInlineAccumulate(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
}
return accumulate;
}
Aggregations