001: package org.drools.ruleflow.core.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.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.drools.ruleflow.common.core.impl.ProcessImpl;
026: import org.drools.ruleflow.core.Node;
027: import org.drools.ruleflow.core.RuleFlowProcess;
028: import org.drools.ruleflow.core.StartNode;
029: import org.drools.ruleflow.core.Variable;
030:
031: /**
032: * Default implementation of a RuleFlow process.
033: *
034: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
035: */
036: public class RuleFlowProcessImpl extends ProcessImpl implements
037: RuleFlowProcess {
038:
039: public static final String RULEFLOW_TYPE = "RuleFlow";
040:
041: private static final long serialVersionUID = 400L;
042:
043: private Map nodes;
044: private List variables;
045: private long lastNodeId;
046: private List imports;
047:
048: public RuleFlowProcessImpl() {
049: super ();
050: setType(RULEFLOW_TYPE);
051: this .nodes = new HashMap();
052: this .variables = new ArrayList();
053: }
054:
055: public StartNode getStart() {
056: for (final Iterator it = this .nodes.values().iterator(); it
057: .hasNext();) {
058: final Node node = (Node) it.next();
059: if (node instanceof StartNode) {
060: return (StartNode) node;
061: }
062: }
063: return null;
064: }
065:
066: public Node[] getNodes() {
067: return (Node[]) this .nodes.values().toArray(
068: new Node[this .nodes.size()]);
069: }
070:
071: public Node getNode(final long id) {
072: final Long idLong = new Long(id);
073: if (!this .nodes.containsKey(idLong)) {
074: throw new IllegalArgumentException("Unknown node id: " + id);
075: }
076: return (Node) this .nodes.get(idLong);
077: }
078:
079: // private EndNode getEnd() {
080: // for ( final Iterator it = this.nodes.values().iterator(); it.hasNext(); ) {
081: // final Node node = (Node) it.next();
082: // if ( node instanceof EndNode ) {
083: // return (EndNode) node;
084: // }
085: // }
086: // return null;
087: // }
088:
089: public void removeNode(final Node node) {
090: if (node == null) {
091: throw new IllegalArgumentException("Node is null");
092: }
093: final Node n = (Node) this .nodes.remove(new Long(node.getId()));
094: if (n == null) {
095: throw new IllegalArgumentException("Unknown node: " + node);
096: }
097: }
098:
099: public List getVariables() {
100: return this .variables;
101: }
102:
103: public void setVariables(final List variables) {
104: if (variables == null) {
105: throw new IllegalArgumentException("Variables is null");
106: }
107: this .variables = variables;
108: }
109:
110: public String[] getVariableNames() {
111: final String[] result = new String[this .variables.size()];
112: for (int i = 0; i < this .variables.size(); i++) {
113: result[i] = ((Variable) this .variables.get(i)).getName();
114: }
115: return result;
116: }
117:
118: public void addNode(final Node node) {
119: validateAddNode(node);
120: if (!this .nodes.containsValue(node)) {
121: node.setId(++this .lastNodeId);
122: this .nodes.put(new Long(node.getId()), node);
123: }
124: }
125:
126: private void validateAddNode(final Node node) {
127: if ((node instanceof StartNode) && (getStart() != null)) {
128: throw new IllegalArgumentException(
129: "A ruleflow process cannot have more than one start node!");
130: }
131: // if ( (node instanceof EndNode) && (getEnd() != null) ) {
132: // throw new IllegalArgumentException( "A ruleflow process cannot have more than one end node!" );
133: // }
134: }
135:
136: public List getImports() {
137: return imports;
138: }
139:
140: public void setImports(List imports) {
141: this.imports = imports;
142: }
143: }
|