use of org.drools.core.spi.DataProvider in project drools by kiegroup.
the class PhreakFromNode method doLeftUpdates.
public void doLeftUpdates(FromNode fromNode, FromMemory fm, LeftTupleSink sink, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
BetaMemory bm = fm.getBetaMemory();
ContextEntry[] context = bm.getContext();
BetaConstraints betaConstraints = fromNode.getBetaConstraints();
AlphaNodeFieldConstraint[] alphaConstraints = fromNode.getAlphaConstraints();
DataProvider dataProvider = fromNode.getDataProvider();
Class<?> resultClass = fromNode.getResultClass();
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
PropagationContext propagationContext = leftTuple.getPropagationContext();
final Map<Object, RightTuple> previousMatches = (Map<Object, RightTuple>) leftTuple.getContextObject();
final Map<Object, RightTuple> newMatches = new HashMap<Object, RightTuple>();
leftTuple.setContextObject(newMatches);
betaConstraints.updateFromTuple(context, wm, leftTuple);
FastIterator rightIt = LinkedList.fastIterator;
for (final java.util.Iterator<?> it = dataProvider.getResults(leftTuple, wm, propagationContext, fm.providerContext); it.hasNext(); ) {
final Object object = it.next();
if ((object == null) || !resultClass.isAssignableFrom(object.getClass())) {
// skip anything if it not assignable
continue;
}
RightTuple rightTuple = previousMatches.remove(object);
if (rightTuple == null) {
// new match, propagate assert
rightTuple = fromNode.createRightTuple(leftTuple, propagationContext, wm, object);
} else {
// previous match, so reevaluate and propagate modify
if (rightIt.next(rightTuple) != null) {
// handle the odd case where more than one object has the same hashcode/equals value
previousMatches.put(object, (RightTuple) rightIt.next(rightTuple));
rightTuple.setNext(null);
}
}
checkConstraintsAndPropagate(sink, leftTuple, rightTuple, alphaConstraints, betaConstraints, propagationContext, wm, fm, context, true, trgLeftTuples, stagedLeftTuples);
fromNode.addToCreatedHandlesMap(newMatches, rightTuple);
}
for (RightTuple rightTuple : previousMatches.values()) {
for (RightTuple current = rightTuple; current != null; current = (RightTuple) rightIt.next(current)) {
deleteChildLeftTuple(propagationContext, trgLeftTuples, stagedLeftTuples, current.getFirstChild());
}
}
leftTuple.clearStaged();
leftTuple = next;
}
betaConstraints.resetTuple(context);
}
use of org.drools.core.spi.DataProvider 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.spi.DataProvider in project drools by kiegroup.
the class PhreakFromNode method doLeftInserts.
public void doLeftInserts(FromNode fromNode, FromMemory fm, LeftTupleSink sink, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
BetaMemory bm = fm.getBetaMemory();
ContextEntry[] context = bm.getContext();
BetaConstraints betaConstraints = fromNode.getBetaConstraints();
AlphaNodeFieldConstraint[] alphaConstraints = fromNode.getAlphaConstraints();
DataProvider dataProvider = fromNode.getDataProvider();
Class<?> resultClass = fromNode.getResultClass();
for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
PropagationContext propagationContext = leftTuple.getPropagationContext();
Map<Object, RightTuple> matches = null;
boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(fromNode, leftTuple);
if (useLeftMemory) {
fm.getBetaMemory().getLeftTupleMemory().add(leftTuple);
matches = new LinkedHashMap<Object, RightTuple>();
leftTuple.setContextObject(matches);
}
betaConstraints.updateFromTuple(context, wm, leftTuple);
for (final java.util.Iterator<?> it = dataProvider.getResults(leftTuple, wm, propagationContext, fm.providerContext); it.hasNext(); ) {
final Object object = it.next();
if ((object == null) || !resultClass.isAssignableFrom(object.getClass())) {
// skip anything if it not assignable
continue;
}
RightTuple rightTuple = fromNode.createRightTuple(leftTuple, propagationContext, wm, object);
checkConstraintsAndPropagate(sink, leftTuple, rightTuple, alphaConstraints, betaConstraints, propagationContext, wm, fm, context, useLeftMemory, trgLeftTuples, null);
if (useLeftMemory) {
fromNode.addToCreatedHandlesMap(matches, rightTuple);
}
}
leftTuple.clearStaged();
leftTuple = next;
}
betaConstraints.resetTuple(context);
}
use of org.drools.core.spi.DataProvider in project drools by kiegroup.
the class LogicTransformer method processElement.
/**
* recurse through the rule condition elements updating the declaration objecs
*/
private void processElement(final DeclarationScopeResolver resolver, final Stack<RuleConditionElement> contextStack, final RuleConditionElement element) {
if (element instanceof Pattern) {
Pattern pattern = (Pattern) element;
for (RuleConditionElement ruleConditionElement : pattern.getNestedElements()) {
processElement(resolver, contextStack, ruleConditionElement);
}
for (Constraint constraint : pattern.getConstraints()) {
if (constraint instanceof Declaration) {
continue;
}
replaceDeclarations(resolver, pattern, constraint);
}
} else if (element instanceof EvalCondition) {
processEvalCondition(resolver, (EvalCondition) element);
} else if (element instanceof Accumulate) {
for (RuleConditionElement rce : element.getNestedElements()) {
processElement(resolver, contextStack, rce);
}
Accumulate accumulate = (Accumulate) element;
replaceDeclarations(resolver, accumulate);
} else if (element instanceof From) {
DataProvider provider = ((From) element).getDataProvider();
Declaration[] decl = provider.getRequiredDeclarations();
for (Declaration aDecl : decl) {
Declaration resolved = resolver.getDeclaration(aDecl.getIdentifier());
if (resolved != null && resolved != aDecl) {
provider.replaceDeclaration(aDecl, resolved);
} else if (resolved == null) {
// it is probably an implicit declaration, so find the corresponding pattern
Pattern old = aDecl.getPattern();
Pattern current = resolver.findPatternByIndex(old.getIndex());
if (current != null && old != current) {
resolved = new Declaration(aDecl.getIdentifier(), aDecl.getExtractor(), current);
provider.replaceDeclaration(aDecl, resolved);
}
}
}
} else if (element instanceof QueryElement) {
QueryElement qe = (QueryElement) element;
Pattern pattern = qe.getResultPattern();
for (Entry<String, Declaration> entry : pattern.getInnerDeclarations().entrySet()) {
Declaration resolved = resolver.getDeclaration(entry.getValue().getIdentifier());
if (resolved != null && resolved != entry.getValue() && resolved.getPattern() != pattern) {
entry.setValue(resolved);
}
}
List<Integer> varIndexes = asList(qe.getVariableIndexes());
for (int i = 0; i < qe.getArguments().length; i++) {
if (!(qe.getArguments()[i] instanceof QueryArgument.Declr)) {
continue;
}
Declaration declr = ((QueryArgument.Declr) qe.getArguments()[i]).getDeclaration();
Declaration resolved = resolver.getDeclaration(declr.getIdentifier());
if (resolved != declr && resolved.getPattern() != pattern) {
qe.getArguments()[i] = new QueryArgument.Declr(resolved);
}
if (ClassObjectType.DroolsQuery_ObjectType.isAssignableFrom(resolved.getPattern().getObjectType())) {
// if the resolved still points to DroolsQuery, we know this is the first unification pattern, so redeclare it as the visible Declaration
declr = pattern.addDeclaration(declr.getIdentifier());
// this bit is different, notice its the ArrayElementReader that we wire up to, not the declaration.
ArrayElementReader reader = new ArrayElementReader(new SelfReferenceClassFieldReader(Object[].class), i, resolved.getDeclarationClass());
declr.setReadAccessor(reader);
varIndexes.add(i);
}
}
qe.setVariableIndexes(toIntArray(varIndexes));
} else if (element instanceof ConditionalBranch) {
processBranch(resolver, (ConditionalBranch) element);
} else {
contextStack.push(element);
for (RuleConditionElement ruleConditionElement : element.getNestedElements()) {
processElement(resolver, contextStack, ruleConditionElement);
}
contextStack.pop();
}
}
Aggregations