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.gworkflowdl.structure;
9   
10  
11  import org.jdom.Attribute;
12  import org.jdom.Element;
13  
14  import java.util.Iterator;
15  import java.util.List;
16  
17  /***
18   * Transformation class
19   * for intern AnalysisTransition representation and JDom AnalysisTransition element.
20   */
21  public final class TransitionJdom {
22      private TransitionJdom() {
23      }
24  
25      /***
26       * transition to Jdom representation.
27       *
28       * @param t AnalysisTransition to be transformed
29       * @return Jdom element
30       */
31      public static Element java2element(final Transition t) {
32          //final Element te = new Element("transition");
33          final Element te = new Element("transition", JdomString.wfSpace);
34  
35          // ID
36          te.setAttribute("ID", t.getID());
37  
38          // description
39          if (t.getDescription() != null) {
40              //final Element de = new Element("description");
41              final Element de = new Element("description", JdomString.wfSpace);
42              de.setText(t.getDescription());
43              te.addContent(de);
44          }
45  
46          // properties
47          Element[] els = PropertiesJdom.java2elements(t.getProperties());
48          for (Element el : els) {
49              te.addContent(el);
50          }
51  
52          // read edges
53          final Edge[] readPlaceRefs = t.getReadEdges();
54          for (Edge readPlaceRef : readPlaceRefs) {
55              final Element pe = new Element("readPlace", JdomString.wfSpace);
56              pe.setAttribute("placeID", readPlaceRef.getPlace().getID());
57  
58              String ex = readPlaceRef.getExpression();
59              if (ex != null) {
60                  pe.setAttribute("edgeExpression", ex);
61              }
62              te.addContent(pe);
63          }
64  
65          // in edges
66          final Edge[] inPlaceRefs = t.getInEdges();
67          for (Edge inPlaceRef : inPlaceRefs) {
68              //final Element pe = new Element("inputPlace");
69              final Element pe = new Element("inputPlace", JdomString.wfSpace);
70              pe.setAttribute("placeID", inPlaceRef.getPlace().getID());
71  
72              String ex = inPlaceRef.getExpression();
73              if (ex != null) {
74                  pe.setAttribute("edgeExpression", ex);
75              }
76              te.addContent(pe);
77          }
78  
79          // write edges
80          final Edge[] writePlaceRefs = t.getWriteEdges();
81          for (Edge writePlaceRef : writePlaceRefs) {
82              final Element pe = new Element("writePlace", JdomString.wfSpace);
83              pe.setAttribute("placeID", writePlaceRef.getPlace().getID());
84  
85              String ex = writePlaceRef.getExpression();
86              if (ex != null) {
87                  pe.setAttribute("edgeExpression", ex);
88              }
89              te.addContent(pe);
90          }
91  
92          // out edges
93          final Edge[] outPlaceRefs = t.getOutEdges();
94          for (Edge outPlaceRef : outPlaceRefs) {
95              //final Element pe = new Element("outputPlace");
96              final Element pe = new Element("outputPlace", JdomString.wfSpace);
97              pe.setAttribute("placeID", outPlaceRef.getPlace().getID());
98  
99              String ex = outPlaceRef.getExpression();
100             if (ex != null) {
101                 pe.setAttribute("edgeExpression", ex);
102             }
103             te.addContent(pe);
104         }
105 
106         // conditions
107         final String[] conditions = t.getConditions();
108         for (String condition : conditions) {
109             //final Element ce = new Element("condition");
110             final Element ce = new Element("condition", JdomString.wfSpace);
111             ce.setText(condition);
112             te.addContent(ce);
113         }
114 
115         // operation
116         if (t.getOperation() != null) {
117             te.addContent(OperationJdom.java2element(t.getOperation()));
118         }
119 
120         return te;
121     }
122 
123     /***
124      * Jdom AnalysisTransition element to internal representation of AnalysisTransition.
125      * embedding workflow must be known because of the place references
126      * (in XML description of the transition only the place IDs are given)
127      *
128      * @param workflow          embedding Workflow
129      * @param transitionElement JDom Element representation
130      * @return transition intern AnalysisTransition representation
131      */
132     public static Transition element2java(final Workflow workflow, final Element transitionElement) throws WorkflowFormatException {
133         List list;
134         Iterator it;
135 
136         final Transition t = Factory.newTransition();
137 
138         // ID
139         final Attribute ida = transitionElement.getAttribute("ID");
140         if (ida==null) throw new WorkflowFormatException("Mandatory attribute \"ID\" of element <transition> is missing!");
141         t.setID(ida.getValue());
142 
143         // description
144         //final Element de = transitionElement.getChild("description");
145         final Element de = transitionElement.getChild("description", JdomString.wfSpace);
146         if (de != null) {
147             t.setDescription(de.getText());
148         }
149 
150         // properties
151         final List els = transitionElement.getChildren("property", JdomString.wfSpace);
152         //final List els = transitionElement.getChildren("property");
153         GenericProperties props = PropertiesJdom.elements2java(els);
154         t.setProperties(props);
155 
156         // conditions
157         //list = transitionElement.getChildren("condition");
158         list = transitionElement.getChildren("condition", JdomString.wfSpace);
159         it = list.iterator();
160         while (it.hasNext()) {
161             final Element ce = (Element) it.next();
162             t.addCondition(ce.getText());
163         }
164 
165         // readPlaces
166         list = transitionElement.getChildren("readPlace", JdomString.wfSpace);
167         it = list.iterator();
168 
169         while (it.hasNext()) {
170             final Element pe = (Element) it.next();
171             final Attribute pida = pe.getAttribute("placeID");
172             if (pida==null) throw new WorkflowFormatException("Mandatory attribute \"placeID\" of element <readPlace> is missing!");
173             final String id = pida.getValue();
174 
175             Attribute a = pe.getAttribute("edgeExpression");
176 
177             final Edge edge = Factory.newEdge();
178             if (a != null) {
179                 final String edgeExpression = pe.getAttribute("edgeExpression").getValue();
180                 edge.setExpression(edgeExpression);
181             }
182 
183             Place place = workflow.getPlace(id);
184             if (place == null) throw new WorkflowFormatException("The readPlace with id \""+id+"\" specified in transition \""+t.getID()+"\" is not available within workflow!");
185             edge.setPlace(place);
186             t.addReadEdge(edge);
187         }
188 
189         // inPlaces
190         list = transitionElement.getChildren("inputPlace", JdomString.wfSpace);
191         it = list.iterator();
192 
193         while (it.hasNext()) {
194             final Element pe = (Element) it.next();
195             final Attribute pida = pe.getAttribute("placeID");
196             if (pida==null) throw new WorkflowFormatException("Mandatory attribute \"placeID\" of element <inputPlace> is missing!");
197             final String id = pida.getValue();
198 
199             Attribute a = pe.getAttribute("edgeExpression");
200 
201             final Edge edge = Factory.newEdge();
202             if (a != null) {
203                 final String edgeExpression = pe.getAttribute("edgeExpression").getValue();
204                 edge.setExpression(edgeExpression);
205             }
206 
207             Place place = workflow.getPlace(id);
208             if (place == null) throw new WorkflowFormatException("The inputPlace with id \""+id+"\" specified in transition \""+t.getID()+"\" is not available within workflow!");
209             edge.setPlace(place);
210             t.addInEdge(edge);
211         }
212 
213         // writePlaces
214         list = transitionElement.getChildren("writePlace", JdomString.wfSpace);
215         it = list.iterator();
216 
217         while (it.hasNext()) {
218             final Element pe = (Element) it.next();
219             final Attribute pida = pe.getAttribute("placeID");
220             if (pida==null) throw new WorkflowFormatException("Mandatory attribute \"placeID\" of element <writePlace> is missing!");
221             final String id = pida.getValue();
222 
223             Attribute a = pe.getAttribute("edgeExpression");
224 
225             final Edge edge = Factory.newEdge();
226             if (a != null) {
227                 final String edgeExpression = pe.getAttribute("edgeExpression").getValue();
228                 edge.setExpression(edgeExpression);
229             }
230 
231             Place place = workflow.getPlace(id);
232             if (place == null) throw new WorkflowFormatException("The writePlace with id \""+id+"\" specified in transition \""+t.getID()+"\" is not available within workflow!");
233             edge.setPlace(place);
234             t.addWriteEdge(edge);
235         }
236 
237         // outPlaces
238         list = transitionElement.getChildren("outputPlace", JdomString.wfSpace);
239         it = list.iterator();
240 
241         while (it.hasNext()) {
242             final Element pe = (Element) it.next();
243             final Attribute pida = pe.getAttribute("placeID");
244             if (pida==null) throw new WorkflowFormatException("Mandatory attribute \"placeID\" of element <outputPlace> is missing!");
245             final String id = pida.getValue();
246 
247             Attribute a = pe.getAttribute("edgeExpression");
248 
249             final Edge edge = Factory.newEdge();
250             if (a != null) {
251                 final String edgeExpression = pe.getAttribute("edgeExpression").getValue();
252                 edge.setExpression(edgeExpression);
253             }
254 
255             Place place = workflow.getPlace(id);
256             if (place == null) throw new WorkflowFormatException("The outputPlace with id \""+id+"\" specified in transition \""+t.getID()+"\" is not available within workflow!");
257             edge.setPlace(place);
258             t.addOutEdge(edge);
259 
260         }
261 
262         // operation
263         final Element ope = transitionElement.getChild("operation", JdomString.wfSpace);
264         if (ope != null) {
265             t.setOperation(OperationJdom.element2java(ope));
266         }
267 
268         return t;
269     }
270 
271 }