X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.fmil.core%2Fsrc%2Forg%2Fsimantics%2Ffmil%2Fcore%2FFMIL.java;h=f4c7ca4f2fbbf7d044be10c09a2b4f896b31fe0b;hb=refs%2Fchanges%2F63%2F2163%2F2;hp=b2928b9474c9bfc9974cb876dbdc56fca8e32ff8;hpb=9dcc8cac323b2fc064007203ef76e57e152699db;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 b2928b9..f4c7ca4 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 @@ -3,19 +3,22 @@ package org.simantics.fmil.core; import java.io.File; import java.io.IOException; 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; import java.util.UUID; import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; import org.osgi.framework.Bundle; -import org.simantics.Simantics; -import org.simantics.fmil.core.ExecEnvironment.ARCHType; import org.simantics.fmil.core.ExecEnvironment.OSType; import org.simantics.utils.FileUtils; import org.slf4j.Logger; @@ -34,9 +37,9 @@ public class FMIL { */ private static int OK = 0; private static int ERROR = 1; - private static String UNSATISFIED_LINK = "Method not found. DLL might not be loaded properly."; - private static String TEMP_FMU_DIRECTORY_NAME = "fmil"; - public static String TEMP_FMU_COMMON_DIRECTORY; + private static String UNSATISFIED_LINK = "Method not found. DLL might not be loaded properly."; + public static final String TEMP_FMU_DIRECTORY_NAME = "fmil"; + public static String TEMP_FMU_COMMON_DIRECTORY; public static String LOCK_FILE_NAME = "fmil.lock"; public static Object syncObject = new Object(); @@ -45,35 +48,28 @@ public class FMIL { * Static: load native libraries required for the FMU simulation to work. */ static { - + File[] libraries = new File[2]; - Bundle bundle = null; - ExecEnvironment env = ExecEnvironment.calculate(); - if (env.os == OSType.WINDOWS) { - if (env.arch == ARCHType.X86) { - bundle = Platform.getBundle("org.simantics.fmil.win32"); - } else if (env.arch == ARCHType.X86_64) { - bundle = Platform.getBundle("org.simantics.fmil.win64"); - } - } - - if (bundle != null) { - try{ - String root = FileLocator.getBundleFile(bundle).getAbsolutePath(); -// if (env.arch == ARCHType.X86_64) { -// File newFIle = new File(root, "libraries/libexpat.dll"); -// System.load(newFIle.getAbsolutePath()); -// } -// libraries[0] = new File(root, "libraries/zlibwapi.dll"); -// libraries[1] = new File(root, "libraries/miniunz.dll"); - libraries[0] = new File(root, "libraries/fmilib_shared.dll"); - libraries[1] = new File(root, "libraries/FMUSimulator.dll"); - } - catch (Exception e) { - e.printStackTrace(); + + try { + URL sharedFMILIBUrl = null; + URL simulatorFMIUrl = null; + Bundle b = Platform.getBundle("org.simantics.fmil.core"); + + if (env.os == OSType.WINDOWS) { + sharedFMILIBUrl = FileLocator.find(b, new Path("libraries/fmilib_shared.dll"), null); + 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/libFMUSimulator.so"), null); } + + libraries[0] = new File(FileLocator.toFileURL(sharedFMILIBUrl).getPath()); + libraries[1] = new File(FileLocator.toFileURL(simulatorFMIUrl).getPath()); + } catch (Exception e) { + LOGGER.error("Failed to resolve native FMU simulation library for execution environment {}.{}", env.os, env.arch, e); } for(File library : libraries) { @@ -93,13 +89,15 @@ public class FMIL { } /** - * Static: initialize fmu temp folder + * Static: initialize fmu temp folder from current working directory */ static { - File dir = Simantics.getTemporaryDirectory(TEMP_FMU_DIRECTORY_NAME); - TEMP_FMU_COMMON_DIRECTORY = dir.getAbsolutePath(); + TEMP_FMU_COMMON_DIRECTORY = Paths.get(".").toAbsolutePath().normalize().toString(); + } + + public static void setTempFMUCommonDir(File dir) { + TEMP_FMU_COMMON_DIRECTORY = dir.getAbsolutePath(); } - private String fmuDir; private int id; @@ -133,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() { @@ -195,23 +194,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(); @@ -225,6 +226,7 @@ public class FMIL { } catch (UnsatisfiedLinkError err) { throw new FMILException(UNSATISFIED_LINK, err); } catch (Exception e) { + LOGGER.error(e.getMessage()); throw new FMILException(e.getMessage()); } } @@ -498,10 +500,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) { @@ -871,7 +874,7 @@ public class FMIL { synchronized(syncObject) { try { - // TODO: printtaa id ja name, jotta saadaan virheessä kiinni + // TODO: printtaa id ja name, jotta saadaan virheessä kiinni double result = getRealValue_(getModelIDNew(), variableMap.get(name)); System.err.println("getRealValue " + name + " = " + result); return result;