1   /*
2    * $Id: WeatherForecast.java 1309 2009-05-22 18:56:38Z andreas.hoheisel@first.fraunhofer.de $
3    *
4    * Copyright (c) 2005, The K-Wf Grid Consortium
5    * Fraunhofer Institute for Computer Architecture and Software Technology
6    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
7    */
8   
9   package net.kwfgrid.gworkflowdl;
10  
11  import junit.framework.Assert;
12  import junit.framework.Test;
13  import junit.framework.TestSuite;
14  import net.kwfgrid.gworkflowdl.structure.*;
15  
16  import java.io.IOException;
17  
18  /***
19   * a "realistic" looking createDataToken for grid workflow.
20   */
21  final class Example {
22  
23      private Example() {
24      }
25  
26      public static Workflow make() throws WorkflowFormatException {
27  
28          //assert false : "Hilfe";
29          //-- places ---------------------------------------
30          Place begin = Factory.newPlace();
31          begin.setID("begin");
32          /*
33          Token mm5i = Factory.newToken();
34          Element txt0 = new Element("txt0");
35          txt0.setText("grigftp:first.fraunhofer.de/data/mm5input.dat");
36          mm5i.set(txt0);
37          */
38  
39          Token mm5j = TokenTest.createDataToken("grigftp:first.fraunhofer.de/lotto/mm5input.dat");
40  
41          boolean test = true;
42          try {
43              //begin.addToken(mm5i);
44              begin.addToken(mm5j);
45              ;
46          } catch (CapacityException e) {
47              test = false;
48          }
49          Assert.assertTrue(test);
50          //---
51          Place weatherModelOutput = Factory.newPlace();
52  
53          try {
54              //weatherModelOutput.addToken(mm5i);
55              weatherModelOutput.setCapacity(4);
56              ;
57          } catch (CapacityException e) {
58              test = false;
59          }
60  
61          weatherModelOutput.setID("weatherModelOutput");
62          GenericProperties gp = Factory.newProperties();
63          gp.put("key1", "val1");
64          gp.put("abrakadabra", "simsalabim");
65          weatherModelOutput.setProperties(gp);
66          //---
67          Place end = Factory.newPlace();
68          end.setID("end");
69          //end.addToken(Factory.newToken());
70  
71          //--  WSOperations -------------------------------
72          OperationCandidate mm5_first = Factory.newOperationCandidate();
73          mm5_first.setType("gs");
74          //mm5_first.setName("mm5@first");
75          //mm5_first.setOwl("http://fhrg.first.fraunhofer.de/ws/mm5/calc");
76          mm5_first.setSelected(true);
77          //---
78          OperationCandidate mm5_cyfronet = Factory.newOperationCandidate();
79          mm5_cyfronet.setType("gs");
80          //mm5_cyfronet.setName("mm5@cyfronet");
81          //mm5_cyfronet.setOwl("http://agh.edu.pl/ws/mm5/calc");
82          //---
83          OperationCandidate mm5_savba = Factory.newOperationCandidate();
84          mm5_savba.setType("gs");
85          //mm5_savba.setName("mm5@savba");
86          //mm5_savba.setOwl("http://savba.sk/ws/mm5/calc");
87          mm5_savba.setResourceName("myWsdl");
88          //---
89          OperationCandidate[] wsops = {mm5_first, mm5_cyfronet, mm5_savba};
90  
91          //-- OperationClass -------------------------------
92          OperationClass mm5 = Factory.newOperationClass();
93          //mm5.setName("MM5");
94          //mm5.setOwl("mm5.xml");
95          mm5.setOperationCandidates(wsops);
96  
97          //-- Operations ---------------------------------------
98          Operation weatherModel = Factory.newOperation();
99          //weatherModel.setName("weatherModel");
100         //weatherModel.setOwl("weatherModel.xml");
101         weatherModel.set(mm5);
102 
103         //---
104         Operation visualization = Factory.newOperation();
105         //visualization.setName("weatherVisualization");
106         //visualization.setOwl("weatherVisualization.xml");
107 
108         OperationCandidate pex0 = Factory.newOperationCandidate();
109         //pex0.setOwl("Hansi");
110         pex0.setType("commandLine");
111         pex0.setResourceName("cyrano");
112         pex0.setOperationName("Mac OS X");
113         pex0.setQuality(0.9f);
114         pex0.setSelected(true);
115 
116         OperationCandidate pex1 = Factory.newOperationCandidate();
117         //pex0.setOwl("Hansi");
118         pex1.setType("ws");
119         pex1.setResourceName("bb");
120         pex1.setOperationName("linux");
121         pex1.setQuality(0.8f);
122 
123         OperationClass pcex = Factory.newOperationClass();
124         pcex.setName("Hansi and Klausi software");
125         //String[] owls = {"hansi", "klausi"};
126         //pcex.setOwls(owls);
127         OperationCandidate[] pexs = {pex0, pex1};
128         pcex.setOperationCandidates(pexs);
129 
130         visualization.set(pcex);
131 
132         //-- transition weather model -------------------------------------
133         Transition weatherModelRun = Factory.newTransition();
134         weatherModelRun.setID("weatherModel");
135         weatherModelRun.setDescription("fine weather model");
136         weatherModelRun.setOperation(weatherModel);
137         weatherModelRun.addCondition("4711 == 4711");
138         weatherModelRun.addCondition("1 != 2");
139 
140         Edge wine = Factory.newEdge();
141         wine.setPlace(begin);
142         wine.setExpression("input");
143         weatherModelRun.addInEdge(wine);
144 
145         Edge woute = Factory.newEdge();
146         woute.setPlace(weatherModelOutput);
147         woute.setExpression("output");
148         weatherModelRun.addOutEdge(woute);
149 
150         //-- transition visualisation -------------------------------------
151         Transition visualizationRun = Factory.newTransition();
152         visualizationRun.getProperties().put("today", "tomorrow");
153         visualizationRun.setID("visualisation");
154         visualizationRun.setDescription("Visualisation of weather");
155         visualizationRun.setOperation(visualization);
156 
157         Edge vine = Factory.newEdge();
158         vine.setPlace(weatherModelOutput);
159         vine.setExpression("input");
160         visualizationRun.addInEdge(vine);
161 
162 
163         Edge voute = Factory.newEdge();
164         voute.setPlace(end);
165         voute.setExpression("output");
166         visualizationRun.addOutEdge(voute);
167 
168 
169         //-- workflow ----------------------------------
170         Workflow wf = Factory.newWorkflow();
171         String[] os = new String[2];
172         os[0] = "anton";
173         os[1] = "berta";
174         //wf.setOwls(os);
175         wf.addPlace(begin);
176         wf.addPlace(weatherModelOutput);
177         wf.addPlace(end);
178         wf.addTransition(weatherModelRun);
179         wf.addTransition(visualizationRun);
180         wf.getProperties().put("gestern", "vorgestern und vorvorgestern");
181         wf.getProperties().put("heute", "morgen");
182 
183         /*
184         ContactFree.makeContactFree(wf);
185 
186         Place p = wf.getPlace("weatherModelOutput");
187         try {
188             p.setCapacity(3);
189         } catch (CapacityException e) {
190             System.out.println(e);
191         }
192         ContactFree.makeContactFree(wf);
193 
194         p = wf.getPlace("weatherModelOutput");
195         try {
196             p.setCapacity(2);
197         } catch (CapacityException e) {
198             System.out.println(e);
199         }
200         ContactFree.makeContactFree(wf);
201         */
202         return wf;
203     }
204 }
205 
206 public final class WeatherForecast extends AbstractTestCase {
207 
208     /***
209      * Create the test case
210      *
211      * @param testName name of the test case
212      */
213     public WeatherForecast(String testName) {
214         super(testName);
215         System.setProperty("gworkflowdl.xml.validation","true");
216     }
217 
218     /***
219      * @return the suite of tests being tested
220      */
221     public static Test suite() {
222         return new TestSuite(WeatherForecast.class);
223     }
224 
225     /***
226      * Rigourous Test :-)
227      */
228     public void testApp() throws WorkflowFormatException {
229         // Crash and burn!
230         Workflow wf1 = Example.make();
231         boolean test = true;
232 
233         String s1 = null;
234         try {
235             s1 = JdomString.workflow2string(wf1);
236         } catch (IOException e) {
237             test = false;
238         }
239         Assert.assertTrue(test);
240 
241         System.out.println(s1);
242 
243         /*
244         try {
245             JdomString.string2verifyWorkflowDoc(s1);
246         } catch (JDOMException e) {
247             e.printStackTrace();
248         } catch (IOException e) {
249             e.printStackTrace();
250         }
251         */
252 
253         Workflow wf2 = null;
254         try {
255             wf2 = JdomString.string2workflow(s1);
256             /*
257             } catch (JDOMException e) {
258                 System.out.println("Hurra 1 " + e);
259                 test = false;
260             } catch (IOException e) {
261                    System.out.println("Hurra 2");
262                 test = false;
263                 */
264 
265         } catch (CapacityException e) {
266             test = false;
267 
268         } catch (WorkflowFormatException e) {
269 	    e.printStackTrace();
270             test = false;
271         }
272         Assert.assertTrue(test);
273 
274         String s2 = null;
275         //wf2 = wf2.deepCopy();
276         try {
277             s2 = JdomString.workflow2string(wf2);
278         } catch (IOException e) {
279             test = false;
280         }
281         System.out.println(s2);
282 
283         Assert.assertTrue(test);
284         Assert.assertEquals("workflow xml",s1,s2);
285 
286     }
287 }