1 | package de.uka.ipd.sdq.pcmsolver.tests; |
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.math.BigInteger; |
9 | import java.util.Collections; |
10 | |
11 | import junit.framework.JUnit4TestAdapter; |
12 | |
13 | import org.apache.log4j.Logger; |
14 | import org.eclipse.emf.common.util.URI; |
15 | import org.eclipse.emf.ecore.EObject; |
16 | import org.eclipse.emf.ecore.resource.Resource; |
17 | import org.eclipse.emf.ecore.resource.ResourceSet; |
18 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
19 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; |
20 | import org.junit.Before; |
21 | import org.junit.Test; |
22 | |
23 | import LqnCore.ActivityMakingCallType; |
24 | import LqnCore.ActivityPhasesType; |
25 | import LqnCore.EntryType; |
26 | import LqnCore.LqnCoreFactory; |
27 | import LqnCore.LqnModelType; |
28 | import LqnCore.PhaseActivities; |
29 | import LqnCore.ProcessorType; |
30 | import LqnCore.SchedulingType; |
31 | import LqnCore.SolverParamsType; |
32 | import LqnCore.TaskSchedulingType; |
33 | import LqnCore.TaskType; |
34 | import LqnCore.TypeType; |
35 | |
36 | |
37 | public class LQNTest { |
38 | |
39 | private static Logger logger = Logger.getLogger(LQNTest.class.getName()); |
40 | private static final String FILENAME_INPUT = "C:\\Temp\\test.xml"; |
41 | private static final String FILENAME_RESULT = "C:\\Temp\\test.out"; |
42 | private static final String FILENAME_LQN = "C:\\Temp\\test.lqn";; |
43 | |
44 | |
45 | |
46 | @Before |
47 | public void setUp(){ |
48 | |
49 | |
50 | } |
51 | |
52 | @Test |
53 | public void handle(){ |
54 | |
55 | LqnCoreFactory fac = LqnCoreFactory.eINSTANCE; |
56 | LqnModelType lmt = fac.createLqnModelType(); |
57 | lmt.setName("cmpdesign"); |
58 | |
59 | SolverParamsType spt = fac.createSolverParamsType(); |
60 | spt.setComment("Design Comparison Heiko"); |
61 | spt.setConvVal("1e-005"); |
62 | spt.setItLimit(50); |
63 | spt.setPrintInt(10); |
64 | spt.setUnderrelaxCoeff("0.5"); |
65 | lmt.setSolverParams(spt); |
66 | |
67 | getProcessor1(fac, lmt); |
68 | getProcessor2(fac, lmt); |
69 | getProcessor3(fac, lmt); |
70 | |
71 | saveToXMIFile(lmt, FILENAME_INPUT); |
72 | fixFile(FILENAME_INPUT); |
73 | runLqnTools(); |
74 | |
75 | printResultToConsole(); |
76 | |
77 | } |
78 | |
79 | |
80 | private void printResultToConsole() { |
81 | FileInputStream fis = null; |
82 | byte b[]= null; |
83 | try { |
84 | fis = new FileInputStream(FILENAME_RESULT); |
85 | int x = 0; |
86 | x = fis.available(); |
87 | b= new byte[x]; |
88 | fis.read(b); |
89 | fis.close(); |
90 | } catch (FileNotFoundException e) { |
91 | e.printStackTrace(); |
92 | } catch (IOException e) { |
93 | e.printStackTrace(); |
94 | } |
95 | |
96 | String content = new String(b); |
97 | System.out.println(content); |
98 | } |
99 | |
100 | private void runLqnTools() { |
101 | try { |
102 | Runtime.getRuntime().exec( "lqn2xml -o"+FILENAME_LQN+" -Olqn "+FILENAME_INPUT ); |
103 | Thread.sleep(500); |
104 | Runtime.getRuntime().exec( "lqns -o"+FILENAME_RESULT+" "+FILENAME_LQN ); |
105 | |
106 | } catch (IOException e) { |
107 | e.printStackTrace(); |
108 | } catch (InterruptedException e) { |
109 | e.printStackTrace(); |
110 | } |
111 | } |
112 | |
113 | private void fixFile(String filename) { |
114 | FileInputStream fis = null; |
115 | byte b[]= null; |
116 | try { |
117 | fis = new FileInputStream(filename); |
118 | int x = 0; |
119 | x = fis.available(); |
120 | b= new byte[x]; |
121 | fis.read(b); |
122 | fis.close(); |
123 | } catch (FileNotFoundException e) { |
124 | e.printStackTrace(); |
125 | } catch (IOException e) { |
126 | e.printStackTrace(); |
127 | } |
128 | |
129 | String content = new String(b); |
130 | content = content.replaceAll("LqnModelType", "lqn-model"); |
131 | content = content.replaceAll("entryPhaseActivities", "entry-phase-activities"); |
132 | content = content.replaceAll("solverParams", "solver-params"); |
133 | content = content.replaceAll("synchCall", "synch-call"); |
134 | content = content.replaceAll("convVal", "conv_val"); |
135 | content = content.replaceAll("itLimit", "it_limit"); |
136 | content = content.replaceAll("printInt", "print_int"); |
137 | content = content.replaceAll("underrelaxCoeff", "underrelax_coeff"); |
138 | content = content.replaceAll("hostDemandMean", "host-demand-mean"); |
139 | content = content.replaceAll("callsMean", "calls-mean"); |
140 | content = content.replaceAll("xmlns=\"file:/C:/Program%20Files/LQN%20Solvers/lqn-core.xsd\"", "xsi:noNamespaceSchemaLocation=\"file:///C:/Program Files/LQN Solvers/lqn.xsd\""); |
141 | content = content.replaceAll("xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\"", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""); |
142 | |
143 | |
144 | FileOutputStream fos; |
145 | try { |
146 | fos = new FileOutputStream(filename); |
147 | fos.write(content.getBytes()); |
148 | fos.close(); |
149 | } catch (FileNotFoundException e) { |
150 | e.printStackTrace(); |
151 | } catch (IOException e) { |
152 | e.printStackTrace(); |
153 | } |
154 | |
155 | } |
156 | |
157 | private void getProcessor3(LqnCoreFactory fac, LqnModelType lmt) { |
158 | |
159 | ProcessorType pt = fac.createProcessorType(); |
160 | pt.setName("FileServer"); |
161 | pt.setScheduling(SchedulingType.FCFS); |
162 | lmt.getProcessor().add(pt); |
163 | |
164 | TaskType tt = fac.createTaskType(); |
165 | tt.setName("FileServer"); |
166 | tt.setScheduling(TaskSchedulingType.FCFS); |
167 | pt.getTask().add(tt); |
168 | |
169 | EntryType et = fac.createEntryType(); |
170 | et.setName("FileServer_Screen"); |
171 | et.setType(TypeType.PH1PH2); |
172 | tt.getEntry().add(et); |
173 | |
174 | PhaseActivities pa = fac.createPhaseActivities(); |
175 | et.setEntryPhaseActivities(pa); |
176 | |
177 | ActivityPhasesType apt = fac.createActivityPhasesType(); |
178 | apt.setName("FileServer_Screen_ph1"); |
179 | apt.setPhase(new BigInteger("1")); |
180 | apt.setHostDemandMean("0.4"); |
181 | pa.getActivity().add(apt); |
182 | |
183 | ActivityPhasesType apt2 = fac.createActivityPhasesType(); |
184 | apt2.setName("FileServer_Screen_ph2"); |
185 | apt2.setPhase(new BigInteger("2")); |
186 | apt2.setHostDemandMean("0.1"); |
187 | pa.getActivity().add(apt2); |
188 | |
189 | |
190 | EntryType et2 = fac.createEntryType(); |
191 | et2.setName("FileServer_Data"); |
192 | et2.setType(TypeType.PH1PH2); |
193 | tt.getEntry().add(et2); |
194 | |
195 | PhaseActivities pa2 = fac.createPhaseActivities(); |
196 | et2.setEntryPhaseActivities(pa2); |
197 | |
198 | ActivityPhasesType apt3 = fac.createActivityPhasesType(); |
199 | apt3.setName("FileServer_Data_ph1"); |
200 | apt3.setPhase(new BigInteger("1")); |
201 | apt3.setHostDemandMean("0.4"); |
202 | pa2.getActivity().add(apt3); |
203 | |
204 | ActivityPhasesType apt4 = fac.createActivityPhasesType(); |
205 | apt4.setName("FileServer_Data_ph1"); |
206 | apt4.setPhase(new BigInteger("2")); |
207 | apt4.setHostDemandMean("0.1"); |
208 | pa2.getActivity().add(apt4); |
209 | } |
210 | |
211 | |
212 | |
213 | |
214 | private void getProcessor2(LqnCoreFactory fac, LqnModelType lmt) { |
215 | ProcessorType pt2 = fac.createProcessorType(); |
216 | pt2.setName("Application"); |
217 | pt2.setScheduling(SchedulingType.FCFS); |
218 | lmt.getProcessor().add(pt2); |
219 | |
220 | TaskType tt2 = fac.createTaskType(); |
221 | tt2.setName("Application"); |
222 | tt2.setScheduling(TaskSchedulingType.FCFS); |
223 | pt2.getTask().add(tt2); |
224 | |
225 | EntryType et2 = fac.createEntryType(); |
226 | et2.setName("Application_Rqst"); |
227 | et2.setType(TypeType.PH1PH2); |
228 | tt2.getEntry().add(et2); |
229 | |
230 | PhaseActivities pa2 = fac.createPhaseActivities(); |
231 | et2.setEntryPhaseActivities(pa2); |
232 | |
233 | ActivityPhasesType apt2 = fac.createActivityPhasesType(); |
234 | apt2.setName("Application_Rqst_ph1"); |
235 | apt2.setPhase(new BigInteger("1")); |
236 | apt2.setHostDemandMean("0.7"); |
237 | pa2.getActivity().add(apt2); |
238 | |
239 | ActivityMakingCallType amct3 = fac.createActivityMakingCallType(); |
240 | amct3.setCallsMean("1"); |
241 | amct3.setDest("FileServer_Data"); |
242 | apt2.getSynchCall().add(amct3); |
243 | |
244 | ActivityPhasesType apt3 = fac.createActivityPhasesType(); |
245 | apt3.setName("Application_Rqst_ph2"); |
246 | apt3.setPhase(new BigInteger("2")); |
247 | apt3.setHostDemandMean("0.3"); |
248 | pa2.getActivity().add(apt3); |
249 | } |
250 | |
251 | private void getProcessor1(LqnCoreFactory fac, LqnModelType lmt) { |
252 | ProcessorType pt = fac.createProcessorType(); |
253 | pt.setName("Clients"); |
254 | pt.setMultiplicity(new BigInteger("0")); |
255 | pt.setScheduling(SchedulingType.FCFS); |
256 | lmt.getProcessor().add(pt); |
257 | |
258 | TaskType tt = fac.createTaskType(); |
259 | tt.setName("Clients"); |
260 | tt.setMultiplicity(new BigInteger("4")); |
261 | tt.setScheduling(TaskSchedulingType.REF); |
262 | pt.getTask().add(tt); |
263 | |
264 | EntryType et = fac.createEntryType(); |
265 | et.setName("Clients"); |
266 | et.setType(TypeType.PH1PH2); |
267 | tt.getEntry().add(et); |
268 | |
269 | PhaseActivities pa = fac.createPhaseActivities(); |
270 | et.setEntryPhaseActivities(pa); |
271 | |
272 | ActivityPhasesType apt = fac.createActivityPhasesType(); |
273 | apt.setName("Clients_ph2"); |
274 | apt.setPhase(new BigInteger("2")); |
275 | apt.setHostDemandMean("10"); |
276 | pa.getActivity().add(apt); |
277 | |
278 | ActivityMakingCallType amct = fac.createActivityMakingCallType(); |
279 | amct.setCallsMean("5"); |
280 | amct.setDest("FileServer_Screen"); |
281 | apt.getSynchCall().add(amct); |
282 | |
283 | ActivityMakingCallType amct2 = fac.createActivityMakingCallType(); |
284 | amct2.setCallsMean("2"); |
285 | amct2.setDest("Application_Rqst"); |
286 | apt.getSynchCall().add(amct2); |
287 | } |
288 | |
289 | |
290 | private void saveToXMIFile(EObject modelToSave, String fileName) { |
291 | // Create a resource set. |
292 | ResourceSet resourceSet = new ResourceSetImpl(); |
293 | |
294 | // Register the default resource factory -- only needed for stand-alone! |
295 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() |
296 | .put(Resource.Factory.Registry.DEFAULT_EXTENSION, |
297 | new XMIResourceFactoryImpl()); |
298 | |
299 | URI fileURI = URI.createFileURI(new File(fileName).getAbsolutePath()); |
300 | Resource resource = resourceSet.createResource(fileURI); |
301 | resource.getContents().add(modelToSave); |
302 | |
303 | try { |
304 | resource.save(Collections.EMPTY_MAP); |
305 | } catch (IOException e) { |
306 | logger.error(e.getMessage()); |
307 | } |
308 | } |
309 | |
310 | |
311 | |
312 | |
313 | public static junit.framework.Test suite() { |
314 | return new JUnit4TestAdapter(LQNTest.class); |
315 | } |
316 | } |