1 | package de.uka.ipd.sdq.pcmsolver.transformations.pcm2lqn; |
2 | |
3 | import java.io.File; |
4 | import java.io.FileInputStream; |
5 | import java.io.FileNotFoundException; |
6 | import java.io.FileOutputStream; |
7 | import java.io.IOException; |
8 | import java.util.Collections; |
9 | |
10 | import org.apache.log4j.Logger; |
11 | import org.eclipse.emf.common.util.URI; |
12 | import org.eclipse.emf.ecore.resource.Resource; |
13 | import org.eclipse.emf.ecore.resource.ResourceSet; |
14 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
15 | import org.eclipse.emf.ecore.xmi.XMIResource; |
16 | import org.eclipse.emf.ecore.xmi.XMLResource; |
17 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; |
18 | |
19 | import LqnCore.LqnModelType; |
20 | |
21 | public class LqnXmlHandler { |
22 | |
23 | private static Logger logger = Logger.getLogger(LqnXmlHandler.class.getName()); |
24 | |
25 | LqnModelType lqnModel; |
26 | |
27 | public LqnXmlHandler(LqnModelType anLqnModel) { |
28 | lqnModel = anLqnModel; |
29 | } |
30 | |
31 | public void saveModelToXMI(String fileName) { |
32 | // Create a resource set. |
33 | ResourceSet resourceSet = new ResourceSetImpl(); |
34 | |
35 | // Register the default resource factory -- only needed for stand-alone! |
36 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() |
37 | .put(Resource.Factory.Registry.DEFAULT_EXTENSION, |
38 | new XMIResourceFactoryImpl()); |
39 | |
40 | URI fileURI = URI.createFileURI(new File(fileName).getAbsolutePath()); |
41 | |
42 | //Resource resource = resourceSet.createResource(fileURI); |
43 | XMIResource resource = (XMIResource) resourceSet.createResource(fileURI); |
44 | resource.getContents().add(lqnModel); |
45 | |
46 | //resource.getDefaultSaveOptions().put(XMLResource.OPTION_KEEP_DEFAULT_CONTENT, true); |
47 | |
48 | try { |
49 | resource.save(Collections.EMPTY_MAP); |
50 | } catch (IOException e) { |
51 | logger.error(e.getMessage()); |
52 | } |
53 | |
54 | fixXMLFile(fileName); |
55 | |
56 | } |
57 | |
58 | /** |
59 | * Restores the corresponding Ecore model, previously serialized via |
60 | * {@link #saveModelToXMI(String)}. |
61 | * |
62 | * @param fileName |
63 | * @return A representation of the model object 'LQN Model Type'; null if |
64 | * the file don't exists or when there were problems reading the |
65 | * file. |
66 | */ |
67 | public static LqnModelType loadModelFromXMI(String fileName) { |
68 | // Revert the Ecore tag names |
69 | revertXMLFile(fileName); |
70 | |
71 | // Create a resource set. |
72 | ResourceSet resourceSet = new ResourceSetImpl(); |
73 | |
74 | // Register the default resource factory -- only needed for stand-alone! |
75 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() |
76 | .put(Resource.Factory.Registry.DEFAULT_EXTENSION, |
77 | new XMIResourceFactoryImpl()); |
78 | |
79 | URI fileURI = URI.createFileURI(new File(fileName).getAbsolutePath()); |
80 | |
81 | Resource resource = resourceSet.createResource(fileURI); |
82 | |
83 | LqnModelType lqnModel = null; |
84 | try { |
85 | resource.load(Collections.EMPTY_MAP); |
86 | lqnModel = (LqnModelType) resource.getContents().get(0); |
87 | } catch (IOException e) { |
88 | logger.error(e.getMessage()); |
89 | } |
90 | |
91 | // Can be null if there were problems retrieving the model |
92 | return lqnModel; |
93 | } |
94 | |
95 | /** |
96 | * Changes the XML-tag names to their original names. They had |
97 | * formerly been changed by the LQN-to-Ecore mapping. |
98 | * |
99 | * @param filename |
100 | */ |
101 | private void fixXMLFile(String filename) { |
102 | FileInputStream fis = null; |
103 | byte b[]= null; |
104 | try { |
105 | fis = new FileInputStream(filename); |
106 | int x = 0; |
107 | x = fis.available(); |
108 | b= new byte[x]; |
109 | fis.read(b); |
110 | fis.close(); |
111 | } catch (FileNotFoundException e) { |
112 | e.printStackTrace(); |
113 | } catch (IOException e) { |
114 | e.printStackTrace(); |
115 | } |
116 | |
117 | //TODO: remove hard coded path to xsd file. |
118 | String content = new String(b); |
119 | content = content.replaceAll("LqnModelType", "lqn-model"); |
120 | |
121 | // String lqnDir = System.getenv("LQNDIR"); |
122 | // if (lqnDir != null && lqnDir !=""){ |
123 | // |
124 | // lqnDir = lqnDir.replaceAll("\\\\", "/"); |
125 | // lqnDir = lqnDir.replaceAll(" ", "%20"); |
126 | // content = content.replaceAll("xmlns=\"file:/C:/Program%20Files/LQN%20Solvers/lqn-core.xsd\"", "xsi:noNamespaceSchemaLocation=\"file:///"+lqnDir+"lqn.xsd\""); |
127 | // } else { |
128 | // content = content.replaceAll("xmlns=\"file:/C:/Program%20Files/LQN%20Solvers/lqn-core.xsd\"", "xsi:noNamespaceSchemaLocation=\"file:///C:/Program Files/LQN Solvers/lqn.xsd\""); |
129 | // } |
130 | |
131 | content = content.replaceAll("xmlns=\"file:/C:/Program%20Files/LQN%20Solvers/lqn-core.xsd\"", "xsi:noNamespaceSchemaLocation=\"file:/C:/Program Files/LQN Solvers/lqn.xsd\""); |
132 | content = content.replaceAll("xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\"", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""); |
133 | |
134 | content = content.replaceAll("entryActivityGraph", "entry-activity-graph"); |
135 | content = content.replaceAll("thinkTime", "think-time"); |
136 | |
137 | content = content.replaceAll("entryPhaseActivities", "entry-phase-activities"); |
138 | content = content.replaceAll("solverParams", "solver-params"); |
139 | content = content.replaceAll("synchCall", "synch-call"); |
140 | content = content.replaceAll("convVal", "conv_val"); |
141 | content = content.replaceAll("itLimit", "it_limit"); |
142 | content = content.replaceAll("printInt", "print_int"); |
143 | content = content.replaceAll("underrelaxCoeff", "underrelax_coeff"); |
144 | content = content.replaceAll("callOrder", "call-order"); |
145 | content = content.replaceAll("hostDemandMean", "host-demand-mean"); |
146 | content = content.replaceAll("hostDemandCvsq", "host-demand-cvsq"); |
147 | content = content.replaceAll("callsMean", "calls-mean"); |
148 | content = content.replaceAll("replyActivity", "reply-activity"); |
149 | content = content.replaceAll("postOR", "post-OR"); |
150 | content = content.replaceAll("postAND", "post-AND"); |
151 | content = content.replaceAll("preOR", "pre-OR"); |
152 | content = content.replaceAll("preAND", "pre-AND"); |
153 | content = content.replaceAll("taskActivities", "task-activities"); |
154 | content = content.replaceAll("boundToEntry", "bound-to-entry"); |
155 | content = content.replaceAll("replyEntry", "reply-entry"); |
156 | content = content.replaceAll("activityGraph", "activity-graph"); |
157 | content = content.replaceAll("speedFactor", "speed-factor"); |
158 | content = content.replaceAll("serviceTimeDistribution", "service-time-distribution"); |
159 | content = content.replaceAll("openArrivalRate", "open-arrival-rate"); |
160 | content = content.replaceAll("openWaitTime", "open-wait-time"); |
161 | content = content.replaceAll("binSize", "bin-size"); |
162 | content = content.replaceAll("midPoint", "mid-point"); |
163 | content = content.replaceAll("numberBins", "number-bins"); |
164 | content = content.replaceAll("stdDev", "std-dev"); |
165 | content = content.replaceAll("histogramBin", "histogram-bin"); |
166 | content = content.replaceAll("overflowBin", "overflow-bin"); |
167 | content = content.replaceAll("waitingVariance", "waiting-variance"); |
168 | content = content.replaceAll("phase1ProcWaiting", "phase1-procWaiting"); |
169 | content = content.replaceAll("phase1ServiceTime", "phase1-serviceTime"); |
170 | content = content.replaceAll("phase1ServiceTimeVariance", "phase1-serviceTime"); |
171 | content = content.replaceAll("resultJoinDelay", "result-join-delay"); |
172 | |
173 | FileOutputStream fos; |
174 | try { |
175 | fos = new FileOutputStream(filename); |
176 | fos.write(content.getBytes()); |
177 | fos.close(); |
178 | } catch (FileNotFoundException e) { |
179 | e.printStackTrace(); |
180 | } catch (IOException e) { |
181 | e.printStackTrace(); |
182 | } |
183 | |
184 | } |
185 | |
186 | /** |
187 | * Revert the previously changed XML-tag names to their corresponding Ecore |
188 | * names. The renaming is required before the model can be restored from an |
189 | * XML file. |
190 | * |
191 | * @param filename |
192 | */ |
193 | private static void revertXMLFile(String filename) { |
194 | FileInputStream fis = null; |
195 | byte b[]= null; |
196 | try { |
197 | fis = new FileInputStream(filename); |
198 | int x = 0; |
199 | x = fis.available(); |
200 | b= new byte[x]; |
201 | fis.read(b); |
202 | fis.close(); |
203 | } catch (FileNotFoundException e) { |
204 | e.printStackTrace(); |
205 | } catch (IOException e) { |
206 | e.printStackTrace(); |
207 | } |
208 | |
209 | String content = new String(b); |
210 | content = content.replaceAll("lqn-model", "LqnModelType"); |
211 | |
212 | // String lqnDir = System.getenv("LQNDIR"); |
213 | // if (lqnDir != null && lqnDir !=""){ |
214 | // lqnDir = lqnDir.replaceAll("\\\\", "/"); |
215 | // //lqnDir = lqnDir.replaceAll(" ", "%20"); |
216 | // content = content.replaceAll("xsi:noNamespaceSchemaLocation=\"file:///C:/Program Files/LQN Solvers/lqn.xsd\"", "xmlns=\"file:///"+lqnDir+"lqn-core.xsd\""); |
217 | // } else { |
218 | // content = content.replaceAll("xsi:noNamespaceSchemaLocation=\"file:///C:/Program Files/LQN Solvers/lqn.xsd\"", "xmlns=\"file:///C:/Program%20Files/LQN%20Solvers/lqn-core.xsd\""); |
219 | // } |
220 | content = content.replaceAll("xsi:noNamespaceSchemaLocation=\"file:///C:/Program Files/LQN Solvers/lqn.xsd\"", "xmlns=\"file:/C:/Program%20Files/LQN%20Solvers/lqn-core.xsd\""); |
221 | |
222 | |
223 | content = content.replaceAll("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\""); |
224 | |
225 | content = content.replaceAll("entry-activity-graph", "entryActivityGraph"); |
226 | content = content.replaceAll("think-time", "thinkTime"); |
227 | |
228 | content = content.replaceAll("entry-phase-activities", "entryPhaseActivities"); |
229 | content = content.replaceAll("solver-params", "solverParams"); |
230 | content = content.replaceAll("synch-call", "synchCall"); |
231 | content = content.replaceAll("conv_val", "convVal"); |
232 | content = content.replaceAll("it_limit", "itLimit"); |
233 | content = content.replaceAll("print_int", "printInt"); |
234 | content = content.replaceAll("underrelax_coeff", "underrelaxCoeff"); |
235 | content = content.replaceAll("host-demand-mean", "hostDemandMean"); |
236 | content = content.replaceAll("host-demand-cvsq", "hostDemandCvsq"); |
237 | content = content.replaceAll("calls-mean", "callsMean"); |
238 | content = content.replaceAll("reply-activity", "replyActivity"); |
239 | content = content.replaceAll("post-OR", "postOR"); |
240 | content = content.replaceAll("pre-OR", "preOR"); |
241 | content = content.replaceAll("postAND", "post-AND"); |
242 | content = content.replaceAll("preAND", "pre-AND"); |
243 | |
244 | content = content.replaceAll("task-activities", "taskActivities"); |
245 | content = content.replaceAll("bound-to-entry", "boundToEntry"); |
246 | content = content.replaceAll("reply-entry", "replyEntry"); |
247 | content = content.replaceAll("activity-graph", "activityGraph"); |
248 | content = content.replaceAll("speed-factor", "speedFactor"); |
249 | content = content.replaceAll("service-time-distribution", "serviceTimeDistribution"); |
250 | content = content.replaceAll("open-arrival-rate", "openArrivalRate"); |
251 | |
252 | // the subsequent replacement rules are relevant only for result files |
253 | content = content.replaceAll("result-general", "resultGeneral"); |
254 | content = content.replaceAll("result-join-delay", "resultJoinDelay"); |
255 | content = content.replaceAll("join-variance", "joinVariance"); |
256 | content = content.replaceAll("join-waiting", "joinWaiting"); |
257 | content = content.replaceAll("conv-val", "convVal"); |
258 | content = content.replaceAll("elapsed-time", "elapsedTime"); |
259 | content = content.replaceAll("solver-info", "solverInfo"); |
260 | content = content.replaceAll("result-task", "resultTask"); |
261 | content = content.replaceAll("phase1-utilization", "phase1Utilization"); |
262 | content = content.replaceAll("proc-utilization", "procUtilization"); |
263 | content = content.replaceAll("result-entry", "resultEntry"); |
264 | content = content.replaceAll("squared-coeff-variation", "squaredCoeffVariation"); |
265 | content = content.replaceAll("throughput-bound", "throughputBound"); |
266 | content = content.replaceAll("result-activity", "resultActivity"); |
267 | content = content.replaceAll("proc-waiting", "procWaiting"); |
268 | // As "service-time" is a prefix of "service-time-variance", a following "=" is needed |
269 | content = content.replaceAll("service-time=", "serviceTime="); |
270 | content = content.replaceAll("service-time-variance", "serviceTimeVariance"); |
271 | content = content.replaceAll("result-call", "resultCall"); |
272 | content = content.replaceAll("result-processor", "resultProcessor"); |
273 | content = content.replaceAll("open-wait-time", "openWaitTime"); |
274 | content = content.replaceAll("bin-size", "binSize"); |
275 | content = content.replaceAll("mid-point", "midPoint"); |
276 | content = content.replaceAll("number-bins", "numberBins"); |
277 | content = content.replaceAll("std-dev", "stdDev"); |
278 | content = content.replaceAll("histogram-bin", "histogramBin"); |
279 | content = content.replaceAll("overflow-bin", "overflowBin"); |
280 | content = content.replaceAll("waiting-variance", "waitingVariance"); |
281 | content = content.replaceAll("phase1-procWaiting", "phase1ProcWaiting"); |
282 | content = content.replaceAll("phase1-serviceTime", "phase1ServiceTime"); |
283 | content = content.replaceAll("phase1-serviceTime", "phase1ServiceTimeVariance"); |
284 | |
285 | FileOutputStream fos; |
286 | try { |
287 | fos = new FileOutputStream(filename); |
288 | fos.write(content.getBytes()); |
289 | fos.close(); |
290 | } catch (FileNotFoundException e) { |
291 | e.printStackTrace(); |
292 | } catch (IOException e) { |
293 | e.printStackTrace(); |
294 | } |
295 | } |
296 | |
297 | } |