]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Merge until trunk@25047 to branches/simantics-1.6 on 2012-05-24. refs #3455
authorlehtonen <lehtonen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 24 May 2012 20:04:04 +0000 (20:04 +0000)
committerlehtonen <lehtonen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 24 May 2012 20:04:04 +0000 (20:04 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/branches@25049 ac1ea38d-2e2b-0410-8846-a27921b304fc

23 files changed:
simantics-1.6/org.simantics.modelica/FMUSolution/FMUSimulator/Release/FMUSimulator.write.1.tlog
simantics-1.6/org.simantics.modelica/FMUSolution/FMUSimulator/Release/vc100.pdb
simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.sdf
simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.suo
simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.dll
simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.exp
simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.lib
simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.pdb
simantics-1.6/org.simantics.sysdyn.product.site.feature/jenkins.buckminster.script
simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/CallFMUBeforeInitializationTests.java
simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUControlTestSuite.java
simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMURealTest.java
simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUResultFilterTest.java [new file with mode: 0644]
simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportFunctionLibrary.java
simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java
simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModuleHandler.java
simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportFunctionLibrary.java
simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java
simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModuleHandler.java
simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ParameterExpression.java
simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java
simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java
simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variability.java

index 924abca6d6af8757788db2d64e04f20043223b4b..1fffa993eec0fb1eed1e5c0d83906c4eec8b0afd 100644 (file)
@@ -83,3 +83,18 @@ D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib
 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
index f79c33fe4d387f5309e2b3728aa32f308d84040d..854213819bcdfa70f89bfb3277f00191ba2f5543 100644 (file)
Binary files a/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSimulator/Release/vc100.pdb and b/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSimulator/Release/vc100.pdb differ
index 708fe51d07e594f2936ae6d0657c97f070ef0106..de61e09943bb83a1e827256b41f0b01a455f778b 100644 (file)
Binary files a/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.sdf and b/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.sdf differ
index 64b5e0eb660da1c8012a383e288279a4fbf52805..b77315432f64dc344d7d223045d9fbb5ee27ce40 100644 (file)
Binary files a/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.suo and b/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.suo differ
index 3b40be2652d79443c304de0aa37b8c924b7347c7..a2e3035a923425df2a1f0f7e78b00f5d99f15ddf 100644 (file)
Binary files a/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.dll and b/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.dll differ
index c3fff80a4043f638a780e7d37c5d7923e7dc4fb5..ee1787a0ed650408132abdfb883365fb62a6240e 100644 (file)
Binary files a/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.exp and b/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.exp differ
index a19f6f3df6d7688131d6f01552f2b45d2a53e5c0..02ef8e8b7a54a93b69fe9d3e4531e0f417308516 100644 (file)
Binary files a/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.lib and b/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.lib differ
index a6e09bcee7a03f02769538cedcfdac73924ccdd0..8b84aba547aab7015ce1958669cfd69ee1074c01 100644 (file)
Binary files a/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.pdb and b/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.pdb differ
index 5fd1c7141c9a2f20422c1f3caf9a8e132e02f7ac..f7398637e5c7c0c6d49e29d085fd4364bb383012 100644 (file)
@@ -1,5 +1,5 @@
-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
index 86a095c847f5d9a3ce29320ecbe01d91b3ff9ea0..c3974d9ee8b578007da135a7674977d3cb58532b 100644 (file)
@@ -37,6 +37,13 @@ public class CallFMUBeforeInitializationTests {
                        // 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
@@ -179,6 +186,13 @@ public class CallFMUBeforeInitializationTests {
                        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
index 322ce185a88cebc9a33a1548f301361b67892500..89b1e1cfe954249d690f2bb613f45a0e5430db40 100644 (file)
@@ -18,7 +18,8 @@ import org.junit.runners.Suite.SuiteClasses;
        CallFMUBeforeInitializationTests.class, \r
        FMUIntegerTest.class, \r
        FMUBooleanTest.class, \r
-       FMURealTest.class })\r
+       FMURealTest.class,\r
+       FMUResultFilterTest.class})\r
 public class FMUControlTestSuite {\r
 \r
 }\r
