Search in sources :

Example 21 with EntryPointNode

use of org.drools.core.reteoo.EntryPointNode in project drools by kiegroup.

the class BaseNode method getSinks.

public Sink[] getSinks() {
    Sink[] sinks = null;
    if (this instanceof EntryPointNode) {
        EntryPointNode source = (EntryPointNode) this;
        Collection<ObjectTypeNode> otns = source.getObjectTypeNodes().values();
        sinks = otns.toArray(new Sink[otns.size()]);
    } else if (this instanceof ObjectSource) {
        ObjectSource source = (ObjectSource) this;
        sinks = source.getObjectSinkPropagator().getSinks();
    } else if (this instanceof LeftTupleSource) {
        LeftTupleSource source = (LeftTupleSource) this;
        sinks = source.getSinkPropagator().getSinks();
    }
    return sinks;
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) Sink(org.drools.core.reteoo.Sink) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ObjectSource(org.drools.core.reteoo.ObjectSource)

Example 22 with EntryPointNode

use of org.drools.core.reteoo.EntryPointNode 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)

Example 23 with EntryPointNode

use of org.drools.core.reteoo.EntryPointNode in project drools by kiegroup.

the class EntryPointBuilder method build.

/* (non-Javadoc)
     * @see org.kie.reteoo.builder.ReteooComponentBuilder#build(org.kie.reteoo.builder.BuildContext, org.kie.reteoo.builder.BuildUtils, org.kie.rule.RuleConditionElement)
     */
public void build(BuildContext context, BuildUtils utils, RuleConditionElement rce) {
    final EntryPointId entry = (EntryPointId) rce;
    context.setCurrentEntryPoint(entry);
    EntryPointNode epn = context.getKnowledgeBase().getRete().getEntryPointNode(entry);
    if (epn == null) {
        NodeFactory nFactory = context.getComponentFactory().getNodeFactoryService();
        context.setObjectSource(utils.attachNode(context, nFactory.buildEntryPointNode(context.getNextId(), context.getKnowledgeBase().getRete(), context)));
    } else {
        context.setObjectSource(epn);
    }
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) EntryPointId(org.drools.core.rule.EntryPointId)

Aggregations

EntryPointNode (org.drools.core.reteoo.EntryPointNode)23 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)16 ClassObjectType (org.drools.core.base.ClassObjectType)8 Test (org.junit.Test)8 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 KieSession (org.kie.api.runtime.KieSession)6 KieHelper (org.kie.internal.utils.KieHelper)6 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)5 ObjectSource (org.drools.core.reteoo.ObjectSource)5 KieBase (org.kie.api.KieBase)5 Rete (org.drools.core.reteoo.Rete)4 ArrayList (java.util.ArrayList)3 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)3 LeftTupleSource (org.drools.core.reteoo.LeftTupleSource)3 NodeFactory (org.drools.core.reteoo.builder.NodeFactory)3 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)2 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2 InternalWorkingMemoryEntryPoint (org.drools.core.common.InternalWorkingMemoryEntryPoint)2 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)2 RuleBasePartitionId (org.drools.core.common.RuleBasePartitionId)2