D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib\r
D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp\r
D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp\r
+^D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\FMUSimulator\FMUSimulator.vcxproj\r
+D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib\r
+D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib\r
+D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp\r
+D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp\r
+^D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\FMUSimulator\FMUSimulator.vcxproj\r
+D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib\r
+D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib\r
+D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp\r
+D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp\r
+^D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\FMUSimulator\FMUSimulator.vcxproj\r
+D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib\r
+D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib\r
+D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp\r
+D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp\r
-importtargetdefinition -A 'd:/target/3.6.2/target-win.target'\r
-import 'org.simantics.sysdyn.product.site/site.cquery'\r
+importtargetdefinition -A 'd:/target/3.7.1/target-win.target'\r
+import 'site.cquery'\r
build\r
perform -D target.os=* -D target.ws=* -D target.arch=* org.simantics.sysdyn.product.site#site.p2\r
perform -D target.os=win32 -D target.ws=win32 -D target.arch=x86 -D p2.profileName=SysdynProfile -D p2.rootId=org.simantics.sysdyn.product.Sysdyn -D build.label=SimanticsSysdyn-b${BUILD_NUMBER}-r${SVN_REVISION} org.simantics.sysdyn.product.site#create.product.zip\r
// control should throw this exception: There is no model to get the variables from\r
}\r
\r
+ try {\r
+ control.filterVariables(".*");\r
+ fail("filterVariables returned something before loading fmu");\r
+ } catch (FMUJNIException e) {\r
+ // control should throw this exception: There is no model to get the variables from\r
+ }\r
+ \r
try {\r
double d = control.getRealValue("dummy");\r
fail("Got a Real value before loading fmu: " + d);\r
fail(e.getMessage());\r
}\r
\r
+ try {\r
+ control.filterVariables(".*");\r
+ } catch (FMUJNIException e) {\r
+ // control should NOT throw this exception: Variables are found in loadFMU\r
+ fail(e.getMessage());\r
+ }\r
+ \r
\r
try {\r
double[] array = new double[0];\r
CallFMUBeforeInitializationTests.class, \r
FMUIntegerTest.class, \r
FMUBooleanTest.class, \r
- FMURealTest.class })\r
+ FMURealTest.class,\r
+ FMUResultFilterTest.class})\r
public class FMUControlTestSuite {\r
\r
}\r
\r
/*\r
Below is the model used for testing. \r
- Model is located in Models\FMUTests\FMUIntegerTest \r
+ Model is located in Models\FMUTests\FMURealTest \r
\r
model FMURealTestModel\r
parameter Real r1 = 1.0;\r
--- /dev/null
+package org.simantics.sysdyn.tests.fmi;\r
+\r
+import java.util.Arrays;\r
+import java.util.List;\r
+\r
+import junit.framework.Assert;\r
+\r
+import org.junit.Test;\r
+import org.simantics.modelica.fmi.FMUControlJNI;\r
+import org.simantics.modelica.fmi.FMUJNIException;\r
+\r
+\r
+/**\r
+ * Tests result filtering\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class FMUResultFilterTest {\r
+ \r
+ /*\r
+ Below is the model used for testing. \r
+ Model is located in Models\FMUTests\FMURealTest \r
+ \r
+ model FMURealTestModel\r
+ parameter Real r1 = 1.0;\r
+ parameter Real r2 = 1.0;\r
+ Real valve;\r
+ Real stock(start=0.0,fixed=true);\r
+ parameter Real time = 0; // Time parameter is set at each time step by simulator\r
+ equation\r
+ if initial() or r1 < 0 or r1 >= 0 then (r1 + r2) else pre(valve);\r
+ der(stock) = valve;\r
+ end FMURealTestModel;\r
+ \r
+ \r
+ The condition for valve is automatically generated in sysdyn. It is a hack for getting \r
+ valve to notice a change in i1 and i2. \r
+ \r
+ Results always contain "time" for each modelica class, hence result size is +1\r
+ */\r
+\r
+ @Test\r
+ public void testReturn3() {\r
+\r
+ FMUControlJNI control = new FMUControlJNI();\r
+\r
+ String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu";\r
+\r
+\r
+ try {\r
+ \r
+ \r
+\r
+ control.loadFMUFile(path);\r
+\r
+ String[] bothRealParameters = control.filterVariables("r(.*)");\r
+ Assert.assertEquals("Filter \"r(.*)\" returned " + bothRealParameters.length + " results. Should have returned 3", bothRealParameters.length, 3);\r
+ List<String> result = Arrays.asList(bothRealParameters);\r
+ Assert.assertTrue("Result did not contain r1", result.contains("r1"));\r
+ Assert.assertTrue("Result did not contain r2", result.contains("r2"));\r
+ Assert.assertTrue("Result did not contain time", result.contains("time"));\r
+\r
+ control.unloadFMU();\r
+ } catch (FMUJNIException e) {\r
+ Assert.fail("Exception in native fmu code: " + e.getMessage());\r
+ }\r
+ }\r
+ \r
+ @Test\r
+ public void testReturn2() {\r
+\r
+ FMUControlJNI control = new FMUControlJNI();\r
+\r
+ String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu";\r
+\r
+\r
+ try {\r
+\r
+ control.loadFMUFile(path);\r
+\r
+ String[] stock = control.filterVariables("stock");\r
+ Assert.assertEquals("Filter \"stock\" returned " + stock.length + " results. Should have returned 2", stock.length, 2);\r
+ List<String> result = Arrays.asList(stock);\r
+ Assert.assertTrue("Result did not contain stock", result.contains("stock"));\r
+ Assert.assertTrue("Result did not contain time", result.contains("time"));\r
+ \r
+ control.unloadFMU();\r
+ } catch (FMUJNIException e) {\r
+ Assert.fail("Exception in native fmu code: " + e.getMessage());\r
+ }\r
+ }\r
+ \r
+ @Test\r
+ public void testReturn1() {\r
+\r
+ FMUControlJNI control = new FMUControlJNI();\r
+\r
+ String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu";\r
+\r
+\r
+ try {\r
+ control.loadFMUFile(path);\r
+ \r
+ String[] empty = control.filterVariables("empty");\r
+ Assert.assertEquals("Filter \"empty\" returned " + empty.length + " results. Should have returned 1", empty.length, 1);\r
+ \r
+ List<String> result = Arrays.asList(empty);\r
+ Assert.assertTrue("Result did not contain time", result.contains("time"));\r
+ \r
+ control.unloadFMU();\r
+ } catch (FMUJNIException e) {\r
+ Assert.fail("Exception in native fmu code: " + e.getMessage());\r
+ }\r
+ }\r
+ \r
+ @Test\r
+ public void testReturn1WhenNullFilter() {\r
+\r
+ FMUControlJNI control = new FMUControlJNI();\r
+\r
+ String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu";\r
+\r
+\r
+ try {\r
+ control.loadFMUFile(path);\r
+ \r
+ String[] NULL = control.filterVariables(null);\r
+ Assert.assertEquals("Filter null returned " + NULL.length + " results. Should have returned 1", NULL.length, 1);\r
+ \r
+ List<String> result = Arrays.asList(NULL);\r
+ Assert.assertTrue("Result did not contain time", result.contains("time"));\r
+ \r
+ control.unloadFMU();\r
+ } catch (FMUJNIException e) {\r
+ Assert.fail("Exception in native fmu code: " + e.getMessage());\r
+ }\r
+ }\r
+ \r
+\r
+ \r
+\r
+}\r
import org.eclipse.swt.widgets.Shell;\r
import org.eclipse.ui.handlers.HandlerUtil;\r
import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.Files;\r
import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.container.DataContainer;\r
+import org.simantics.databoard.container.DataContainers;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, functionLibrary));\r
\r
try {\r
- Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
+ DataContainers.writeFile(new File(selected), new DataContainer("sysdynFunctionLibrary", 1, new Variant(TransferableGraph1.BINDING, tg)));\r
} catch (RuntimeBindingConstructionException e) {\r
e.printStackTrace();\r
} catch (IOException e) {\r
import org.eclipse.swt.widgets.Shell;\r
import org.eclipse.ui.handlers.HandlerUtil;\r
import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.Files;\r
import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.container.DataContainer;\r
+import org.simantics.databoard.container.DataContainers;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
\r
@Override\r
public void run(ReadGraph graph) throws DatabaseException {\r
+ \r
Layer0 l0 = Layer0.getInstance(graph);\r
String name = graph.syncRequest(new PossibleRelatedValue<String>(model, l0.HasName, Bindings.STRING ));\r
ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();\r
TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, model));\r
\r
try {\r
- Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
+ DataContainers.writeFile(new File(selected), new DataContainer("sysdynModel", 1, new Variant(TransferableGraph1.BINDING, tg)));\r
} catch (RuntimeBindingConstructionException e) {\r
e.printStackTrace();\r
} catch (IOException e) {\r
import org.eclipse.swt.widgets.Shell;\r
import org.eclipse.ui.handlers.HandlerUtil;\r
import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.Files;\r
import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.container.DataContainer;\r
+import org.simantics.databoard.container.DataContainers;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
graph.deny(replacement.first, sr.Redeclaration_replacedEnumeration_Inverse, replacement.second);\r
\r
TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, component));\r
- Files.createFile(new File(path), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
+ DataContainers.writeFile(new File(path), new DataContainer("sysdynModule", 1, new Variant(TransferableGraph1.BINDING, tg)));\r
\r
for(Pair<Resource,Resource> replacement : replacements)\r
graph.claim(replacement.first, sr.Redeclaration_replacedEnumeration_Inverse, replacement.second);\r
\r
import java.io.File;\r
import java.io.IOException;\r
+import java.util.HashMap;\r
\r
import org.eclipse.core.commands.AbstractHandler;\r
import org.eclipse.core.commands.ExecutionEvent;\r
import org.eclipse.swt.widgets.Shell;\r
import org.eclipse.ui.handlers.HandlerUtil;\r
import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.Files;\r
-import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.container.DataContainer;\r
+import org.simantics.databoard.container.DataContainers;\r
+import org.simantics.databoard.container.DataFormatException;\r
+import org.simantics.databoard.container.FormatHandler;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
\r
Activator.getDefault().getPreferenceStore().setValue(IMPORTFUNCTIONLIBRARYPATH, (new File(selected)).getParent());\r
\r
- // Load the transferable graph\r
- TransferableGraph1 tg = null;\r
- try {\r
- tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
- } catch (RuntimeBindingConstructionException e) {\r
- e.printStackTrace();\r
- return null;\r
- } catch (IOException e) {\r
- MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
- mb.setText("Error");\r
- mb.setMessage("The imported file is not of type: Function Library");\r
- mb.open();\r
- return null;\r
- } \r
- if(tg == null) return null;\r
-\r
- // Make sure the "http://SharedOntologies resource exists\r
- try {\r
- Boolean hasSharedOntologies;\r
- hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
+ // Ensure that shared functions ontology exists\r
+ ensureSharedOntologies();\r
+ \r
+ // Handler for importing function libraries\r
+ HashMap<String, FormatHandler<Object>> handlers = new HashMap<String, FormatHandler<Object>>();\r
+ handlers.put("sysdynFunctionLibrary:1", new FormatHandler<Object>() {\r
+ @Override\r
+ public Binding getBinding() {\r
+ return TransferableGraph1.BINDING;\r
+ }\r
\r
- @Override\r
- public Boolean perform(ReadGraph graph) throws DatabaseException {\r
- try {\r
- graph.getResource("http://SharedOntologies");\r
- } catch (ResourceNotFoundException e) {\r
- return false;\r
- } \r
- return true;\r
+ @Override\r
+ public Object process(DataContainer container) throws DataFormatException {\r
+ SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(functionLibrary);\r
+ try {\r
+ DefaultPasteHandler.defaultExecute((TransferableGraph1)container.content.getValue(), functionLibrary, ia);\r
+ return ia.getRoot();\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ return null;\r
}\r
- });\r
-\r
- if(!hasSharedOntologies) {\r
- SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
-\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- GraphUtils.create2(graph, l0.Library, \r
- l0.HasName, "SharedOntologies",\r
- l0.PartOf, graph.getResource("http:/"));\r
- }\r
- });\r
-\r
}\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- return null;\r
- }\r
+ });\r
\r
- \r
- // Import a function library from the transferable graph\r
- SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(functionLibrary);\r
+ // Read function library file\r
+ Object result = null;\r
try {\r
- DefaultPasteHandler.defaultExecute(tg, functionLibrary, ia);\r
- } catch (Exception e) {\r
+ File file = new File(selected);\r
+ if(!file.isFile()) {\r
+ displayErrorMessage(shell, "File " + selected + " not found");\r
+ return null;\r
+ } else { \r
+ result = DataContainers.readFile(file, handlers);\r
+ }\r
+ } catch(DataFormatException e) {\r
+ e.printStackTrace();\r
+ } catch(IOException e) {\r
e.printStackTrace();\r
+ } catch(Exception e) {\r
+ if(e instanceof RuntimeException)\r
+ throw (RuntimeException)e;\r
+ else\r
+ throw new RuntimeException(e);\r
}\r
\r
- final Resource root = ia.getRoot(); // Root of the library\r
- \r
- // Link the imported library to the selected resource (functionLibrary)\r
- SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
- \r
- /**\r
- * Link the imported library to the selected resource (functionLibrary)\r
- * The imported library can be either SysdynModelicaFunctionLibrary or SysdynFunctionOntology\r
- */\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- // Case: SharedFunctionOntology. Link to SharedOntologies\r
- if(graph.isInstanceOf(root, SysdynResource.getInstance(graph).SharedFunctionOntology)) {\r
- Resource library = graph.getResource("http://SharedOntologies");\r
- if(!graph.hasStatement(library, l0.ConsistsOf, root)) {\r
- graph.claim(library, l0.ConsistsOf, root);\r
- }\r
+ if(result == null || !(result instanceof Resource)) {\r
+ displayErrorMessage(shell, "Function library could not be imported.");\r
+ } else {\r
+ final Resource FunctionLibraryRoot = (Resource) result;\r
+ // Link the imported library to the selected resource (functionLibrary)\r
+ SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
\r
- // Link model to the shared library\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- Resource model = functionLibrary;\r
- while(!graph.isInstanceOf(model, sr.SysdynModel) && graph.isInstanceOf(model, l0.Ontology))\r
- model = graph.getSingleObject(model, l0.PartOf);\r
- if(graph.isInstanceOf(model, sr.SysdynModel)) {\r
- graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, root);\r
- }\r
+ /**\r
+ * Link the imported library to the selected resource (functionLibrary)\r
+ * The imported library can be either SysdynModelicaFunctionLibrary or SysdynFunctionOntology\r
+ */\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ // Case: SharedFunctionOntology. Link to SharedOntologies\r
+ if(graph.isInstanceOf(FunctionLibraryRoot, SysdynResource.getInstance(graph).SharedFunctionOntology)) {\r
+ Resource library = graph.getResource("http://SharedOntologies");\r
+ if(!graph.hasStatement(library, l0.ConsistsOf, FunctionLibraryRoot)) {\r
+ graph.claim(library, l0.ConsistsOf, FunctionLibraryRoot);\r
+ }\r
\r
- // Case: not SharedFunctionOntology or SysdynModelicaFunctionLibrary. \r
- } else if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) {\r
- Resource instanceOf = graph.getPossibleObject(root,l0.InstanceOf);\r
- String type = "...";\r
- if(instanceOf != null)\r
- type = NameUtils.getSafeName(graph, instanceOf);\r
- else {\r
- Resource inheritedFrom = graph.getPossibleObject(root, l0.Inherits);\r
- if(inheritedFrom != null)\r
- type = NameUtils.getSafeName(graph, inheritedFrom);\r
- }\r
- final String ft = type; \r
- \r
- // Remove the functionLibrary ConsistsOf root relation\r
- graph.deny(root, l0.PartOf);\r
- \r
- // Display error message\r
- shell.getDisplay().asyncExec(new Runnable() {\r
- \r
- @Override\r
- public void run() {\r
- MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
- mb.setText("Error");\r
- mb.setMessage("The imported file is not of type: Function Library (" + ft +")");\r
- mb.open(); \r
+ // Link model to the shared library\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Resource model = functionLibrary;\r
+ while(!graph.isInstanceOf(model, sr.SysdynModel) && graph.isInstanceOf(model, l0.Ontology))\r
+ model = graph.getSingleObject(model, l0.PartOf);\r
+ if(graph.isInstanceOf(model, sr.SysdynModel)) {\r
+ graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, FunctionLibraryRoot);\r
}\r
- });\r
- } else {\r
- // imported library is already in the right place. Update function library files.\r
- FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary);\r
- }\r
\r
- }\r
- });\r
+ // Case: not SharedFunctionOntology or SysdynModelicaFunctionLibrary. \r
+ } else if(!graph.isInstanceOf(FunctionLibraryRoot, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) {\r
+ Resource instanceOf = graph.getPossibleObject(FunctionLibraryRoot,l0.InstanceOf);\r
+ String type = "...";\r
+ if(instanceOf != null)\r
+ type = NameUtils.getSafeName(graph, instanceOf);\r
+ else {\r
+ Resource inheritedFrom = graph.getPossibleObject(FunctionLibraryRoot, l0.Inherits);\r
+ if(inheritedFrom != null)\r
+ type = NameUtils.getSafeName(graph, inheritedFrom);\r
+ }\r
+ final String ft = type; \r
+\r
+ // Remove the functionLibrary ConsistsOf root relation\r
+ graph.deny(FunctionLibraryRoot, l0.PartOf);\r
+\r
+ // Display error message\r
+ shell.getDisplay().asyncExec(new Runnable() {\r
+\r
+ @Override\r
+ public void run() {\r
+ displayErrorMessage(shell, "The imported file is not of type: Function Library (" + ft +")"); \r
+ }\r
+ });\r
+ } else {\r
+ // imported library is already in the right place. Update function library files.\r
+ FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary);\r
+ }\r
+\r
+ }\r
+ });\r
+ }\r
return null;\r
}\r
\r
}\r
\r
}\r
+ \r
+ private void ensureSharedOntologies() {\r
+ // Make sure the "http://SharedOntologies resource exists\r
+ try {\r
+ Boolean hasSharedOntologies;\r
+ hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
+\r
+ @Override\r
+ public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+ try {\r
+ graph.getResource("http://SharedOntologies");\r
+ } catch (ResourceNotFoundException e) {\r
+ return false;\r
+ } \r
+ return true;\r
+ }\r
+ });\r
+\r
+ if(!hasSharedOntologies) {\r
+ SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ GraphUtils.create2(graph, l0.Library, \r
+ l0.HasName, "SharedOntologies",\r
+ l0.PartOf, graph.getResource("http:/"));\r
+ }\r
+ });\r
+\r
+ }\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+\r
+ }\r
+ \r
+ private void displayErrorMessage(Shell shell, String message) {\r
+ MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+ mb.setText("Error");\r
+ mb.setMessage("The imported file is not of type: Model");\r
+ mb.open();\r
+ }\r
\r
}\r
\r
import java.io.File;\r
import java.io.IOException;\r
-import java.util.HashSet;\r
-import java.util.UUID;\r
+import java.util.HashMap;\r
\r
import org.eclipse.core.commands.AbstractHandler;\r
import org.eclipse.core.commands.ExecutionEvent;\r
import org.eclipse.swt.widgets.MessageBox;\r
import org.eclipse.swt.widgets.Shell;\r
import org.eclipse.ui.handlers.HandlerUtil;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.Files;\r
-import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.container.DataContainer;\r
+import org.simantics.databoard.container.DataContainers;\r
+import org.simantics.databoard.container.DataFormatException;\r
+import org.simantics.databoard.container.FormatHandler;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.ObjectsWithType;\r
import org.simantics.db.common.request.WriteRequest;\r
import org.simantics.db.common.utils.NameUtils;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
import org.simantics.graph.representation.TransferableGraph1;\r
import org.simantics.layer0.Layer0;\r
-import org.simantics.layer0.utils.direct.GraphUtils;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.operation.Layer0X;\r
-import org.simantics.simulation.ontology.SimulationResource;\r
-import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.ui.Activator;\r
-import org.simantics.sysdyn.ui.utils.OldTransferableGraph1;\r
-import org.simantics.sysdyn.ui.utils.SheetUtils;\r
import org.simantics.ui.SimanticsUI;\r
\r
/**\r
*\r
*/\r
public class ImportModelHandler extends AbstractHandler {\r
- \r
+\r
public static String IMPORTMODELTPATH = "IMPORT_MODEL_PATH";\r
\r
@Override\r
public Object execute(ExecutionEvent event) throws ExecutionException {\r
- \r
- Resource project = SimanticsUI.getProject().get();\r
+\r
+ final Resource project = SimanticsUI.getProject().get();\r
if(project == null) return null;\r
\r
// Get imported transferable graph file using FileDialog\r
final Shell shell = HandlerUtil.getActiveShellChecked(event);\r
FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
fd.setText("Import Model");\r
- \r
+\r
String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMODELTPATH);\r
if(path.isEmpty() || !(new File(path).exists()))\r
path = Platform.getLocation().toOSString();\r
fd.setFilterExtensions(filterExt);\r
String selected = fd.open();\r
if(selected == null) return null;\r
- \r
+\r
Activator.getDefault().getPreferenceStore().setValue(IMPORTMODELTPATH, (new File(selected)).getParent());\r
- \r
- // Get the transferable graph from file\r
- TransferableGraph1 tg = null;\r
- try {\r
- tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
- } catch (RuntimeBindingConstructionException e) {\r
- e.printStackTrace();\r
- } catch (IOException e) { \r
- try {\r
- // "Version migration". Use OldTransferableGraph1 if the file was imported using old methods\r
- OldTransferableGraph1 otg = (OldTransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(OldTransferableGraph1.class));\r
- tg = new TransferableGraph1(otg.resourceCount, otg.identities, otg.statements, otg.values);\r
- } catch (RuntimeBindingConstructionException e1) {\r
- e1.printStackTrace();\r
- } catch (IOException e1) {\r
- MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
- mb.setText("Error");\r
- mb.setMessage("The imported file is not of type: Model");\r
- mb.open();\r
- return null;\r
+\r
+\r
+\r
+ HashMap<String, FormatHandler<Object>> handlers = new HashMap<String, FormatHandler<Object>>();\r
+ handlers.put("sysdynModel:1", new FormatHandler<Object>() {\r
+ @Override\r
+ public Binding getBinding() {\r
+ return TransferableGraph1.BINDING;\r
}\r
- }\r
- if(tg == null) return null;\r
- \r
- try {\r
- \r
- DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(project);\r
- DefaultPasteHandler.defaultExecute(tg, SimanticsUI.getProject().get(), ia);\r
\r
- \r
- // Check that imported resource was actually a model \r
- //and fix changes made to old ontology versions\r
- final Resource root = ia.getRoot();\r
- SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
- \r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- \r
- if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModel)) {\r
- // Imported model was not a SysdynModel, display error message.\r
- Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf);\r
- String type = "...";\r
- if(instanceOf != null)\r
- type = NameUtils.getSafeName(graph, instanceOf);\r
- else {\r
- Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits);\r
- if(inheritedFrom != null)\r
- type = NameUtils.getSafeName(graph, inheritedFrom);\r
- }\r
- final String ft = type; \r
- graph.deny(root, Layer0.getInstance(graph).PartOf);\r
- \r
- shell.getDisplay().asyncExec(new Runnable() {\r
- \r
- @Override\r
- public void run() {\r
- MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
- mb.setText("Error");\r
- mb.setMessage("The imported file is not of type: System Dynamics Model (" + ft +")");\r
- mb.open(); \r
- }\r
- });\r
+ @Override\r
+ public Object process(DataContainer container) throws DataFormatException {\r
+ DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(project);\r
+ try {\r
+ DefaultPasteHandler.defaultExecute((TransferableGraph1)container.content.getValue(), project, ia);\r
+ return ia.getRoot();\r
\r
- } else {\r
- \r
- Resource project = SimanticsUI.getProject().get();\r
- graph.claim(root, Layer0X.getInstance(graph).IsActivatedBy, project);\r
- \r
- // Do all modifications that are necessary\r
- updateOldConfigurationToBaseRealization(graph, root);\r
- addDefaultOntologyLinks(graph, root);\r
- addURIsToDiagrams(graph, root);\r
- addSpreadSheetBook(graph, root);\r
- }\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ return null;\r
}\r
- });\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- } catch (Exception e) {\r
- e.printStackTrace();\r
- }\r
+ }\r
+ });\r
\r
- return null;\r
- }\r
- \r
- /**\r
- * In old versions base realization was separate. Newer versions use configuration as base realization. \r
- * @param graph WriteGraph\r
- * @param model Imported model\r
- */\r
- private static void updateOldConfigurationToBaseRealization(WriteGraph graph, Resource model) {\r
- Layer0X L0X = Layer0X.getInstance(graph);\r
+ Object result = null;\r
try {\r
- Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration);\r
- if(configuration != null && !graph.hasStatement(configuration, L0X.IsBaseRealizationOf, model))\r
- graph.claim(configuration, L0X.IsBaseRealizationOf, model);\r
- } catch (DatabaseException e) {\r
+ File file = new File(selected);\r
+ if(!file.isFile()) {\r
+ displayErrorMessage(shell, "File " + selected + " not found");\r
+ return null;\r
+ } else { \r
+ result = DataContainers.readFile(file, handlers);\r
+ }\r
+ } catch(DataFormatException e) {\r
e.printStackTrace();\r
- }\r
- \r
- }\r
- \r
- /**\r
- * Links should be exported and imported automatically. If it has failed, the \r
- * default ontology links sysdyn and layer0 are added.\r
- * \r
- * @param graph WriteGraph\r
- * @param model Imported model\r
- */\r
- \r
- private static void addDefaultOntologyLinks(WriteGraph graph, Resource model) {\r
- try {\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- // The links should be exported and imported automatically\r
- Resource sysdyn = graph.getResource("http://www.simantics.org/Sysdyn-1.1");\r
- Resource layer0 = graph.getResource("http://www.simantics.org/Layer0-1.0");\r
- if(!graph.hasStatement(model, l0.IsLinkedTo, sysdyn))\r
- graph.claim(model, l0.IsLinkedTo, sysdyn);\r
- if(!graph.hasStatement(model, l0.IsLinkedTo, layer0))\r
- graph.claim(model, l0.IsLinkedTo, layer0);\r
- } catch (DatabaseException e) {\r
+ } catch(IOException e) {\r
e.printStackTrace();\r
+ } catch(Exception e) {\r
+ if(e instanceof RuntimeException)\r
+ throw (RuntimeException)e;\r
+ else\r
+ throw new RuntimeException(e);\r
}\r
- }\r
- \r
- \r
- /**\r
- * Add a container for providing URIs to diagrams. \r
- * \r
- * @param graph WriteGraph\r
- * @param model Imported model\r
- */\r
- private static void addURIsToDiagrams(WriteGraph graph, Resource model) {\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- SimulationResource simu = SimulationResource.getInstance(graph);\r
- ModelingResources mr = ModelingResources.getInstance(graph);\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
- try {\r
- HashSet<Resource> configurations = new HashSet<Resource>();\r
\r
- Resource configuration = graph.getPossibleObject(model, simu.HasConfiguration);\r
- if(configuration != null) \r
- configurations.add(configuration);\r
- \r
- for(Resource r : graph.getObjects(model, l0.ConsistsOf)) {\r
- if(graph.isInheritedFrom(r, sr.Module)) {\r
- Resource moduleConfiguration = graph.getPossibleObject(r, sr2.IsDefinedBy);\r
- if(moduleConfiguration != null)\r
- configurations.add(moduleConfiguration);\r
- }\r
- }\r
- \r
- for(Resource conf : configurations) {\r
- Resource configurationDiagram = graph.getPossibleObject(conf, mr.CompositeToDiagram);\r
- if(configurationDiagram != null && !graph.hasStatement(configurationDiagram, l0.PartOf)) {\r
- GraphUtils.create2(graph, l0.Library, \r
- l0.HasName, "__CONTAINER__",\r
- l0.PartOf, conf,\r
- l0.ConsistsOf, configurationDiagram);\r
- }\r
+ if(result == null || !(result instanceof Resource)) {\r
+ displayErrorMessage(shell, "Model could not be imported.");\r
+ } else {\r
+ try {\r
+ final Resource ModelRoot = (Resource) result;\r
+ SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+ \r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ if(!graph.isInstanceOf(ModelRoot, SysdynResource.getInstance(graph).SysdynModel)) {\r
+ // Imported model was not a SysdynModel, display error message.\r
+ Resource instanceOf = graph.getPossibleObject(ModelRoot, Layer0.getInstance(graph).InstanceOf);\r
+ String type = "...";\r
+ if(instanceOf != null)\r
+ type = NameUtils.getSafeName(graph, instanceOf);\r
+ else {\r
+ Resource inheritedFrom = graph.getPossibleObject(ModelRoot, Layer0.getInstance(graph).Inherits);\r
+ if(inheritedFrom != null)\r
+ type = NameUtils.getSafeName(graph, inheritedFrom);\r
+ }\r
+ final String ft = type; \r
+ graph.deny(ModelRoot, Layer0.getInstance(graph).PartOf);\r
+\r
+ shell.getDisplay().asyncExec(new Runnable() {\r
+\r
+ @Override\r
+ public void run() {\r
+ displayErrorMessage(shell, "The imported file is not of type: System Dynamics Model (" + ft +")");\r
+ }\r
+ });\r
+\r
+ } \r
+ }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
}\r
- \r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
}\r
+\r
+ return null;\r
+\r
}\r
- \r
- \r
- /**\r
- * Add a missing spreadsheet book to the model\r
- * \r
- * @param graph\r
- * @param model\r
- */\r
- private static void addSpreadSheetBook(WriteGraph graph, Resource model) {\r
- try {\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- SpreadsheetResource ssr = SpreadsheetResource.getInstance(graph);\r
- SimulationResource simu = SimulationResource.getInstance(graph);\r
- Resource conf = graph.getPossibleObject(model, simu.HasConfiguration);\r
- if(conf != null && graph.syncRequest(new ObjectsWithType(conf, l0.ConsistsOf, ssr.Book)).isEmpty()) {\r
- Resource book = graph.newResource();\r
- graph.claim(book, l0.InstanceOf, null, ssr.Book);\r
- graph.addLiteral(book, l0.HasName, l0.NameOf, l0.String, "Book" + UUID.randomUUID().toString(), Bindings.STRING);\r
- graph.claim(conf, l0.ConsistsOf, l0.PartOf, book);\r
\r
- SheetUtils.createSheet(graph, book, "Sheet1", new String[] { }, new int[] { 50 });\r
- }\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- }\r
+ private void displayErrorMessage(Shell shell, String message) {\r
+ MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+ mb.setText("Error");\r
+ mb.setMessage("The imported file is not of type: Model");\r
+ mb.open();\r
}\r
- \r
}\r
\r
import java.io.File;\r
import java.io.IOException;\r
+import java.util.HashMap;\r
\r
import org.eclipse.core.commands.AbstractHandler;\r
import org.eclipse.core.commands.ExecutionEvent;\r
import org.eclipse.swt.widgets.Shell;\r
import org.eclipse.ui.handlers.HandlerUtil;\r
import org.simantics.browsing.ui.common.node.AbstractNode;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.Files;\r
-import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.container.DataContainer;\r
+import org.simantics.databoard.container.DataContainers;\r
+import org.simantics.databoard.container.DataFormatException;\r
+import org.simantics.databoard.container.FormatHandler;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.common.request.WriteRequest;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
-import org.simantics.graph.db.MissingDependencyException;\r
import org.simantics.graph.representation.TransferableGraph1;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.SysdynResource;\r
*/\r
public class ImportModuleHandler extends AbstractHandler {\r
\r
- public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH";\r
-\r
- @Override\r
- public Object execute(ExecutionEvent event) throws ExecutionException {\r
- ISelection sel = HandlerUtil.getCurrentSelection(event);\r
-\r
- @SuppressWarnings("unchecked")\r
- AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);\r
- if (node == null)\r
- return null;\r
-\r
- final Resource model = node.data;\r
-\r
- // Get imported transferable graph file using FileDialog\r
- final Shell shell = HandlerUtil.getActiveShellChecked(event);\r
- FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
- fd.setText("Import Module");\r
-\r
- String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMODULETPATH);\r
- if(path.isEmpty() || !(new File(path).exists()))\r
- path = Platform.getLocation().toOSString();\r
- fd.setFilterPath(path);\r
- String[] filterExt = {"*.tg"};\r
- fd.setFilterExtensions(filterExt);\r
- String selected = fd.open();\r
- if(selected == null) return null;\r
-\r
- Activator.getDefault().getPreferenceStore().setValue(IMPORTMODULETPATH, (new File(selected)).getParent());\r
-\r
- // Get the transferable graph from file\r
- TransferableGraph1 tg = null;\r
- try {\r
- tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
- } catch (RuntimeBindingConstructionException e) {\r
- e.printStackTrace();\r
- } catch (IOException e) {\r
- MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
- mb.setText("Error");\r
- mb.setMessage("The imported file is not of type: Module Type");\r
- mb.open();\r
- return null;\r
- }\r
- if(tg == null) return null;\r
-\r
-\r
- // Import the module to model\r
- DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(model);\r
- try {\r
- DefaultPasteHandler.defaultExecute(tg, model, ia);\r
- } catch (MissingDependencyException e) {\r
- e.printStackTrace();\r
- } catch (Exception e) {\r
- e.printStackTrace();\r
- }\r
-\r
- final Resource root = ia.getRoot();\r
-\r
- // Check that the imported file actually was a module. Display error message otherwise.\r
- SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
-\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- if(!graph.isInheritedFrom(root, SysdynResource.getInstance(graph).Module)) {\r
- Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf);\r
- String type = "...";\r
- if(instanceOf != null)\r
- type = NameUtils.getSafeName(graph, instanceOf);\r
- else {\r
- Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits);\r
- if(inheritedFrom != null)\r
- type = NameUtils.getSafeName(graph, inheritedFrom);\r
- }\r
- final String ft = type; \r
- graph.deny(root, Layer0.getInstance(graph).PartOf);\r
-\r
- shell.getDisplay().asyncExec(new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
- MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
- mb.setText("Error");\r
- mb.setMessage("The imported file is not of type: System Dynamics Module (" + ft +")");\r
- mb.open(); \r
- }\r
- });\r
- } \r
- }\r
- });\r
- return null;\r
- }\r
+ public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH";\r
+\r
+ @Override\r
+ public Object execute(ExecutionEvent event) throws ExecutionException {\r
+ ISelection sel = HandlerUtil.getCurrentSelection(event);\r
+\r
+ @SuppressWarnings("unchecked")\r
+ AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);\r
+ if (node == null)\r
+ return null;\r
+\r
+ final Resource model = node.data;\r
+\r
+ // Get imported transferable graph file using FileDialog\r
+ final Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+ FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
+ fd.setText("Import Module");\r
+\r
+ String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMODULETPATH);\r
+ if(path.isEmpty() || !(new File(path).exists()))\r
+ path = Platform.getLocation().toOSString();\r
+ fd.setFilterPath(path);\r
+ String[] filterExt = {"*.tg"};\r
+ fd.setFilterExtensions(filterExt);\r
+ String selected = fd.open();\r
+ if(selected == null) return null;\r
+\r
+ Activator.getDefault().getPreferenceStore().setValue(IMPORTMODULETPATH, (new File(selected)).getParent());\r
+\r
+ // Handler for importing modules\r
+ HashMap<String, FormatHandler<Object>> handlers = new HashMap<String, FormatHandler<Object>>();\r
+ handlers.put("sysdynModule:1", new FormatHandler<Object>() {\r
+ @Override\r
+ public Binding getBinding() {\r
+ return TransferableGraph1.BINDING;\r
+ }\r
+\r
+ @Override\r
+ public Object process(DataContainer container) throws DataFormatException {\r
+ DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(model);\r
+ try {\r
+ DefaultPasteHandler.defaultExecute((TransferableGraph1)container.content.getValue(), model, ia);\r
+ return ia.getRoot();\r
+\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ return null;\r
+ }\r
+ }\r
+ });\r
+\r
+ // Read module file\r
+ Object result = null;\r
+ try {\r
+ File file = new File(selected);\r
+ if(!file.isFile()) {\r
+ displayErrorMessage(shell, "File " + selected + " not found");\r
+ return null;\r
+ } else { \r
+ result = DataContainers.readFile(file, handlers);\r
+ }\r
+ } catch(DataFormatException e) {\r
+ e.printStackTrace();\r
+ } catch(IOException e) {\r
+ e.printStackTrace();\r
+ } catch(Exception e) {\r
+ if(e instanceof RuntimeException)\r
+ throw (RuntimeException)e;\r
+ else\r
+ throw new RuntimeException(e);\r
+ }\r
+\r
+\r
+ if(result == null || !(result instanceof Resource)) {\r
+ displayErrorMessage(shell, "Module could not be imported.");\r
+ } else {\r
+ final Resource ModuleRoot = (Resource) result;\r
+ // Check that the imported file actually was a module. Display error message otherwise.\r
+ SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ if(!graph.isInheritedFrom(ModuleRoot, SysdynResource.getInstance(graph).Module)) {\r
+ Resource instanceOf = graph.getPossibleObject(ModuleRoot, Layer0.getInstance(graph).InstanceOf);\r
+ String type = "...";\r
+ if(instanceOf != null)\r
+ type = NameUtils.getSafeName(graph, instanceOf);\r
+ else {\r
+ Resource inheritedFrom = graph.getPossibleObject(ModuleRoot, Layer0.getInstance(graph).Inherits);\r
+ if(inheritedFrom != null)\r
+ type = NameUtils.getSafeName(graph, inheritedFrom);\r
+ }\r
+ final String ft = type; \r
+ graph.deny(ModuleRoot, Layer0.getInstance(graph).PartOf);\r
+\r
+ shell.getDisplay().asyncExec(new Runnable() {\r
+\r
+ @Override\r
+ public void run() {\r
+ displayErrorMessage(shell, "The imported file is not of type: System Dynamics Module (" + ft +")");\r
+ }\r
+ });\r
+ } \r
+ }\r
+ });\r
+\r
+ }\r
+ return null;\r
+ }\r
+\r
+\r
+ private void displayErrorMessage(Shell shell, String message) {\r
+ MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+ mb.setText("Error");\r
+ mb.setMessage("The imported file is not of type: Model");\r
+ mb.open();\r
+ }\r
\r
}\r
} catch (DatabaseException e) {\r
throw new DatabaseException(e.getMessage());\r
} catch (FMUJNIException e) {\r
- e.printStackTrace();\r
+ System.err.println("ParameterExpression.java: " + e.getMessage());\r
return null;\r
}\r
}\r
import java.util.HashMap;\r
\r
import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.core.runtime.jobs.Job;\r
import org.simantics.db.AsyncReadGraph;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
\r
@Override\r
public void simulateDuration(double duration) {\r
- if(subscription == null || this.results == null || control == null)\r
- return;\r
- \r
- if(duration <= 0.0)\r
- duration = stepDuration;\r
- \r
- \r
- try {\r
- control.setStepLength(stepLength); // Set step length each time in case there has been changes\r
+ Job job = new simulateDurationJob("Simulate steps", duration);\r
+ // Start the Job\r
+ job.schedule();\r
+ }\r
+ \r
+ private class simulateDurationJob extends Job {\r
\r
- double time = control.getTime();\r
- double eTime = time + duration;\r
- double[] results = new double[subscription.length];\r
+ private double duration;\r
+ public simulateDurationJob(String name, double duration) {\r
+ super(name);\r
+ this.duration = duration;\r
+ }\r
\r
- while(control.getTime() < (eTime - 1e-9)) { // Substract a very small number, because OpenModelica is not very precise with its Real numbers\r
- \r
- control.simulateStep();\r
+ @Override\r
+ protected IStatus run(IProgressMonitor monitor) {\r
+ \r
+ int nSteps = (int)(duration / stepLength); \r
+ int work = 1 + nSteps * 3 + 2; // initialization + number of steps * number of phases per step + set result + call result listeners \r
+ \r
+ monitor.beginTask("Simulating " + duration + " time steps", work);\r
+ monitor.subTask("Initialize step simulation");\r
+\r
+ if(subscription == null || SysdynGameExperiment.this.results == null || control == null)\r
+ return Status.OK_STATUS;\r
+ \r
+ if(duration <= 0.0)\r
+ duration = stepDuration;\r
+ \r
+ \r
+ try {\r
+ control.setStepLength(stepLength); // Set step length each time in case there has been changes\r
+\r
+ double time = control.getTime();\r
+ double eTime = time + duration;\r
+ double[] results = new double[subscription.length];\r
\r
- results = control.getSubscribedResults(results);\r
+ monitor.worked(1);\r
\r
- for(int k = 0; k < subscription.length; k++) {\r
- this.results.get(subscription[k]).add(results[k]);\r
+ while(control.getTime() < (eTime - 1e-9)) { // Substract a very small number, because OpenModelica is not very precise with its Real numbers\r
+ monitor.subTask("Simulate step (time = " + control.getTime() + ")");\r
+ control.simulateStep();\r
+ monitor.worked(1);\r
+ \r
+ monitor.subTask("Get results (time = " + control.getTime() + ")");\r
+ results = control.getSubscribedResults(results);\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
+ }\r
+ monitor.worked(1);\r
}\r
+ \r
+ monitor.subTask("Display results");\r
+ sysdynModel.getSysdynResult().setResult(new GameResult(SysdynGameExperiment.this.results, SysdynGameExperiment.this.subscription));\r
+ monitor.worked(1);\r
+ \r
+ sysdynModel.resultChanged();\r
+ monitor.worked(1);\r
+ \r
+ } catch (FMUJNIException e) {\r
+ System.err.println("SysdynGameExperiment simulateDuration failed: \n\t" + e.getMessage());\r
}\r
\r
- sysdynModel.getSysdynResult().setResult(new GameResult(this.results, this.subscription));\r
- sysdynModel.resultChanged();\r
- \r
- } catch (FMUJNIException e) {\r
- System.err.println("SysdynGameExperiment simulateDuration failed: \n\t" + e.getMessage());\r
+ return Status.OK_STATUS;\r
}\r
}\r
\r
import java.util.ArrayList;\r
import java.util.Iterator;\r
\r
+import org.simantics.objmap.annotations.RelatedElements;\r
+import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.representation.expressions.IExpression;\r
import org.simantics.sysdyn.representation.utils.FormatUtils;\r
\r
*\r
*/\r
public abstract class IndependentVariable extends Variable {\r
+ \r
+ @RelatedElements(\r
+ value = SysdynResource.URIs.Variable_isHeadOf,\r
+ composition = true)\r
+ private ArrayList<IElement> isHeadOf = new ArrayList<IElement>();\r
\r
/**\r
* Get the declaration of this variable\r
return sb.toString();\r
}\r
}\r
+ \r
+ public ArrayList<IElement> getIncomingDependencies() {\r
+ return isHeadOf;\r
+ }\r
}\r
\r
import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
import org.simantics.sysdyn.expressionParser.ParseException;\r
+import org.simantics.sysdyn.expressionParser.Token;\r
import org.simantics.sysdyn.representation.utils.FormatUtils;\r
import org.simantics.sysdyn.representation.utils.RepresentationUtils;\r
\r
* @return Variabilty of a variable\r
*/\r
static public Variability getVariability(IndependentVariable variable) {\r
- if(RepresentationUtils.isGameExperimentActive() && !(variable instanceof Stock))\r
- // FIXME: Game experiment may not be runnable even if normal experiments are. \r
- return getVariability(variable, false, null);\r
- else\r
+ if(RepresentationUtils.isGameExperimentActive() && !(variable instanceof Stock)) {\r
+ \r
+ /* \r
+ * Game experiments cannot use as many parameter variables as normal experiments.\r
+ * If a parameter variable is changed, other parameter values depending on that \r
+ * parameter value are not automatically changed.\r
+ * \r
+ * Something of a hack: \r
+ * Allow variable references, if there are not incoming dependencies to the variable.\r
+ * This enables the use of derived variables for initial values of stocks.\r
+ */\r
+ if(variable.getIncomingDependencies() == null || variable.getIncomingDependencies().isEmpty())\r
+ return getVariability(variable, true, null);\r
+ else\r
+ return getVariability(variable, false, null);\r
+ } else {\r
return getVariability(variable, true, null);\r
+ }\r
}\r
\r
/**\r
return Variability.PARAMETER;\r
} else {\r
Set<String> references = parser.getReferences().keySet();\r
+ \r
+ if(parser.getForIndices()!=null) {\r
+ for(Token t : parser.getForIndices().keySet()) {\r
+ references.remove(t.image);\r
+ }\r
+ }\r
\r
// Go through each reference\r
for(String reference : references) {\r