Search in sources :

Example 1 with DroolsQuery

use of org.drools.core.base.DroolsQuery in project drools by kiegroup.

the class PhreakQueryNode method doLeftUpdates.

public void doLeftUpdates(QueryElementNode queryNode, QueryElementNodeMemory qmem, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples) {
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        InternalFactHandle fh = (InternalFactHandle) leftTuple.getContextObject();
        DroolsQuery dquery = (DroolsQuery) fh.getObject();
        dquery.setParameters(queryNode.getActualArguments(leftTuple, wm));
        SegmentMemory qsmem = qmem.getQuerySegmentMemory();
        LeftInputAdapterNode lian = (LeftInputAdapterNode) qsmem.getRootNode();
        LiaNodeMemory lmem = (LiaNodeMemory) qsmem.getNodeMemories().getFirst();
        if (dquery.isOpen()) {
            // there is only one, all other LTs are peers
            LeftTuple childLeftTuple = fh.getFirstLeftTuple();
            LeftInputAdapterNode.doUpdateObject(childLeftTuple, childLeftTuple.getPropagationContext(), wm, lian, false, lmem, qmem.getQuerySegmentMemory());
        } else {
            if (fh.getFirstLeftTuple() != null) {
                // @TODO remove later (mdp)
                throw new RuntimeException("defensive programming while testing");
            }
            LiaNodeMemory lm = (LiaNodeMemory) qmem.getQuerySegmentMemory().getNodeMemories().get(0);
            LeftInputAdapterNode.doInsertObject(fh, leftTuple.getPropagationContext(), lian, wm, lm, false, dquery.isOpen());
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) DroolsQuery(org.drools.core.base.DroolsQuery) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 2 with DroolsQuery

use of org.drools.core.base.DroolsQuery in project drools by kiegroup.

the class PhreakQueryNode method doLeftInserts.

public void doLeftInserts(QueryElementNode queryNode, QueryElementNodeMemory qmem, StackEntry stackEntry, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples) {
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        PropagationContext pCtx = leftTuple.getPropagationContext();
        InternalFactHandle handle = queryNode.createFactHandle(pCtx, wm, leftTuple);
        DroolsQuery dquery = queryNode.createDroolsQuery(leftTuple, handle, stackEntry, qmem.getSegmentMemory().getPathMemories(), qmem, stackEntry.getSink(), wm);
        LeftInputAdapterNode lian = (LeftInputAdapterNode) qmem.getQuerySegmentMemory().getRootNode();
        LiaNodeMemory lm = (LiaNodeMemory) qmem.getQuerySegmentMemory().getNodeMemories().get(0);
        LeftInputAdapterNode.doInsertObject(handle, pCtx, lian, wm, lm, false, dquery.isOpen());
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) DroolsQuery(org.drools.core.base.DroolsQuery) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 3 with DroolsQuery

use of org.drools.core.base.DroolsQuery in project drools by kiegroup.

the class PhreakQueryTerminalNode method doLeftInserts.

public void doLeftInserts(QueryTerminalNode qtnNode, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, LinkedList<StackEntry> stack) {
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        // qtnNode.assertLeftTuple( leftTuple, leftTuple.getPropagationContext(), wm );
        PropagationContext pCtx = RuleTerminalNode.findMostRecentPropagationContext(leftTuple, leftTuple.getPropagationContext());
        // find the DroolsQuery object
        Tuple rootEntry = leftTuple.getRootTuple();
        DroolsQuery dquery = (DroolsQuery) rootEntry.getFactHandle().getObject();
        dquery.setQuery(qtnNode.getQuery());
        if (dquery.getStackEntry() != null) {
            checkAndTriggerQueryReevaluation(agenda, stack, rootEntry, dquery);
        }
        // Add results to the adapter
        dquery.getQueryResultCollector().rowAdded(qtnNode.getQuery(), leftTuple, pCtx, agenda.getWorkingMemory());
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) LeftTuple(org.drools.core.reteoo.LeftTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) Tuple(org.drools.core.spi.Tuple) DroolsQuery(org.drools.core.base.DroolsQuery)

Example 4 with DroolsQuery

use of org.drools.core.base.DroolsQuery in project drools by kiegroup.

the class MvelConstraint method isAllowedCachedRight.

public boolean isAllowedCachedRight(Tuple tuple, ContextEntry context) {
    if (isUnification) {
        DroolsQuery query = (DroolsQuery) tuple.get(0).getObject();
        Variable v = query.getVariables()[((UnificationContextEntry) context).getReader().getIndex()];
        if (v != null) {
            return true;
        }
        context = ((UnificationContextEntry) context).getContextEntry();
    }
    MvelContextEntry mvelContextEntry = (MvelContextEntry) context;
    return evaluate(mvelContextEntry.rightHandle, mvelContextEntry.workingMemory, tuple);
}
Also used : Variable(org.kie.api.runtime.rule.Variable) DroolsQuery(org.drools.core.base.DroolsQuery)

Example 5 with DroolsQuery

use of org.drools.core.base.DroolsQuery in project drools by kiegroup.

the class KiePackagesBuilder method addQueryPattern.

private void addQueryPattern(Query query, QueryImpl queryImpl, RuleContext ctx) {
    Pattern pattern = new Pattern(ctx.getNextPatternIndex(), // tupleIndex is 0 by default
    0, // patternIndex is 0 by default
    0, ClassObjectType.DroolsQuery_ObjectType, null);
    InternalReadAccessor extractor = new LambdaReadAccessor(DroolsQuery.class, q -> ((DroolsQuery) q).getName());
    QueryNameConstraint constraint = new QueryNameConstraint(extractor, query.getName());
    pattern.addConstraint(constraint);
    queryImpl.getLhs().addChild(pattern);
    Variable<?>[] args = query.getArguments();
    Declaration[] declarations = new Declaration[args.length];
    for (int i = 0; i < args.length; i++) {
        int index = i;
        LambdaReadAccessor accessor = new LambdaReadAccessor(index, args[index].getType(), obj -> ((DroolsQuery) obj).getElements()[index]);
        declarations[i] = new Declaration(args[i].getName(), accessor, pattern, false);
        pattern.addDeclaration(declarations[i]);
        ctx.addQueryDeclaration(args[i], declarations[i]);
    }
    queryImpl.setParameters(declarations);
}
Also used : QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) GroupByPattern(org.drools.model.GroupByPattern) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) 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) LambdaReadAccessor(org.drools.modelcompiler.constraints.LambdaReadAccessor) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) EntryPoint(org.drools.model.EntryPoint) AbstractConstraint(org.drools.modelcompiler.constraints.AbstractConstraint) Constraint(org.drools.model.Constraint) CombinedConstraint(org.drools.modelcompiler.constraints.CombinedConstraint) AbstractSingleConstraint(org.drools.model.constraints.AbstractSingleConstraint) DSL.entryPoint(org.drools.model.DSL.entryPoint) DroolsQuery(org.drools.core.base.DroolsQuery)

Aggregations

DroolsQuery (org.drools.core.base.DroolsQuery)24 LeftTuple (org.drools.core.reteoo.LeftTuple)12 InternalFactHandle (org.drools.core.common.InternalFactHandle)11 PropagationContext (org.drools.core.spi.PropagationContext)10 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)6 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)6 Declaration (org.drools.core.rule.Declaration)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 QueryResultsImpl (org.drools.core.QueryResultsImpl)3 QueryRowWithSubruleIndex (org.drools.core.base.QueryRowWithSubruleIndex)3 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 QueryTerminalNode (org.drools.core.reteoo.QueryTerminalNode)2 SegmentMemory (org.drools.core.reteoo.SegmentMemory)2 TerminalNode (org.drools.core.reteoo.TerminalNode)2 LiveQueryImpl (org.drools.core.runtime.rule.impl.LiveQueryImpl)2 OpenQueryViewChangedEventListenerAdapter (org.drools.core.runtime.rule.impl.OpenQueryViewChangedEventListenerAdapter)2 Tuple (org.drools.core.spi.Tuple)2 TreeMap (java.util.TreeMap)1