use of org.drools.core.rule.MultiAccumulate in project drools by kiegroup.
the class KiePackagesBuilder method addPatternForVariable.
private Pattern addPatternForVariable(RuleContext ctx, GroupElement group, Variable patternVariable) {
Pattern pattern = new Pattern(ctx.getNextPatternIndex(), // offset will be set by ReteooBuilder
0, getObjectType(patternVariable), patternVariable.getName(), true);
if (patternVariable instanceof org.drools.model.Declaration) {
org.drools.model.Declaration decl = (org.drools.model.Declaration) patternVariable;
if (decl.getSource() != null) {
if (decl.getSource() instanceof EntryPoint) {
pattern.setSource(new EntryPointId(((EntryPoint) decl.getSource()).getName()));
} else if (decl.getSource() instanceof WindowReference) {
WindowReference<?> window = (WindowReference) decl.getSource();
if (!ctx.getPkg().getWindowDeclarations().containsKey(window.getName())) {
createWindowReference(ctx, window);
}
pattern.setSource(new org.drools.core.rule.WindowReference(window.getName()));
} else if (decl.getSource() instanceof From) {
From<?> from = (From) decl.getSource();
DataProvider provider = new LambdaDataProvider(ctx.getDeclaration(from.getVariable()), from.getProvider(), from.isReactive());
org.drools.core.rule.From fromSource = new org.drools.core.rule.From(provider);
fromSource.setResultPattern(pattern);
pattern.setSource(fromSource);
} else if (decl.getSource() instanceof UnitData) {
UnitData unitData = (UnitData) decl.getSource();
pattern.setSource(new EntryPointId(ctx.getRule().getRuleUnitClassName() + "." + unitData.getName()));
} else {
throw new UnsupportedOperationException("Unknown source: " + decl.getSource());
}
} else {
Accumulate accSource = ctx.getAccumulateSource(patternVariable);
if (accSource != null) {
for (RuleConditionElement element : group.getChildren()) {
if (element instanceof Pattern && ((Pattern) element).getSource() == accSource) {
if (accSource instanceof MultiAccumulate) {
((Pattern) element).getConstraints().forEach(pattern::addConstraint);
((Pattern) element).getDeclarations().values().forEach(d -> {
pattern.addDeclaration(d);
d.setPattern(pattern);
});
}
group.getChildren().remove(element);
break;
}
}
pattern.setSource(accSource);
}
}
if (decl.getWindow() != null) {
pattern.addBehavior(createWindow(decl.getWindow()));
}
}
ctx.registerPattern(patternVariable, pattern);
return pattern;
}
use of org.drools.core.rule.MultiAccumulate in project drools by kiegroup.
the class KiePackagesBuilder method buildAccumulate.
private Accumulate buildAccumulate(RuleContext ctx, AccumulatePattern accPattern, RuleConditionElement source, Pattern pattern, List<String> usedVariableName, Binding binding) {
AccumulateFunction[] accFunctions = accPattern.getAccumulateFunctions();
Accumulate accumulate;
if (accFunctions.length == 1) {
final Class<?> functionClass = accFunctions[0].getFunctionClass();
final Accumulator accumulator = createAccumulator(usedVariableName, binding, functionClass);
final Variable boundVar = accPattern.getBoundVariables()[0];
final Declaration declaration = new Declaration(boundVar.getName(), getReadAcessor(JAVA_CLASS_OBJECT_TYPE), pattern, true);
pattern.addDeclaration(declaration);
Declaration[] bindingDeclaration = binding != null ? new Declaration[0] : new Declaration[] { ctx.getPattern(accFunctions[0].getSource()).getDeclaration() };
accumulate = new SingleAccumulate(source, bindingDeclaration, accumulator);
} else {
InternalReadAccessor reader = new SelfReferenceClassFieldReader(Object[].class);
Accumulator[] accumulators = new Accumulator[accFunctions.length];
for (int i = 0; i < accFunctions.length; i++) {
final Class<?> functionClass = accFunctions[i].getFunctionClass();
final Accumulator accumulator = createAccumulator(usedVariableName, binding, functionClass);
Variable boundVar = accPattern.getBoundVariables()[i];
pattern.addDeclaration(new Declaration(boundVar.getName(), new ArrayElementReader(reader, i, boundVar.getType()), pattern, true));
accumulators[i] = accumulator;
}
accumulate = new MultiAccumulate(source, new Declaration[0], accumulators);
}
for (Variable boundVar : accPattern.getBoundVariables()) {
ctx.addAccumulateSource(boundVar, accumulate);
}
return accumulate;
}
use of org.drools.core.rule.MultiAccumulate 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);
} 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);
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) {
DialectUtil.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.core.rule.MultiAccumulate in project drools by kiegroup.
the class KiePackagesBuilder method addConstraintsToPattern.
private void addConstraintsToPattern(RuleContext ctx, Pattern pattern, org.drools.model.Pattern modelPattern, Constraint constraint) {
if (constraint.getType() == Constraint.Type.SINGLE) {
SingleConstraint singleConstraint = (SingleConstraint) constraint;
boolean isEqual = singleConstraint.getIndex() != null && singleConstraint.getIndex().getConstraintType() == Index.ConstraintType.EQUAL;
if (singleConstraint.getVariables().length > 0) {
Variable[] vars = singleConstraint.getVariables();
Declaration[] declarations = new Declaration[vars.length];
Declaration unificationDeclaration = null;
for (int i = 0; i < vars.length; i++) {
declarations[i] = ctx.getDeclaration(vars[i]);
if (isEqual && declarations[i].getPattern().getObjectType().equals(ClassObjectType.DroolsQuery_ObjectType)) {
unificationDeclaration = declarations[i];
} else if (pattern.getSource() instanceof MultiAccumulate) {
Declaration accDeclaration = pattern.getDeclarations().get(declarations[i].getBindingName());
if (accDeclaration != null) {
declarations[i].setReadAccessor(accDeclaration.getExtractor());
}
}
}
ConstraintEvaluator constraintEvaluator = singleConstraint.isTemporal() ? new TemporalConstraintEvaluator(declarations, pattern, singleConstraint) : new ConstraintEvaluator(declarations, pattern, singleConstraint);
org.drools.core.spi.Constraint droolsConstraint = unificationDeclaration != null ? new UnificationConstraint(unificationDeclaration, constraintEvaluator) : new LambdaConstraint(constraintEvaluator);
pattern.addConstraint(droolsConstraint);
}
} else if (modelPattern.getConstraint().getType() == Constraint.Type.AND) {
for (Constraint child : constraint.getChildren()) {
addConstraintsToPattern(ctx, pattern, modelPattern, child);
}
}
}
use of org.drools.core.rule.MultiAccumulate in project drools by kiegroup.
the class JavaAccumulateBuilder method buildExternalFunctionCall.
private Accumulate buildExternalFunctionCall(RuleBuildContext context, AccumulateDescr accumDescr, RuleConditionElement source, Map<String, Declaration> declsInScope, Map<String, Class<?>> declCls, boolean readLocalsFromTuple) {
// list of functions to build
final List<AccumulateFunctionCallDescr> funcCalls = accumDescr.getFunctions();
// list of available source declarations
final Declaration[] sourceDeclArr = source.getOuterDeclarations().values().toArray(new Declaration[source.getOuterDeclarations().size()]);
Arrays.sort(sourceDeclArr, RuleTerminalNode.SortDeclarations.instance);
// set of required previous declarations
Set<Declaration> requiredDecl = new HashSet<Declaration>();
Pattern pattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
if (accumDescr.isMultiFunction()) {
// the accumulator array
Accumulator[] accumulators = new Accumulator[funcCalls.size()];
// creating the custom array reader
InternalReadAccessor reader = new SelfReferenceClassFieldReader(Object[].class);
int index = 0;
for (AccumulateFunctionCallDescr fc : funcCalls) {
AccumulateFunction function = getAccumulateFunction(context, accumDescr, fc, source, declCls);
if (function == null) {
return null;
}
bindReaderToDeclaration(context, accumDescr, pattern, fc, new ArrayElementReader(reader, index, function.getResultType()), function.getResultType(), index);
accumulators[index++] = buildAccumulator(context, accumDescr, declsInScope, declCls, readLocalsFromTuple, sourceDeclArr, requiredDecl, fc, function);
}
return new MultiAccumulate(source, requiredDecl.toArray(new Declaration[requiredDecl.size()]), accumulators);
} else {
AccumulateFunctionCallDescr fc = accumDescr.getFunctions().get(0);
AccumulateFunction function = getAccumulateFunction(context, accumDescr, fc, source, declCls);
if (function == null) {
return null;
}
Class<?> returnType = function.getResultType();
if (!pattern.isCompatibleWithAccumulateReturnType(returnType)) {
context.addError(new DescrBuildError(accumDescr, context.getRuleDescr(), null, "Pattern of type: '" + pattern.getObjectType() + "' on rule '" + context.getRuleDescr().getName() + "' is not compatible with type " + returnType.getCanonicalName() + " returned by accumulate function."));
return null;
}
bindReaderToDeclaration(context, accumDescr, pattern, fc, new SelfReferenceClassFieldReader(function.getResultType()), function.getResultType(), -1);
Accumulator accumulator = buildAccumulator(context, accumDescr, declsInScope, declCls, readLocalsFromTuple, sourceDeclArr, requiredDecl, fc, function);
return new SingleAccumulate(source, requiredDecl.toArray(new Declaration[requiredDecl.size()]), accumulator);
}
}
Aggregations