View Javadoc

1   /*
2    * Copyright 2010 Fraunhofer Gesellschaft, Munich, Germany,
3    * for its Fraunhofer Institute for Computer Architecture and Software
4    * Technology (FIRST), Berlin, Germany. All rights reserved.
5    * http://www.first.fraunhofer.de/
6    */
7   
8   package net.kwfgrid.gwes.workflowgenerator;
9   
10  import net.kwfgrid.gworkflowdl.structure.*;
11  
12  import java.util.List;
13  import java.util.Map;
14  import java.util.ArrayList;
15  
16  /**
17   * This class can be used as helper class to automatically generate workflows that invoke single jobs.
18   * @author Andreas Hoheisel
19   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
20   * @version $Id: SingleJob.java 1422 2010-11-01 14:15:25Z hoheisel $
21   */
22  public class SingleJob extends WorkflowGenerator {
23  
24      private OperationClass operationClass;
25      private OperationCandidate operationCandidate;
26      private Transition transition;
27  
28      /**
29       * Generate a workflow with one single job that invokes a specified operation class with a set of input and
30       * output parameters.
31       * @param operationClass The operation class to invoke. This will also be used as transition identifier.
32       * @param inputs A Map that contains the input parameter names as keys and the input parameter values (without any
33       * parent elements) as values. If input map is <code>null</code>, then only a control token is introduced. Please
34       * use a {@link java.util.LinkedHashMap} if you want to preserve the order of the inputs.
35       * @param outputs A List that contains the output parameter names. If outputs are <code>null</code>, then a
36       * control output place is generated.
37       * @throws WorkflowFormatException
38       * @throws CapacityException
39       */
40      public SingleJob(String operationClass, Map<String,String> inputs, List<String> outputs) throws WorkflowFormatException, CapacityException {
41          super("Single job invoking '"+operationClass+"'");
42          operationCandidate = null;
43  
44          // generate transition
45          transition = Factory.newTransition();
46          transition.setID(operationClass);
47  
48          // generate operation with operationClass
49          this.operationClass = Factory.newOperationClass();
50          this.operationClass.setName(operationClass);
51          Operation op = Factory.newOperation();
52          op.set(this.operationClass);
53          transition.setOperation(op);
54  
55          // generate places
56          List<Place> places = generatePlaces(inputs, outputs, transition);
57  
58          // add places and transitions to workflow
59          Place[] placeArray = new Place[places.size()];
60          workflow.setPlaces(places.toArray(placeArray));
61          workflow.addTransition(transition);
62      }
63  
64      /**
65       * Generate a workflow with one single job that invokes a specified operation with a set of input and
66       * output parameters.
67       * @param operationClass The operation class to invoke. This will also be used as transition identifier.
68       * @param operationName The concrete operation to invoke.
69       * @param resourceName The concrete resource to use to invoke the operation.
70       * @param inputs A Map that contains the input parameter names as keys and the input parameter values (without any
71       * parent elements) as values. If input map is <code>null</code>, then only a control token is introduced.
72       * @param outputs A List that contains the output parameter names. If outputs are <code>null</code>, then a
73       * control output place is generated.
74       * @throws WorkflowFormatException
75       * @throws CapacityException
76       */
77      public SingleJob(String operationClass, String operationType, String operationName, String resourceName, Map<String,String> inputs, List<String> outputs) throws WorkflowFormatException, CapacityException {
78          this(operationClass, inputs, outputs);
79  
80          // set operation candidate
81          operationCandidate = Factory.newOperationCandidate();
82          operationCandidate.setType(operationType);
83          operationCandidate.setOperationName(operationName);
84          operationCandidate.setResourceName(resourceName);
85          operationCandidate.setSelected(true);
86          this.operationClass.addOperationCandidate(operationCandidate);
87      }
88  
89      private List<Place> generatePlaces(Map<String, String> inputs, List<String> outputs, Transition transition) throws WorkflowFormatException, CapacityException {
90          List<Place> places = new ArrayList<Place>();
91  
92          if (inputs != null && inputs.size()>0) {
93              // generate input places with token and connect with transition
94              for (String inputName : inputs.keySet()) {
95                  // Data
96                  StringBuffer buffer = new StringBuffer();
97                  buffer.append("<data><").append(inputName).append(">").append(inputs.get(inputName)).append("</").append(inputName).append("></data>");
98                  Data data = Factory.newData();
99                  data.fromXML(buffer.toString());
100                 // Token
101                 Token token = Factory.newToken(data);
102                 // Place
103                 Place place = Factory.newPlace();
104                 place.setID(inputName);
105                 place.addToken(token);
106                 places.add(place);
107                 // Edge
108                 Edge inputEdge = Factory.newEdge();
109                 inputEdge.setExpression(inputName);
110                 inputEdge.setPlace(place);
111                 transition.addInEdge(inputEdge);
112             }
113         } else {
114             // only one control input place
115             Token token = Factory.newToken(true);
116             Place place = Factory.newPlace();
117             place.setID("start");
118             place.addToken(token);
119             places.add(place);
120             // Edge
121             Edge inputEdge = Factory.newEdge();
122             inputEdge.setPlace(place);
123             transition.addInEdge(inputEdge);
124         }
125 
126         if (outputs != null && outputs.size()>0) {
127             // generate output places and connect with transition
128             for (String outputName : outputs) {
129                 // Place
130                 Place place = Factory.newPlace();
131                 place.setID(outputName);
132                 places.add(place);
133                 // Edge
134                 Edge outputEdge = Factory.newEdge();
135                 outputEdge.setExpression(outputName);
136                 outputEdge.setPlace(place);
137                 transition.addOutEdge(outputEdge);
138             }
139         } else {
140             // only one control output place
141             Place place = Factory.newPlace();
142             place.setID("end");
143             places.add(place);
144             // Edge
145             Edge outputEdge = Factory.newEdge();
146             outputEdge.setPlace(place);
147             transition.addOutEdge(outputEdge);
148         }
149 
150         return places;
151     }
152 
153 
154     /**
155      * Get the operation class.
156      * @return
157      */
158     public OperationClass getOperationClass() {
159         return operationClass;
160     }
161 
162     /**
163      * Get the operation candidate or <code>null</code> if there is no operation candidate.
164      * @return
165      */
166     public OperationCandidate getOperationCandidate() {
167         return operationCandidate;
168     }
169 
170     /**
171      * Get the transition.
172      * @return
173      */
174     public Transition getTransition() {
175         return transition;
176     }
177 }