]> gerrit.simantics Code Review - simantics/fmil.git/blob - org.simantics.fmil/src/org/simantics/fmil/FMIL.java
70d6028a2c410f5f45697bfb3e816c81da1208dd
[simantics/fmil.git] / org.simantics.fmil / src / org / simantics / fmil / FMIL.java
1 package org.simantics.fmil;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.io.RandomAccessFile;\r
6 import java.nio.channels.FileChannel;\r
7 import java.nio.channels.FileLock;\r
8 import java.util.ArrayList;\r
9 import java.util.HashSet;\r
10 import java.util.List;\r
11 import java.util.Set;\r
12 import java.util.UUID;\r
13 \r
14 import org.eclipse.core.runtime.FileLocator;\r
15 import org.eclipse.core.runtime.Platform;\r
16 import org.osgi.framework.Bundle;\r
17 import org.simantics.Simantics;\r
18 import org.simantics.fmil.ExecEnvironment.ARCHType;\r
19 import org.simantics.fmil.ExecEnvironment.OSType;\r
20 import org.simantics.utils.FileUtils;\r
21 \r
22 import gnu.trove.list.array.TIntArrayList;\r
23 import gnu.trove.map.hash.TObjectIntHashMap;\r
24 \r
25 \r
26 public class FMIL {\r
27 \r
28     /**\r
29      * Static variables\r
30      */\r
31     private static int      ERROR               = 0;\r
32     private static int      OK                  = 1;\r
33     private static String   UNSATISFIED_LINK    = "Method not found. DLL might not be loaded properly.";    \r
34     private static String   TEMP_FMU_DIRECTORY_NAME = "fmil";    \r
35     public static String    TEMP_FMU_COMMON_DIRECTORY;  \r
36     public static String    LOCK_FILE_NAME      = "fmil.lock";\r
37 \r
38     public static Object syncObject = new Object();\r
39 \r
40     /**\r
41      * Static: load native libraries required for the FMU simulation to work.\r
42      */\r
43     static {\r
44         \r
45         File[] libraries = new File[2];\r
46 \r
47         Bundle bundle = null;\r
48         \r
49         ExecEnvironment env = ExecEnvironment.calculate();\r
50         if (env.os == OSType.WINDOWS) {\r
51             if (env.arch == ARCHType.X86) {\r
52                 bundle = Platform.getBundle("org.simantics.fmil.win32");\r
53             } else if (env.arch == ARCHType.X86_64) {\r
54                 bundle = Platform.getBundle("org.simantics.fmil.win64");\r
55             }\r
56         }\r
57         \r
58         if (bundle != null) {\r
59             try{\r
60                 String root = FileLocator.getBundleFile(bundle).getAbsolutePath();\r
61 //                if (env.arch == ARCHType.X86_64) {\r
62 //                    File newFIle = new File(root, "libraries/libexpat.dll");\r
63 //                    System.load(newFIle.getAbsolutePath());\r
64 //                }\r
65 //                libraries[0] = new File(root, "libraries/zlibwapi.dll");\r
66 //                libraries[1] = new File(root, "libraries/miniunz.dll");\r
67                 libraries[0] = new File(root, "libraries/fmilib_shared.dll");\r
68                 libraries[1] = new File(root, "libraries/FMUSimulator.dll");\r
69             }\r
70             catch (Exception e) {\r
71                 e.printStackTrace();\r
72             }\r
73         }\r
74 \r
75         for(File library : libraries) {\r
76             if(library == null) {\r
77                 System.err.println("FMU library not loaded. FMU simulation not working.");\r
78                 continue;\r
79             } else if(!library.isFile()) {\r
80                 System.err.println(library.getAbsolutePath() + " not found");\r
81             } else {\r
82                 try {\r
83                     System.load(library.getAbsolutePath());\r
84                 } catch (Throwable t) {\r
85                     System.err.println(t.getMessage());\r
86                 }\r
87             } \r
88         }\r
89     }\r
90 \r
91     /**\r
92      * Static: initialize fmu temp folder\r
93      */\r
94     static {\r
95         File dir = Simantics.getTemporaryDirectory(TEMP_FMU_DIRECTORY_NAME);\r
96         TEMP_FMU_COMMON_DIRECTORY = dir.getAbsolutePath(); \r
97     }\r
98 \r
99 \r
100     private String fmuDir;\r
101     private int id;\r
102 \r
103     public String TEMP_FOLDER_1;\r
104     public String TEMP_FOLDER_2;\r
105     public String TEMP_FMU_DIRECTORY;\r
106     private String dirName;\r
107 \r
108         private String[] variableNames;\r
109         private int[] variableReferences;\r
110         private TObjectIntHashMap<String> variableMap = new TObjectIntHashMap<String>();\r
111         \r
112         private Set<String> subscriptionSet = new HashSet<String>();\r
113         private TIntArrayList subscription = new TIntArrayList();\r
114         private ArrayList<String> subscribedNames = new ArrayList<String>();\r
115         \r
116         public List<String> getSubscribedNames() {\r
117                 return subscribedNames;\r
118         }\r
119         \r
120         public boolean subscribe(String name) throws FMILException {\r
121                 // Safety check\r
122                 int vr = variableMap.get(name);\r
123                 if(vr == 0) return false;\r
124                 if(!subscriptionSet.add(name)) return false;\r
125                 subscribedNames.add(name);\r
126                 System.err.println("subscribed : " + name + " => " + subscribedNames.size());\r
127                 subscription.add(vr);\r
128                 subscribe(new int[] { vr });\r
129                 return true;\r
130         }\r
131 \r
132     public FMIL() {\r
133         // Create a directory for this control\r
134         File tempDir = new File(TEMP_FMU_COMMON_DIRECTORY, UUID.randomUUID().toString());\r
135         tempDir.mkdir();\r
136         TEMP_FMU_DIRECTORY = tempDir.getAbsolutePath();\r
137 \r
138         // Create two directories inside the temp directory for this control\r
139         dirName = UUID.randomUUID().toString();\r
140         File fmuDir = new File(TEMP_FMU_DIRECTORY, dirName);\r
141         fmuDir.mkdir();\r
142 \r
143         TEMP_FOLDER_1 = fmuDir.toString();\r
144         TEMP_FOLDER_2 = fmuDir.toString() + "_2";\r
145 \r
146         // Lock fmu directory in temp directory\r
147         lockFMUDirectory();\r
148     }\r
149 \r
150     public int getModelIDNew() {\r
151         return id;\r
152     }\r
153 \r
154     public String getModelID() {\r
155         return dirName;\r
156     }\r
157 \r
158     public String getFmuDir() {\r
159         return fmuDir;\r
160     }\r
161 \r
162     /**\r
163      * Load fmu from a given file path. Releases the (possible) previously\r
164      * loaded fmu.\r
165      * \r
166      * @param path absolute file path for fmu file\r
167      * @throws FMILException\r
168      */\r
169     private int fmuN = 0;\r
170     private boolean fmuLoaded = false;\r
171     public void loadFMUFile(String path) throws FMILException {\r
172 \r
173         synchronized(syncObject) {\r
174 \r
175             if(fmuN % 2 == 0) {\r
176                 fmuDir = TEMP_FOLDER_1;\r
177                 fmuN++;\r
178             } else {\r
179                 fmuDir = TEMP_FOLDER_2;\r
180                 fmuN = 0;\r
181             }\r
182 \r
183             File tempDir = new File(fmuDir);\r
184             if(tempDir.isDirectory()) {\r
185                 try {\r
186                     FileUtils.deleteAll(tempDir);\r
187                 } catch (IOException e) {\r
188                     throw new FMILException("Could not create temp folder for fmu");\r
189                 }\r
190                 tempDir.mkdir();\r
191             } else {\r
192                 tempDir.mkdir();\r
193             }\r
194 \r
195 \r
196             try {\r
197                 String tmpPath = tempDir.getAbsolutePath();\r
198                 if(!tmpPath.endsWith("\\"))\r
199                     tmpPath = tmpPath + "\\";\r
200                 id = loadFMUFile_(path, tmpPath);\r
201                 \r
202                 getAllVariables();\r
203                 getAllVariableReferences();\r
204                 \r
205                 for(int i=0;i<variableNames.length;i++) {\r
206                         variableMap.put(variableNames[i], variableReferences[i]);\r
207                 }\r
208 \r
209                 fmuLoaded = true;\r
210             } catch (UnsatisfiedLinkError err) {\r
211                 throw new FMILException(UNSATISFIED_LINK);\r
212             } catch (Exception e) {\r
213                 throw new FMILException(e.getMessage());\r
214             }\r
215         }\r
216     }\r
217 \r
218     private native int loadFMUFile_(String path, String toDir);\r
219 \r
220     /**\r
221      * Set a step length for simulation\r
222      * \r
223      * @param step Step length for simulation\r
224      * @throws FMILException\r
225      */\r
226     public void setStepLength(double step) throws FMILException {\r
227         synchronized(syncObject) {\r
228 \r
229             try {\r
230 \r
231                 int ret = setStepLength_(getModelIDNew(), step);\r
232                 if(ret == ERROR)\r
233                     throw new FMILException(getLastErrorMessage());\r
234 \r
235             } catch (UnsatisfiedLinkError err) {\r
236                 throw new FMILException(UNSATISFIED_LINK);\r
237             } catch (Exception e) {\r
238                 throw new FMILException(e.getMessage());\r
239             }\r
240         }\r
241     }\r
242 \r
243     private native int setStepLength_(int id, double step);\r
244 \r
245     /**\r
246      * Instantiates a simulation. \r
247      * <p>\r
248      * Make sure that an FMU is loaded first.\r
249      * @throws FMILException\r
250      */\r
251     public void instantiateSimulation() throws FMILException {\r
252         synchronized(syncObject) {\r
253 \r
254             try {\r
255 \r
256                 int ret = instantiateSimulation_(getModelIDNew()); \r
257                 if(ret == ERROR)\r
258                     throw new FMILException(getLastErrorMessage());\r
259 \r
260             } catch (UnsatisfiedLinkError err) {\r
261                 throw new FMILException(UNSATISFIED_LINK);\r
262             } catch (Exception e) {\r
263                 throw new FMILException(e.getMessage());\r
264             }\r
265         }\r
266     }\r
267 \r
268     private native int instantiateSimulation_(int id);\r
269 \r
270     \r
271     /**\r
272      * Initializes a simulation. \r
273      * <p>\r
274      * Make sure that simulation is instantiated first!\r
275      * @throws FMILException\r
276      */\r
277     public void initializeSimulation() throws FMILException {\r
278         synchronized(syncObject) {\r
279 \r
280             try {\r
281 \r
282                 int ret = initializeSimulation_(getModelIDNew()); \r
283                 if(ret == ERROR)\r
284                     throw new FMILException(getLastErrorMessage());\r
285 \r
286             } catch (UnsatisfiedLinkError err) {\r
287                 throw new FMILException(UNSATISFIED_LINK);\r
288             } catch (Exception e) {\r
289                 throw new FMILException(e.getMessage());\r
290             }\r
291         }\r
292     }\r
293 \r
294     private native int initializeSimulation_(int id);\r
295 \r
296     /**\r
297      * Subscribe a set of variables from a loaded simulation.\r
298      * <p>\r
299      * Make sure that an FMU is loaded first.\r
300      * @param variables Array of variables\r
301      * @throws FMILException\r
302      */\r
303     public void subscribe(int[] variables) throws FMILException {\r
304         synchronized(syncObject) {\r
305 \r
306             try {\r
307 \r
308                 int ret = subscribe_(getModelIDNew(), variables); \r
309                 if(ret == ERROR)\r
310                     throw new FMILException(getLastErrorMessage());\r
311 \r
312             } catch (UnsatisfiedLinkError err) {\r
313                 throw new FMILException(UNSATISFIED_LINK);\r
314             } catch (Exception e) {\r
315                 throw new FMILException(e.getMessage());\r
316             }\r
317         }\r
318     }\r
319 \r
320     private native int subscribe_(int id, int[] variables);\r
321 \r
322     /**\r
323      * Set a new (Real, double) value for a variable. If the variable is a \r
324      * parameter, the change is effective immediately.\r
325      *  \r
326      * @param name Variable\r
327      * @param value New (Real, double) value\r
328      * @throws FMILException\r
329      */\r
330     public void setRealValue(String name, double value) throws FMILException {\r
331         \r
332         synchronized(syncObject) {\r
333 \r
334             try {\r
335 \r
336                 int ret = setRealValue_(getModelIDNew(), variableMap.get(name), value); \r
337                 if(ret == ERROR)\r
338                     throw new FMILException(getLastErrorMessage());\r
339 \r
340             } catch (UnsatisfiedLinkError err) {\r
341                 throw new FMILException(UNSATISFIED_LINK);\r
342             } catch (Exception e) {\r
343                 throw new FMILException(e.getMessage());\r
344             }\r
345             \r
346         }\r
347         \r
348     }\r
349 \r
350     public void setRealValue(int variableReference, double value) throws FMILException {\r
351         \r
352         synchronized(syncObject) {\r
353 \r
354             try {\r
355 \r
356                 int ret = setRealValue_(getModelIDNew(), variableReference, value); \r
357                 if(ret == ERROR)\r
358                     throw new FMILException(getLastErrorMessage());\r
359 \r
360             } catch (UnsatisfiedLinkError err) {\r
361                 throw new FMILException(UNSATISFIED_LINK);\r
362             } catch (Exception e) {\r
363                 throw new FMILException(e.getMessage());\r
364             }\r
365             \r
366         }\r
367         \r
368     }\r
369 \r
370     private native int setRealValue_(int id, int variableReference, double value);\r
371 \r
372 //    /**\r
373 //     * Set a new (integer) value for a variable. If the variable is a \r
374 //     * parameter, the change is effective immediately.\r
375 //     *  \r
376 //     * @param name Variable\r
377 //     * @param value New (integer) value\r
378 //     * @throws FMILException\r
379 //     */\r
380 //    public void setIntegerValue(String name, int value) throws FMILException {\r
381 //        synchronized(syncObject) {\r
382 //\r
383 //            try {\r
384 //\r
385 //                int ret = setIntegerValue_(getModelID(), name, value); \r
386 //                if(ret == ERROR)\r
387 //                    throw new FMILException(getLastErrorMessage());\r
388 //\r
389 //            } catch (UnsatisfiedLinkError err) {\r
390 //                throw new FMILException(UNSATISFIED_LINK);\r
391 //            } catch (Exception e) {\r
392 //                throw new FMILException(e.getMessage());\r
393 //            }\r
394 //        }\r
395 //    }\r
396 //    private native int setIntegerValue_(String id, String name, int value);\r
397 //\r
398 //    /**\r
399 //     * Set a new (boolean) value for a variable. If the variable is a \r
400 //     * parameter, the change is effective immediately.\r
401 //     *  \r
402 //     * @param name Variable\r
403 //     * @param value New (boolean) value\r
404 //     * @throws FMILException\r
405 //     */\r
406 //    public void setBooleanValue(String name, boolean value) throws FMILException {\r
407 //        synchronized(syncObject) {\r
408 //\r
409 //            try {\r
410 //\r
411 //                int ret = setBooleanValue_(getModelID(), name, value); \r
412 //                if(ret == ERROR)\r
413 //                    throw new FMILException(getLastErrorMessage());\r
414 //\r
415 //            } catch (UnsatisfiedLinkError err) {\r
416 //                throw new FMILException(UNSATISFIED_LINK);\r
417 //            } catch (Exception e) {\r
418 //                throw new FMILException(e.getMessage());\r
419 //            }\r
420 //        }\r
421 //    }\r
422 //    private native int setBooleanValue_(String id, String name, boolean value);\r
423 //\r
424 //    public void setTime(double time) throws FMILException {\r
425 //        synchronized(syncObject) {\r
426 //\r
427 //            try {\r
428 //\r
429 //                int ret = setTime_(getModelID(), time); \r
430 //                if(ret == ERROR)\r
431 //                    throw new FMILException(getLastErrorMessage());\r
432 //\r
433 //            } catch (UnsatisfiedLinkError err) {\r
434 //                throw new FMILException(UNSATISFIED_LINK);\r
435 //            } catch (Exception e) {\r
436 //                throw new FMILException(e.getMessage());\r
437 //            }\r
438 //        }\r
439 //    }\r
440 //    private native int setTime_(String id, double time);\r
441 \r
442     /**\r
443      * Simulate one step forward. The step length can be set with\r
444      * setStepLength()\r
445      * \r
446      * @throws FMILException\r
447      */\r
448     public void simulateStep() throws FMILException {\r
449         synchronized(syncObject) {\r
450 \r
451             try {\r
452 \r
453                 int ret = simulateStep_(getModelIDNew()); \r
454                 if(ret == ERROR)\r
455                     throw new FMILException(getLastErrorMessage());\r
456 \r
457             } catch (UnsatisfiedLinkError err) {\r
458                 throw new FMILException(UNSATISFIED_LINK);\r
459             } catch (Exception e) {\r
460                 throw new FMILException(e.getMessage());\r
461             }\r
462         }\r
463     }\r
464     private native int simulateStep_(int id);\r
465 \r
466     /**\r
467      * Get an array containing the current values for subscribed variables. The\r
468      * values are in the same order as in the subscription.\r
469      * \r
470      * @param results An array the size of subscribed results\r
471      * @return\r
472      */\r
473     public double[] getSubscribedResults() throws FMILException {\r
474         synchronized(syncObject) {\r
475 \r
476             try {\r
477 \r
478                 double[] results = new double[subscription.size()];\r
479                 return getSubscribedResults_(getModelIDNew(), results);\r
480 \r
481             } catch (UnsatisfiedLinkError err) {\r
482                 throw new FMILException(UNSATISFIED_LINK);\r
483             } catch (Exception e) {\r
484                 throw new FMILException(e.getMessage());\r
485             }\r
486         }\r
487     }\r
488 \r
489     private native double[] getSubscribedResults_(int id, double[] results);\r
490     \r
491 \r
492     /**\r
493      * Unload FMU and the dll:s that it requires.\r
494      * <p>\r
495      * To be called after all FMU simulations are ended. \r
496      * If the fmu is loaded again / changed, call to loadFMUFile is sufficient. loadFMUFile \r
497      * releases the previous fmu.dll  \r
498      * \r
499      * @throws FMILException\r
500      */\r
501     public void unloadFMU() throws FMILException {\r
502         synchronized(syncObject) {\r
503 \r
504             try {\r
505 \r
506                 unlockFMUDirectory();\r
507                 if(fmuLoaded) {\r
508                     int ret = unloadFMU_(getModelIDNew()); \r
509                     if(ret == ERROR)\r
510                         throw new FMILException(getLastErrorMessage());\r
511                 }\r
512                 removeFMUDirectoryContents();\r
513 \r
514             } catch (UnsatisfiedLinkError err) {\r
515                 throw new FMILException(UNSATISFIED_LINK);\r
516             } catch (Exception e) {\r
517                 throw new FMILException(e.getMessage());\r
518             }\r
519         }\r
520     }\r
521     private native int unloadFMU_(int id);\r
522     \r
523 //    /**\r
524 //     * Checks if fmu has been initialized\r
525 //     * @return current simulation time\r
526 //     */\r
527 //    public boolean isInitialized() throws FMILException {\r
528 //        synchronized(syncObject) {\r
529 //            try {\r
530 //                return isInitialized_(getModelID());\r
531 //            } catch (UnsatisfiedLinkError err) {\r
532 //                throw new FMILException(UNSATISFIED_LINK);\r
533 //            } catch (Exception e) {\r
534 //                throw new FMILException(e.getMessage());\r
535 //            }\r
536 //        }\r
537 //    }\r
538 //\r
539 //    private native boolean isInitialized_(String id);\r
540 //\r
541     /**\r
542      * Get the current simulation time\r
543      * @return current simulation time\r
544      */\r
545     public double getTime() throws FMILException {\r
546         synchronized(syncObject) {\r
547 \r
548             try {\r
549 \r
550                 return getTime_(getModelIDNew());\r
551 \r
552             } catch (UnsatisfiedLinkError err) {\r
553                 throw new FMILException(UNSATISFIED_LINK);\r
554             } catch (Exception e) {\r
555                 throw new FMILException(e.getMessage());\r
556             }\r
557         }\r
558     }\r
559 \r
560     private native double getTime_(int id);\r
561 \r
562     /**\r
563      * Get all variables in a loaded model\r
564      * @return all variables in a loaded model\r
565      */\r
566     public String[] getAllVariables() throws FMILException {\r
567         synchronized(syncObject) {\r
568 \r
569             try {\r
570 \r
571                 if(variableNames == null) {\r
572                         variableNames = getAllVariables_(getModelIDNew());\r
573                 }\r
574                 return variableNames;\r
575 \r
576             } catch (UnsatisfiedLinkError err) {\r
577                 throw new FMILException(UNSATISFIED_LINK);\r
578             } catch (Exception e) {\r
579                 throw new FMILException(e.getMessage());\r
580             }\r
581         }\r
582     }\r
583 \r
584     private native String[] getAllVariables_(int id);\r
585 \r
586     /**\r
587      * Get all variables in a loaded model\r
588      * @return all variables in a loaded model\r
589      */\r
590     public int[] getAllVariableReferences() throws FMILException {\r
591         synchronized(syncObject) {\r
592 \r
593             try {\r
594 \r
595                 if(variableReferences == null) {\r
596                         variableReferences = getAllVariableReferences_(getModelIDNew(), new int[variableNames.length]); \r
597                 }\r
598                 return variableReferences;\r
599 \r
600             } catch (UnsatisfiedLinkError err) {\r
601                 throw new FMILException(UNSATISFIED_LINK);\r
602             } catch (Exception e) {\r
603                 throw new FMILException(e.getMessage());\r
604             }\r
605         }\r
606     }\r
607 \r
608     private native int[] getAllVariableReferences_(int id, int[] array);\r
609     \r
610 //\r
611 //    /**\r
612 //     * Get all variables from model that match the filter (and time variable)\r
613 //     * \r
614 //     * @param regexp Regular expression filter\r
615 //     * @return An array of variable names that match regexp filter (and time-variable)\r
616 //     * @throws FMILException\r
617 //     */\r
618 //    public String[] filterVariables(String regexp) throws FMILException {       \r
619 //        synchronized(syncObject) {\r
620 //            try {\r
621 //\r
622 //                return filterVariables_(getModelID(), regexp + "|time");\r
623 //\r
624 //            } catch (UnsatisfiedLinkError err) {\r
625 //                throw new FMILException(UNSATISFIED_LINK);\r
626 //            } catch (Exception e) {\r
627 //                throw new FMILException(e.getMessage());\r
628 //            }\r
629 //        }\r
630 //    }\r
631 //\r
632 //    private native String[] filterVariables_(String id, String regexp);\r
633 //\r
634     /**\r
635      * Get the last error message\r
636      * @return Last error message\r
637      */\r
638     public String getLastErrorMessage() throws FMILException {\r
639         synchronized(syncObject) {\r
640 \r
641             try {\r
642 \r
643                 return "err";\r
644                 //return getLastErrorMessage_(getModelID());\r
645 \r
646             } catch (UnsatisfiedLinkError err) {\r
647                 throw new FMILException(UNSATISFIED_LINK);\r
648             } catch (Exception e) {\r
649                 throw new FMILException(e.getMessage());\r
650             }\r
651         }\r
652     }\r
653 //\r
654 //    private native String getLastErrorMessage_(String id);\r
655 \r
656     /**\r
657      * Get a real (double) value for variable\r
658      * @param name Name of the variable\r
659      * @return value\r
660      * @throws FMILException\r
661      */\r
662     public double getRealValue(String name) throws FMILException {\r
663         synchronized(syncObject) {\r
664 \r
665             try {\r
666                 // TODO: printtaa id ja name, jotta saadaan virheessä kiinni\r
667                 double result = getRealValue_(getModelIDNew(), variableMap.get(name));\r
668                 System.err.println("getRealValue " + name + " = " + result);\r
669                 return  result;\r
670             } catch (UnsatisfiedLinkError err) {\r
671                 throw new FMILException(UNSATISFIED_LINK);\r
672             } catch (Exception e) {\r
673                 throw new FMILException(e.getMessage());\r
674             }\r
675         }\r
676     }\r
677 \r
678     private native double getRealValue_(int id, int variableReference);\r
679 \r
680 //    /**\r
681 //     * Get a string value for variable\r
682 //     * @param name Name of the variable\r
683 //     * @return value\r
684 //     * @throws FMILException\r
685 //     */\r
686 //    public String getStringValue(String name) throws FMILException {\r
687 //        synchronized(syncObject) {\r
688 //\r
689 //            try {\r
690 //                return getStringValue_(getModelID(), name); \r
691 //            } catch (UnsatisfiedLinkError err) {\r
692 //                throw new FMILException(UNSATISFIED_LINK);\r
693 //            } catch (Exception e) {\r
694 //                throw new FMILException(e.getMessage());\r
695 //            }\r
696 //        }\r
697 //    }\r
698 //\r
699 //    private native String getStringValue_(String id, String name);\r
700 //\r
701 //    /**\r
702 //     * Get an integer value for variable\r
703 //     * @param name Name of the variable\r
704 //     * @return value\r
705 //     * @throws FMILException\r
706 //     */\r
707 //    public int getIntegerValue(String name) throws FMILException {\r
708 //        synchronized(syncObject) {\r
709 //\r
710 //            try {\r
711 //                return getIntegerValue_(getModelID(), name); \r
712 //            } catch (UnsatisfiedLinkError err) {\r
713 //                throw new FMILException(UNSATISFIED_LINK);\r
714 //            } catch (Exception e) {\r
715 //                throw new FMILException(e.getMessage());\r
716 //            }\r
717 //        }\r
718 //    }\r
719 //\r
720 //    private native int getIntegerValue_(String id, String name);\r
721 //\r
722 //    /**\r
723 //     * Get a real (double) value for variable\r
724 //     * @param name Name of the variable\r
725 //     * @return value\r
726 //     * @throws FMILException\r
727 //     */\r
728 //    public boolean getBooleanValue(String name) throws FMILException {\r
729 //        synchronized(syncObject) {\r
730 //\r
731 //            try {\r
732 //                return getBooleanValue_(getModelID(), name); \r
733 //            } catch (UnsatisfiedLinkError err) {\r
734 //                throw new FMILException(UNSATISFIED_LINK);\r
735 //            } catch (Exception e) {\r
736 //                throw new FMILException(e.getMessage());\r
737 //            }\r
738 //        }\r
739 //    }\r
740 //\r
741 //    private native boolean getBooleanValue_(String id, String name);\r
742 \r
743     private FileChannel channel; \r
744     private FileLock lock;\r
745 \r
746     @SuppressWarnings("resource")\r
747     private boolean lockFMUDirectory() {\r
748 \r
749         try {\r
750             // Get a file channel for the lock file\r
751             File lockFile = new File(TEMP_FMU_DIRECTORY, LOCK_FILE_NAME);\r
752             if(!lockFile.isFile())\r
753                 lockFile.createNewFile();\r
754 \r
755             channel = new RandomAccessFile(lockFile, "rw").getChannel();\r
756 \r
757             // Use the file channel to create a lock on the file.\r
758             // This method blocks until it can retrieve the lock.\r
759             lock = channel.lock();\r
760 \r
761             //          // Try acquiring the lock without blocking. This method returns\r
762             //          // null or throws an exception if the file is already locked.\r
763             //          try {\r
764             //              lock = channel.tryLock();\r
765             //          } catch (OverlappingFileLockException e) {\r
766             //              // File is already locked in this thread or virtual machine\r
767             //          }\r
768         } catch (IOException e) {\r
769             return false;\r
770         }\r
771 \r
772         return true;\r
773     }\r
774 \r
775     private boolean unlockFMUDirectory() {\r
776         try {\r
777             // Release the lock\r
778             if(lock != null)\r
779                 lock.release();\r
780 \r
781             // Close the file\r
782             if(channel != null)\r
783                 channel.close();\r
784         } catch (IOException e) {\r
785             return false;\r
786         }\r
787         return true;\r
788     }\r
789 \r
790     private boolean removeFMUDirectoryContents() {\r
791         // Remove contents\r
792         try {\r
793             File tempDir = new File(TEMP_FMU_DIRECTORY);\r
794             FileUtils.deleteAll(tempDir);\r
795             tempDir.delete();\r
796         } catch (IOException e) {\r
797             return false;\r
798         }\r
799         return true;\r
800     }\r
801 }\r