KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > graph > node > ProcessFactory


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 JavaDoc[] nodes, String JavaDoc[] transitions) {
8     ProcessDefinition pd = new ProcessDefinition();
9     addNodesAndTransitions(pd, nodes, transitions);
10     return pd;
11   }
12
13   public static void addNodesAndTransitions(ProcessDefinition pd, String JavaDoc[] nodes, String JavaDoc[] 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 JavaDoc[] 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 JavaDoc getTypeName(Node node) {
29     if (node==null) return null;
30     return NodeTypes.getNodeName(node.getClass());
31   }
32
33   /**
34    * @throws NullPointerException if text is null.
35    */

36   public static Node createNode(String JavaDoc text) {
37     Node node = null;
38     
39     String JavaDoc typeName = null;
40     String JavaDoc 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 JavaDoc nodeType = NodeTypes.getNodeType(typeName);
53     if ( nodeType==null ) throw new IllegalArgumentException JavaDoc("unknown node type name '" + typeName + "'");
54     try {
55       node = (Node) nodeType.newInstance();
56       node.setName(name);
57     } catch (Exception JavaDoc e) {
58       throw new RuntimeException JavaDoc("couldn't instantiate nodehandler for type '" + typeName + "'");
59     }
60     return node;
61   }
62
63   public static String JavaDoc[] cutTransitionText(String JavaDoc transitionText) {
64     String JavaDoc[] parts = new String JavaDoc[3];
65     if ( transitionText == null ) {
66       throw new NullPointerException JavaDoc( "transitionText is null" );
67     }
68     int start = transitionText.indexOf( "--" );
69     if ( start == -1 ) {
70       throw new IllegalArgumentException JavaDoc( "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