Search in sources :

Example 6 with TerminalNode

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

the class ReteooRuleBuilder method addRule.

/**
 * Creates the corresponting Rete network for the given <code>Rule</code> and adds it to
 * the given rule base.
 *
 * @param rule
 *            The rule to add.
 * @param kBase
 *            The rulebase to add the rule to.
 *
 * @return a List<BaseNode> of terminal nodes for the rule
 * @throws InvalidPatternException
 */
public List<TerminalNode> addRule(final RuleImpl rule, final InternalKnowledgeBase kBase) throws InvalidPatternException {
    // the list of terminal nodes
    final List<TerminalNode> nodes = new ArrayList<TerminalNode>();
    // transform rule and gets the array of subrules
    final GroupElement[] subrules = rule.getTransformedLhs(kBase.getConfiguration().getComponentFactory().getLogicTransformerFactory().getLogicTransformer(), kBase.getGlobals());
    for (int i = 0; i < subrules.length; i++) {
        // creates a clean build context for each subrule
        final BuildContext context = new BuildContext(kBase);
        context.setRule(rule);
        // if running in STREAM mode, calculate temporal distance for events
        if (EventProcessingOption.STREAM.equals(kBase.getConfiguration().getEventProcessingMode())) {
            TemporalDependencyMatrix temporal = this.utils.calculateTemporalDistance(subrules[i]);
            context.setTemporalDistance(temporal);
        }
        if (kBase.getConfiguration().isSequential()) {
            context.setTupleMemoryEnabled(false);
            context.setObjectTypeNodeMemoryEnabled(false);
        } else {
            context.setTupleMemoryEnabled(true);
            context.setObjectTypeNodeMemoryEnabled(true);
        }
        // adds subrule
        final TerminalNode node = this.addSubRule(context, subrules[i], i, rule);
        // adds the terminal node to the list of terminal nodes
        nodes.add(node);
    }
    return nodes;
}
Also used : TemporalDependencyMatrix(org.drools.core.time.TemporalDependencyMatrix) GroupElement(org.drools.core.rule.GroupElement) ArrayList(java.util.ArrayList) TerminalNode(org.drools.core.reteoo.TerminalNode) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint)

Example 7 with TerminalNode

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

the class ActivationIterator method next.

public Object next() {
    Activation acc = null;
    if (this.currentTuple != null) {
        Object obj = currentTuple.getContextObject();
        acc = obj == Boolean.TRUE ? null : (Activation) obj;
        currentTuple = leftTupleIter.next();
        while (currentTuple == null && (node = (TerminalNode) nodeIter.next()) != null) {
            if (!(node instanceof RuleTerminalNode)) {
                continue;
            }
            leftTupleIter = LeftTupleIterator.iterator(wm, node);
            this.currentTuple = leftTupleIter.next();
        }
    }
    return acc;
}
Also used : Activation(org.drools.core.spi.Activation) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) TerminalNode(org.drools.core.reteoo.TerminalNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Example 8 with TerminalNode

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

Aggregations

TerminalNode (org.drools.core.reteoo.TerminalNode)8 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)3 ArrayList (java.util.ArrayList)2 ActivationListenerFactory (org.drools.core.ActivationListenerFactory)2 LeftTuple (org.drools.core.reteoo.LeftTuple)2 PathMemory (org.drools.core.reteoo.PathMemory)2 SegmentMemory (org.drools.core.reteoo.SegmentMemory)2 GroupElement (org.drools.core.rule.GroupElement)2 BaseNode (org.drools.core.common.BaseNode)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 Memory (org.drools.core.common.Memory)1 NetworkNode (org.drools.core.common.NetworkNode)1 TerminalNodeIterator (org.drools.core.common.TerminalNodeIterator)1 TupleSetsImpl (org.drools.core.common.TupleSetsImpl)1 UpdateContext (org.drools.core.common.UpdateContext)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 AbstractTerminalNode (org.drools.core.reteoo.AbstractTerminalNode)1 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)1 BetaMemory (org.drools.core.reteoo.BetaMemory)1