1 | /* |
---|
2 | * This file is part of Modelica Development Tooling. |
---|
3 | * |
---|
4 | * Copyright (c) 2005, Link�pings universitet, Department of |
---|
5 | * Computer and Information Science, PELAB |
---|
6 | * |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * (The new BSD license, see also |
---|
10 | * http://www.opensource.org/licenses/bsd-license.php) |
---|
11 | * |
---|
12 | * |
---|
13 | * Redistribution and use in source and binary forms, with or without |
---|
14 | * modification, are permitted provided that the following conditions are |
---|
15 | * met: |
---|
16 | * |
---|
17 | * * Redistributions of source code must retain the above copyright |
---|
18 | * notice, this list of conditions and the following disclaimer. |
---|
19 | * |
---|
20 | * * Redistributions in binary form must reproduce the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer in |
---|
22 | * the documentation and/or other materials provided with the |
---|
23 | * distribution. |
---|
24 | * |
---|
25 | * * Neither the name of Link�pings universitet nor the names of its |
---|
26 | * contributors may be used to endorse or promote products derived from |
---|
27 | * this software without specific prior written permission. |
---|
28 | * |
---|
29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
30 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
31 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
32 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
33 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
34 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
35 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
36 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
37 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
38 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
39 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
40 | */ |
---|
41 | |
---|
42 | package org.modelica.mdt.omc; |
---|
43 | |
---|
44 | import java.io.BufferedReader; |
---|
45 | import java.io.File; |
---|
46 | import java.io.FileReader; |
---|
47 | import java.io.IOException; |
---|
48 | import java.util.Collection; |
---|
49 | import java.util.LinkedList; |
---|
50 | import java.util.StringTokenizer; |
---|
51 | |
---|
52 | import org.eclipse.core.resources.IFile; |
---|
53 | import org.eclipse.core.runtime.Platform; |
---|
54 | import org.modelica.mdt.core.IModelicaClass; |
---|
55 | import org.modelica.mdt.core.IllegalTypeException; |
---|
56 | import org.modelica.mdt.core.List; |
---|
57 | import org.modelica.mdt.core.ListElement; |
---|
58 | import org.modelica.mdt.core.compiler.ConnectException; |
---|
59 | import org.modelica.mdt.core.compiler.ElementsInfo; |
---|
60 | import org.modelica.mdt.core.compiler.IModelicaCompiler; |
---|
61 | import org.modelica.mdt.core.compiler.InvocationError; |
---|
62 | import org.modelica.mdt.core.compiler.ModelicaParser; |
---|
63 | import org.modelica.mdt.core.compiler.UnexpectedReplyException; |
---|
64 | import org.modelica.mdt.internal.core.ElementLocation; |
---|
65 | import org.modelica.mdt.internal.core.ErrorManager; |
---|
66 | import org.modelica.mdt.omc.internal.ParseResults; |
---|
67 | import org.modelica.mdt.omc.internal.OMCParser; |
---|
68 | import org.modelica.mdt.omc.internal.corba.OmcCommunication; |
---|
69 | import org.modelica.mdt.omc.internal.corba.OmcCommunicationHelper; |
---|
70 | import org.omg.CORBA.ORB; |
---|
71 | |
---|
72 | /** |
---|
73 | * The OMCProxy is the glue between the OpenModelica Compiler and MDT. |
---|
74 | * It uses the interactive API of OMC to get information about classes |
---|
75 | * and to load classes into OMC. |
---|
76 | * |
---|
77 | * @author Andreas Remar |
---|
78 | */ |
---|
79 | public class OMCProxy implements IModelicaCompiler |
---|
80 | { |
---|
81 | /* the CORBA object */ |
---|
82 | private static OmcCommunication omcc; |
---|
83 | |
---|
84 | /* what Operating System we're running on */ |
---|
85 | private static String os; |
---|
86 | |
---|
87 | /* indicates if we've setup the communication with OMC */ |
---|
88 | private boolean hasInitialized = false; |
---|
89 | |
---|
90 | /* indicates if the Modelica System Library has been loaded */ |
---|
91 | private boolean systemLibraryLoaded = false; |
---|
92 | |
---|
93 | private String[] standardLibraryPackages = { "Modelica" }; |
---|
94 | |
---|
95 | /* should we trace the calls to sendExpression? */ |
---|
96 | private static boolean traceOMCCalls = false; |
---|
97 | private static boolean traceOMCStatus = false; |
---|
98 | static |
---|
99 | { |
---|
100 | /* load debug options and set debug flag variables accordingly */ |
---|
101 | |
---|
102 | |
---|
103 | String value = Platform.getDebugOption /*load trace/omcCalls flag */ |
---|
104 | ("org.modelica.mdt.omc/trace/omcCalls"); |
---|
105 | if (value != null && value.equalsIgnoreCase("true")) |
---|
106 | { |
---|
107 | traceOMCCalls = true; |
---|
108 | } |
---|
109 | |
---|
110 | value = Platform.getDebugOption |
---|
111 | ("org.modelica.mdt.omc/trace/omcStatus"); |
---|
112 | if (value != null && value.equalsIgnoreCase("true")) |
---|
113 | { |
---|
114 | traceOMCStatus = true; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | public OMCProxy() |
---|
119 | { |
---|
120 | |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Reads in the OMC CORBA object reference from a file on disk. |
---|
125 | * @return the object reference as a <code>String</code> |
---|
126 | */ |
---|
127 | private static String readObjectFromFile() throws ConnectException |
---|
128 | { |
---|
129 | File f = new File(getPathToObject()); |
---|
130 | String stringifiedObjectReference = null; |
---|
131 | |
---|
132 | BufferedReader br = null; |
---|
133 | FileReader fr = null; |
---|
134 | try |
---|
135 | { |
---|
136 | fr = new FileReader(f); |
---|
137 | } |
---|
138 | catch(IOException e) |
---|
139 | { |
---|
140 | throw new ConnectException |
---|
141 | ("Unable to read OpenModelica Compiler CORBA object from " |
---|
142 | + f.toString()); |
---|
143 | } |
---|
144 | |
---|
145 | br = new BufferedReader(fr); |
---|
146 | |
---|
147 | try |
---|
148 | { |
---|
149 | stringifiedObjectReference = br.readLine(); |
---|
150 | } |
---|
151 | catch(IOException e) |
---|
152 | { |
---|
153 | throw new ConnectException("Unable to read OpenModelica Compiler" |
---|
154 | + " CORBA object from " + getPathToObject()); |
---|
155 | } |
---|
156 | return stringifiedObjectReference; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * @return Returns the path to the OMC CORBA object that is stored on disk. |
---|
161 | */ |
---|
162 | private static String getPathToObject() |
---|
163 | { |
---|
164 | String fileName = null; |
---|
165 | if(os.equals("Unix")) |
---|
166 | { |
---|
167 | /* This mirrors the way OMC creates the object file. */ |
---|
168 | String username = System.getenv("USER"); |
---|
169 | if(username == null) |
---|
170 | { |
---|
171 | username = "nobody"; |
---|
172 | } |
---|
173 | fileName = "/tmp/openmodelica." + username + ".objid"; |
---|
174 | } |
---|
175 | else if(os.equals("Windows")) |
---|
176 | { |
---|
177 | String temp = System.getenv("TMP"); |
---|
178 | fileName = temp + "\\openmodelica.objid"; |
---|
179 | } |
---|
180 | |
---|
181 | logOMCStatus("Will look for OMC object reference in '" |
---|
182 | + fileName + "'."); |
---|
183 | |
---|
184 | return fileName; |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * Start a new OMC server. |
---|
189 | */ |
---|
190 | private static void startServer() throws ConnectException |
---|
191 | { |
---|
192 | String pathToOmc = null; |
---|
193 | |
---|
194 | /* |
---|
195 | * Path to omc (or omc.exe) can be found in the OPENMODELICAHOME |
---|
196 | * variable. |
---|
197 | */ |
---|
198 | String omHome = System.getenv("OPENMODELICAHOME"); |
---|
199 | if(omHome == null) |
---|
200 | { |
---|
201 | final String m = "Environment variable OPENMODELICAHOME not set"; |
---|
202 | logOMCStatus("Environment variable OPENMODELICAHOME not set,"+ |
---|
203 | " don't know how to start OMC."); |
---|
204 | throw new ConnectException(m); |
---|
205 | } |
---|
206 | |
---|
207 | if(os.equals("Unix")) |
---|
208 | { |
---|
209 | pathToOmc = omHome + "/omc"; |
---|
210 | } |
---|
211 | else if(os.equals("Windows")) |
---|
212 | { |
---|
213 | pathToOmc = omHome + "\\omc.exe"; |
---|
214 | } |
---|
215 | |
---|
216 | /* |
---|
217 | * Delete old object reference file. We need to do this because we're |
---|
218 | * checking if the file exists to determine if the server has started |
---|
219 | * or not (further down). |
---|
220 | */ |
---|
221 | File f = new File(getPathToObject()); |
---|
222 | if(f.exists()) |
---|
223 | { |
---|
224 | logOMCStatus("Removing old OMC object reference file."); |
---|
225 | f.delete(); |
---|
226 | } |
---|
227 | |
---|
228 | String command[] = { pathToOmc, "+d=interactiveCorba" }; |
---|
229 | try |
---|
230 | { |
---|
231 | logOMCStatus("Running command " + command[0] + " " + command[1]); |
---|
232 | Runtime.getRuntime().exec(command); |
---|
233 | logOMCStatus("Command run successfully."); |
---|
234 | } |
---|
235 | catch(IOException e) |
---|
236 | { |
---|
237 | /* |
---|
238 | * If we fail to start the compiler, maybe the executable is in |
---|
239 | * the Compiler directory (if we've compiled the compiler from |
---|
240 | * source). Try starting OMC from this secondary location. |
---|
241 | */ |
---|
242 | logOMCStatus("Error running command " + e.getMessage() |
---|
243 | + ", trying alternative path to the binary."); |
---|
244 | String secondaryPathToOmc = null; |
---|
245 | try |
---|
246 | { |
---|
247 | if(os.equals("Unix")) |
---|
248 | { |
---|
249 | secondaryPathToOmc = omHome + "/Compiler/omc"; |
---|
250 | } |
---|
251 | else if(os.equals("Windows")) |
---|
252 | { |
---|
253 | secondaryPathToOmc = omHome + "\\Compiler\\omc.exe"; |
---|
254 | } |
---|
255 | |
---|
256 | command = |
---|
257 | new String[]{secondaryPathToOmc, "+d=interactiveCorba"}; |
---|
258 | logOMCStatus("Running command " |
---|
259 | + command[0] + " " + command[1]); |
---|
260 | Runtime.getRuntime().exec(command); |
---|
261 | logOMCStatus("Command run successfully."); |
---|
262 | } |
---|
263 | catch(IOException ex) |
---|
264 | { |
---|
265 | logOMCStatus("Unable to start OMC, giving up."); |
---|
266 | throw new ConnectException |
---|
267 | ("Unable to start the OpenModelica Compiler. " |
---|
268 | + "Tried starting " + pathToOmc |
---|
269 | + " and " + secondaryPathToOmc); |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | logOMCStatus("Wait for OMC CORBA object reference to appear on disk."); |
---|
274 | |
---|
275 | /* |
---|
276 | * Wait until the object exists on disk, but if it takes longer than |
---|
277 | * 5 seconds, abort. (Very arbitrary 5 seconds..) |
---|
278 | */ |
---|
279 | int ticks = 0; |
---|
280 | while(!f.exists()) |
---|
281 | { |
---|
282 | try |
---|
283 | { |
---|
284 | Thread.sleep(100); |
---|
285 | } |
---|
286 | catch(InterruptedException e) |
---|
287 | { |
---|
288 | // Ignore |
---|
289 | } |
---|
290 | ticks++; |
---|
291 | |
---|
292 | /* If we've waited for 5 seconds, abort the wait for OMC */ |
---|
293 | if(ticks > 50) |
---|
294 | { |
---|
295 | logOMCStatus("No OMC object reference file created after " + |
---|
296 | "approximately 5 seconds."); |
---|
297 | logOMCStatus("It seems OMC does not want to come up, giving " + |
---|
298 | "up."); |
---|
299 | throw new ConnectException |
---|
300 | ("Unable to start the Open Modelica Compiler. Waited for 5" |
---|
301 | +" seconds, but it didn't respond."); |
---|
302 | } |
---|
303 | } |
---|
304 | logOMCStatus("OMC object reference found."); |
---|
305 | } |
---|
306 | |
---|
307 | /** |
---|
308 | * Initializes an ORB, converts the stringified OMC object to a real |
---|
309 | * CORBA object, and then narrows that object to an OmcCommunication |
---|
310 | * object. |
---|
311 | */ |
---|
312 | private static void setupOmcc(String stringifiedObjectReference) |
---|
313 | { |
---|
314 | /* Can't remember why this is needed. But it is. */ |
---|
315 | String args[] = {null}; |
---|
316 | |
---|
317 | ORB orb; |
---|
318 | orb = ORB.init(args, null); |
---|
319 | |
---|
320 | /* Convert string to object. */ |
---|
321 | org.omg.CORBA.Object obj |
---|
322 | = orb.string_to_object(stringifiedObjectReference); |
---|
323 | |
---|
324 | /* Convert object to OmcCommunication object. */ |
---|
325 | omcc = OmcCommunicationHelper.narrow(obj); |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * @return the name of the operating system. If an unknown os is found, |
---|
330 | * the default is Unix. |
---|
331 | */ |
---|
332 | private static String getOs() |
---|
333 | { |
---|
334 | String osName = System.getProperty("os.name"); |
---|
335 | if(osName.contains("Linux")) |
---|
336 | { |
---|
337 | return "Unix"; |
---|
338 | } |
---|
339 | else if(osName.contains("Windows")) |
---|
340 | { |
---|
341 | return "Windows"; |
---|
342 | } |
---|
343 | else |
---|
344 | { |
---|
345 | ErrorManager.logWarning("'" + osName + "' is unsupported OS"); |
---|
346 | /* If the OS is not GNU/Linux or Windows, default to Unix */ |
---|
347 | return "Unix"; |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | /** |
---|
352 | * Initialize the communication with OMC |
---|
353 | * @throws ConnectException if we're unable to start communicating with |
---|
354 | * the server |
---|
355 | */ |
---|
356 | private void init() throws ConnectException |
---|
357 | { |
---|
358 | /* |
---|
359 | * Get type of operating system, used for finding object |
---|
360 | * reference and starting OMC if the reference is faulty |
---|
361 | */ |
---|
362 | os = getOs(); |
---|
363 | |
---|
364 | /* See if an OMC server is already running */ |
---|
365 | File f = new File(getPathToObject()); |
---|
366 | String stringifiedObjectReference = null; |
---|
367 | if(!f.exists()) |
---|
368 | { |
---|
369 | /* If a server isn't running, start it */ |
---|
370 | logOMCStatus("No OMC object reference found, starting server."); |
---|
371 | startServer(); |
---|
372 | } |
---|
373 | else |
---|
374 | { |
---|
375 | logOMCStatus("Old OMC CORBA object reference present," + |
---|
376 | " assuming OMC is running."); |
---|
377 | } |
---|
378 | |
---|
379 | /* Read in the CORBA OMC object from a file on disk */ |
---|
380 | stringifiedObjectReference = readObjectFromFile(); |
---|
381 | |
---|
382 | /* |
---|
383 | * Setup up OMC object reference by initializing ORB and then |
---|
384 | * converting the string object to a real CORBA object. |
---|
385 | */ |
---|
386 | setupOmcc(stringifiedObjectReference); |
---|
387 | |
---|
388 | try |
---|
389 | { |
---|
390 | /* |
---|
391 | * Test the server by trying to send an expression to it. |
---|
392 | * This might fail if the object reference found on disk didn't |
---|
393 | * have a corresponding server running. If a server is missing, |
---|
394 | * catch an exception and try starting a server. |
---|
395 | */ |
---|
396 | logOMCStatus("Trying to send expression to OMC."); |
---|
397 | omcc.sendExpression("1+1"); |
---|
398 | logOMCStatus("Expression sent successfully."); |
---|
399 | } |
---|
400 | catch(org.omg.CORBA.COMM_FAILURE e) |
---|
401 | { |
---|
402 | /* Start server and set up omcc */ |
---|
403 | logOMCStatus("Failed sending expression, will try to start OMC."); |
---|
404 | startServer(); |
---|
405 | stringifiedObjectReference = readObjectFromFile(); |
---|
406 | setupOmcc(stringifiedObjectReference); |
---|
407 | |
---|
408 | try |
---|
409 | { |
---|
410 | /* Once again try to send an expression to OMC. If it fails this |
---|
411 | * time it's time to send back an exception to the caller of |
---|
412 | * this function. */ |
---|
413 | logOMCStatus("Trying to send expression to OMC."); |
---|
414 | omcc.sendExpression("1+1"); |
---|
415 | logOMCStatus("Expression sent successfully."); |
---|
416 | } |
---|
417 | catch(org.omg.CORBA.COMM_FAILURE x) |
---|
418 | { |
---|
419 | logOMCStatus("Failed sending expression, giving up."); |
---|
420 | throw new ConnectException("Unable to start the OpenModelica" |
---|
421 | +" Compiler."); |
---|
422 | } |
---|
423 | } |
---|
424 | |
---|
425 | hasInitialized = true; |
---|
426 | } |
---|
427 | |
---|
428 | /** |
---|
429 | * Send expression to OMC. If communication is not initialized, it |
---|
430 | * is initialized here. |
---|
431 | * @param exp the expression to send to OMC |
---|
432 | * @throws ConnectException if we're unable to start communicating with |
---|
433 | * the server |
---|
434 | */ |
---|
435 | // TODO add synchronization so that two threads don't fudge up each others |
---|
436 | // communication with OMC |
---|
437 | // old synchronization aka 'private synchronized String sendExpression(String exp)' |
---|
438 | // doesnt work when there is possibility of multiple instances of OMCProxy objects |
---|
439 | private String sendExpression(String exp) |
---|
440 | throws ConnectException |
---|
441 | { |
---|
442 | String retval = null; |
---|
443 | |
---|
444 | if(hasInitialized == false) |
---|
445 | { |
---|
446 | init(); |
---|
447 | } |
---|
448 | |
---|
449 | try |
---|
450 | { |
---|
451 | logOMCCall(exp); |
---|
452 | retval = omcc.sendExpression(exp); |
---|
453 | logOMCReply(retval); |
---|
454 | } |
---|
455 | catch(org.omg.CORBA.COMM_FAILURE x) |
---|
456 | { |
---|
457 | logOMCCallError("Error while sending expression " + exp + " ["+x+"]"); |
---|
458 | /* lost connection to OMC or something */ |
---|
459 | throw new ConnectException("Couldn't send expression to the "+ |
---|
460 | "OpenModelica Compiler. Tried sending: " + exp); |
---|
461 | } |
---|
462 | |
---|
463 | return retval; |
---|
464 | } |
---|
465 | |
---|
466 | /** |
---|
467 | * Logs the expression sent to OMC if the |
---|
468 | * tracing flag (traceOMCCalls) is set |
---|
469 | * |
---|
470 | * @param expression the expression that is about to be sent to OMC |
---|
471 | */ |
---|
472 | private static void logOMCCall(String expression) |
---|
473 | { |
---|
474 | if (!traceOMCCalls) |
---|
475 | { |
---|
476 | return; |
---|
477 | } |
---|
478 | System.out.println(">> " + expression); |
---|
479 | } |
---|
480 | |
---|
481 | /** |
---|
482 | * outputs the message about a call error that occured |
---|
483 | * when communicating with omc |
---|
484 | * @param message the message to log |
---|
485 | */ |
---|
486 | private static void logOMCCallError(String message) |
---|
487 | { |
---|
488 | if(!traceOMCCalls) |
---|
489 | { |
---|
490 | return; |
---|
491 | } |
---|
492 | System.out.println(message); |
---|
493 | } |
---|
494 | |
---|
495 | /** |
---|
496 | * loggs the message conserning OMC status if the |
---|
497 | * tracing flag traceOMCStatus is set |
---|
498 | * @param message the message to log |
---|
499 | */ |
---|
500 | private static void logOMCStatus(String message) |
---|
501 | { |
---|
502 | if (!traceOMCStatus) |
---|
503 | { |
---|
504 | return; |
---|
505 | } |
---|
506 | System.out.println("OMCSTATUS: " + message); |
---|
507 | } |
---|
508 | |
---|
509 | /** |
---|
510 | * Logs the reply received from OMC if |
---|
511 | * the tracing flag (traceOMCCalls) is set |
---|
512 | * |
---|
513 | * @param reply the reply recieved from the OMC |
---|
514 | */ |
---|
515 | private static void logOMCReply(String reply) |
---|
516 | { |
---|
517 | if (!traceOMCCalls) |
---|
518 | { |
---|
519 | return; |
---|
520 | } |
---|
521 | |
---|
522 | StringTokenizer tokenizer = new StringTokenizer(reply, "\n"); |
---|
523 | |
---|
524 | while (tokenizer.hasMoreTokens()) |
---|
525 | { |
---|
526 | System.out.println("<< " + tokenizer.nextToken()); |
---|
527 | } |
---|
528 | } |
---|
529 | |
---|
530 | /** |
---|
531 | * Get the classes contained in a class (a package is a class..) |
---|
532 | * |
---|
533 | * |
---|
534 | * @param className full class name where to look for packages |
---|
535 | * @return an array of subclasses defined (and loaded into OMC) |
---|
536 | * inside the class named className, but don't return packages in this |
---|
537 | * class. The results is returned as Vector of objects but objects |
---|
538 | * are actually String's. |
---|
539 | * |
---|
540 | * @throws ConnectException |
---|
541 | * @throws UnexpectedReplyException |
---|
542 | * @throws InitializationException |
---|
543 | */ |
---|
544 | public List getClassNames(String className) |
---|
545 | throws ConnectException, UnexpectedReplyException |
---|
546 | { |
---|
547 | String retval = sendExpression("getClassNames("+className+")"); |
---|
548 | |
---|
549 | /* fetch error string but ignore it */ |
---|
550 | getErrorString(); |
---|
551 | |
---|
552 | return ModelicaParser.parseList(retval); |
---|
553 | } |
---|
554 | |
---|
555 | /** |
---|
556 | * Gets the restriction type of a class. |
---|
557 | * |
---|
558 | * @param className fully qualified class name |
---|
559 | * @return the restriction type of the class or Type.CLASS if |
---|
560 | * type can't be determined |
---|
561 | * @throws ConnectException |
---|
562 | */ |
---|
563 | public IModelicaClass.Type getRestrictionType(String className) |
---|
564 | throws ConnectException, UnexpectedReplyException |
---|
565 | { |
---|
566 | String reply = |
---|
567 | sendExpression("getClassRestriction(" + className + ")"); |
---|
568 | |
---|
569 | /* remove " around the reply */ |
---|
570 | reply = reply.trim(); |
---|
571 | |
---|
572 | if(reply.equals("")) |
---|
573 | { |
---|
574 | throw new UnexpectedReplyException("getClassRestriction("+className |
---|
575 | +") returned an empty result"); |
---|
576 | } |
---|
577 | |
---|
578 | reply = reply.substring(1, reply.length()-1); |
---|
579 | |
---|
580 | /* fetch error string but ignore it */ |
---|
581 | getErrorString(); |
---|
582 | |
---|
583 | IModelicaClass.Type type = null; |
---|
584 | try |
---|
585 | { |
---|
586 | type = IModelicaClass.Type.parse(reply); |
---|
587 | } |
---|
588 | catch(IllegalTypeException e) |
---|
589 | { |
---|
590 | throw new UnexpectedReplyException("Illegal type: " |
---|
591 | + e.getMessage()); |
---|
592 | } |
---|
593 | |
---|
594 | return type; |
---|
595 | } |
---|
596 | |
---|
597 | /** |
---|
598 | * Fetches the error string from OMC. This should be called after an "Error" |
---|
599 | * is received. |
---|
600 | * @return |
---|
601 | * @throws ConnectException |
---|
602 | */ |
---|
603 | private String getErrorString() |
---|
604 | throws ConnectException |
---|
605 | { |
---|
606 | String res = sendExpression("getErrorString()"); |
---|
607 | if(res != null) |
---|
608 | { |
---|
609 | res = res.trim(); |
---|
610 | return res.substring(1, res.length() - 1); |
---|
611 | } |
---|
612 | else |
---|
613 | return ""; |
---|
614 | } |
---|
615 | |
---|
616 | |
---|
617 | /** |
---|
618 | * Tries to load file into OMC which causes it to be parsed and the syntax |
---|
619 | * checked. |
---|
620 | * @param file the file we want to load |
---|
621 | * @return either returns the classes (and packages) found in the file or |
---|
622 | * the error messages from OMC |
---|
623 | * @throws ConnectException |
---|
624 | * @throws UnexpectedReplyException |
---|
625 | * @throws InitializationException |
---|
626 | */ |
---|
627 | public ParseResults loadSourceFile(IFile file) |
---|
628 | throws ConnectException, UnexpectedReplyException |
---|
629 | { |
---|
630 | ParseResults res = new ParseResults(); |
---|
631 | |
---|
632 | String fullName = file.getLocation().toString(); |
---|
633 | String retval = |
---|
634 | sendExpression("loadFileInteractiveQualified(\"" + fullName + "\")"); |
---|
635 | |
---|
636 | /* Always keep your stuff nice and tidy! */ |
---|
637 | retval = retval.trim(); |
---|
638 | |
---|
639 | /* |
---|
640 | * See if there were parse errors, an empty list {} also denotes error |
---|
641 | */ |
---|
642 | if(retval.toLowerCase().contains("error") || retval.equals("{}")) |
---|
643 | { |
---|
644 | res.setCompileErrors(OMCParser.parseErrorString(getErrorString())); |
---|
645 | res.setClassNames(new List()); |
---|
646 | } |
---|
647 | /* |
---|
648 | * file loaded and parse successsfully |
---|
649 | */ |
---|
650 | else |
---|
651 | { |
---|
652 | res.setClassNames(ModelicaParser.parseList(retval)); |
---|
653 | } |
---|
654 | |
---|
655 | /* |
---|
656 | * If there were errors, but the compilation went through, |
---|
657 | * collect the error messages. (Test if errorString != "") |
---|
658 | */ |
---|
659 | String errorString = getErrorString(); |
---|
660 | if(errorString.equals("") == false) |
---|
661 | { |
---|
662 | res.setCompileErrors(OMCParser.parseErrorString(errorString)); |
---|
663 | } |
---|
664 | |
---|
665 | return res; |
---|
666 | } |
---|
667 | |
---|
668 | /** |
---|
669 | * Gets the location (file, line number and column number) of a Modelica |
---|
670 | * element. |
---|
671 | * @param className the element we want to get location of |
---|
672 | * @return an IElementLocation containing the file, line number and column |
---|
673 | * number of the given class |
---|
674 | * @throws ConnectException |
---|
675 | * @throws UnexpectedReplyException |
---|
676 | * @throws InvocationError |
---|
677 | */ |
---|
678 | public ElementLocation getClassLocation(String className) |
---|
679 | throws ConnectException, UnexpectedReplyException, InvocationError |
---|
680 | { |
---|
681 | String retval = sendExpression("getCrefInfo(" + className + ")"); |
---|
682 | |
---|
683 | /* fetch error string but ignore it */ |
---|
684 | getErrorString(); |
---|
685 | |
---|
686 | if(retval.contains("Error") || retval.contains("error")) |
---|
687 | { |
---|
688 | throw new |
---|
689 | InvocationError("fetching file position of " + className, |
---|
690 | "getCrefInfo(" + className + ")"); |
---|
691 | } |
---|
692 | |
---|
693 | |
---|
694 | /* |
---|
695 | * The getCrefInfo reply have the following format: |
---|
696 | * |
---|
697 | * <file path>,<something>,<start line>,<start column>,<end line>,<end column> |
---|
698 | * |
---|
699 | * for example: |
---|
700 | * /foo/Modelica/package.mo,writable,1,1,1029,13 |
---|
701 | */ |
---|
702 | |
---|
703 | /* For some reason, the list returned doesn't contain curly braces. */ |
---|
704 | retval = retval.trim(); |
---|
705 | retval = "{" + retval + "}"; |
---|
706 | |
---|
707 | List tokens = ModelicaParser.parseList(retval); |
---|
708 | |
---|
709 | String filePath = tokens.elementAt(0).toString(); |
---|
710 | int startLine; |
---|
711 | int startColumn; |
---|
712 | int endLine; |
---|
713 | int endColumn; |
---|
714 | |
---|
715 | try |
---|
716 | { |
---|
717 | startLine = Integer.parseInt(tokens.elementAt(2).toString()); |
---|
718 | startColumn = Integer.parseInt(tokens.elementAt(3).toString()); |
---|
719 | endLine = Integer.parseInt(tokens.elementAt(4).toString()); |
---|
720 | endColumn = Integer.parseInt(tokens.elementAt(5).toString()); |
---|
721 | } |
---|
722 | catch (NumberFormatException e) |
---|
723 | { |
---|
724 | throw new |
---|
725 | UnexpectedReplyException("Can't parse getCrefInfo() reply, "+ |
---|
726 | "unexpected format"); |
---|
727 | } |
---|
728 | |
---|
729 | return new ElementLocation(filePath, |
---|
730 | startLine, startColumn, endLine, endColumn); |
---|
731 | } |
---|
732 | |
---|
733 | /** |
---|
734 | * Queries the compiler if a particular modelica class/package is a package. |
---|
735 | * |
---|
736 | * @param className fully qualified name of the class/package |
---|
737 | * @return true if className is a package false otherwise |
---|
738 | * @throws ConnectException |
---|
739 | */ |
---|
740 | public boolean isPackage(String className) |
---|
741 | throws ConnectException |
---|
742 | { |
---|
743 | String retval = sendExpression("isPackage(" + className + ")"); |
---|
744 | |
---|
745 | /* fetch error string but ignore it */ |
---|
746 | getErrorString(); |
---|
747 | |
---|
748 | return retval.contains("true"); |
---|
749 | } |
---|
750 | |
---|
751 | public Collection<ElementsInfo> getElementsInfo(String className) |
---|
752 | throws ConnectException, InvocationError, UnexpectedReplyException |
---|
753 | { |
---|
754 | String retval = sendExpression("getElementsInfo("+ className +")"); |
---|
755 | |
---|
756 | /* fetch error string but ignore it */ |
---|
757 | getErrorString(); |
---|
758 | |
---|
759 | /* |
---|
760 | * we need a efficient way to check if the result is |
---|
761 | * humongosly huge list or 'Error' or maybe 'error' |
---|
762 | */ |
---|
763 | for (int i = 0; i < retval.length(); i++) |
---|
764 | { |
---|
765 | if (retval.charAt(i) == '{') |
---|
766 | { |
---|
767 | /* |
---|
768 | * we found the begining of the list, send it to parser and |
---|
769 | * hope for the best |
---|
770 | */ |
---|
771 | List parsedList = ModelicaParser.parseList(retval); |
---|
772 | |
---|
773 | /* convert the parsedList to a collection of ElementsInfo:s */ |
---|
774 | LinkedList<ElementsInfo> elementsInfo = |
---|
775 | new LinkedList<ElementsInfo>(); |
---|
776 | |
---|
777 | for (ListElement element : parsedList) |
---|
778 | { |
---|
779 | elementsInfo.add(new ElementsInfo((List)element)); |
---|
780 | } |
---|
781 | |
---|
782 | return elementsInfo; |
---|
783 | } |
---|
784 | else if (retval.charAt(i) == 'E' || retval.charAt(i) == 'e') |
---|
785 | { |
---|
786 | /* |
---|
787 | * this is the unreadable way to check if the retval |
---|
788 | * equals 'Error' or 'error' |
---|
789 | */ |
---|
790 | if (retval.substring(i+1,i+5).equals("rror")) |
---|
791 | { |
---|
792 | throw new |
---|
793 | InvocationError("fetching contents of " + className, |
---|
794 | "getElementsInfo("+ className +")"); |
---|
795 | } |
---|
796 | else |
---|
797 | { |
---|
798 | /* OMC returned someting wierd, panic mode ! */ |
---|
799 | break; |
---|
800 | } |
---|
801 | } |
---|
802 | } |
---|
803 | /* we have no idea what OMC returned */ |
---|
804 | throw new UnexpectedReplyException("getElementsInfo("+ className +")" + |
---|
805 | "replies:'" + retval + "'"); |
---|
806 | } |
---|
807 | |
---|
808 | |
---|
809 | public String getCompilerName() |
---|
810 | { |
---|
811 | return "OpenModelica Compiler"; |
---|
812 | } |
---|
813 | |
---|
814 | /** |
---|
815 | * Loads in the Modelica System Library and returns names of the top-level |
---|
816 | * packages. |
---|
817 | * |
---|
818 | * @throws ConnectException if we're unable to start communicating with |
---|
819 | * the server |
---|
820 | */ |
---|
821 | public String[] getStandardLibrary() throws ConnectException |
---|
822 | { |
---|
823 | if (!systemLibraryLoaded) |
---|
824 | { |
---|
825 | sendExpression("loadModel(Modelica)"); |
---|
826 | |
---|
827 | /* fetch error string but ignore it */ |
---|
828 | getErrorString(); |
---|
829 | |
---|
830 | systemLibraryLoaded = true; |
---|
831 | } |
---|
832 | |
---|
833 | return standardLibraryPackages; |
---|
834 | } |
---|
835 | } |
---|