View Javadoc

1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gworkflowdl.protocol.structure;
7   
8   import net.kwfgrid.gworkflowdl.structure.*;
9   import net.kwfgrid.gworkflowdl.protocol.xml.GWDLNamespace;
10  import net.kwfgrid.gworkflowdl.protocol.util.StructureUtils;
11  import net.kwfgrid.gworkflowdl.protocol.calls.*;
12  import net.kwfgrid.gworkflowdl.protocol.IMethodCallStrategy;
13  
14  import org.jdom.*;
15  
16  import java.util.*;
17  
18  /***
19   * Protocol implementation of Workflow.
20   */
21  public class ProtocolWorkflow extends ProtocolRootOwls implements Workflow, GWDLNamespace {
22      /*** The namespace of this object. Used for event notification. */
23      public static final String NAMESPACE = GWDL_NS;
24      /*** The namespace of the properties of this object. Used for event notification. */
25      public static final String NAMESPACE_PROPERTIES = GWDL_ATTRIBUTE_NS;
26      /*** The name of this object. Used for event notification. */
27      public static final String NAME = "workflow";
28      /*** The name of the ID property of this object. Used for event notification. */
29      //public static final String NAME_ID = "ID";
30      /*** The name of the description property of this object. Used for event notification. */
31      public static final String NAME_DESCRIPTION = "description";
32      /*** The name of the description property of this object. Used for event notification. */
33      public static final String NAME_OWL = "owl";
34  
35      protected Workflow _delegate;
36      protected IMethodCallStrategy _strategy;
37  
38      /***
39         Constructor.
40         @param delegate The workflow implementation used as a delegate by this workflow.
41       */
42      protected ProtocolWorkflow(Workflow delegate, IMethodCallStrategy strategy) {
43  	super(delegate, delegate.getID());
44  	_strategy = strategy;
45  	_delegate = delegate;	
46  	((ProtocolProperties)_delegate.getProperties()).setParent(this);
47      }
48  
49      /// ----------------------------------------------------------------------------------------------------
50  
51      public IMethodCallStrategy getMethodCallStrategy() {
52  	return _strategy;
53      }
54  
55      public String getOwlNamespace() {
56  	return NAMESPACE;
57      }
58      
59      public String getOwlName() {
60  	return NAME_OWL;
61      }
62      
63      /// 
64      /// Standard Set API
65      /// ....................................................................................................
66  
67      public void fromXML(String gworkflowdl) throws WorkflowFormatException, CapacityException {
68  	try {
69  	    getMethodCallStrategy().execute(new WorkflowFromXML(this, gworkflowdl));
70  	} catch (MethodCallException mx) {
71  	    Throwable cause = mx.getCause();
72  	    if (cause instanceof CapacityException)
73  		throw (CapacityException)cause;
74  	    else
75  		throw (WorkflowFormatException)cause;
76  	}
77      }
78  
79      /*
80      public void setID(String id) {
81  	getMethodCallStrategy().execute(new WorkflowSetID(this, id));
82      }
83      */
84  
85      public void addPlace(Place place) {
86  	getMethodCallStrategy().execute(new WorkflowAddPlace(this, (ProtocolPlace)place));
87      }
88  
89      public void removePlace(int i) {
90  	getMethodCallStrategy().execute(new WorkflowRemovePlace(this, i));
91      }
92  
93      public void addTransition(Transition transition) {
94  	getMethodCallStrategy().execute(new WorkflowAddTransition(this, (ProtocolTransition)transition));
95      }
96  
97      public void removeTransition(int i) {
98  	getMethodCallStrategy().execute(new WorkflowRemoveTransition(this, i));
99      }
100 
101     public void setDescription(String description) {
102 	getMethodCallStrategy().execute(new WorkflowSetDescription(this, description));
103     }
104 
105     public void setProperties(GenericProperties props) {
106 	getMethodCallStrategy().execute(new WorkflowSetProperties(this, (ProtocolProperties)props));
107     }
108 
109     public void setTransitions(Transition[] transitions) {
110 	getMethodCallStrategy().execute(new WorkflowSetTransitions(this, (ProtocolTransition[])StructureUtils.castArray(ProtocolTransition.class, transitions)));
111     }
112 
113     public void setPlaces(Place[] places) {
114 	getMethodCallStrategy().execute(new WorkflowSetPlaces(this, (ProtocolPlace[])StructureUtils.castArray(ProtocolPlace.class, places)));
115     }
116 
117     ///
118     /// Set API called Protocol internally
119     /// ....................................................................................................
120 
121     /***
122        This method is only to be used by the protocol.        
123      */
124     public void __fromXML(String gworkflowdl) throws WorkflowFormatException, CapacityException {
125 	Element wfe = JdomString.string2verifyWorkflowElement(gworkflowdl);
126 
127 	// clear target
128 
129 	__setTransitions(new ProtocolTransition[0]);
130 	__setPlaces(new ProtocolPlace[0]);
131 	__setDescription(Workflow.DEFAULT_DESCRIPTION);
132 	__setOwls(new String[0]);
133 	((ProtocolProperties)getProperties()).__setProperties(new ProtocolProperty[0]);
134 	
135 
136 	// Copy JDOM Element to target.
137 
138         List list = wfe.getChildren("owl", JdomString.wfSpace);
139         Iterator it = list.iterator();
140 
141         while (it.hasNext()) {
142             Element oe = (Element) it.next();
143             __addOwl(oe.getText());
144         }
145 	
146         // description
147         Element de = wfe.getChild("description", JdomString.wfSpace);
148         if (de != null) {
149             __setDescription(de.getText());
150         }
151 
152         // properties
153         List els = wfe.getChildren("property", JdomString.wfSpace);
154 
155         GenericProperties props = PropertiesJdom.elements2java(els);
156         __setProperties((ProtocolProperties)props);
157 
158 
159 
160         // places FIRST (before transitions)
161 
162         list = wfe.getChildren("place", JdomString.wfSpace);
163         it = list.iterator();
164         while (it.hasNext()) {
165             Element pe = (Element) it.next();
166             __addPlace((ProtocolPlace)PlaceJdom.element2java(pe));
167         }
168 
169         // transitions
170 
171         list = wfe.getChildren("transition", JdomString.wfSpace);
172         it = list.iterator();
173         while (it.hasNext()) {
174             Element te = (Element) it.next();
175             __addTransition((ProtocolTransition)TransitionJdom.element2java(this, te));
176         }
177     }
178 
179     /***
180        This method is only to be used by the protocol.        
181      */
182     /*
183     public void __setID(String id) {
184 	_delegate.setID(id);
185 	firePropertyChanged(NAMESPACE_PROPERTIES, NAME_ID, id);
186     }
187     */
188 
189     /***
190        This method is only to be used by the protocol.        
191      */
192     public void __addPlace(ProtocolPlace place) {
193 	_delegate.addPlace(place);
194 	place.setParent(this);
195 	fireObjectAdded(ProtocolPlace.NAMESPACE, ProtocolPlace.NAME, place);
196     }
197 
198     /***
199        This method is only to be used by the protocol.        
200      */
201     public void __removePlace(int i) {
202 	ProtocolPlace place = (ProtocolPlace)_delegate.getPlace(i);
203 	_delegate.removePlace(i);
204 	place.setParent(null);
205 	fireObjectRemoved(ProtocolPlace.NAMESPACE, ProtocolPlace.NAME, place);
206     }
207 
208     /***
209        This method is only to be used by the protocol.        
210      */
211     public void __addTransition(ProtocolTransition transition) {
212 	_delegate.addTransition(transition);
213 	transition.setParent(this);
214 	fireObjectAdded(ProtocolTransition.NAMESPACE, ProtocolTransition.NAME, transition);
215     }
216 
217     /***
218        This method is only to be used by the protocol.        
219      */
220     public void __removeTransition(int i) {
221 	ProtocolTransition transition = (ProtocolTransition)_delegate.getTransition(i);
222 	_delegate.removeTransition(i);
223 	transition.setParent(null);
224 	fireObjectRemoved(ProtocolTransition.NAMESPACE, ProtocolTransition.NAME, transition);
225     }
226 
227     /***
228        This method is only to be used by the protocol.        
229      */
230     public void __setDescription(String description) {
231 	_delegate.setDescription(description);
232 	firePropertyChanged(NAMESPACE_PROPERTIES, NAME_DESCRIPTION, description);
233     }
234 
235     /***
236        This method is only to be used by the protocol.        
237      */
238     public void __setProperties(ProtocolProperties props) {
239 	ProtocolProperties old = (ProtocolProperties)_delegate.getProperties();	
240 	_delegate.setProperties(props);
241 	old.setParent(null);
242 	fireObjectRemoved(ProtocolProperties.NAMESPACE, ProtocolProperties.NAME, old);
243 	props.setParent(this);
244 	fireObjectAdded(ProtocolProperties.NAMESPACE, ProtocolProperties.NAME, props);	
245     }
246 
247     /***
248        This method is only to be used by the protocol.        
249      */
250     public void __setTransitions(ProtocolTransition[] transitions) {
251 	Transition[] old = _delegate.getTransitions();
252 	_delegate.setTransitions(transitions);
253 	if (old.length > 0) {
254 	    StructureUtils.setParent(old, null);
255 	    fireObjectsRemoved(ProtocolTransition.NAMESPACE, ProtocolTransition.NAME, old);
256 	}
257 	if (transitions.length > 0) {
258 	    StructureUtils.setParent(transitions, this);
259 	    fireObjectsAdded(ProtocolTransition.NAMESPACE, ProtocolTransition.NAME, transitions);
260 	}
261     }
262 
263     /***
264        This method is only to be used by the protocol.        
265      */
266     public void __setPlaces(ProtocolPlace[] places) {
267 	Place[] old = _delegate.getPlaces();
268 	_delegate.setPlaces(places);
269 	if (old.length > 0) {
270 	    StructureUtils.setParent(old, null);
271 	    fireObjectsRemoved(ProtocolPlace.NAMESPACE, ProtocolPlace.NAME, old);
272 	}
273 	if (places.length > 0) {
274 	    StructureUtils.setParent(places, this);
275 	    fireObjectsAdded(ProtocolPlace.NAMESPACE, ProtocolPlace.NAME, places);
276 	}
277     }
278 
279     ///
280     /// Get API
281     /// ....................................................................................................
282 
283     public String getID() {
284 	return _delegate.getID();
285     }
286 
287     public String getDescription() {
288 	return _delegate.getDescription();
289     }
290 
291     public GenericProperties getProperties() {
292 	return _delegate.getProperties();
293     }
294 
295     public String[] getPlaceIDs() {
296 	return _delegate.getPlaceIDs();
297     }
298 
299     public Place getPlace(String id) {
300 	return _delegate.getPlace(id);
301     }
302 
303     public int getPlaceIndex(String id) {
304 	return _delegate.getPlaceIndex(id);
305     }
306 
307     public Place getPlace(int i) {
308 	return _delegate.getPlace(i);
309     }
310 
311     public Place[] getPlaces() {
312 	return _delegate.getPlaces();
313     }
314 
315     public String[] getTransitionIDs() {
316 	return _delegate.getTransitionIDs();
317     }
318 
319     public Transition getTransition(String id) {
320 	return _delegate.getTransition(id);
321     }
322 
323     public int getTransitionIndex(String id) {
324 	return _delegate.getTransitionIndex(id);
325     }
326 
327     public Transition getTransition(int i) {
328 	return _delegate.getTransition(i);
329     }
330 
331     public Transition[] getTransitions() {
332 	return _delegate.getTransitions();
333     }
334 
335     public Transition[] getEnabledTransitions() {
336 	return _delegate.getEnabledTransitions();
337     }
338 
339     public int placeCount() {
340 	return _delegate.placeCount();
341     }
342 
343     public int transitionCount() {
344 	return _delegate.transitionCount();
345     }
346 }