*/
private static int OK = 0;
private static int ERROR = 1;
+ private static int PENDING = 2;
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;
private String[] declaredTypeQuantities;
private String[] declaredTypeUnits;
- private TObjectIntHashMap<String> variableMap = new TObjectIntHashMap<String>();
+ private static int NO_VARIABLE_KEY = -1;
+
+ private TObjectIntHashMap<String> variableMap = new TObjectIntHashMap<String>(10, 0.5f, NO_VARIABLE_KEY);
private Set<String> subscriptionSet = new HashSet<String>();
private TIntArrayList subscription = new TIntArrayList();
synchronized(syncObject) {
// Safety check
int vr = variableMap.get(name);
- if(vr == 0) return false;
+ if(vr == NO_VARIABLE_KEY) return false;
if(!subscriptionSet.add(name)) return false;
subscribedNames.add(name);
subscription.add(vr);
* @throws FMILException
*/
private int fmuN = 0;
- private boolean fmuLoaded = false;
+ private boolean instantiated = false;
public void loadFMUFile(String path) throws FMILException {
if (!Files.exists(Paths.get(path)))
for(int i=0;i<variableNames.length;i++) {
variableMap.put(variableNames[i], variableReferences[i]);
}
-
- fmuLoaded = true;
+
+ instantiated = false;
} catch (UnsatisfiedLinkError err) {
throw new FMILException(UNSATISFIED_LINK, err);
} catch (Exception e) {
try {
int ret = instantiateSimulation_(getModelIDNew());
- if(ret != OK)
+ if(ret != OK) {
LOGGER.warn("Function return value != OK, an exception should have been thrown from native code!");
-
+ } else {
+ instantiated = true;
+ }
} catch (FMILException e) {
throw e;
} catch (UnsatisfiedLinkError err) {
* @throws FMILException
*/
public void setRealValue(String name, double value) throws FMILException {
- setRealValue(variableMap.get(name), value);
+ int key = variableMap.get(name);
+ if(key == NO_VARIABLE_KEY)
+ throw new FMILException("No variable with name " + name);
+ setRealValue(key, value);
}
/**
* @throws FMILException
*/
public void setIntegerValue(String name, int value) throws FMILException {
- setIntegerValue(variableMap.get(name), value);
+ int key = variableMap.get(name);
+ if(key == NO_VARIABLE_KEY)
+ throw new FMILException("No variable with name " + name);
+ setIntegerValue(key, value);
}
/**
* @throws FMILException
*/
public void setBooleanValue(String name, boolean value) throws FMILException {
- setBooleanValue(variableMap.get(name), value);
+ int key = variableMap.get(name);
+ if(key == NO_VARIABLE_KEY)
+ throw new FMILException("No variable with name " + name);
+ setBooleanValue(key, value);
}
/**
* @throws FMILException
*/
public void setStringValue(String name, String value) throws FMILException {
- setStringValue(variableMap.get(name), value);
+ int key = variableMap.get(name);
+ if(key == NO_VARIABLE_KEY)
+ throw new FMILException("No variable with name " + name);
+ setStringValue(key, value);
}
/**
try {
- int ret = simulateStep_(getModelIDNew());
- if(ret != OK)
+ int ret = simulateStep_(getModelIDNew()); //0 is ok, 1 is error, 2 is pending
+ if(ret == PENDING)
+ LOGGER.warn("Pending status return from FMU. This is not implemented in our Simulator yet!");
+ else if(ret != OK)
LOGGER.warn("Function return value != OK, an exception should have been thrown from native code!");
-
} catch (FMILException e) {
throw e;
} catch (UnsatisfiedLinkError err) {
try {
unlockFMUDirectory();
- if(fmuLoaded) {
+
+ // Many FMUs will not correctly clean-up during unload if the simulation isn't instantiated
+ // Essentially we'd likely be passing an invalid pointer to the FMU to cleanup, causing native c-level crashes.
+
+ if(instantiated) {
int ret = unloadFMU_(getModelIDNew());
if(ret != OK)
LOGGER.warn("Function return value != OK, an exception should have been thrown from native code!");
- fmuLoaded = false;
+ instantiated = false;
}
removeFMUDirectoryContents();
* @throws FMILException
*/
public double getRealValue(String name) throws FMILException {
- double result = getRealValue(variableMap.get(name));
+ int key = variableMap.get(name);
+ if(key == NO_VARIABLE_KEY)
+ throw new FMILException("No variable with name " + name);
+ double result = getRealValue(key);
if (DEBUG) System.err.println("getRealValue " + name + " = " + result);
return result;
}
* @throws FMILException
*/
public int getIntegerValue(String name) throws FMILException {
- int result = getIntegerValue(variableMap.get(name));
+ int key = variableMap.get(name);
+ if(key == NO_VARIABLE_KEY)
+ throw new FMILException("No variable with name " + name);
+ int result = getIntegerValue(key);
if (DEBUG) System.err.println("getIntegerValue " + name + " = " + result);
return result;
}
* @throws FMILException
*/
public boolean getBooleanValue(String name) throws FMILException {
- boolean result = getBooleanValue(variableMap.get(name));
+ int key = variableMap.get(name);
+ if(key == NO_VARIABLE_KEY)
+ throw new FMILException("No variable with name " + name);
+ boolean result = getBooleanValue(key);
if (DEBUG) System.err.println("getBooleanValue " + name + " = " + result);
return result;
}
* @throws FMILException
*/
public String getStringValue(String name) throws FMILException {
- String result = getStringValue(variableMap.get(name));
+ int key = variableMap.get(name);
+ if(key == NO_VARIABLE_KEY)
+ throw new FMILException("No variable with name " + name);
+ String result = getStringValue(key);
if (DEBUG) System.err.println("getIntegerValue " + name + " = " + result);
return result;
}