001: package org.drools.ruleflow.instance.impl;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.drools.common.RuleFlowGroupNode;
023: import org.drools.ruleflow.core.Connection;
024: import org.drools.ruleflow.core.Constraint;
025: import org.drools.ruleflow.core.Split;
026: import org.drools.ruleflow.instance.RuleFlowNodeInstance;
027: import org.drools.spi.Activation;
028: import org.drools.spi.RuleFlowGroup;
029:
030: /**
031: * Runtime counterpart of a split node.
032: *
033: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
034: */
035: public class RuleFlowSplitInstanceImpl extends RuleFlowNodeInstanceImpl
036: implements RuleFlowNodeInstance {
037:
038: protected Split getSplitNode() {
039: return (Split) getNode();
040: }
041:
042: public void trigger(final RuleFlowNodeInstance from) {
043: final Split split = getSplitNode();
044: switch (split.getType()) {
045: case Split.TYPE_AND:
046: List outgoing = split.getOutgoingConnections();
047: for (final Iterator iterator = outgoing.iterator(); iterator
048: .hasNext();) {
049: final Connection connection = (Connection) iterator
050: .next();
051: getProcessInstance()
052: .getNodeInstance(connection.getTo()).trigger(
053: this );
054: }
055: break;
056: case Split.TYPE_XOR:
057: outgoing = split.getOutgoingConnections();
058: int priority = Integer.MAX_VALUE;
059: Connection selected = null;
060: RuleFlowGroup systemRuleFlowGroup = getProcessInstance()
061: .getAgenda().getRuleFlowGroup("DROOLS_SYSTEM");
062: for (final Iterator iterator = outgoing.iterator(); iterator
063: .hasNext();) {
064: final Connection connection = (Connection) iterator
065: .next();
066: Constraint constraint = split.getConstraint(connection);
067: if (constraint != null
068: && constraint.getPriority() < priority) {
069: String rule = "RuleFlow-"
070: + getProcessInstance().getProcess().getId()
071: + "-" + getNode().getId() + "-"
072: + connection.getTo().getId();
073: for (Iterator activations = systemRuleFlowGroup
074: .iterator(); activations.hasNext();) {
075: Activation activation = ((RuleFlowGroupNode) activations
076: .next()).getActivation();
077: if (rule.equals(activation.getRule().getName())) {
078: selected = connection;
079: priority = constraint.getPriority();
080: break;
081: }
082: }
083: }
084: }
085: if (selected == null) {
086: throw new IllegalArgumentException(
087: "XOR split could not find at least one valid outgoing connection for split "
088: + getSplitNode().getName());
089: }
090: getProcessInstance().getNodeInstance(selected.getTo())
091: .trigger(this );
092: break;
093: case Split.TYPE_OR:
094: outgoing = split.getOutgoingConnections();
095: boolean found = false;
096: systemRuleFlowGroup = getProcessInstance().getAgenda()
097: .getRuleFlowGroup("DROOLS_SYSTEM");
098: for (final Iterator iterator = outgoing.iterator(); iterator
099: .hasNext();) {
100: final Connection connection = (Connection) iterator
101: .next();
102: Constraint constraint = split.getConstraint(connection);
103: if (constraint != null) {
104: String rule = "RuleFlow-"
105: + getProcessInstance().getProcess().getId()
106: + "-" + getNode().getId() + "-"
107: + connection.getTo().getId();
108: for (Iterator activations = systemRuleFlowGroup
109: .iterator(); activations.hasNext();) {
110: Activation activation = ((RuleFlowGroupNode) activations
111: .next()).getActivation();
112: if (rule.equals(activation.getRule().getName())) {
113: getProcessInstance().getNodeInstance(
114: connection.getTo()).trigger(this );
115: found = true;
116: break;
117: }
118: }
119: }
120: if (!found) {
121: throw new IllegalArgumentException(
122: "OR split could not find at least one valid outgoing connection for split "
123: + getSplitNode().getName());
124: }
125: }
126: break;
127: default:
128: throw new IllegalArgumentException("Illegal split type "
129: + split.getType());
130: }
131: }
132:
133: }
|