index 532f7e78896e0a9a72bcb1b047ce54efe21e034f..f996b7a915f68d0ea55a49e1f286d38b62553589 100644 (file)
@@ -20,7 +20,7 @@ public class FMURealTest {
        \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
diff --git a/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUResultFilterTest.java b/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUResultFilterTest.java
new file mode 100644 (file)
index 0000000..0cbb9f5
--- /dev/null
@@ -0,0 +1,142 @@
+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
index 0b65cfd8e4ed6270dd7ee0e30aebb47454527935..0784dd7c6ddeb00cec3f64a4b39bd7c8291be731 100644 (file)
@@ -25,8 +25,10 @@ import org.eclipse.swt.widgets.FileDialog;
 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
@@ -107,7 +109,7 @@ public class ExportFunctionLibrary  extends AbstractHandler {
                                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
index 2c58e90c9bd0a311d6c6e1be7b515fc15395216b..7434b851460e7fa9481e09b83aed068e5796874c 100644 (file)
@@ -25,8 +25,10 @@ import org.eclipse.swt.widgets.FileDialog;
 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
@@ -101,6 +103,7 @@ public class ExportModelHandler extends AbstractHandler {
                        \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
@@ -108,7 +111,7 @@ public class ExportModelHandler extends AbstractHandler {
                                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
index 7b12325e14b17408ecf437ab3ba6e9d5fad37db7..34958a0e82fcad94454adebf741be3bee4a14ef1 100644 (file)
@@ -27,8 +27,10 @@ import org.eclipse.swt.widgets.MessageBox;
 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
@@ -246,7 +248,7 @@ public class ExportModuleHandler extends AbstractHandler {
                                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
index b20206c43b08102b72e9f0aa61576ed3b37101c3..dad7c060cbbad5568fb7096168701c171e679936 100644 (file)
@@ -13,6 +13,7 @@ package org.simantics.sysdyn.ui.handlers.imports;
 \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
@@ -25,8 +26,11 @@ import org.eclipse.swt.widgets.MessageBox;
 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
@@ -91,127 +95,113 @@ public class ImportFunctionLibrary  extends AbstractHandler {
 \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
@@ -252,5 +242,48 @@ public class ImportFunctionLibrary  extends AbstractHandler {
                }\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
index ccecf5bbebac2ea2d0d6d55a6304033212bfcc45..64c06a70f81e7be49041ebd056e37017f80f1426 100644 (file)
@@ -13,8 +13,7 @@ package org.simantics.sysdyn.ui.handlers.imports;
 \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
@@ -25,12 +24,13 @@ import org.eclipse.swt.widgets.FileDialog;
 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
@@ -38,16 +38,8 @@ import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
 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
@@ -57,20 +49,20 @@ import org.simantics.ui.SimanticsUI;
  *\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
@@ -79,200 +71,99 @@ public class ImportModelHandler extends AbstractHandler {
                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
index 9a0cfceb746289371ad1def87b7832c49076d0f8..cf42242512b7b42a3bce196a002d8747ab9e965c 100644 (file)
@@ -13,6 +13,7 @@ package org.simantics.sysdyn.ui.handlers.imports;
 \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
@@ -25,9 +26,11 @@ import org.eclipse.swt.widgets.MessageBox;
 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
@@ -35,7 +38,6 @@ import org.simantics.db.common.utils.NameUtils;
 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
@@ -51,95 +53,122 @@ import org.simantics.ui.utils.AdaptionUtils;
  */\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
index 7d37311f3a1064749f3dc252e12ed48291e3b819..131705c93f7891492a38868b08b52648ca60a473 100644 (file)
@@ -87,7 +87,7 @@ public class ParameterExpression extends BasicExpression {
                                                } 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
index be625aa33ac9f838d5a2354c6718ef12009fa041..8d82b12f7aacbf39b815e8ff75811766daacbebd 100644 (file)
@@ -16,6 +16,9 @@ import java.util.ArrayList;
 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
@@ -201,36 +204,72 @@ public class SysdynGameExperiment extends SysdynExperiment {
 \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
index 0b95bf6bc377d75fd0914aa1fe5c8f076a505dc6..c2975cb7eceebc5a651c9628da86cf1f115325d1 100644 (file)
@@ -14,6 +14,8 @@ package org.simantics.sysdyn.representation;
 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
@@ -24,6 +26,11 @@ import org.simantics.sysdyn.representation.utils.FormatUtils;
  *\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
@@ -150,4 +157,8 @@ public abstract class IndependentVariable extends Variable {
             return sb.toString();\r
         }\r
     }\r
+    \r
+    public ArrayList<IElement> getIncomingDependencies() {\r
+       return isHeadOf;\r
+    }\r
 }\r
index 8fb226a459a764f08ff061aa2c55879e46ff087c..7a5f165bd69f9eecca3a98b6ba16f228d388a7db 100644 (file)
@@ -18,6 +18,7 @@ import java.util.Set;
 \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
@@ -135,11 +136,24 @@ public enum Variability {
      * @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
@@ -216,6 +230,12 @@ public enum Variability {
                 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