001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.graph.node;
023:
024: import org.jbpm.JbpmException;
025: import org.jbpm.graph.def.Node;
026: import org.jbpm.graph.def.ProcessDefinition;
027: import org.jbpm.graph.def.Transition;
028:
029: public class ProcessFactory {
030:
031: public static ProcessDefinition createProcessDefinition(
032: String[] nodes, String[] transitions) {
033: ProcessDefinition pd = new ProcessDefinition();
034: addNodesAndTransitions(pd, nodes, transitions);
035: return pd;
036: }
037:
038: public static void addNodesAndTransitions(ProcessDefinition pd,
039: String[] nodes, String[] transitions) {
040: for (int i = 0; i < nodes.length; i++) {
041: pd.addNode(createNode(nodes[i]));
042: }
043:
044: for (int i = 0; i < transitions.length; i++) {
045: String[] parsedTransition = cutTransitionText(transitions[i]);
046: Node from = pd.getNode(parsedTransition[0]);
047: Node to = pd.getNode(parsedTransition[2]);
048: Transition t = new Transition(parsedTransition[1]);
049: from.addLeavingTransition(t);
050: to.addArrivingTransition(t);
051: }
052: }
053:
054: public static String getTypeName(Node node) {
055: if (node == null)
056: return null;
057: return NodeTypes.getNodeName(node.getClass());
058: }
059:
060: /**
061: * @throws JbpmException if text is null.
062: */
063: public static Node createNode(String text) {
064: if (text == null)
065: throw new JbpmException("text is null");
066:
067: Node node = null;
068:
069: String typeName = null;
070: String name = null;
071:
072: text = text.trim();
073: int spaceIndex = text.indexOf(' ');
074: if (spaceIndex != -1) {
075: typeName = text.substring(0, spaceIndex);
076: name = text.substring(spaceIndex + 1);
077: } else {
078: typeName = text;
079: name = null;
080: }
081:
082: Class nodeType = NodeTypes.getNodeType(typeName);
083: if (nodeType == null)
084: throw new IllegalArgumentException(
085: "unknown node type name '" + typeName + "'");
086: try {
087: node = (Node) nodeType.newInstance();
088: node.setName(name);
089: } catch (Exception e) {
090: throw new JbpmException(
091: "couldn't instantiate nodehandler for type '"
092: + typeName + "'");
093: }
094: return node;
095: }
096:
097: public static String[] cutTransitionText(String transitionText) {
098: String[] parts = new String[3];
099: if (transitionText == null) {
100: throw new JbpmException("transitionText is null");
101: }
102: int start = transitionText.indexOf("--");
103: if (start == -1) {
104: throw new JbpmException(
105: "incorrect transition format exception : nodefrom --transitionname--> nodeto");
106: }
107: parts[0] = transitionText.substring(0, start).trim();
108:
109: int end = transitionText.indexOf("-->", start);
110: if (start < end) {
111: parts[1] = transitionText.substring(start + 2, end).trim();
112: } else {
113: parts[1] = null;
114: }
115: parts[2] = transitionText.substring(end + 3).trim();
116: return parts;
117: }
118: }
|