Search in sources :

Example 1 with DataProvider

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);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) PropagationContext(org.drools.core.spi.PropagationContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakJoinNode.updateChildLeftTuple(org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) ContextEntry(org.drools.core.rule.ContextEntry) DataProvider(org.drools.core.spi.DataProvider) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) FastIterator(org.drools.core.util.FastIterator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with DataProvider

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;
}
Also used : QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) MultiAccumulate(org.drools.core.rule.MultiAccumulate) EntryPoint(org.drools.model.EntryPoint) From(org.drools.model.From) RuleConditionElement(org.drools.core.rule.RuleConditionElement) WindowReference(org.drools.model.WindowReference) UnitData(org.drools.model.UnitData) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) LambdaDataProvider(org.drools.modelcompiler.constraints.LambdaDataProvider) DataProvider(org.drools.core.spi.DataProvider) EntryPointId(org.drools.core.rule.EntryPointId) LambdaDataProvider(org.drools.modelcompiler.constraints.LambdaDataProvider) Declaration(org.drools.core.rule.Declaration) WindowDeclaration(org.drools.core.rule.WindowDeclaration) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 3 with DataProvider

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);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) PropagationContext(org.drools.core.spi.PropagationContext) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakJoinNode.updateChildLeftTuple(org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) ContextEntry(org.drools.core.rule.ContextEntry) DataProvider(org.drools.core.spi.DataProvider) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint)

Example 4 with DataProvider

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();
    }
}
Also used : Constraint(org.drools.core.spi.Constraint) Constraint(org.drools.core.spi.Constraint) DataProvider(org.drools.core.spi.DataProvider) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader)

Aggregations

DataProvider (org.drools.core.spi.DataProvider)4 BetaConstraints (org.drools.core.common.BetaConstraints)2 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)2 BetaMemory (org.drools.core.reteoo.BetaMemory)2 LeftTuple (org.drools.core.reteoo.LeftTuple)2 RightTuple (org.drools.core.reteoo.RightTuple)2 ContextEntry (org.drools.core.rule.ContextEntry)2 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)2 PropagationContext (org.drools.core.spi.PropagationContext)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ArrayElementReader (org.drools.core.base.extractors.ArrayElementReader)1 SelfReferenceClassFieldReader (org.drools.core.base.extractors.SelfReferenceClassFieldReader)1 Accumulate (org.drools.core.rule.Accumulate)1 Declaration (org.drools.core.rule.Declaration)1 EntryPointId (org.drools.core.rule.EntryPointId)1 MultiAccumulate (org.drools.core.rule.MultiAccumulate)1 Pattern (org.drools.core.rule.Pattern)1 RuleConditionElement (org.drools.core.rule.RuleConditionElement)1