Search in sources :

Example 11 with BaseNode

use of org.drools.core.common.BaseNode 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 12 with BaseNode

use of org.drools.core.common.BaseNode in project drools by kiegroup.

the class ReteooRuleBuilder method addSubRule.

private TerminalNode addSubRule(final BuildContext context, final GroupElement subrule, final int subruleIndex, final RuleImpl rule) throws InvalidPatternException {
    context.setSubRule(subrule);
    // gets the appropriate builder
    ReteooComponentBuilder builder = this.utils.getBuilderFor(subrule);
    // checks if an initial-fact is needed
    if (builder.requiresLeftActivation(this.utils, subrule)) {
        this.addInitialFactPattern(subrule);
    }
    // builds and attach
    builder.build(context, this.utils, subrule);
    if (context.isTerminated()) {
        context.setTerminated(false);
        return ((TerminalNode) context.getLastNode());
    }
    if (rule.getTimer() != null) {
        builder = this.utils.getBuilderFor(Timer.class);
        builder.build(context, this.utils, rule.getTimer());
    }
    ActivationListenerFactory factory = context.getKnowledgeBase().getConfiguration().getActivationListenerFactory(rule.getActivationListener());
    TerminalNode terminal = factory.createActivationListener(context.getNextId(), context.getTupleSource(), rule, subrule, subruleIndex, context);
    BaseNode baseTerminalNode = (BaseNode) terminal;
    baseTerminalNode.networkUpdated(new UpdateContext());
    baseTerminalNode.attach(context);
    setPathEndNodes(context);
    AddRemoveRule.addRule(terminal, context.getWorkingMemories(), context.getKnowledgeBase());
    // adds the terminal node to the list of nodes created/added by this sub-rule
    context.getNodes().add(baseTerminalNode);
    return terminal;
}
Also used : Timer(org.drools.core.time.impl.Timer) BaseNode(org.drools.core.common.BaseNode) UpdateContext(org.drools.core.common.UpdateContext) ActivationListenerFactory(org.drools.core.ActivationListenerFactory) TerminalNode(org.drools.core.reteoo.TerminalNode)

Example 13 with BaseNode

use of org.drools.core.common.BaseNode in project drools by kiegroup.

the class ReteooBuilder method resetMasks.

private void resetMasks(Collection<BaseNode> nodes) {
    NodeSet leafSet = new NodeSet();
    for (BaseNode node : nodes) {
        if (node.getType() == NodeTypeEnums.AlphaNode) {
            ObjectSource source = (AlphaNode) node;
            while (true) {
                source.resetInferredMask();
                BaseNode parent = source.getParentObjectSource();
                if (parent.getType() != NodeTypeEnums.AlphaNode) {
                    break;
                }
                source = (ObjectSource) parent;
            }
            updateLeafSet(source, leafSet);
        } else if (NodeTypeEnums.isBetaNode(node)) {
            BetaNode betaNode = (BetaNode) node;
            if (betaNode.isInUse()) {
                leafSet.add(betaNode);
            }
        } else if (NodeTypeEnums.isTerminalNode(node)) {
            RuleTerminalNode rtNode = (RuleTerminalNode) node;
            if (rtNode.isInUse()) {
                leafSet.add(rtNode);
            }
        }
    }
    for (BaseNode node : leafSet) {
        if (NodeTypeEnums.isTerminalNode(node)) {
            ((TerminalNode) node).initInferredMask();
        } else {
            // else node instanceof BetaNode
            ((BetaNode) node).initInferredMask();
        }
    }
}
Also used : BaseNode(org.drools.core.common.BaseNode)

Example 14 with BaseNode

use of org.drools.core.common.BaseNode in project drools by kiegroup.

the class BuildUtils method attachNode.

/**
 * Attaches a node into the network. If a node already exists that could
 * substitute, it is used instead.
 *
 * @param context
 *            The current build context
 * @param candidate
 *            The node to attach.
 *
 * @return the actual attached node that may be the one given as parameter
 *         or eventually one that was already in the cache if sharing is enabled
 */
public <T extends BaseNode> T attachNode(BuildContext context, T candidate) {
    BaseNode node = null;
    RuleBasePartitionId partition = null;
    if (candidate.getType() == NodeTypeEnums.EntryPointNode) {
        // entry point nodes are always shared
        node = context.getKnowledgeBase().getRete().getEntryPointNode(((EntryPointNode) candidate).getEntryPoint());
        // all EntryPointNodes belong to the main partition
        partition = RuleBasePartitionId.MAIN_PARTITION;
    } else if (candidate.getType() == NodeTypeEnums.ObjectTypeNode) {
        // object type nodes are always shared
        Map<ObjectType, ObjectTypeNode> map = context.getKnowledgeBase().getRete().getObjectTypeNodes(context.getCurrentEntryPoint());
        if (map != null) {
            ObjectTypeNode otn = map.get(((ObjectTypeNode) candidate).getObjectType());
            if (otn != null) {
                // adjusting expiration offset
                otn.mergeExpirationOffset((ObjectTypeNode) candidate);
                node = otn;
            }
        }
        // all ObjectTypeNodes belong to the main partition
        partition = RuleBasePartitionId.MAIN_PARTITION;
    } else if (isSharingEnabledForNode(context, candidate)) {
        if ((context.getTupleSource() != null) && NodeTypeEnums.isLeftTupleSink(candidate)) {
            node = context.getTupleSource().getSinkPropagator().getMatchingNode(candidate);
        } else if ((context.getObjectSource() != null) && NodeTypeEnums.isObjectSink(candidate)) {
            node = context.getObjectSource().getObjectSinkPropagator().getMatchingNode(candidate);
        } else {
            throw new RuntimeException("This is a bug on node sharing verification. Please report to development team.");
        }
    }
    if (node != null && !areNodesCompatibleForSharing(context, node, candidate)) {
        node = null;
    }
    if (node == null) {
        // only attach() if it is a new node
        node = candidate;
        // new node, so it must be labeled
        if (partition == null) {
            // if it does not has a predefined label
            if (context.getPartitionId() == null) {
                // if no label in current context, create one
                context.setPartitionId(context.getKnowledgeBase().createNewPartitionId());
            }
            partition = context.getPartitionId();
        }
        // set node whit the actual partition label
        node.setPartitionId(context, partition);
        node.attach(context);
        // adds the node to the context list to track all added nodes
        context.getNodes().add(node);
    } else {
        // shared node found
        mergeNodes(node, candidate);
        // undo previous id assignment
        context.releaseId(candidate);
        if (partition == null && context.getPartitionId() == null) {
            partition = node.getPartitionId();
            // if no label in current context, create one
            context.setPartitionId(partition);
        }
    }
    node.addAssociation(context, context.getRule());
    return (T) node;
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) RuleBasePartitionId(org.drools.core.common.RuleBasePartitionId) BaseNode(org.drools.core.common.BaseNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

BaseNode (org.drools.core.common.BaseNode)14 ArrayList (java.util.ArrayList)5 List (java.util.List)4 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)4 ClassObjectType (org.drools.core.base.ClassObjectType)4 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)4 Test (org.junit.Test)4 KieSession (org.kie.api.runtime.KieSession)4 KieBase (org.kie.api.KieBase)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Person (org.drools.compiler.Person)2 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2 Sink (org.drools.core.reteoo.Sink)2 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Cheese (org.drools.compiler.Cheese)1 ActivationListenerFactory (org.drools.core.ActivationListenerFactory)1 QueryResultsImpl (org.drools.core.QueryResultsImpl)1 DroolsQuery (org.drools.core.base.DroolsQuery)1