01: package org.drools.ruleflow.instance.impl;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import java.util.HashMap;
20: import java.util.Iterator;
21: import java.util.Map;
22:
23: import org.drools.ruleflow.core.Connection;
24: import org.drools.ruleflow.core.Join;
25: import org.drools.ruleflow.core.Node;
26: import org.drools.ruleflow.instance.RuleFlowNodeInstance;
27:
28: /**
29: * Runtime counterpart of a join node.
30: *
31: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
32: */
33: public class RuleFlowJoinInstanceImpl extends RuleFlowNodeInstanceImpl
34: implements RuleFlowNodeInstance {
35:
36: private final Map triggers = new HashMap();
37:
38: protected Join getJoinNode() {
39: return (Join) getNode();
40: }
41:
42: public void trigger(final RuleFlowNodeInstance from) {
43: final Join join = getJoinNode();
44: switch (join.getType()) {
45: case Join.TYPE_XOR:
46: triggerCompleted();
47: break;
48: case Join.TYPE_AND:
49: final Node node = getProcessInstance().getRuleFlowProcess()
50: .getNode(from.getNodeId());
51: final Integer count = (Integer) this .triggers.get(node);
52: if (count == null) {
53: this .triggers.put(node, new Integer(1));
54: } else {
55: this .triggers.put(node, new Integer(
56: count.intValue() + 1));
57: }
58: checkActivation();
59: break;
60: default:
61: throw new IllegalArgumentException("Illegal join type "
62: + join.getType());
63: }
64: }
65:
66: private void checkActivation() {
67: // check whether all parent nodes have been triggered
68: for (final Iterator it = getJoinNode().getIncomingConnections()
69: .iterator(); it.hasNext();) {
70: final Connection connection = (Connection) it.next();
71: if (this .triggers.get(connection.getFrom()) == null) {
72: return;
73: }
74: }
75: // if true, decrease trigger count for all parents and trigger children
76: for (final Iterator it = getJoinNode().getIncomingConnections()
77: .iterator(); it.hasNext();) {
78: final Connection connection = (Connection) it.next();
79: final Integer count = (Integer) this .triggers
80: .get(connection.getFrom());
81: if (count.intValue() == 1) {
82: this .triggers.remove(connection.getFrom());
83: } else {
84: this .triggers.put(connection.getFrom(), new Integer(
85: count.intValue() - 1));
86: }
87: }
88: triggerCompleted();
89: }
90:
91: public void triggerCompleted() {
92: getProcessInstance().getNodeInstance(
93: getJoinNode().getTo().getTo()).trigger(this);
94: }
95:
96: }
|