use of org.drools.core.rule.Accumulate in project drools by kiegroup.
the class CollectBuilder method build.
/**
* @inheritDoc
*/
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
boolean existSubNetwort = false;
final Collect collect = (Collect) rce;
context.pushRuleComponent(collect);
final List<BetaNodeFieldConstraint> resultBetaConstraints = context.getBetaconstraints();
final List<AlphaNodeFieldConstraint> resultAlphaConstraints = context.getAlphaConstraints();
final Pattern sourcePattern = collect.getSourcePattern();
// get builder for the pattern
final ReteooComponentBuilder builder = utils.getBuilderFor(sourcePattern);
// save tuple source and pattern offset for later if needed
final LeftTupleSource tupleSource = context.getTupleSource();
final int currentPatternIndex = context.getCurrentPatternOffset();
// builds the source pattern
builder.build(context, utils, sourcePattern);
// if object source is null, then we need to adapt tuple source into a subnetwork
if (context.getObjectSource() == null) {
RightInputAdapterNode riaNode = context.getComponentFactory().getNodeFactoryService().buildRightInputNode(context.getNextId(), context.getTupleSource(), tupleSource, context);
// attach right input adapter node to convert tuple source into an object source
context.setObjectSource(utils.attachNode(context, riaNode));
// restore tuple source from before the start of the sub network
context.setTupleSource(tupleSource);
// create a tuple start equals constraint and set it in the context
final TupleStartEqualsConstraint constraint = TupleStartEqualsConstraint.getInstance();
final List<BetaNodeFieldConstraint> betaConstraints = new ArrayList<BetaNodeFieldConstraint>();
betaConstraints.add(constraint);
context.setBetaconstraints(betaConstraints);
existSubNetwort = true;
}
BetaConstraints binder = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), false);
// indexing for the results should be always disabled
BetaConstraints resultBinder = utils.createBetaNodeConstraint(context, resultBetaConstraints, true);
CollectAccumulator accumulator = new CollectAccumulator(collect, existSubNetwort);
Accumulate accumulate = new SingleAccumulate(sourcePattern, sourcePattern.getRequiredDeclarations(), accumulator);
AccumulateNode accNode = context.getComponentFactory().getNodeFactoryService().buildAccumulateNode(context.getNextId(), context.getTupleSource(), context.getObjectSource(), resultAlphaConstraints.toArray(new AlphaNodeFieldConstraint[resultAlphaConstraints.size()]), // source binder
binder, resultBinder, accumulate, existSubNetwort, context);
context.setTupleSource(utils.attachNode(context, accNode));
// source pattern was bound, so nulling context
context.setObjectSource(null);
context.setCurrentPatternOffset(currentPatternIndex);
context.popRuleComponent();
}
use of org.drools.core.rule.Accumulate 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.Accumulate 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.Accumulate 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.Accumulate in project drools by kiegroup.
the class PhreakAccumulateNode method doRightUpdates.
public void doRightUpdates(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples) {
BetaMemory bm = am.getBetaMemory();
TupleMemory ltm = bm.getLeftTupleMemory();
ContextEntry[] contextEntry = bm.getContext();
BetaConstraints constraints = accNode.getRawConstraints();
Accumulate accumulate = accNode.getAccumulate();
for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; ) {
RightTuple next = rightTuple.getStagedNext();
if (ltm != null && ltm.size() > 0) {
LeftTuple childLeftTuple = rightTuple.getFirstChild();
FastIterator leftIt = accNode.getLeftIterator(ltm);
LeftTuple leftTuple = accNode.getFirstLeftTuple(rightTuple, ltm, leftIt);
constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandleForEvaluation());
// We assume a bucket change if leftTuple == null
if (childLeftTuple != null && ltm.isIndexed() && !leftIt.isFullIterator() && (leftTuple == null || (leftTuple.getMemory() != childLeftTuple.getLeftParent().getMemory()))) {
// our index has changed, so delete all the previous matches
removePreviousMatchesForRightTuple(accNode, accumulate, rightTuple, wm, am, childLeftTuple, trgLeftTuples);
// null so the next check will attempt matches for new bucket
childLeftTuple = null;
}
// if LeftTupleMemory is empty, there are no matches to modify
if (leftTuple != null) {
if (leftTuple.getStagedType() == LeftTuple.NONE) {
trgLeftTuples.addUpdate(leftTuple);
}
doRightUpdatesProcessChildren(accNode, am, wm, bm, constraints, accumulate, leftIt, rightTuple, childLeftTuple, leftTuple, trgLeftTuples);
}
}
rightTuple.clearStaged();
rightTuple = next;
}
constraints.resetFactHandle(contextEntry);
}
Aggregations