use of org.drools.drl.ast.descr.AccumulateDescr in project drools by kiegroup.
the class MVELAccumulateBuilder method build.
@SuppressWarnings("unchecked")
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
boolean typesafe = context.isTypesafe();
try {
final AccumulateDescr accumDescr = (AccumulateDescr) descr;
if (!accumDescr.hasValidInput()) {
return null;
}
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(accumDescr.getInput().getClass());
// create source CE
final RuleConditionElement source = builder.build(context, accumDescr.getInput());
if (source == null) {
return null;
}
MVELDialect dialect = (MVELDialect) context.getDialect();
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
Map<String, Declaration> sourceOuterDeclr = source.getOuterDeclarations();
Map<String, Class<?>> declarationClasses = DeclarationScopeResolver.getDeclarationClasses(decls);
declarationClasses.putAll(DeclarationScopeResolver.getDeclarationClasses(sourceOuterDeclr));
BoundIdentifiers boundIds = new BoundIdentifiers(declarationClasses, context);
final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
Accumulator[] accumulators;
if (accumDescr.isExternalFunction()) {
// uses accumulate functions
accumulators = buildExternalFunctions(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple, source, declarationClasses);
} else {
// it is a custom accumulate
accumulators = buildCustomAccumulate(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple);
}
List<Declaration> requiredDeclarations = new ArrayList<Declaration>();
for (Accumulator acc : accumulators) {
MvelAccumulator mvelAcc = (MvelAccumulator) acc;
Collections.addAll(requiredDeclarations, mvelAcc.getRequiredDeclarations());
}
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
Accumulate accumulate;
if (accumDescr.isMultiFunction()) {
accumulate = new MultiAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators, accumulators.length);
int index = 0;
for (Accumulator accumulator : accumulators) {
data.addCompileable(((MultiAccumulate) accumulate).new Wirer(index++), (MVELCompileable) accumulator);
((MVELCompileable) accumulator).compile(data, context.getRule());
}
} else {
accumulate = new SingleAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators[0]);
data.addCompileable(((SingleAccumulate) accumulate).new Wirer(), (MVELCompileable) accumulators[0]);
((MVELCompileable) accumulators[0]).compile(data, context.getRule());
}
return accumulate;
} catch (Exception e) {
AsmUtil.copyErrorLocation(e, descr);
context.addError(new DescrBuildError(context.getParentDescr(), descr, e, "Unable to build expression for 'accumulate' : " + e.getMessage()));
return null;
} finally {
context.setTypesafe(typesafe);
}
}
use of org.drools.drl.ast.descr.AccumulateDescr in project drools by kiegroup.
the class RuleParserTest method testAccumulateWithNestedFrom.
@Test
public void testAccumulateWithNestedFrom() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulate_with_nested_from.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
final AccumulateDescr accumulate = (AccumulateDescr) out.getSource();
PatternDescr person = (PatternDescr) accumulate.getInputPattern();
assertEquals("Person", person.getObjectType());
final CollectDescr collect2 = (CollectDescr) person.getSource();
final PatternDescr people = collect2.getInputPattern();
assertEquals("People", people.getObjectType());
}
use of org.drools.drl.ast.descr.AccumulateDescr in project drools by kiegroup.
the class RuleParserTest method testAccumulateMultipleFunctions.
@Test
public void testAccumulateMultipleFunctions() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulateMultipleFunctions.drl");
assertEquals(1, pkg.getRules().size());
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Object", out.getObjectType());
AccumulateDescr accum = (AccumulateDescr) out.getSource();
assertTrue(accum.isExternalFunction());
List<AccumulateDescr.AccumulateFunctionCallDescr> functions = accum.getFunctions();
assertEquals(3, functions.size());
assertEquals("average", functions.get(0).getFunction());
assertEquals("$a1", functions.get(0).getBind());
assertEquals("$price", functions.get(0).getParams()[0]);
assertEquals("min", functions.get(1).getFunction());
assertEquals("$m1", functions.get(1).getBind());
assertEquals("$price", functions.get(1).getParams()[0]);
assertEquals("max", functions.get(2).getFunction());
assertEquals("$M1", functions.get(2).getBind());
assertEquals("$price", functions.get(2).getParams()[0]);
final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
assertEquals("Cheese", pattern.getObjectType());
}
use of org.drools.drl.ast.descr.AccumulateDescr in project drools by kiegroup.
the class RuleParserTest method testAccumulateExternalFunction.
@Test
public void testAccumulateExternalFunction() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulateExternalFunction.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
final AccumulateDescr accum = (AccumulateDescr) out.getSource();
assertEqualsIgnoreWhitespace("$age", accum.getFunctions().get(0).getParams()[0]);
assertEqualsIgnoreWhitespace("average", accum.getFunctions().get(0).getFunction());
assertTrue(accum.isExternalFunction());
final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
assertEquals("Person", pattern.getObjectType());
}
use of org.drools.drl.ast.descr.AccumulateDescr in project drools by kiegroup.
the class RuleParserTest method testAccumulateReverse.
@Test
public void testAccumulateReverse() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulateReverse.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
final AccumulateDescr accum = (AccumulateDescr) out.getSource();
assertEqualsIgnoreWhitespace("int x = 0 ;", accum.getInitCode());
assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
assertEqualsIgnoreWhitespace("x--;", accum.getReverseCode());
assertEqualsIgnoreWhitespace("new Integer(x)", accum.getResultCode());
assertFalse(accum.isExternalFunction());
final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
assertEquals("Person", pattern.getObjectType());
}
Aggregations