1 package org.jbpm.graph.node; 2 3 import org.jbpm.graph.def.*; 4 5 public class ProcessFactory { 6 7 public static ProcessDefinition createProcessDefinition(String [] nodes, String [] transitions) { 8 ProcessDefinition pd = new ProcessDefinition(); 9 addNodesAndTransitions(pd, nodes, transitions); 10 return pd; 11 } 12 13 public static void addNodesAndTransitions(ProcessDefinition pd, String [] nodes, String [] transitions) { 14 for ( int i = 0; i < nodes.length; i++ ) { 15 pd.addNode( createNode( nodes[i] ) ); 16 } 17 18 for ( int i = 0; i < transitions.length; i++ ) { 19 String [] parsedTransition = cutTransitionText( transitions[i] ); 20 Node from = pd.getNode( parsedTransition[0] ); 21 Node to = pd.getNode( parsedTransition[2] ); 22 Transition t = new Transition( parsedTransition[1] ); 23 from.addLeavingTransition(t); 24 to.addArrivingTransition(t); 25 } 26 } 27 28 public static String getTypeName(Node node) { 29 if (node==null) return null; 30 return NodeTypes.getNodeName(node.getClass()); 31 } 32 33 36 public static Node createNode(String text) { 37 Node node = null; 38 39 String typeName = null; 40 String name = null; 41 42 text = text.trim(); 43 int spaceIndex = text.indexOf(' '); 44 if (spaceIndex!=-1) { 45 typeName = text.substring(0, spaceIndex); 46 name = text.substring(spaceIndex + 1); 47 } else { 48 typeName = text; 49 name = null; 50 } 51 52 Class nodeType = NodeTypes.getNodeType(typeName); 53 if ( nodeType==null ) throw new IllegalArgumentException ("unknown node type name '" + typeName + "'"); 54 try { 55 node = (Node) nodeType.newInstance(); 56 node.setName(name); 57 } catch (Exception e) { 58 throw new RuntimeException ("couldn't instantiate nodehandler for type '" + typeName + "'"); 59 } 60 return node; 61 } 62 63 public static String [] cutTransitionText(String transitionText) { 64 String [] parts = new String [3]; 65 if ( transitionText == null ) { 66 throw new NullPointerException ( "transitionText is null" ); 67 } 68 int start = transitionText.indexOf( "--" ); 69 if ( start == -1 ) { 70 throw new IllegalArgumentException ( "incorrect transition format exception : nodefrom --transitionname--> nodeto" ); 71 } 72 parts[0] = transitionText.substring(0,start).trim(); 73 74 int end = transitionText.indexOf( "-->", start ); 75 if ( start < end ) { 76 parts[1] = transitionText.substring(start+2,end).trim(); 77 } else { 78 parts[1] = null; 79 } 80 parts[2] = transitionText.substring(end+3).trim(); 81 return parts; 82 } 83 } 84 | Popular Tags |