JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_setStepLength_1\r
(JNIEnv *, jobject, jstring, jdouble);\r
\r
+/*\r
+ * Class: org_simantics_fmu_FMUControlJNI\r
+ * Method: instantiateSimulation_\r
+ * Signature: (Ljava/lang/String;)I\r
+ */\r
+JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_instantiateSimulation_1\r
+ (JNIEnv *, jobject, jstring);\r
+\r
/*\r
* Class: org_simantics_fmu_FMUControlJNI\r
* Method: initializeSimulation_\r
JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_unloadFMU_1\r
(JNIEnv *, jobject, jstring);\r
\r
+/*\r
+ * Class: org_simantics_fmu_FMUControlJNI\r
+ * Method: isInitialized_\r
+ * Signature: (Ljava/lang/String;)Z\r
+ */\r
+JNIEXPORT jboolean JNICALL Java_org_simantics_fmu_FMUControlJNI_isInitialized_1\r
+ (JNIEnv *, jobject, jstring);\r
+\r
/*\r
* Class: org_simantics_fmu_FMUControlJNI\r
* Method: getTime_\r
int nz; // number of state event indicators\r
double *z; // state event indicators\r
double *prez; // previous values of state event indicators\r
+ \r
+ bool initialized; // has the fmu been initialized\r
\r
vector<int> subscription; // result subscriptions\r
vector<string> allVariables; // all variables in an initialized model\r
}\r
}\r
\r
+JNIEXPORT jboolean JNICALL Java_org_simantics_fmu_FMUControlJNI_isInitialized_1\r
+ (JNIEnv *env, jobject obj, jstring id) {\r
+ const char *fmuId = env->GetStringUTFChars(id, 0);\r
+ if(exists(fmuId)) {\r
+ FMUControlStruct& fmuStruct = fmus[fmuId];\r
+ env->ReleaseStringUTFChars(id, fmuId);\r
+ return fmuStruct.initialized;\r
+ } else {\r
+ string message = fmuId;\r
+ env->ReleaseStringUTFChars(id, fmuId);\r
+ throwException(env, "getTime: Model id " + message + " not found");\r
+ return false;\r
+ }\r
+}\r
+\r
+\r
JNIEXPORT jdouble JNICALL Java_org_simantics_fmu_FMUControlJNI_getTime_1\r
(JNIEnv *env, jobject obj, jstring id) {\r
const char *fmuId = env->GetStringUTFChars(id, 0);\r
return result;\r
}\r
\r
-JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_initializeSimulation_1\r
+JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_instantiateSimulation_1\r
(JNIEnv *env, jobject obj, jstring id) {\r
\r
const char *fmuId = env->GetStringUTFChars(id, 0);\r
if(fmu.modelDescription == NULL)\r
return throwException(env, "No FMU loaded");\r
\r
- fmiBoolean toleranceControlled = fmiFalse;\r
-\r
fmuStruct.currentTime = 0; // start time\r
\r
// instantiate the fmu\r
//if (!x || !xdot) return error("out of memory");\r
if (!fmuStruct.x || !fmuStruct.xdot || fmuStruct.nz>0 && (!fmuStruct.z || !fmuStruct.prez)) return throwException(env, "out of memory");\r
\r
- // set the start time and initialize\r
+ // set the start time\r
fmuStruct.fmiFlag = fmu.setTime(fmuStruct.c, fmuStruct.currentTime);\r
if (fmuStruct.fmiFlag > fmiWarning) return throwException(env, "could not set time");\r
- fmuStruct.fmiFlag = fmu.initialize(fmuStruct.c, toleranceControlled, fmuStruct.currentTime, &(fmuStruct.eventInfo));\r
- if (fmuStruct.fmiFlag > fmiWarning) return throwException(env, "could not initialize model");\r
\r
// Clear all variables -vector\r
fmuStruct.allVariables.clear();\r
fmuStruct.indexes.insert ( pair<string,int>(s,k) );\r
}\r
\r
+ // Set initialized flag to 0\r
+ fmuStruct.initialized = false;\r
+\r
+ fflush(stdout);\r
+ return 1;\r
+\r
+ } else {\r
+ string message = fmuId;\r
+ env->ReleaseStringUTFChars(id, fmuId);\r
+ return throwException(env, "instantiateSimulation: Model id " + message + " not found");\r
+ }\r
+}\r
+\r
+JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_initializeSimulation_1\r
+ (JNIEnv *env, jobject obj, jstring id) {\r
+\r
+ const char *fmuId = env->GetStringUTFChars(id, 0);\r
+ if(exists(fmuId)) {\r
+ FMUControlStruct& fmuStruct = fmus[fmuId];\r
+ env->ReleaseStringUTFChars(id, fmuId);\r
+\r
+ FMU& fmu = fmuStruct.fmu;\r
+\r
+ if(fmu.modelDescription == NULL)\r
+ return throwException(env, "No FMU loaded");\r
+\r
+ if(fmuStruct.initialized == true)\r
+ return throwException(env, "FMU already initialized. Instantiate it first.");\r
+\r
+ fmiBoolean toleranceControlled = fmiFalse;\r
+\r
+ fmuStruct.fmiFlag = fmu.initialize(fmuStruct.c, toleranceControlled, fmuStruct.currentTime, &(fmuStruct.eventInfo));\r
+ if (fmuStruct.fmiFlag > fmiWarning) return throwException(env, "could not initialize model");\r
+\r
+ fmuStruct.initialized = true;\r
+\r
fflush(stdout);\r
return 1;\r
\r
}\r
\r
if(fmuStruct.x == NULL) {\r
- return throwException(env, "Simulate step failed - fmu not initialized");\r
+ return throwException(env, "Simulate step failed - fmu not instantiated");\r
+ }\r
+\r
+ if(fmuStruct.initialized == false) {\r
+ fmiBoolean toleranceControlled = fmiFalse;\r
+ fmuStruct.fmiFlag = fmuStruct.fmu.initialize(fmuStruct.c, toleranceControlled, fmuStruct.currentTime, &(fmuStruct.eventInfo));\r
+ if (fmuStruct.fmiFlag > fmiWarning) return throwException(env, "could not initialize model");\r
+ fmuStruct.initialized = true;\r
}\r
\r
FMU& fmu = fmuStruct.fmu;\r
private native int setStepLength_(String id, double step);\r
\r
/**\r
- * Initializes a simulation. \r
+ * Instantiates a simulation. \r
* <p>\r
* Make sure that an FMU is loaded first.\r
* @throws FMUJNIException\r
*/\r
+ public void instantiateSimulation() throws FMUJNIException {\r
+ synchronized(syncObject) {\r
+\r
+ try {\r
+\r
+ int ret = instantiateSimulation_(getModelID()); \r
+ if(ret == ERROR)\r
+ throw new FMUJNIException(getLastErrorMessage());\r
+\r
+ } catch (UnsatisfiedLinkError err) {\r
+ throw new FMUJNIException(UNSATISFIED_LINK);\r
+ } catch (Exception e) {\r
+ throw new FMUJNIException(e.getMessage());\r
+ }\r
+ }\r
+ }\r
+\r
+ private native int instantiateSimulation_(String id);\r
+\r
+ \r
+ /**\r
+ * Initializes a simulation. \r
+ * <p>\r
+ * Make sure that simulation is instantiated first!\r
+ * @throws FMUJNIException\r
+ */\r
public void initializeSimulation() throws FMUJNIException {\r
synchronized(syncObject) {\r
\r
}\r
}\r
private native int unloadFMU_(String id);\r
+ \r
+ /**\r
+ * Checks if fmu has been initialized\r
+ * @return current simulation time\r
+ */\r
+ public boolean isInitialized() throws FMUJNIException {\r
+ synchronized(syncObject) {\r
+ try {\r
+ return isInitialized_(getModelID());\r
+ } catch (UnsatisfiedLinkError err) {\r
+ throw new FMUJNIException(UNSATISFIED_LINK);\r
+ } catch (Exception e) {\r
+ throw new FMUJNIException(e.getMessage());\r
+ }\r
+ }\r
+ }\r
+\r
+ private native boolean isInitialized_(String id);\r
\r
/**\r
* Get the current simulation time\r
JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_setStepLength_1\r
(JNIEnv *, jobject, jstring, jdouble);\r
\r
+/*\r
+ * Class: org_simantics_fmu_FMUControlJNI\r
+ * Method: instantiateSimulation_\r
+ * Signature: (Ljava/lang/String;)I\r
+ */\r
+JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_instantiateSimulation_1\r
+ (JNIEnv *, jobject, jstring);\r
+\r
/*\r
* Class: org_simantics_fmu_FMUControlJNI\r
* Method: initializeSimulation_\r
JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_unloadFMU_1\r
(JNIEnv *, jobject, jstring);\r
\r
+/*\r
+ * Class: org_simantics_fmu_FMUControlJNI\r
+ * Method: isInitialized_\r
+ * Signature: (Ljava/lang/String;)Z\r
+ */\r
+JNIEXPORT jboolean JNICALL Java_org_simantics_fmu_FMUControlJNI_isInitialized_1\r
+ (JNIEnv *, jobject, jstring);\r
+\r
/*\r
* Class: org_simantics_fmu_FMUControlJNI\r
* Method: getTime_\r
import org.eclipse.core.runtime.Platform;\r
import org.eclipse.core.runtime.Status;\r
import org.eclipse.core.runtime.jobs.Job;\r
-import org.simantics.Simantics;\r
import org.simantics.db.Resource;\r
import org.simantics.db.Session;\r
import org.simantics.db.WriteGraph;\r
int resultItemsNumber = sysdynResult.numberOfVariables();\r
monitor.beginTask("Save result", resultItemsNumber * 2); \r
try {\r
- File file = Simantics.getSession().syncRequest(new WriteResultRequest<File>() {\r
+ File file = session.syncRequest(new WriteResultRequest<File>() {\r
\r
@Override\r
public File perform(WriteGraph graph) throws DatabaseException {\r
import org.simantics.db.AsyncReadGraph;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.db.procedure.AsyncListener;\r
import org.simantics.db.request.Read;\r
try {\r
control.loadFMUFile(simulationLocation.executableFile.getAbsolutePath()); // unzip and load fmu\r
control.setStepLength(stepLength); // FIXME: fixed step lenghth\r
- control.initializeSimulation(); // initialize simulation\r
- if(inits.get("variableFilter") == null || inits.get("variableFilter").equals(".*"))\r
- subscription = control.getAllVariables();\r
- else\r
- subscription = control.filterVariables(inits.get("variableFilter"));\r
- control.subscribe(subscription); // subscribe all variables\r
- \r
- // Initialize results\r
- results.clear();\r
- \r
- double[] initialValues = new double[subscription.length];\r
- initialValues = control.getSubscribedResults(initialValues);\r
- for(int k = 0; k < subscription.length; k++) {\r
- results.put(subscription[k], new ArrayList<Double>());\r
- results.get(subscription[k]).add(initialValues[k]);\r
- }\r
- \r
- getCurrentResult().setResult(new GameResult(this.results, this.subscription));\r
- resultsChanged();\r
+ instantiate();\r
\r
} catch (FMUJNIException e) {\r
System.err.println("SysdynGameExperiment initialization failed:\n\t" + e.getMessage());\r
\r
\r
try {\r
+ \r
+ // initialize if not initialized\r
+ if(!control.isInitialized()) {\r
+ control.initializeSimulation();\r
+ getInitialResultValues();\r
+ }\r
+ \r
control.setStepLength(stepLength); // Set step length each time in case there has been changes\r
\r
double time = control.getTime();\r
return;\r
\r
if(time >-0.001 && time < 0.001) {\r
- initialize();\r
+ instantiate();\r
} else {\r
System.out.println("rewindTo");\r
}\r
}\r
\r
- private synchronized void initialize() {\r
+ @Override\r
+ public void refresh(Session session) {\r
+ try {\r
+ control.initializeSimulation();\r
+ getInitialResultValues();\r
+ \r
+ } catch (FMUJNIException e) {\r
+ System.err.println("SysdynGameExperiment instantiate failed: " + e.getMessage());\r
+ }\r
+ }\r
+ \r
+ private synchronized void instantiate() {\r
try {\r
HashMap<String, String> inits = getExperimentParameters(null);\r
\r
control.setStepLength(stepLength); // FIXME: fixed step lenghth\r
- control.initializeSimulation(); // initialize simulation\r
+ control.instantiateSimulation(); // instantiate simulation\r
if(inits.get("variableFilter") == null || inits.get("variableFilter").equals(".*"))\r
subscription = control.getAllVariables();\r
else\r
subscription = control.filterVariables(inits.get("variableFilter"));\r
control.subscribe(subscription); // subscribe all variables\r
\r
+ getInitialResultValues();\r
+ \r
+ } catch (FMUJNIException e) {\r
+ System.err.println("SysdynGameExperiment instantiate failed: " + e.getMessage());\r
+ }\r
+ }\r
+\r
+ private synchronized void getInitialResultValues() {\r
+ try {\r
// Initialize results\r
results.clear();\r
- \r
+\r
double[] initialValues = new double[subscription.length];\r
initialValues = control.getSubscribedResults(initialValues);\r
for(int k = 0; k < subscription.length; k++) {\r
results.put(subscription[k], new ArrayList<Double>());\r
results.get(subscription[k]).add(initialValues[k]);\r
}\r
- \r
+\r
getCurrentResult().setResult(new GameResult(this.results, this.subscription));\r
- resultsChanged(); \r
+ resultsChanged(); \r
} catch (FMUJNIException e) {\r
- System.err.println("SysdynGameExperiment rewind failed: " + e.getMessage());\r
+ System.err.println("SysdynGameExperiment getInitialResultValues failed: " + e.getMessage());\r
}\r
}\r
}\r