Search in sources :

Example 6 with DroolsQuery

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

the class StatefulKnowledgeSessionImpl method openLiveQuery.

public LiveQuery openLiveQuery(final String query, final Object[] arguments, final ViewChangedEventListener listener) {
    try {
        startOperation();
        this.lock.lock();
        this.kBase.executeQueuedActions();
        agenda.executeFlush();
        DroolsQuery queryObject = new DroolsQuery(query, arguments, new OpenQueryViewChangedEventListenerAdapter(listener), true, null, null, null, null, null);
        InternalFactHandle handle = this.handleFactory.newFactHandle(queryObject, null, this, this);
        final PropagationContext pCtx = pctxFactory.createPropagationContext(getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, null, null, handle, getEntryPoint());
        evalQuery(queryObject.getName(), queryObject, handle, pCtx, false);
        return new LiveQueryImpl(this, handle);
    } finally {
        this.lock.unlock();
        endOperation();
    }
}
Also used : OpenQueryViewChangedEventListenerAdapter(org.drools.core.runtime.rule.impl.OpenQueryViewChangedEventListenerAdapter) PropagationContext(org.drools.core.spi.PropagationContext) InternalFactHandle(org.drools.core.common.InternalFactHandle) LiveQueryImpl(org.drools.core.runtime.rule.impl.LiveQueryImpl) DroolsQuery(org.drools.core.base.DroolsQuery)

Example 7 with DroolsQuery

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

the class StatefulKnowledgeSessionImpl method internalGetQueryResult.

protected QueryResultsImpl internalGetQueryResult(boolean calledFromRHS, String queryName, Object... arguments) {
    try {
        startOperation();
        this.lock.lock();
        this.kBase.executeQueuedActions();
        // eventually enqueued by events that have been inserted when already expired
        if (calledFromRHS) {
            flushPropagations();
            flushPropagations();
        } else {
            agenda.executeFlush();
            agenda.executeFlush();
        }
        DroolsQuery queryObject = new DroolsQuery(queryName, arguments, getQueryListenerInstance(), false, null, null, null, null, null);
        InternalFactHandle handle = this.handleFactory.newFactHandle(queryObject, null, this, this);
        final PropagationContext pCtx = pctxFactory.createPropagationContext(getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, null, null, handle, getEntryPoint());
        BaseNode[] tnodes = evalQuery(queryName, queryObject, handle, pCtx, calledFromRHS);
        List<Map<String, Declaration>> decls = new ArrayList<Map<String, Declaration>>();
        if (tnodes != null) {
            for (BaseNode node : tnodes) {
                decls.add(((QueryTerminalNode) node).getSubRule().getOuterDeclarations());
            }
        }
        this.handleFactory.destroyFactHandle(handle);
        return new QueryResultsImpl((List<QueryRowWithSubruleIndex>) queryObject.getQueryResultCollector().getResults(), decls.toArray(new Map[decls.size()]), this, (queryObject.getQuery() != null) ? queryObject.getQuery().getParameters() : new Declaration[0]);
    } finally {
        this.lock.unlock();
        endOperation();
    }
}
Also used : QueryRowWithSubruleIndex(org.drools.core.base.QueryRowWithSubruleIndex) QueryResultsImpl(org.drools.core.QueryResultsImpl) PropagationContext(org.drools.core.spi.PropagationContext) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) QueryTerminalNode(org.drools.core.reteoo.QueryTerminalNode) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DroolsQuery(org.drools.core.base.DroolsQuery)

Example 8 with DroolsQuery

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

the class UnificationConstraint method evaluateUnification.

private boolean evaluateUnification(InternalFactHandle handle, Tuple tuple, InternalWorkingMemory workingMemory) {
    if (!unification) {
        return evaluator.evaluate(handle, tuple, workingMemory);
    }
    DroolsQuery query = (DroolsQuery) tuple.getObject(0);
    if (query.getVariables()[declaration.getExtractor().getIndex()] != null) {
        return true;
    }
    if (evaluator != null) {
        return evaluator.evaluate(handle, tuple, workingMemory);
    }
    Object argument = declaration.getValue(null, query);
    return handle.getObject().equals(argument);
}
Also used : DroolsQuery(org.drools.core.base.DroolsQuery)

Example 9 with DroolsQuery

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

the class PhreakQueryNode method doLeftDeletes.

public void doLeftDeletes(QueryElementNodeMemory qmem, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    for (LeftTuple leftTuple = srcLeftTuples.getDeleteFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        InternalFactHandle fh = (InternalFactHandle) leftTuple.getContextObject();
        DroolsQuery dquery = (DroolsQuery) fh.getObject();
        if (dquery.isOpen()) {
            LeftInputAdapterNode lian = (LeftInputAdapterNode) qmem.getQuerySegmentMemory().getRootNode();
            LiaNodeMemory lm = (LiaNodeMemory) qmem.getQuerySegmentMemory().getNodeMemories().get(0);
            // there is only one, all other LTs are peers
            LeftTuple childLeftTuple = fh.getFirstLeftTuple();
            LeftInputAdapterNode.doDeleteObject(childLeftTuple, childLeftTuple.getPropagationContext(), qmem.getQuerySegmentMemory(), wm, lian, false, lm);
        } else {
            LeftTuple childLeftTuple = leftTuple.getFirstChild();
            while (childLeftTuple != null) {
                LeftTuple nextChild = childLeftTuple.getHandleNext();
                RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                childLeftTuple = nextChild;
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : 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 10 with DroolsQuery

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

the class PhreakQueryTerminalNode method doLeftUpdates.

public void doLeftUpdates(QueryTerminalNode qtnNode, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, LinkedList<StackEntry> stack) {
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        PropagationContext pCtx = RuleTerminalNode.findMostRecentPropagationContext(leftTuple, leftTuple.getPropagationContext());
        // qtnNode.modifyLeftTuple( leftTuple, leftTuple.getPropagationContext(), wm );
        LeftTuple rootEntry = leftTuple;
        // find the DroolsQuery object
        while (rootEntry.getParent() != null) {
            rootEntry = rootEntry.getParent();
        }
        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().rowUpdated(qtnNode.getQuery(), leftTuple, pCtx, agenda.getWorkingMemory());
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) LeftTuple(org.drools.core.reteoo.LeftTuple) DroolsQuery(org.drools.core.base.DroolsQuery)

Aggregations

DroolsQuery (org.drools.core.base.DroolsQuery)12 LeftTuple (org.drools.core.reteoo.LeftTuple)6 PropagationContext (org.drools.core.spi.PropagationContext)6 InternalFactHandle (org.drools.core.common.InternalFactHandle)5 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)3 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)3 Declaration (org.drools.core.rule.Declaration)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 QueryResultsImpl (org.drools.core.QueryResultsImpl)1 QueryRowWithSubruleIndex (org.drools.core.base.QueryRowWithSubruleIndex)1 BaseNode (org.drools.core.common.BaseNode)1 QueryTerminalNode (org.drools.core.reteoo.QueryTerminalNode)1 SegmentMemory (org.drools.core.reteoo.SegmentMemory)1 Pattern (org.drools.core.rule.Pattern)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 WindowDeclaration (org.drools.core.rule.WindowDeclaration)1