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;
}
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;
}
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;
}
Aggregations