X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=org.simantics.fmil.core%2Fsrc%2Forg%2Fsimantics%2Ffmil%2Fcore%2FFMIL.java;h=1f1e098d7eade1d37c3edd6a2c5dec8c13acff83;hb=f5e59cf2a25d35424f2b85ea382f2db13bcadff7;hp=3a7c031dc2522a60d8d1ff2e2028947a0e16cb8b;hpb=b74cf2632ea8a42163add129b4fe8ec36a4f39d1;p=simantics%2Ffmil.git diff --git a/org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java b/org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java index 3a7c031..1f1e098 100644 --- a/org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java +++ b/org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java @@ -6,8 +6,10 @@ import java.io.RandomAccessFile; import java.net.URL; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; +import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -61,7 +63,7 @@ public class FMIL { simulatorFMIUrl = FileLocator.find(b, new Path("libraries/FMUSimulator.dll"), null); } else if(env.os == OSType.LINUX) { sharedFMILIBUrl = FileLocator.find(b, new Path("libraries/libfmilib_shared.so"), null); - simulatorFMIUrl = FileLocator.find(b, new Path("libraries/FMUSimulator.so"), null); + simulatorFMIUrl = FileLocator.find(b, new Path("libraries/libFMUSimulator.so"), null); } libraries[0] = new File(FileLocator.toFileURL(sharedFMILIBUrl).getPath()); @@ -129,15 +131,16 @@ public class FMIL { } public boolean subscribe(String name) throws FMILException { - // Safety check - int vr = variableMap.get(name); - if(vr == 0) return false; - if(!subscriptionSet.add(name)) return false; - subscribedNames.add(name); - System.err.println("subscribed : " + name + " => " + subscribedNames.size()); - subscription.add(vr); - subscribe(new int[] { vr }); - return true; + synchronized(syncObject) { + // Safety check + int vr = variableMap.get(name); + if(vr == 0) return false; + if(!subscriptionSet.add(name)) return false; + subscribedNames.add(name); + subscription.add(vr); + subscribe(new int[] { vr }); + return true; + } } public FMIL() { @@ -181,6 +184,11 @@ public class FMIL { private boolean fmuLoaded = false; public void loadFMUFile(String path) throws FMILException { + if (!Files.exists(Paths.get(path))) + throw new FMILException("File " + path + " does not exist"); + if (!Files.isRegularFile(Paths.get(path))) + throw new FMILException("Path " + path + " is not a file"); + synchronized(syncObject) { if(fmuN % 2 == 0) { @@ -191,23 +199,25 @@ public class FMIL { fmuN = 0; } - File tempDir = new File(fmuDir); - if(tempDir.isDirectory()) { + java.nio.file.Path tempDir = Paths.get(fmuDir); + if(Files.exists(tempDir) && Files.isDirectory(tempDir)) { try { - FileUtils.deleteAll(tempDir); + FileUtils.emptyDirectory(tempDir); } catch (IOException e) { - throw new FMILException("Could not create temp folder for fmu"); + throw new FMILException("Could not delete existing files from temp folder for fmu " + path, e); } - tempDir.mkdir(); } else { - tempDir.mkdir(); + try { + Files.createDirectory(tempDir); + } catch (IOException e) { + throw new FMILException("Could not create temp folder for fmu " + path, e); + } } - try { - String tmpPath = tempDir.getAbsolutePath(); - if(!tmpPath.endsWith("\\")) - tmpPath = tmpPath + "\\"; + String tmpPath = tempDir.toString(); + if(!tmpPath.endsWith("\\") && !tmpPath.endsWith("/")) + tmpPath = tmpPath + "/"; id = loadFMUFile_(path, tmpPath); getAllVariables(); @@ -221,12 +231,13 @@ public class FMIL { } catch (UnsatisfiedLinkError err) { throw new FMILException(UNSATISFIED_LINK, err); } catch (Exception e) { + LOGGER.error(e.getMessage()); throw new FMILException(e.getMessage()); } } } - private native int loadFMUFile_(String path, String toDir); + private native int loadFMUFile_(String path, String toDir) throws FMILException; /** * Set a step length for simulation @@ -494,10 +505,11 @@ public class FMIL { synchronized(syncObject) { try { - + double[] results = new double[subscription.size()]; - return getSubscribedResults_(getModelIDNew(), results); - + Arrays.fill(results, Double.NaN); + + return getSubscribedResults_(getModelIDNew(), results); } catch (UnsatisfiedLinkError err) { throw new FMILException(UNSATISFIED_LINK); } catch (Exception e) {