From: lehtonen Date: Thu, 24 May 2012 20:04:04 +0000 (+0000) Subject: Merge until trunk@25047 to branches/simantics-1.6 on 2012-05-24. refs #3455 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=81dfa602d32ef9a0e72f5f8bfa58bb8719ebf690;p=simantics%2Fsysdyn.git Merge until trunk@25047 to branches/simantics-1.6 on 2012-05-24. refs #3455 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/branches@25049 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSimulator/Release/FMUSimulator.write.1.tlog b/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSimulator/Release/FMUSimulator.write.1.tlog index 924abca6..1fffa993 100644 --- a/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSimulator/Release/FMUSimulator.write.1.tlog +++ b/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSimulator/Release/FMUSimulator.write.1.tlog @@ -83,3 +83,18 @@ D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp +^D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\FMUSimulator\FMUSimulator.vcxproj +D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib +D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib +D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp +D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp +^D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\FMUSimulator\FMUSimulator.vcxproj +D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib +D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib +D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp +D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp +^D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\FMUSimulator\FMUSimulator.vcxproj +D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib +D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.lib +D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp +D:\Workspaces\Sysdyn\org.simantics.modelica\FMUSolution\Release\FMUSimulator.exp diff --git a/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSimulator/Release/vc100.pdb b/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSimulator/Release/vc100.pdb index f79c33fe..85421381 100644 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 diff --git a/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.sdf b/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.sdf index 708fe51d..de61e099 100644 Binary files a/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.sdf and b/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.sdf differ diff --git a/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.suo b/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.suo index 64b5e0eb..b7731543 100644 Binary files a/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.suo and b/simantics-1.6/org.simantics.modelica/FMUSolution/FMUSolution.suo differ diff --git a/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.dll b/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.dll index 3b40be26..a2e3035a 100644 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 diff --git a/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.exp b/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.exp index c3fff80a..ee1787a0 100644 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 diff --git a/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.lib b/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.lib index a19f6f3d..02ef8e8b 100644 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 diff --git a/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.pdb b/simantics-1.6/org.simantics.modelica/FMUSolution/Release/FMUSimulator.pdb index a6e09bce..8b84aba5 100644 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 diff --git a/simantics-1.6/org.simantics.sysdyn.product.site.feature/jenkins.buckminster.script b/simantics-1.6/org.simantics.sysdyn.product.site.feature/jenkins.buckminster.script index 5fd1c714..f7398637 100644 --- a/simantics-1.6/org.simantics.sysdyn.product.site.feature/jenkins.buckminster.script +++ b/simantics-1.6/org.simantics.sysdyn.product.site.feature/jenkins.buckminster.script @@ -1,5 +1,5 @@ -importtargetdefinition -A 'd:/target/3.6.2/target-win.target' -import 'org.simantics.sysdyn.product.site/site.cquery' +importtargetdefinition -A 'd:/target/3.7.1/target-win.target' +import 'site.cquery' build perform -D target.os=* -D target.ws=* -D target.arch=* org.simantics.sysdyn.product.site#site.p2 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 diff --git a/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/CallFMUBeforeInitializationTests.java b/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/CallFMUBeforeInitializationTests.java index 86a095c8..c3974d9e 100644 --- a/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/CallFMUBeforeInitializationTests.java +++ b/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/CallFMUBeforeInitializationTests.java @@ -37,6 +37,13 @@ public class CallFMUBeforeInitializationTests { // control should throw this exception: There is no model to get the variables from } + try { + control.filterVariables(".*"); + fail("filterVariables returned something before loading fmu"); + } catch (FMUJNIException e) { + // control should throw this exception: There is no model to get the variables from + } + try { double d = control.getRealValue("dummy"); fail("Got a Real value before loading fmu: " + d); @@ -179,6 +186,13 @@ public class CallFMUBeforeInitializationTests { fail(e.getMessage()); } + try { + control.filterVariables(".*"); + } catch (FMUJNIException e) { + // control should NOT throw this exception: Variables are found in loadFMU + fail(e.getMessage()); + } + try { double[] array = new double[0]; diff --git a/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUControlTestSuite.java b/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUControlTestSuite.java index 322ce185..89b1e1cf 100644 --- a/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUControlTestSuite.java +++ b/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUControlTestSuite.java @@ -18,7 +18,8 @@ import org.junit.runners.Suite.SuiteClasses; CallFMUBeforeInitializationTests.class, FMUIntegerTest.class, FMUBooleanTest.class, - FMURealTest.class }) + FMURealTest.class, + FMUResultFilterTest.class}) public class FMUControlTestSuite { } diff --git a/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMURealTest.java b/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMURealTest.java index 532f7e78..f996b7a9 100644 --- a/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMURealTest.java +++ b/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMURealTest.java @@ -20,7 +20,7 @@ public class FMURealTest { /* Below is the model used for testing. - Model is located in Models\FMUTests\FMUIntegerTest + Model is located in Models\FMUTests\FMURealTest model FMURealTestModel parameter Real r1 = 1.0; 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 index 00000000..0cbb9f5a --- /dev/null +++ b/simantics-1.6/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUResultFilterTest.java @@ -0,0 +1,142 @@ +package org.simantics.sysdyn.tests.fmi; + +import java.util.Arrays; +import java.util.List; + +import junit.framework.Assert; + +import org.junit.Test; +import org.simantics.modelica.fmi.FMUControlJNI; +import org.simantics.modelica.fmi.FMUJNIException; + + +/** + * Tests result filtering + * @author Teemu Lempinen + * + */ +public class FMUResultFilterTest { + + /* + Below is the model used for testing. + Model is located in Models\FMUTests\FMURealTest + + model FMURealTestModel + parameter Real r1 = 1.0; + parameter Real r2 = 1.0; + Real valve; + Real stock(start=0.0,fixed=true); + parameter Real time = 0; // Time parameter is set at each time step by simulator + equation + if initial() or r1 < 0 or r1 >= 0 then (r1 + r2) else pre(valve); + der(stock) = valve; + end FMURealTestModel; + + + The condition for valve is automatically generated in sysdyn. It is a hack for getting + valve to notice a change in i1 and i2. + + Results always contain "time" for each modelica class, hence result size is +1 + */ + + @Test + public void testReturn3() { + + FMUControlJNI control = new FMUControlJNI(); + + String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu"; + + + try { + + + + control.loadFMUFile(path); + + String[] bothRealParameters = control.filterVariables("r(.*)"); + Assert.assertEquals("Filter \"r(.*)\" returned " + bothRealParameters.length + " results. Should have returned 3", bothRealParameters.length, 3); + List result = Arrays.asList(bothRealParameters); + Assert.assertTrue("Result did not contain r1", result.contains("r1")); + Assert.assertTrue("Result did not contain r2", result.contains("r2")); + Assert.assertTrue("Result did not contain time", result.contains("time")); + + control.unloadFMU(); + } catch (FMUJNIException e) { + Assert.fail("Exception in native fmu code: " + e.getMessage()); + } + } + + @Test + public void testReturn2() { + + FMUControlJNI control = new FMUControlJNI(); + + String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu"; + + + try { + + control.loadFMUFile(path); + + String[] stock = control.filterVariables("stock"); + Assert.assertEquals("Filter \"stock\" returned " + stock.length + " results. Should have returned 2", stock.length, 2); + List result = Arrays.asList(stock); + Assert.assertTrue("Result did not contain stock", result.contains("stock")); + Assert.assertTrue("Result did not contain time", result.contains("time")); + + control.unloadFMU(); + } catch (FMUJNIException e) { + Assert.fail("Exception in native fmu code: " + e.getMessage()); + } + } + + @Test + public void testReturn1() { + + FMUControlJNI control = new FMUControlJNI(); + + String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu"; + + + try { + control.loadFMUFile(path); + + String[] empty = control.filterVariables("empty"); + Assert.assertEquals("Filter \"empty\" returned " + empty.length + " results. Should have returned 1", empty.length, 1); + + List result = Arrays.asList(empty); + Assert.assertTrue("Result did not contain time", result.contains("time")); + + control.unloadFMU(); + } catch (FMUJNIException e) { + Assert.fail("Exception in native fmu code: " + e.getMessage()); + } + } + + @Test + public void testReturn1WhenNullFilter() { + + FMUControlJNI control = new FMUControlJNI(); + + String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu"; + + + try { + control.loadFMUFile(path); + + String[] NULL = control.filterVariables(null); + Assert.assertEquals("Filter null returned " + NULL.length + " results. Should have returned 1", NULL.length, 1); + + List result = Arrays.asList(NULL); + Assert.assertTrue("Result did not contain time", result.contains("time")); + + control.unloadFMU(); + } catch (FMUJNIException e) { + Assert.fail("Exception in native fmu code: " + e.getMessage()); + } + } + + + + +} diff --git a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportFunctionLibrary.java b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportFunctionLibrary.java index 0b65cfd8..0784dd7c 100644 --- a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportFunctionLibrary.java +++ b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportFunctionLibrary.java @@ -25,8 +25,10 @@ import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.databoard.Bindings; -import org.simantics.databoard.Files; import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.databoard.container.DataContainer; +import org.simantics.databoard.container.DataContainers; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.primitiverequest.PossibleRelatedValue; @@ -107,7 +109,7 @@ public class ExportFunctionLibrary extends AbstractHandler { TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, functionLibrary)); try { - Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg); + DataContainers.writeFile(new File(selected), new DataContainer("sysdynFunctionLibrary", 1, new Variant(TransferableGraph1.BINDING, tg))); } catch (RuntimeBindingConstructionException e) { e.printStackTrace(); } catch (IOException e) { diff --git a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java index 2c58e90c..7434b851 100644 --- a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java +++ b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java @@ -25,8 +25,10 @@ import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.databoard.Bindings; -import org.simantics.databoard.Files; import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.databoard.container.DataContainer; +import org.simantics.databoard.container.DataContainers; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.primitiverequest.PossibleRelatedValue; @@ -101,6 +103,7 @@ public class ExportModelHandler extends AbstractHandler { @Override public void run(ReadGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); String name = graph.syncRequest(new PossibleRelatedValue(model, l0.HasName, Bindings.STRING )); ArrayList> roots = new ArrayList>(); @@ -108,7 +111,7 @@ public class ExportModelHandler extends AbstractHandler { TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, model)); try { - Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg); + DataContainers.writeFile(new File(selected), new DataContainer("sysdynModel", 1, new Variant(TransferableGraph1.BINDING, tg))); } catch (RuntimeBindingConstructionException e) { e.printStackTrace(); } catch (IOException e) { diff --git a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModuleHandler.java b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModuleHandler.java index 7b12325e..34958a0e 100644 --- a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModuleHandler.java +++ b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModuleHandler.java @@ -27,8 +27,10 @@ import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.databoard.Bindings; -import org.simantics.databoard.Files; import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.databoard.container.DataContainer; +import org.simantics.databoard.container.DataContainers; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; @@ -246,7 +248,7 @@ public class ExportModuleHandler extends AbstractHandler { graph.deny(replacement.first, sr.Redeclaration_replacedEnumeration_Inverse, replacement.second); TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, component)); - Files.createFile(new File(path), Bindings.getBindingUnchecked(TransferableGraph1.class), tg); + DataContainers.writeFile(new File(path), new DataContainer("sysdynModule", 1, new Variant(TransferableGraph1.BINDING, tg))); for(Pair replacement : replacements) graph.claim(replacement.first, sr.Redeclaration_replacedEnumeration_Inverse, replacement.second); diff --git a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportFunctionLibrary.java b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportFunctionLibrary.java index b20206c4..dad7c060 100644 --- a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportFunctionLibrary.java +++ b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportFunctionLibrary.java @@ -13,6 +13,7 @@ package org.simantics.sysdyn.ui.handlers.imports; import java.io.File; import java.io.IOException; +import java.util.HashMap; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; @@ -25,8 +26,11 @@ import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.databoard.Bindings; -import org.simantics.databoard.Files; -import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.container.DataContainer; +import org.simantics.databoard.container.DataContainers; +import org.simantics.databoard.container.DataFormatException; +import org.simantics.databoard.container.FormatHandler; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; @@ -91,127 +95,113 @@ public class ImportFunctionLibrary extends AbstractHandler { Activator.getDefault().getPreferenceStore().setValue(IMPORTFUNCTIONLIBRARYPATH, (new File(selected)).getParent()); - // Load the transferable graph - TransferableGraph1 tg = null; - try { - tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class)); - } catch (RuntimeBindingConstructionException e) { - e.printStackTrace(); - return null; - } catch (IOException e) { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: Function Library"); - mb.open(); - return null; - } - if(tg == null) return null; - - // Make sure the "http://SharedOntologies resource exists - try { - Boolean hasSharedOntologies; - hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read() { + // Ensure that shared functions ontology exists + ensureSharedOntologies(); + + // Handler for importing function libraries + HashMap> handlers = new HashMap>(); + handlers.put("sysdynFunctionLibrary:1", new FormatHandler() { + @Override + public Binding getBinding() { + return TransferableGraph1.BINDING; + } - @Override - public Boolean perform(ReadGraph graph) throws DatabaseException { - try { - graph.getResource("http://SharedOntologies"); - } catch (ResourceNotFoundException e) { - return false; - } - return true; + @Override + public Object process(DataContainer container) throws DataFormatException { + SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(functionLibrary); + try { + DefaultPasteHandler.defaultExecute((TransferableGraph1)container.content.getValue(), functionLibrary, ia); + return ia.getRoot(); + } catch (Exception e) { + e.printStackTrace(); + return null; } - }); - - if(!hasSharedOntologies) { - SimanticsUI.getSession().syncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - GraphUtils.create2(graph, l0.Library, - l0.HasName, "SharedOntologies", - l0.PartOf, graph.getResource("http:/")); - } - }); - } - } catch (DatabaseException e) { - e.printStackTrace(); - return null; - } + }); - - // Import a function library from the transferable graph - SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(functionLibrary); + // Read function library file + Object result = null; try { - DefaultPasteHandler.defaultExecute(tg, functionLibrary, ia); - } catch (Exception e) { + File file = new File(selected); + if(!file.isFile()) { + displayErrorMessage(shell, "File " + selected + " not found"); + return null; + } else { + result = DataContainers.readFile(file, handlers); + } + } catch(DataFormatException e) { + e.printStackTrace(); + } catch(IOException e) { e.printStackTrace(); + } catch(Exception e) { + if(e instanceof RuntimeException) + throw (RuntimeException)e; + else + throw new RuntimeException(e); } - final Resource root = ia.getRoot(); // Root of the library - - // Link the imported library to the selected resource (functionLibrary) - SimanticsUI.getSession().asyncRequest(new WriteRequest() { - - /** - * Link the imported library to the selected resource (functionLibrary) - * The imported library can be either SysdynModelicaFunctionLibrary or SysdynFunctionOntology - */ - @Override - public void perform(WriteGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - // Case: SharedFunctionOntology. Link to SharedOntologies - if(graph.isInstanceOf(root, SysdynResource.getInstance(graph).SharedFunctionOntology)) { - Resource library = graph.getResource("http://SharedOntologies"); - if(!graph.hasStatement(library, l0.ConsistsOf, root)) { - graph.claim(library, l0.ConsistsOf, root); - } + if(result == null || !(result instanceof Resource)) { + displayErrorMessage(shell, "Function library could not be imported."); + } else { + final Resource FunctionLibraryRoot = (Resource) result; + // Link the imported library to the selected resource (functionLibrary) + SimanticsUI.getSession().asyncRequest(new WriteRequest() { - // Link model to the shared library - SysdynResource sr = SysdynResource.getInstance(graph); - Resource model = functionLibrary; - while(!graph.isInstanceOf(model, sr.SysdynModel) && graph.isInstanceOf(model, l0.Ontology)) - model = graph.getSingleObject(model, l0.PartOf); - if(graph.isInstanceOf(model, sr.SysdynModel)) { - graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, root); - } + /** + * Link the imported library to the selected resource (functionLibrary) + * The imported library can be either SysdynModelicaFunctionLibrary or SysdynFunctionOntology + */ + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + // Case: SharedFunctionOntology. Link to SharedOntologies + if(graph.isInstanceOf(FunctionLibraryRoot, SysdynResource.getInstance(graph).SharedFunctionOntology)) { + Resource library = graph.getResource("http://SharedOntologies"); + if(!graph.hasStatement(library, l0.ConsistsOf, FunctionLibraryRoot)) { + graph.claim(library, l0.ConsistsOf, FunctionLibraryRoot); + } - // Case: not SharedFunctionOntology or SysdynModelicaFunctionLibrary. - } else if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) { - Resource instanceOf = graph.getPossibleObject(root,l0.InstanceOf); - String type = "..."; - if(instanceOf != null) - type = NameUtils.getSafeName(graph, instanceOf); - else { - Resource inheritedFrom = graph.getPossibleObject(root, l0.Inherits); - if(inheritedFrom != null) - type = NameUtils.getSafeName(graph, inheritedFrom); - } - final String ft = type; - - // Remove the functionLibrary ConsistsOf root relation - graph.deny(root, l0.PartOf); - - // Display error message - shell.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: Function Library (" + ft +")"); - mb.open(); + // Link model to the shared library + SysdynResource sr = SysdynResource.getInstance(graph); + Resource model = functionLibrary; + while(!graph.isInstanceOf(model, sr.SysdynModel) && graph.isInstanceOf(model, l0.Ontology)) + model = graph.getSingleObject(model, l0.PartOf); + if(graph.isInstanceOf(model, sr.SysdynModel)) { + graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, FunctionLibraryRoot); } - }); - } else { - // imported library is already in the right place. Update function library files. - FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary); - } - } - }); + // Case: not SharedFunctionOntology or SysdynModelicaFunctionLibrary. + } else if(!graph.isInstanceOf(FunctionLibraryRoot, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) { + Resource instanceOf = graph.getPossibleObject(FunctionLibraryRoot,l0.InstanceOf); + String type = "..."; + if(instanceOf != null) + type = NameUtils.getSafeName(graph, instanceOf); + else { + Resource inheritedFrom = graph.getPossibleObject(FunctionLibraryRoot, l0.Inherits); + if(inheritedFrom != null) + type = NameUtils.getSafeName(graph, inheritedFrom); + } + final String ft = type; + + // Remove the functionLibrary ConsistsOf root relation + graph.deny(FunctionLibraryRoot, l0.PartOf); + + // Display error message + shell.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + displayErrorMessage(shell, "The imported file is not of type: Function Library (" + ft +")"); + } + }); + } else { + // imported library is already in the right place. Update function library files. + FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary); + } + + } + }); + } return null; } @@ -252,5 +242,48 @@ public class ImportFunctionLibrary extends AbstractHandler { } } + + private void ensureSharedOntologies() { + // Make sure the "http://SharedOntologies resource exists + try { + Boolean hasSharedOntologies; + hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public Boolean perform(ReadGraph graph) throws DatabaseException { + try { + graph.getResource("http://SharedOntologies"); + } catch (ResourceNotFoundException e) { + return false; + } + return true; + } + }); + + if(!hasSharedOntologies) { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + GraphUtils.create2(graph, l0.Library, + l0.HasName, "SharedOntologies", + l0.PartOf, graph.getResource("http:/")); + } + }); + + } + } catch (DatabaseException e) { + e.printStackTrace(); + } + + } + + private void displayErrorMessage(Shell shell, String message) { + MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); + mb.setText("Error"); + mb.setMessage("The imported file is not of type: Model"); + mb.open(); + } } diff --git a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java index ccecf5bb..64c06a70 100644 --- a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java +++ b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java @@ -13,8 +13,7 @@ package org.simantics.sysdyn.ui.handlers.imports; import java.io.File; import java.io.IOException; -import java.util.HashSet; -import java.util.UUID; +import java.util.HashMap; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; @@ -25,12 +24,13 @@ import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; -import org.simantics.databoard.Bindings; -import org.simantics.databoard.Files; -import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.container.DataContainer; +import org.simantics.databoard.container.DataContainers; +import org.simantics.databoard.container.DataFormatException; +import org.simantics.databoard.container.FormatHandler; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; @@ -38,16 +38,8 @@ import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; import org.simantics.graph.representation.TransferableGraph1; import org.simantics.layer0.Layer0; -import org.simantics.layer0.utils.direct.GraphUtils; -import org.simantics.modeling.ModelingResources; -import org.simantics.operation.Layer0X; -import org.simantics.simulation.ontology.SimulationResource; -import org.simantics.spreadsheet.resource.SpreadsheetResource; -import org.simantics.structural.stubs.StructuralResource2; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.Activator; -import org.simantics.sysdyn.ui.utils.OldTransferableGraph1; -import org.simantics.sysdyn.ui.utils.SheetUtils; import org.simantics.ui.SimanticsUI; /** @@ -57,20 +49,20 @@ import org.simantics.ui.SimanticsUI; * */ public class ImportModelHandler extends AbstractHandler { - + public static String IMPORTMODELTPATH = "IMPORT_MODEL_PATH"; @Override public Object execute(ExecutionEvent event) throws ExecutionException { - - Resource project = SimanticsUI.getProject().get(); + + final Resource project = SimanticsUI.getProject().get(); if(project == null) return null; // Get imported transferable graph file using FileDialog final Shell shell = HandlerUtil.getActiveShellChecked(event); FileDialog fd = new FileDialog(shell, SWT.OPEN); fd.setText("Import Model"); - + String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMODELTPATH); if(path.isEmpty() || !(new File(path).exists())) path = Platform.getLocation().toOSString(); @@ -79,200 +71,99 @@ public class ImportModelHandler extends AbstractHandler { fd.setFilterExtensions(filterExt); String selected = fd.open(); if(selected == null) return null; - + Activator.getDefault().getPreferenceStore().setValue(IMPORTMODELTPATH, (new File(selected)).getParent()); - - // Get the transferable graph from file - TransferableGraph1 tg = null; - try { - tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class)); - } catch (RuntimeBindingConstructionException e) { - e.printStackTrace(); - } catch (IOException e) { - try { - // "Version migration". Use OldTransferableGraph1 if the file was imported using old methods - OldTransferableGraph1 otg = (OldTransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(OldTransferableGraph1.class)); - tg = new TransferableGraph1(otg.resourceCount, otg.identities, otg.statements, otg.values); - } catch (RuntimeBindingConstructionException e1) { - e1.printStackTrace(); - } catch (IOException e1) { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: Model"); - mb.open(); - return null; + + + + HashMap> handlers = new HashMap>(); + handlers.put("sysdynModel:1", new FormatHandler() { + @Override + public Binding getBinding() { + return TransferableGraph1.BINDING; } - } - if(tg == null) return null; - - try { - - DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(project); - DefaultPasteHandler.defaultExecute(tg, SimanticsUI.getProject().get(), ia); - - // Check that imported resource was actually a model - //and fix changes made to old ontology versions - final Resource root = ia.getRoot(); - SimanticsUI.getSession().syncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - - if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModel)) { - // Imported model was not a SysdynModel, display error message. - Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf); - String type = "..."; - if(instanceOf != null) - type = NameUtils.getSafeName(graph, instanceOf); - else { - Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits); - if(inheritedFrom != null) - type = NameUtils.getSafeName(graph, inheritedFrom); - } - final String ft = type; - graph.deny(root, Layer0.getInstance(graph).PartOf); - - shell.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: System Dynamics Model (" + ft +")"); - mb.open(); - } - }); + @Override + public Object process(DataContainer container) throws DataFormatException { + DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(project); + try { + DefaultPasteHandler.defaultExecute((TransferableGraph1)container.content.getValue(), project, ia); + return ia.getRoot(); - } else { - - Resource project = SimanticsUI.getProject().get(); - graph.claim(root, Layer0X.getInstance(graph).IsActivatedBy, project); - - // Do all modifications that are necessary - updateOldConfigurationToBaseRealization(graph, root); - addDefaultOntologyLinks(graph, root); - addURIsToDiagrams(graph, root); - addSpreadSheetBook(graph, root); - } + } catch (Exception e) { + e.printStackTrace(); + return null; } - }); - } catch (DatabaseException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } + } + }); - return null; - } - - /** - * In old versions base realization was separate. Newer versions use configuration as base realization. - * @param graph WriteGraph - * @param model Imported model - */ - private static void updateOldConfigurationToBaseRealization(WriteGraph graph, Resource model) { - Layer0X L0X = Layer0X.getInstance(graph); + Object result = null; try { - Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration); - if(configuration != null && !graph.hasStatement(configuration, L0X.IsBaseRealizationOf, model)) - graph.claim(configuration, L0X.IsBaseRealizationOf, model); - } catch (DatabaseException e) { + File file = new File(selected); + if(!file.isFile()) { + displayErrorMessage(shell, "File " + selected + " not found"); + return null; + } else { + result = DataContainers.readFile(file, handlers); + } + } catch(DataFormatException e) { e.printStackTrace(); - } - - } - - /** - * Links should be exported and imported automatically. If it has failed, the - * default ontology links sysdyn and layer0 are added. - * - * @param graph WriteGraph - * @param model Imported model - */ - - private static void addDefaultOntologyLinks(WriteGraph graph, Resource model) { - try { - Layer0 l0 = Layer0.getInstance(graph); - // The links should be exported and imported automatically - Resource sysdyn = graph.getResource("http://www.simantics.org/Sysdyn-1.1"); - Resource layer0 = graph.getResource("http://www.simantics.org/Layer0-1.0"); - if(!graph.hasStatement(model, l0.IsLinkedTo, sysdyn)) - graph.claim(model, l0.IsLinkedTo, sysdyn); - if(!graph.hasStatement(model, l0.IsLinkedTo, layer0)) - graph.claim(model, l0.IsLinkedTo, layer0); - } catch (DatabaseException e) { + } catch(IOException e) { e.printStackTrace(); + } catch(Exception e) { + if(e instanceof RuntimeException) + throw (RuntimeException)e; + else + throw new RuntimeException(e); } - } - - - /** - * Add a container for providing URIs to diagrams. - * - * @param graph WriteGraph - * @param model Imported model - */ - private static void addURIsToDiagrams(WriteGraph graph, Resource model) { - Layer0 l0 = Layer0.getInstance(graph); - SimulationResource simu = SimulationResource.getInstance(graph); - ModelingResources mr = ModelingResources.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - StructuralResource2 sr2 = StructuralResource2.getInstance(graph); - try { - HashSet configurations = new HashSet(); - Resource configuration = graph.getPossibleObject(model, simu.HasConfiguration); - if(configuration != null) - configurations.add(configuration); - - for(Resource r : graph.getObjects(model, l0.ConsistsOf)) { - if(graph.isInheritedFrom(r, sr.Module)) { - Resource moduleConfiguration = graph.getPossibleObject(r, sr2.IsDefinedBy); - if(moduleConfiguration != null) - configurations.add(moduleConfiguration); - } - } - - for(Resource conf : configurations) { - Resource configurationDiagram = graph.getPossibleObject(conf, mr.CompositeToDiagram); - if(configurationDiagram != null && !graph.hasStatement(configurationDiagram, l0.PartOf)) { - GraphUtils.create2(graph, l0.Library, - l0.HasName, "__CONTAINER__", - l0.PartOf, conf, - l0.ConsistsOf, configurationDiagram); - } + if(result == null || !(result instanceof Resource)) { + displayErrorMessage(shell, "Model could not be imported."); + } else { + try { + final Resource ModelRoot = (Resource) result; + SimanticsUI.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + if(!graph.isInstanceOf(ModelRoot, SysdynResource.getInstance(graph).SysdynModel)) { + // Imported model was not a SysdynModel, display error message. + Resource instanceOf = graph.getPossibleObject(ModelRoot, Layer0.getInstance(graph).InstanceOf); + String type = "..."; + if(instanceOf != null) + type = NameUtils.getSafeName(graph, instanceOf); + else { + Resource inheritedFrom = graph.getPossibleObject(ModelRoot, Layer0.getInstance(graph).Inherits); + if(inheritedFrom != null) + type = NameUtils.getSafeName(graph, inheritedFrom); + } + final String ft = type; + graph.deny(ModelRoot, Layer0.getInstance(graph).PartOf); + + shell.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + displayErrorMessage(shell, "The imported file is not of type: System Dynamics Model (" + ft +")"); + } + }); + + } + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); } - - } catch (DatabaseException e) { - e.printStackTrace(); } + + return null; + } - - - /** - * Add a missing spreadsheet book to the model - * - * @param graph - * @param model - */ - private static void addSpreadSheetBook(WriteGraph graph, Resource model) { - try { - Layer0 l0 = Layer0.getInstance(graph); - SpreadsheetResource ssr = SpreadsheetResource.getInstance(graph); - SimulationResource simu = SimulationResource.getInstance(graph); - Resource conf = graph.getPossibleObject(model, simu.HasConfiguration); - if(conf != null && graph.syncRequest(new ObjectsWithType(conf, l0.ConsistsOf, ssr.Book)).isEmpty()) { - Resource book = graph.newResource(); - graph.claim(book, l0.InstanceOf, null, ssr.Book); - graph.addLiteral(book, l0.HasName, l0.NameOf, l0.String, "Book" + UUID.randomUUID().toString(), Bindings.STRING); - graph.claim(conf, l0.ConsistsOf, l0.PartOf, book); - SheetUtils.createSheet(graph, book, "Sheet1", new String[] { }, new int[] { 50 }); - } - } catch (DatabaseException e) { - e.printStackTrace(); - } + private void displayErrorMessage(Shell shell, String message) { + MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); + mb.setText("Error"); + mb.setMessage("The imported file is not of type: Model"); + mb.open(); } - } diff --git a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModuleHandler.java b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModuleHandler.java index 9a0cfceb..cf422425 100644 --- a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModuleHandler.java +++ b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModuleHandler.java @@ -13,6 +13,7 @@ package org.simantics.sysdyn.ui.handlers.imports; import java.io.File; import java.io.IOException; +import java.util.HashMap; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; @@ -25,9 +26,11 @@ import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.browsing.ui.common.node.AbstractNode; -import org.simantics.databoard.Bindings; -import org.simantics.databoard.Files; -import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.container.DataContainer; +import org.simantics.databoard.container.DataContainers; +import org.simantics.databoard.container.DataFormatException; +import org.simantics.databoard.container.FormatHandler; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.WriteRequest; @@ -35,7 +38,6 @@ import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; -import org.simantics.graph.db.MissingDependencyException; import org.simantics.graph.representation.TransferableGraph1; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; @@ -51,95 +53,122 @@ import org.simantics.ui.utils.AdaptionUtils; */ public class ImportModuleHandler extends AbstractHandler { - public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH"; - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - ISelection sel = HandlerUtil.getCurrentSelection(event); - - @SuppressWarnings("unchecked") - AbstractNode node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class); - if (node == null) - return null; - - final Resource model = node.data; - - // Get imported transferable graph file using FileDialog - final Shell shell = HandlerUtil.getActiveShellChecked(event); - FileDialog fd = new FileDialog(shell, SWT.OPEN); - fd.setText("Import Module"); - - String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMODULETPATH); - if(path.isEmpty() || !(new File(path).exists())) - path = Platform.getLocation().toOSString(); - fd.setFilterPath(path); - String[] filterExt = {"*.tg"}; - fd.setFilterExtensions(filterExt); - String selected = fd.open(); - if(selected == null) return null; - - Activator.getDefault().getPreferenceStore().setValue(IMPORTMODULETPATH, (new File(selected)).getParent()); - - // Get the transferable graph from file - TransferableGraph1 tg = null; - try { - tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class)); - } catch (RuntimeBindingConstructionException e) { - e.printStackTrace(); - } catch (IOException e) { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: Module Type"); - mb.open(); - return null; - } - if(tg == null) return null; - - - // Import the module to model - DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(model); - try { - DefaultPasteHandler.defaultExecute(tg, model, ia); - } catch (MissingDependencyException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - - final Resource root = ia.getRoot(); - - // Check that the imported file actually was a module. Display error message otherwise. - SimanticsUI.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - if(!graph.isInheritedFrom(root, SysdynResource.getInstance(graph).Module)) { - Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf); - String type = "..."; - if(instanceOf != null) - type = NameUtils.getSafeName(graph, instanceOf); - else { - Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits); - if(inheritedFrom != null) - type = NameUtils.getSafeName(graph, inheritedFrom); - } - final String ft = type; - graph.deny(root, Layer0.getInstance(graph).PartOf); - - shell.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: System Dynamics Module (" + ft +")"); - mb.open(); - } - }); - } - } - }); - return null; - } + public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH"; + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + ISelection sel = HandlerUtil.getCurrentSelection(event); + + @SuppressWarnings("unchecked") + AbstractNode node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class); + if (node == null) + return null; + + final Resource model = node.data; + + // Get imported transferable graph file using FileDialog + final Shell shell = HandlerUtil.getActiveShellChecked(event); + FileDialog fd = new FileDialog(shell, SWT.OPEN); + fd.setText("Import Module"); + + String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMODULETPATH); + if(path.isEmpty() || !(new File(path).exists())) + path = Platform.getLocation().toOSString(); + fd.setFilterPath(path); + String[] filterExt = {"*.tg"}; + fd.setFilterExtensions(filterExt); + String selected = fd.open(); + if(selected == null) return null; + + Activator.getDefault().getPreferenceStore().setValue(IMPORTMODULETPATH, (new File(selected)).getParent()); + + // Handler for importing modules + HashMap> handlers = new HashMap>(); + handlers.put("sysdynModule:1", new FormatHandler() { + @Override + public Binding getBinding() { + return TransferableGraph1.BINDING; + } + + @Override + public Object process(DataContainer container) throws DataFormatException { + DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(model); + try { + DefaultPasteHandler.defaultExecute((TransferableGraph1)container.content.getValue(), model, ia); + return ia.getRoot(); + + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + }); + + // Read module file + Object result = null; + try { + File file = new File(selected); + if(!file.isFile()) { + displayErrorMessage(shell, "File " + selected + " not found"); + return null; + } else { + result = DataContainers.readFile(file, handlers); + } + } catch(DataFormatException e) { + e.printStackTrace(); + } catch(IOException e) { + e.printStackTrace(); + } catch(Exception e) { + if(e instanceof RuntimeException) + throw (RuntimeException)e; + else + throw new RuntimeException(e); + } + + + if(result == null || !(result instanceof Resource)) { + displayErrorMessage(shell, "Module could not be imported."); + } else { + final Resource ModuleRoot = (Resource) result; + // Check that the imported file actually was a module. Display error message otherwise. + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + if(!graph.isInheritedFrom(ModuleRoot, SysdynResource.getInstance(graph).Module)) { + Resource instanceOf = graph.getPossibleObject(ModuleRoot, Layer0.getInstance(graph).InstanceOf); + String type = "..."; + if(instanceOf != null) + type = NameUtils.getSafeName(graph, instanceOf); + else { + Resource inheritedFrom = graph.getPossibleObject(ModuleRoot, Layer0.getInstance(graph).Inherits); + if(inheritedFrom != null) + type = NameUtils.getSafeName(graph, inheritedFrom); + } + final String ft = type; + graph.deny(ModuleRoot, Layer0.getInstance(graph).PartOf); + + shell.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + displayErrorMessage(shell, "The imported file is not of type: System Dynamics Module (" + ft +")"); + } + }); + } + } + }); + + } + return null; + } + + + private void displayErrorMessage(Shell shell, String message) { + MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); + mb.setText("Error"); + mb.setMessage("The imported file is not of type: Model"); + mb.open(); + } } diff --git a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ParameterExpression.java b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ParameterExpression.java index 7d37311f..131705c9 100644 --- a/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ParameterExpression.java +++ b/simantics-1.6/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ParameterExpression.java @@ -87,7 +87,7 @@ public class ParameterExpression extends BasicExpression { } catch (DatabaseException e) { throw new DatabaseException(e.getMessage()); } catch (FMUJNIException e) { - e.printStackTrace(); + System.err.println("ParameterExpression.java: " + e.getMessage()); return null; } } diff --git a/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java b/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java index be625aa3..8d82b12f 100644 --- a/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java +++ b/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java @@ -16,6 +16,9 @@ import java.util.ArrayList; import java.util.HashMap; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; import org.simantics.db.AsyncReadGraph; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -201,36 +204,72 @@ public class SysdynGameExperiment extends SysdynExperiment { @Override public void simulateDuration(double duration) { - if(subscription == null || this.results == null || control == null) - return; - - if(duration <= 0.0) - duration = stepDuration; - - - try { - control.setStepLength(stepLength); // Set step length each time in case there has been changes + Job job = new simulateDurationJob("Simulate steps", duration); + // Start the Job + job.schedule(); + } + + private class simulateDurationJob extends Job { - double time = control.getTime(); - double eTime = time + duration; - double[] results = new double[subscription.length]; + private double duration; + public simulateDurationJob(String name, double duration) { + super(name); + this.duration = duration; + } - while(control.getTime() < (eTime - 1e-9)) { // Substract a very small number, because OpenModelica is not very precise with its Real numbers - - control.simulateStep(); + @Override + protected IStatus run(IProgressMonitor monitor) { + + int nSteps = (int)(duration / stepLength); + int work = 1 + nSteps * 3 + 2; // initialization + number of steps * number of phases per step + set result + call result listeners + + monitor.beginTask("Simulating " + duration + " time steps", work); + monitor.subTask("Initialize step simulation"); + + if(subscription == null || SysdynGameExperiment.this.results == null || control == null) + return Status.OK_STATUS; + + if(duration <= 0.0) + duration = stepDuration; + + + try { + control.setStepLength(stepLength); // Set step length each time in case there has been changes + + double time = control.getTime(); + double eTime = time + duration; + double[] results = new double[subscription.length]; - results = control.getSubscribedResults(results); + monitor.worked(1); - for(int k = 0; k < subscription.length; k++) { - this.results.get(subscription[k]).add(results[k]); + while(control.getTime() < (eTime - 1e-9)) { // Substract a very small number, because OpenModelica is not very precise with its Real numbers + monitor.subTask("Simulate step (time = " + control.getTime() + ")"); + control.simulateStep(); + monitor.worked(1); + + monitor.subTask("Get results (time = " + control.getTime() + ")"); + results = control.getSubscribedResults(results); + monitor.worked(1); + + monitor.subTask("Save results (time = " + control.getTime() + ")"); + for(int k = 0; k < subscription.length; k++) { + SysdynGameExperiment.this.results.get(subscription[k]).add(results[k]); + } + monitor.worked(1); } + + monitor.subTask("Display results"); + sysdynModel.getSysdynResult().setResult(new GameResult(SysdynGameExperiment.this.results, SysdynGameExperiment.this.subscription)); + monitor.worked(1); + + sysdynModel.resultChanged(); + monitor.worked(1); + + } catch (FMUJNIException e) { + System.err.println("SysdynGameExperiment simulateDuration failed: \n\t" + e.getMessage()); } - sysdynModel.getSysdynResult().setResult(new GameResult(this.results, this.subscription)); - sysdynModel.resultChanged(); - - } catch (FMUJNIException e) { - System.err.println("SysdynGameExperiment simulateDuration failed: \n\t" + e.getMessage()); + return Status.OK_STATUS; } } diff --git a/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java b/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java index 0b95bf6b..c2975cb7 100644 --- a/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java +++ b/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java @@ -14,6 +14,8 @@ package org.simantics.sysdyn.representation; import java.util.ArrayList; import java.util.Iterator; +import org.simantics.objmap.annotations.RelatedElements; +import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.representation.expressions.IExpression; import org.simantics.sysdyn.representation.utils.FormatUtils; @@ -24,6 +26,11 @@ import org.simantics.sysdyn.representation.utils.FormatUtils; * */ public abstract class IndependentVariable extends Variable { + + @RelatedElements( + value = SysdynResource.URIs.Variable_isHeadOf, + composition = true) + private ArrayList isHeadOf = new ArrayList(); /** * Get the declaration of this variable @@ -150,4 +157,8 @@ public abstract class IndependentVariable extends Variable { return sb.toString(); } } + + public ArrayList getIncomingDependencies() { + return isHeadOf; + } } diff --git a/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variability.java b/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variability.java index 8fb226a4..7a5f165b 100644 --- a/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variability.java +++ b/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variability.java @@ -18,6 +18,7 @@ import java.util.Set; import org.simantics.sysdyn.expressionParser.ExpressionParser; import org.simantics.sysdyn.expressionParser.ParseException; +import org.simantics.sysdyn.expressionParser.Token; import org.simantics.sysdyn.representation.utils.FormatUtils; import org.simantics.sysdyn.representation.utils.RepresentationUtils; @@ -135,11 +136,24 @@ public enum Variability { * @return Variabilty of a variable */ static public Variability getVariability(IndependentVariable variable) { - if(RepresentationUtils.isGameExperimentActive() && !(variable instanceof Stock)) - // FIXME: Game experiment may not be runnable even if normal experiments are. - return getVariability(variable, false, null); - else + if(RepresentationUtils.isGameExperimentActive() && !(variable instanceof Stock)) { + + /* + * Game experiments cannot use as many parameter variables as normal experiments. + * If a parameter variable is changed, other parameter values depending on that + * parameter value are not automatically changed. + * + * Something of a hack: + * Allow variable references, if there are not incoming dependencies to the variable. + * This enables the use of derived variables for initial values of stocks. + */ + if(variable.getIncomingDependencies() == null || variable.getIncomingDependencies().isEmpty()) + return getVariability(variable, true, null); + else + return getVariability(variable, false, null); + } else { return getVariability(variable, true, null); + } } /** @@ -216,6 +230,12 @@ public enum Variability { return Variability.PARAMETER; } else { Set references = parser.getReferences().keySet(); + + if(parser.getForIndices()!=null) { + for(Token t : parser.getForIndices().keySet()) { + references.remove(t.image); + } + } // Go through each reference for(String reference : references) {