if(experiment == null)\r
return new double[0];\r
\r
- FMUControlJNI control = null;\r
- if(experiment instanceof SysdynGameExperiment) {\r
- SysdynGameExperiment exp = (SysdynGameExperiment)experiment;\r
- ExperimentState state = exp.getSysdynExperimentState();\r
- // Get value from control only if the simulation is running or stopped, not before initialization\r
- if(ExperimentState.RUNNING.equals(state) || ExperimentState.STOPPED.equals(state))\r
- control = ((SysdynGameExperiment)this.experiment).getFMUControl();\r
- }\r
-\r
Collection<SysdynResult> results = experiment.getActiveResults();\r
ArrayList<String> variableNames = getVariableNamesWithIndexNumbers();\r
\r
double[] result = new double[variableNames.size()];\r
for(int i = 0; i < variableNames.size(); i++) {\r
for(SysdynResult r : results) { \r
- if(control != null) {\r
- try {\r
- result[i] = control.getRealValue(variableNames.get(i));\r
- } catch (FMUJNIException e) {\r
- result[i] = 0;\r
- }\r
+ if(experiment instanceof SysdynGameExperiment) {\r
+ result[i] = ((SysdynGameExperiment)experiment).getCurrentValue(variableNames.get(i));\r
} else {\r
SysdynDataSet ds = r.getDataSet(variableNames.get(i));\r
if(ds != null && ds.values != null && ds.values.length > 0) {\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.fmu.FMUJNIException;\r
import org.simantics.sysdyn.manager.SysdynDataSet;\r
import org.simantics.sysdyn.manager.SysdynGameExperiment;\r
import org.simantics.sysdyn.manager.SysdynResult;\r
result[i][j] = ds.values[j];\r
}\r
\r
- // if this is the actual simulation result and experiment is game experiment, \r
- // get the last value from fmu control.\r
+ /*\r
+ * If this is the actual simulation result and experiment is game experiment, \r
+ * get the last value from experiment. It might be different from the result\r
+ * file, if it has been modified in current time step \r
+ */\r
if(experiment instanceof SysdynGameExperiment && ds.result == null) {\r
- try {\r
- double d = ((SysdynGameExperiment)experiment).getFMUControl().getRealValue(variableNames.get(i));\r
- result[i][ds.values.length - 1] = d;\r
- } catch (FMUJNIException e) {\r
- e.printStackTrace();\r
- }\r
+ double d = ((SysdynGameExperiment)experiment).getCurrentValue(variableNames.get(i));\r
+ result[i][ds.values.length - 1] = d;\r
}\r
- \r
+\r
} else {\r
result[i] = new double[0];\r
}\r
\r
private FMUControlJNI control;\r
private String[] subscription;\r
+ private HashMap<String, Integer> subscriptionIndexes;\r
private HashMap<String, ArrayList<Double>> results;\r
private double stepLength = DEFAULT_STEP_LENGTH;\r
private double stepDuration = DEFAULT_STEP_DURATION;\r
public static double DEFAULT_STEP_LENGTH = 0.1;\r
public static int DEFAULT_OUTPUT_INTERVAL = 1;\r
\r
+ double[] currentValues; // Current values from FMU. Updated with updateSubscriptions\r
+ \r
public SysdynGameExperiment(Resource experiment, Resource model) {\r
super(experiment, model);\r
}\r
\r
double time = control.getTime();\r
double eTime = time + duration;\r
- double[] results = new double[subscription.length];\r
\r
monitor.worked(1);\r
\r
\r
if(stepNumber % savePer == 0) {\r
monitor.subTask("Get results (time = " + control.getTime() + ")");\r
- results = control.getSubscribedResults(results);\r
+ currentValues = control.getSubscribedResults(currentValues);\r
monitor.worked(1);\r
\r
monitor.subTask("Save results (time = " + control.getTime() + ")");\r
for(int k = 0; k < subscription.length; k++) {\r
- SysdynGameExperiment.this.results.get(subscription[k]).add(results[k]);\r
+ SysdynGameExperiment.this.results.get(subscription[k]).add(currentValues[k]);\r
}\r
} else {\r
monitor.worked(1);\r
subscription = control.getAllVariables();\r
else\r
subscription = control.filterVariables(inits.get("variableFilter"));\r
- control.subscribe(subscription); // subscribe all variables\r
+ \r
+ // Initialize subscription indexes map for fast result reading in getValue()\r
+ if(subscriptionIndexes == null)\r
+ subscriptionIndexes = new HashMap<String, Integer>();\r
+ \r
+ subscriptionIndexes.clear();\r
+ for(int i = 0; i < subscription.length; i++) {\r
+ subscriptionIndexes.put(subscription[i], i);\r
+ }\r
+\r
+ // Initialize container for current simulation results\r
+ currentValues = new double[subscription.length];\r
+ \r
+ // subscribe all variables\r
+ control.subscribe(subscription); \r
\r
getInitialResultValues();\r
\r
// Initialize results\r
results.clear();\r
\r
- double[] initialValues = new double[subscription.length];\r
- initialValues = control.getSubscribedResults(initialValues);\r
+ currentValues = control.getSubscribedResults(currentValues);\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
+ results.get(subscription[k]).add(currentValues[k]);\r
}\r
\r
getCurrentResult().setResult(new GameResult(this.results, this.subscription));\r
System.err.println("SysdynGameExperiment getInitialResultValues failed: " + e.getMessage());\r
}\r
}\r
+ \r
+ @Override\r
+ public void updateSubscriptions() {\r
+ try {\r
+ currentValues = control.getSubscribedResults(currentValues);\r
+ } catch (FMUJNIException e) {\r
+ e.printStackTrace();\r
+ }\r
+ super.updateSubscriptions();\r
+ }\r
+ \r
+ public Double getCurrentValue(String name) {\r
+ Integer index = subscriptionIndexes.get(name);\r
+ if(index != null) {\r
+ return currentValues[index];\r
+ } else {\r
+ return null;\r
+ }\r
+ }\r
}\r