From: lempinen Date: Tue, 20 Nov 2012 11:07:08 +0000 (+0000) Subject: refs #3511 X-Git-Tag: simantics-1.10.1~112 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=d183b09e985b6bb660f77f82615b9b349fa5e7d0;p=simantics%2Fsysdyn.git refs #3511 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26373 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.fmu.me.win32/libraries/FMUSimulator.dll b/org.simantics.fmu.me.win32/libraries/FMUSimulator.dll index b07fd46a..52c78ab7 100644 Binary files a/org.simantics.fmu.me.win32/libraries/FMUSimulator.dll and b/org.simantics.fmu.me.win32/libraries/FMUSimulator.dll differ diff --git a/org.simantics.fmu/FMUSolution/FMUSimulator/include/org_simantics_fmu_FMUControlJNI.h b/org.simantics.fmu/FMUSolution/FMUSimulator/include/org_simantics_fmu_FMUControlJNI.h index 5f0cf1f9..9f2697b4 100644 --- a/org.simantics.fmu/FMUSolution/FMUSimulator/include/org_simantics_fmu_FMUControlJNI.h +++ b/org.simantics.fmu/FMUSolution/FMUSimulator/include/org_simantics_fmu_FMUControlJNI.h @@ -63,6 +63,14 @@ JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_setIntegerValue_1 JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_setBooleanValue_1 (JNIEnv *, jobject, jstring, jstring, jboolean); +/* + * Class: org_simantics_fmu_FMUControlJNI + * Method: setTime_ + * Signature: (Ljava/lang/String;D)I + */ +JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_setTime_1 + (JNIEnv *, jobject, jstring, jdouble); + /* * Class: org_simantics_fmu_FMUControlJNI * Method: simulateStep_ diff --git a/org.simantics.fmu/FMUSolution/FMUSimulator/src/fmu_control.cpp b/org.simantics.fmu/FMUSolution/FMUSimulator/src/fmu_control.cpp index c5eb62aa..f1384510 100644 --- a/org.simantics.fmu/FMUSolution/FMUSimulator/src/fmu_control.cpp +++ b/org.simantics.fmu/FMUSolution/FMUSimulator/src/fmu_control.cpp @@ -27,6 +27,11 @@ extern "C" { #include "sim_support.h" } +#define PRINT(fmt,args) { FILE *fp = fopen("R:\\Simantics\\Sysdyn\\log.txt", "ab"); fprintf(fp, fmt, args); fclose(fp); } + +#include +#define GetCurrentDir _getcwd + using namespace std; @@ -529,6 +534,25 @@ JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_initializeSimulation } } +JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_setTime_1 + (JNIEnv *env, jobject obj, jstring id, jdouble time) { + + const char *fmuId = env->GetStringUTFChars(id, 0); + if(exists(fmuId)) { + FMUControlStruct& fmuStruct = fmus[fmuId]; + env->ReleaseStringUTFChars(id, fmuId); + + FMU& fmu = fmuStruct.fmu; + + fmu.setTime(fmuStruct.c, time); + return 1; + } else { + string message = fmuId; + env->ReleaseStringUTFChars(id, fmuId); + return throwException(env, "setTime: Model id " + message + " not found"); + } +} + jobjectArray filterVariables(JNIEnv *env, jobject obj, jstring id, string regexp) { const char *fmuId = env->GetStringUTFChars(id, 0); if(exists(fmuId)) { @@ -739,9 +763,11 @@ JNIEXPORT jdouble JNICALL Java_org_simantics_fmu_FMUControlJNI_getRealValue_1 if(referenceExists(fmuStruct, name)) { fmiValueReference vr = getReference(fmuStruct, name); + double real; fmuStruct.fmu.getReal(fmuStruct.c, &vr, 1, &real); env->ReleaseStringUTFChars(variable, name); + return real; } else { diff --git a/org.simantics.fmu/src/org/simantics/fmu/FMUControlJNI.java b/org.simantics.fmu/src/org/simantics/fmu/FMUControlJNI.java index 1a778c3c..f49c2627 100644 --- a/org.simantics.fmu/src/org/simantics/fmu/FMUControlJNI.java +++ b/org.simantics.fmu/src/org/simantics/fmu/FMUControlJNI.java @@ -26,6 +26,8 @@ public class FMUControlJNI { public static String TEMP_FMU_COMMON_DIRECTORY; public static String LOCK_FILE_NAME = "fmu.lock"; + public static Object syncObject = new Object(); + /** * Static: load native libraries required for the FMU simulation to work. */ @@ -106,11 +108,11 @@ public class FMUControlJNI { TEMP_FOLDER_1 = fmuDir.toString(); TEMP_FOLDER_2 = fmuDir.toString() + "_2"; - + // Lock fmu directory in temp directory lockFMUDirectory(); } - + public String getModelID() { return dirName; } @@ -130,40 +132,43 @@ public class FMUControlJNI { private boolean fmuLoaded = false; public void loadFMUFile(String path) throws FMUJNIException { - if(fmuN % 2 == 0) { - fmuDir = TEMP_FOLDER_1; - fmuN++; - } else { - fmuDir = TEMP_FOLDER_2; - fmuN = 0; - } + synchronized(syncObject) { - File tempDir = new File(fmuDir); - if(tempDir.isDirectory()) { - try { - FileUtils.deleteAll(tempDir); - } catch (IOException e) { - throw new FMUJNIException("Could not create temp folder for fmu"); + if(fmuN % 2 == 0) { + fmuDir = TEMP_FOLDER_1; + fmuN++; + } else { + fmuDir = TEMP_FOLDER_2; + fmuN = 0; } - tempDir.mkdir(); - } else { - tempDir.mkdir(); - } + File tempDir = new File(fmuDir); + if(tempDir.isDirectory()) { + try { + FileUtils.deleteAll(tempDir); + } catch (IOException e) { + throw new FMUJNIException("Could not create temp folder for fmu"); + } + tempDir.mkdir(); + } else { + tempDir.mkdir(); + } - try { - String tmpPath = tempDir.getAbsolutePath(); - if(!tmpPath.endsWith("\\")) - tmpPath = tmpPath + "\\"; - int ret = loadFMUFile_(getModelID(), path, tmpPath); - if(ret == ERROR) - throw new FMUJNIException(getLastErrorMessage()); - - fmuLoaded = true; - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + + try { + String tmpPath = tempDir.getAbsolutePath(); + if(!tmpPath.endsWith("\\")) + tmpPath = tmpPath + "\\"; + int ret = loadFMUFile_(getModelID(), path, tmpPath); + if(ret == ERROR) + throw new FMUJNIException(getLastErrorMessage()); + + fmuLoaded = true; + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } @@ -176,16 +181,19 @@ public class FMUControlJNI { * @throws FMUJNIException */ public void setStepLength(double step) throws FMUJNIException { - try { + synchronized(syncObject) { - int ret = setStepLength_(getModelID(), step); - if(ret == ERROR) - throw new FMUJNIException(getLastErrorMessage()); + try { + + int ret = setStepLength_(getModelID(), step); + if(ret == ERROR) + throw new FMUJNIException(getLastErrorMessage()); - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } @@ -198,16 +206,19 @@ public class FMUControlJNI { * @throws FMUJNIException */ public void initializeSimulation() throws FMUJNIException { - try { + synchronized(syncObject) { - int ret = initializeSimulation_(getModelID()); - if(ret == ERROR) - throw new FMUJNIException(getLastErrorMessage()); + try { - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + int ret = initializeSimulation_(getModelID()); + if(ret == ERROR) + throw new FMUJNIException(getLastErrorMessage()); + + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } @@ -221,16 +232,19 @@ public class FMUControlJNI { * @throws FMUJNIException */ public void subscribe(String[] variables) throws FMUJNIException { - try { + synchronized(syncObject) { + + try { - int ret = subscribe_(getModelID(), variables, variables.length); - if(ret == ERROR) - throw new FMUJNIException(getLastErrorMessage()); + int ret = subscribe_(getModelID(), variables, variables.length); + if(ret == ERROR) + throw new FMUJNIException(getLastErrorMessage()); - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } @@ -246,16 +260,19 @@ public class FMUControlJNI { * @throws FMUJNIException */ public void setRealValue(String name, double value) throws FMUJNIException { - try { + synchronized(syncObject) { - int ret = setRealValue_(getModelID(), name, value); - if(ret == ERROR) - throw new FMUJNIException(getLastErrorMessage()); + try { - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + int ret = setRealValue_(getModelID(), name, value); + if(ret == ERROR) + throw new FMUJNIException(getLastErrorMessage()); + + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } @@ -270,16 +287,19 @@ public class FMUControlJNI { * @throws FMUJNIException */ public void setIntegerValue(String name, int value) throws FMUJNIException { - try { + synchronized(syncObject) { - int ret = setIntegerValue_(getModelID(), name, value); - if(ret == ERROR) - throw new FMUJNIException(getLastErrorMessage()); + try { - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + int ret = setIntegerValue_(getModelID(), name, value); + if(ret == ERROR) + throw new FMUJNIException(getLastErrorMessage()); + + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } private native int setIntegerValue_(String id, String name, int value); @@ -293,20 +313,40 @@ public class FMUControlJNI { * @throws FMUJNIException */ public void setBooleanValue(String name, boolean value) throws FMUJNIException { - try { + synchronized(syncObject) { - int ret = setBooleanValue_(getModelID(), name, value); - if(ret == ERROR) - throw new FMUJNIException(getLastErrorMessage()); + try { + + int ret = setBooleanValue_(getModelID(), name, value); + if(ret == ERROR) + throw new FMUJNIException(getLastErrorMessage()); - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } private native int setBooleanValue_(String id, String name, boolean value); + public void setTime(double time) throws FMUJNIException { + synchronized(syncObject) { + + try { + + int ret = setTime_(getModelID(), time); + if(ret == ERROR) + throw new FMUJNIException(getLastErrorMessage()); + + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } + } + } + private native int setTime_(String id, double time); /** * Simulate one step forward. The step length can be set with @@ -315,16 +355,19 @@ public class FMUControlJNI { * @throws FMUJNIException */ public void simulateStep() throws FMUJNIException { - try { + synchronized(syncObject) { - int ret = simulateStep_(getModelID()); - if(ret == ERROR) - throw new FMUJNIException(getLastErrorMessage()); + try { - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + int ret = simulateStep_(getModelID()); + if(ret == ERROR) + throw new FMUJNIException(getLastErrorMessage()); + + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } private native int simulateStep_(String id); @@ -337,14 +380,17 @@ public class FMUControlJNI { * @return */ public double[] getSubscribedResults(double[] results) throws FMUJNIException { - try { + synchronized(syncObject) { - return getSubscribedResults_(getModelID(), results); + try { - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + return getSubscribedResults_(getModelID(), results); + + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } @@ -360,20 +406,23 @@ public class FMUControlJNI { * @throws FMUJNIException */ public void unloadFMU() throws FMUJNIException { - try { + synchronized(syncObject) { - unlockFMUDirectory(); - if(fmuLoaded) { - int ret = unloadFMU_(getModelID()); - if(ret == ERROR) - throw new FMUJNIException(getLastErrorMessage()); - } - removeFMUDirectoryContents(); + try { - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + unlockFMUDirectory(); + if(fmuLoaded) { + int ret = unloadFMU_(getModelID()); + if(ret == ERROR) + throw new FMUJNIException(getLastErrorMessage()); + } + removeFMUDirectoryContents(); + + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } private native int unloadFMU_(String id); @@ -383,14 +432,17 @@ public class FMUControlJNI { * @return current simulation time */ public double getTime() throws FMUJNIException { - try { + synchronized(syncObject) { + + try { - return getTime_(getModelID()); + return getTime_(getModelID()); - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } @@ -401,19 +453,22 @@ public class FMUControlJNI { * @return all variables in a loaded model */ public String[] getAllVariables() throws FMUJNIException { - try { + synchronized(syncObject) { + + try { - return getAllVariables_(getModelID()); + return getAllVariables_(getModelID()); - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } private native String[] getAllVariables_(String id); - + /** * Get all variables from model that match the filter (and time variable) * @@ -421,18 +476,20 @@ public class FMUControlJNI { * @return An array of variable names that match regexp filter (and time-variable) * @throws FMUJNIException */ - public String[] filterVariables(String regexp) throws FMUJNIException { - try { + public String[] filterVariables(String regexp) throws FMUJNIException { + synchronized(syncObject) { + try { - return filterVariables_(getModelID(), regexp + "|time"); + return filterVariables_(getModelID(), regexp + "|time"); - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } - + private native String[] filterVariables_(String id, String regexp); /** @@ -440,14 +497,17 @@ public class FMUControlJNI { * @return Last error message */ public String getLastErrorMessage() throws FMUJNIException { - try { + synchronized(syncObject) { - return getLastErrorMessage_(getModelID()); + try { - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + return getLastErrorMessage_(getModelID()); + + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } @@ -460,12 +520,16 @@ public class FMUControlJNI { * @throws FMUJNIException */ public double getRealValue(String name) throws FMUJNIException { - try { - return getRealValue_(getModelID(), name); - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + synchronized(syncObject) { + + try { + // TODO: printtaa id ja name, jotta saadaan virheessä kiinni + return getRealValue_(getModelID(), name); + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } @@ -478,17 +542,20 @@ public class FMUControlJNI { * @throws FMUJNIException */ public String getStringValue(String name) throws FMUJNIException { - try { - return getStringValue_(getModelID(), name); - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + synchronized(syncObject) { + + try { + return getStringValue_(getModelID(), name); + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } private native String getStringValue_(String id, String name); - + /** * Get an integer value for variable * @param name Name of the variable @@ -496,17 +563,20 @@ public class FMUControlJNI { * @throws FMUJNIException */ public int getIntegerValue(String name) throws FMUJNIException { - try { - return getIntegerValue_(getModelID(), name); - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + synchronized(syncObject) { + + try { + return getIntegerValue_(getModelID(), name); + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } private native int getIntegerValue_(String id, String name); - + /** * Get a real (double) value for variable * @param name Name of the variable @@ -514,12 +584,15 @@ public class FMUControlJNI { * @throws FMUJNIException */ public boolean getBooleanValue(String name) throws FMUJNIException { - try { - return getBooleanValue_(getModelID(), name); - } catch (UnsatisfiedLinkError err) { - throw new FMUJNIException(UNSATISFIED_LINK); - } catch (Exception e) { - throw new FMUJNIException(e.getMessage()); + synchronized(syncObject) { + + try { + return getBooleanValue_(getModelID(), name); + } catch (UnsatisfiedLinkError err) { + throw new FMUJNIException(UNSATISFIED_LINK); + } catch (Exception e) { + throw new FMUJNIException(e.getMessage()); + } } } @@ -571,7 +644,7 @@ public class FMUControlJNI { } return true; } - + private boolean removeFMUDirectoryContents() { // Remove contents try { diff --git a/org.simantics.fmu/src/org_simantics_fmu_FMUControlJNI.h b/org.simantics.fmu/src/org_simantics_fmu_FMUControlJNI.h index 5f0cf1f9..9f2697b4 100644 --- a/org.simantics.fmu/src/org_simantics_fmu_FMUControlJNI.h +++ b/org.simantics.fmu/src/org_simantics_fmu_FMUControlJNI.h @@ -63,6 +63,14 @@ JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_setIntegerValue_1 JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_setBooleanValue_1 (JNIEnv *, jobject, jstring, jstring, jboolean); +/* + * Class: org_simantics_fmu_FMUControlJNI + * Method: setTime_ + * Signature: (Ljava/lang/String;D)I + */ +JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_setTime_1 + (JNIEnv *, jobject, jstring, jdouble); + /* * Class: org_simantics_fmu_FMUControlJNI * Method: simulateStep_ diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index 6e0ebb77..94ba196f 100644 Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ diff --git a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph index 7169987d..4c743105 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -23,8 +23,8 @@ SYSDYN.ImportedOntologies : PROJ.NamespaceRequirement "http://www.simantics.org/Sysdyn-1.1" : L0.URI "http://www.simantics.org/Layer0-1.1" : L0.URI "http://www.simantics.org/SelectionView-1.2" : L0.URI -// "http://www.simantics.org/Documentation-1.1" : L0.URI // Experimental documentation tool -// "http://www.simantics.org/DocumentWorkbench-1.0" : L0.URI // Experimental documentation tool + "http://www.simantics.org/Documentation-1.1" : L0.URI // Experimental documentation tool + "http://www.simantics.org/DocumentWorkbench-1.0" : L0.URI // Experimental documentation tool SYSDYN.SharedFunctionOntology -- SYSDYN.Expression.equation --> L0.String -- SYSDYN.Expression.equation --> L0.String -- SYSDYN.Expression.arrayRange --> L0.String { if(graph.isInstanceOf(r, sr.HistoryDataset)) { result.add(graph.adapt(r, AbstractNode.class)); } else { + result.add(new HistoryDataNode(r)); + String resultPath = (String)graph.getPossibleRelatedValue(r, sr.Result_resultFile); File file = new File(resultPath); - if(file.exists()) { - result.add(new HistoryDataNode(r)); - } else { + if(!file.exists()) { graph.asyncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - graph.deny(r, l0.PartOf); - graph.deny(r, graph.getInverse(SysdynResource.getInstance(graph).Experiment_result)); - + String resultPath = (String)graph.getPossibleRelatedValue(r, SysdynResource.getInstance(graph).Result_resultFile); + File file = new File(resultPath); + if(!file.exists()) { + Layer0 l0 = Layer0.getInstance(graph); + graph.deny(r, l0.PartOf); + graph.deny(r, graph.getInverse(SysdynResource.getInstance(graph).Experiment_result)); + } } }); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/CloudFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/CloudFactory.java index 2e5a5571..e9280c6e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/CloudFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/CloudFactory.java @@ -19,8 +19,13 @@ import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; import java.util.Collection; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.G2DUtils; +import org.simantics.diagram.stubs.G2DResource; import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.ElementHints; import org.simantics.g2d.element.ElementUtils; @@ -29,6 +34,7 @@ import org.simantics.g2d.element.SceneGraphNodeKey; import org.simantics.g2d.element.handler.HandleMouseEvent; import org.simantics.g2d.element.handler.InternalSize; import org.simantics.g2d.element.handler.SceneGraph; +import org.simantics.g2d.element.handler.impl.BorderColorImpl; import org.simantics.g2d.element.handler.impl.BoundsOutline; import org.simantics.g2d.element.handler.impl.DefaultTransform; import org.simantics.g2d.element.handler.impl.HoverImpl; @@ -96,9 +102,23 @@ public class CloudFactory extends SysdynElementFactory { CloudSceneGraph.INSTANCE, HoverImpl.INSTANCE, BoundsOutline.INSTANCE, + new BorderColorImpl(Color.BLACK), new WholeElementTerminals(terminals) ).setId(CloudFactory.class.getSimpleName()); } + + @Override + public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource element, IElement e) + throws DatabaseException { + super.load(graph, canvas, diagram, element, e); + + Resource colorResource = graph.getPossibleObject(element, G2DResource.getInstance(graph).HasColor); + Color color = null; + if(colorResource != null) { + color = G2DUtils.getColor(graph, colorResource); + e.setHint(ElementHints.KEY_BORDER_COLOR, color); + } + } @Override protected ElementClass compileElementClass(Resource elementType, Collection terminals) { @@ -124,7 +144,7 @@ public class CloudFactory extends SysdynElementFactory { node.setStroke(STROKE); node.setScaleStroke(true); - node.setColor(Color.BLACK); + node.setColor(ElementUtils.getBorderColor(e, Color.BLACK)); node.setShape(getCloudShape()); if(at != null) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/StockFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/StockFactory.java index fcba20c1..765f25b2 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/StockFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/StockFactory.java @@ -12,11 +12,21 @@ package org.simantics.sysdyn.ui.elements; import java.awt.BasicStroke; +import java.awt.Color; import java.awt.geom.Rectangle2D; import java.util.Collection; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.G2DUtils; +import org.simantics.diagram.stubs.G2DResource; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.ElementHints; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.impl.BorderColorImpl; import org.simantics.g2d.element.handler.impl.BoundsOutline; import org.simantics.g2d.element.handler.impl.DefaultTransform; import org.simantics.g2d.element.handler.impl.ObjectTerminal; @@ -51,8 +61,21 @@ public class StockFactory extends SysdynElementFactory { StaticSymbolImageInitializer.INSTANCE, new HoverTextElementHandler(0, 0, Alignment.LEADING, 1f), BoundsOutline.INSTANCE, + new BorderColorImpl(Color.BLACK), new WholeElementTerminals(terminals) ).setId(StockFactory.class.getSimpleName()); } + @Override + public void load(ReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e) throws DatabaseException { + super.load(graph, canvas, diagram, element, e); + + Resource colorResource = graph.getPossibleObject(element, G2DResource.getInstance(graph).HasColor); + Color color = null; + if(colorResource != null) { + color = G2DUtils.getColor(graph, colorResource); + e.setHint(ElementHints.KEY_BORDER_COLOR, color); + } + } + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/ValveFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/ValveFactory.java index 00c89fd3..b3c2d6fc 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/ValveFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/ValveFactory.java @@ -163,7 +163,8 @@ public class ValveFactory extends SysdynElementFactory { // Calculate borders from text node bounds. node.setStroke(STROKE); node.setScaleStroke(true); - node.setColor(Color.BLACK); + Color color = e.getHint(ElementHints.KEY_TEXT_COLOR); + node.setColor(color != null ? color : Color.BLACK); boolean rotated = false; String orientation = SysdynElementUtils.getOrientation(e); if(orientation != null && orientation.equals("Vertical")) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/FlowArrowLineStyle.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/FlowArrowLineStyle.java index 47120e94..73d0879a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/FlowArrowLineStyle.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/FlowArrowLineStyle.java @@ -37,14 +37,20 @@ public class FlowArrowLineStyle implements ILineEndStyle, Serializable { protected ArrowType type; protected Path2D path; protected double lineEndLength; + protected Color color; - public FlowArrowLineStyle(String desc) { + public FlowArrowLineStyle(String desc, Color color) { this.type = ArrowType.None; this.lineEndLength = 0.0; double l = length; double w = width; double s = space; + + if(color != null) + this.color = color; + else + this.color = Color.BLACK; StringTokenizer tokenizer = new StringTokenizer(desc); if (tokenizer.hasMoreTokens()) { @@ -79,7 +85,7 @@ public class FlowArrowLineStyle implements ILineEndStyle, Serializable { AffineTransform old = g.getTransform(); g.translate(x, y); g.rotate(dir*Math.PI*0.5); - g.setColor(Color.BLACK); + g.setColor(color); switch (type) { case Fill: diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/RouteFlowConnectionFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/RouteFlowConnectionFactory.java index e30d2578..f8c172bf 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/RouteFlowConnectionFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/RouteFlowConnectionFactory.java @@ -35,6 +35,7 @@ import org.simantics.db.common.procedure.adapter.TransientCacheListener; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.AsyncProcedure; +import org.simantics.diagram.G2DUtils; import org.simantics.diagram.adapter.SyncElementFactory; import org.simantics.diagram.connection.ConnectionVisuals; import org.simantics.diagram.connection.RouteGraph; @@ -50,6 +51,7 @@ import org.simantics.diagram.content.ResourceTerminal; import org.simantics.diagram.content.TerminalMap; import org.simantics.diagram.query.DiagramRequests; import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.diagram.stubs.G2DResource; import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; import org.simantics.diagram.synchronization.graph.RouteGraphConnection; import org.simantics.diagram.ui.DiagramModelHints; @@ -73,7 +75,6 @@ import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; import org.simantics.scenegraph.g2d.nodes.connection.IRouteGraphListener; import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphChangeEvent; -import org.simantics.scenegraph.utils.GeometryUtils; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.structural2.modelingRules.CPTerminal; import org.simantics.structural2.modelingRules.IAttachmentRelationMap; @@ -127,6 +128,13 @@ public class RouteFlowConnectionFactory extends SyncElementFactory { // FIXME: With undo this seems to happen, don't know why yet! return; + Resource colorResource = graph.getPossibleObject(connection, G2DResource.getInstance(graph).HasColor); + Color color = null; + if(colorResource != null) { + color = G2DUtils.getColor(graph, colorResource); + element.setHint(ElementHints.KEY_TEXT_COLOR, color); + } + RouteGraph rg = new RouteGraph(); Set nodes = new HashSet(); @@ -271,7 +279,7 @@ public class RouteFlowConnectionFactory extends SyncElementFactory { direction = 0xf; //System.out.println("load line style: " + NameUtils.getSafeLabel(graph, attachmentRelation)); - ILineEndStyle endStyle = loadLineEndStyle(graph, te, attachmentRelation); + ILineEndStyle endStyle = loadLineEndStyle(graph, te, attachmentRelation, color); RouteTerminal routeTerminal = rg.addBigTerminal(/*x, y,*/ minx, miny, maxx, maxy, /*direction,*/ endStyle); routeTerminal.setData( RouteGraphConnection.serialize(graph, connector) ); @@ -340,10 +348,12 @@ public class RouteFlowConnectionFactory extends SyncElementFactory { if (connectionType != null) cv = graph.syncRequest(DiagramRequests.getConnectionVisuals(connectionType), TransientCacheListener. instance()); - - Color lineColor = cv != null ? cv.toColor() : null; + + + Color lineColor = element.getHint(ElementHints.KEY_TEXT_COLOR); if (lineColor == null) - lineColor = Color.DARK_GRAY; + lineColor = cv != null ? cv.toColor() : Color.DARK_GRAY; + Stroke lineStroke = cv != null ? cv.stroke : null; if (lineStroke == null) lineStroke = new BasicStroke(0.1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10, null, 0); @@ -372,17 +382,17 @@ public class RouteFlowConnectionFactory extends SyncElementFactory { return null; } - public ILineEndStyle loadLineEndStyle(ReadGraph graph, IElement te, Resource attachmentRelation) + public ILineEndStyle loadLineEndStyle(ReadGraph graph, IElement te, Resource attachmentRelation, Color color) throws DatabaseException { ILineEndStyle style; // TODO: change bounds according to terminal type: Very small rectangle for Valves, Text box size for Stocks and Clouds if(te.getElementClass().containsClass(ValveSceneGraph.class)) { - style = new FlowArrowLineStyle("none 0 0 0"); + style = new FlowArrowLineStyle("none 0 0 0", color); } else { if (graph.isSubrelationOf(attachmentRelation, DIA.HasHeadConnector)) { - style = new FlowArrowLineStyle("fill 2 2 0"); + style = new FlowArrowLineStyle("fill 2 2 0", color); } else { - style = new FlowArrowLineStyle("none 0 0 0"); + style = new FlowArrowLineStyle("none 0 0 0", color); } } return style; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/menu/FontContextMenuContribution.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/menu/FontContextMenuContribution.java index dee43f15..9f69f7b3 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/menu/FontContextMenuContribution.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/menu/FontContextMenuContribution.java @@ -91,17 +91,19 @@ public class FontContextMenuContribution extends DynamicMenuContribution { for(Object o : selections) { Resource element = ResourceAdaptionUtils .adaptToResource(o); - Resource colorResource = graph.getPossibleObject(element, G2D.HasColor); - if(colorResource != null ) { - Color newColor = G2DUtils.getColor(graph, colorResource); - if(color == null) - color = newColor; - - if(color != null && !color.equals(newColor)) { - color = null; - break; - } - } + if(element != null) { + Resource colorResource = graph.getPossibleObject(element, G2D.HasColor); + if(colorResource != null ) { + Color newColor = G2DUtils.getColor(graph, colorResource); + if(color == null) + color = newColor; + + if(color != null && !color.equals(newColor)) { + color = null; + break; + } + } + } } } catch (DatabaseException e) { } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java index e8f84068..5088e5b3 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java @@ -20,6 +20,7 @@ import java.util.List; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Statement; +import org.simantics.db.common.utils.NameUtils; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; @@ -56,6 +57,7 @@ public class ValidationUtils { if(expressionList == null || expressionList.isEmpty()) throw new UndefinedExpressionException(); for(Resource expression : expressionList) { + Collection statements = graph.getStatements(expression, sr.Expression_equation); if(statements.isEmpty()) throw new UndefinedExpressionException(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariable.java index 5ab09733..61e5d9f4 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariable.java @@ -113,7 +113,8 @@ public class ValueIndexVariable extends IndexVariable { } } catch (FMUJNIException e) { } - } + } + exp.updateSubscriptions(); } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java index 4d6a3dcb..11706465 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java @@ -332,4 +332,45 @@ public class SysdynGameExperiment extends SysdynExperiment { } } } + + @Override + public void rewindTo(double time) { + if(control == null) + return; + + if(time >-0.001 && time < 0.001) { + initialize(); + } else { + System.out.println("rewindTo"); + } + } + + private synchronized void initialize() { + try { + HashMap inits = getExperimentParameters(null); + + control.setStepLength(stepLength); // FIXME: fixed step lenghth + control.initializeSimulation(); // initialize simulation + if(inits.get("variableFilter") == null || inits.get("variableFilter").equals(".*")) + subscription = control.getAllVariables(); + else + subscription = control.filterVariables(inits.get("variableFilter")); + control.subscribe(subscription); // subscribe all variables + + // Initialize results + results.clear(); + + double[] initialValues = new double[subscription.length]; + initialValues = control.getSubscribedResults(initialValues); + for(int k = 0; k < subscription.length; k++) { + results.put(subscription[k], new ArrayList()); + results.get(subscription[k]).add(initialValues[k]); + } + + getCurrentResult().setResult(new GameResult(this.results, this.subscription)); + resultsChanged(); + } catch (FMUJNIException e) { + System.err.println("SysdynGameExperiment rewind failed: " + e.getMessage()); + } + } }