/machinelearning/5.0/drools-core/src/main/java/org/drools/workflow/core/impl/NodeImpl.java

https://github.com/etirelli/droolsjbpm-contributed-experiments · Java · 206 lines · 152 code · 32 blank · 22 comment · 30 complexity · d2ff11505c9b596dc20a9f1f2960aa63 MD5 · raw file

  1. package org.drools.workflow.core.impl;
  2. /*
  3. * Copyright 2005 JBoss Inc
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import java.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.drools.process.core.Context;
  24. import org.drools.workflow.core.Connection;
  25. import org.drools.workflow.core.Node;
  26. import org.drools.workflow.core.NodeContainer;
  27. /**
  28. * Default implementation of a node.
  29. *
  30. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  31. */
  32. public abstract class NodeImpl implements Node, Serializable {
  33. protected static final NodeImpl[] EMPTY_NODE_ARRAY = new NodeImpl[0];
  34. private long id;
  35. private String name;
  36. private Map<String, List<Connection>> incomingConnections;
  37. private Map<String, List<Connection>> outgoingConnections;
  38. private NodeContainer nodeContainer;
  39. private Map<String, Context> contexts = new HashMap<String, Context>();
  40. private Map<String, Object> metaData = new HashMap<String, Object>();
  41. public NodeImpl() {
  42. this.id = -1;
  43. this.incomingConnections = new HashMap<String, List<Connection>>();
  44. this.outgoingConnections = new HashMap<String, List<Connection>>();
  45. }
  46. public long getId() {
  47. return this.id;
  48. }
  49. public void setId(final long id) {
  50. this.id = id;
  51. }
  52. public String getName() {
  53. return this.name;
  54. }
  55. public void setName(final String name) {
  56. this.name = name;
  57. }
  58. public Map<String, List<Connection>> getIncomingConnections() {
  59. // TODO: users can still modify the lists inside this Map
  60. return Collections.unmodifiableMap(this.incomingConnections);
  61. }
  62. public Map<String, List<Connection>> getOutgoingConnections() {
  63. // TODO: users can still modify the lists inside this Map
  64. return Collections.unmodifiableMap(this.outgoingConnections);
  65. }
  66. public void addIncomingConnection(final String type, final Connection connection) {
  67. validateAddIncomingConnection(type, connection);
  68. List<Connection> connections = this.incomingConnections.get(type);
  69. if (connections == null) {
  70. connections = new ArrayList<Connection>();
  71. this.incomingConnections.put(type, connections);
  72. }
  73. connections.add(connection);
  74. }
  75. public void validateAddIncomingConnection(final String type, final Connection connection) {
  76. if (type == null) {
  77. throw new IllegalArgumentException("Connection type cannot be null");
  78. }
  79. if (connection == null) {
  80. throw new IllegalArgumentException("Connection cannot be null");
  81. }
  82. }
  83. public List<Connection> getIncomingConnections(String type) {
  84. List<Connection> result = incomingConnections.get(type);
  85. if (result == null) {
  86. return new ArrayList<Connection>();
  87. }
  88. return result;
  89. }
  90. public void addOutgoingConnection(final String type, final Connection connection) {
  91. validateAddOutgoingConnection(type, connection);
  92. List<Connection> connections = this.outgoingConnections.get(type);
  93. if (connections == null) {
  94. connections = new ArrayList<Connection>();
  95. this.outgoingConnections.put(type, connections);
  96. }
  97. connections.add(connection);
  98. }
  99. public void validateAddOutgoingConnection(final String type, final Connection connection) {
  100. if (type == null) {
  101. throw new IllegalArgumentException("Connection type cannot be null");
  102. }
  103. if (connection == null) {
  104. throw new IllegalArgumentException("Connection cannot be null");
  105. }
  106. }
  107. public List<Connection> getOutgoingConnections(String type) {
  108. List<Connection> result = outgoingConnections.get(type);
  109. if (result == null) {
  110. return new ArrayList<Connection>();
  111. }
  112. return result;
  113. }
  114. public void removeIncomingConnection(final String type, final Connection connection) {
  115. validateRemoveIncomingConnection(type, connection);
  116. this.incomingConnections.get(type).remove(connection);
  117. }
  118. public void validateRemoveIncomingConnection(final String type, final Connection connection) {
  119. if (type == null) {
  120. throw new IllegalArgumentException("Connection type cannot be null");
  121. }
  122. if (connection == null) {
  123. throw new IllegalArgumentException("Connection is null");
  124. }
  125. if (!incomingConnections.get(type).contains(connection)) {
  126. throw new IllegalArgumentException("Given connection <"
  127. + connection + "> is not part of the incoming connections");
  128. }
  129. }
  130. public void removeOutgoingConnection(final String type, final Connection connection) {
  131. validateRemoveOutgoingConnection(type, connection);
  132. this.outgoingConnections.get(type).remove(connection);
  133. }
  134. public void validateRemoveOutgoingConnection(final String type, final Connection connection) {
  135. if (type == null) {
  136. throw new IllegalArgumentException("Connection type cannot be null");
  137. }
  138. if (connection == null) {
  139. throw new IllegalArgumentException("Connection is null");
  140. }
  141. if (!this.outgoingConnections.get(type).contains(connection)) {
  142. throw new IllegalArgumentException("Given connection <"
  143. + connection + "> is not part of the outgoing connections");
  144. }
  145. }
  146. public NodeContainer getNodeContainer() {
  147. return nodeContainer;
  148. }
  149. public void setNodeContainer(NodeContainer nodeContainer) {
  150. this.nodeContainer = nodeContainer;
  151. }
  152. public void setContext(String contextId, Context context) {
  153. this.contexts.put(contextId, context);
  154. }
  155. public Context getContext(String contextId) {
  156. return this.contexts.get(contextId);
  157. }
  158. public Context resolveContext(String contextId, Object param) {
  159. Context context = getContext(contextId);
  160. if (context != null) {
  161. context = context.resolveContext(param);
  162. if (context != null) {
  163. return context;
  164. }
  165. }
  166. return nodeContainer.resolveContext(contextId, param);
  167. }
  168. public void setMetaData(String name, Object value) {
  169. this.metaData.put(name, value);
  170. }
  171. public Object getMetaData(String name) {
  172. return this.metaData.get(name);
  173. }
  174. }