]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Version migration support to sysdyn. Changes paths in .tg files to enable importing...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 26 Nov 2012 12:11:17 +0000 (12:11 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 26 Nov 2012 12:11:17 +0000 (12:11 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26446 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ontology/graph.tg
org.simantics.sysdyn.ontology/graph/Migration.pgraph [new file with mode: 0644]
org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph
org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/SysdynVariableRemover.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/newComponents/NewModelHandler.java

index 94ba196f587199812ae146e85c8e41ed5af25ddd..2299b42f6efc4bb2fef7d935010a59965c78321c 100644 (file)
Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ
diff --git a/org.simantics.sysdyn.ontology/graph/Migration.pgraph b/org.simantics.sysdyn.ontology/graph/Migration.pgraph
new file mode 100644 (file)
index 0000000..a1d6d1f
--- /dev/null
@@ -0,0 +1,100 @@
+L0 = <http://www.simantics.org/Layer0-1.1>\r
+SYSDYN = <http://www.simantics.org/Sysdyn-1.1>\r
+\r
+move : L0.Template\r
+    @template %subject %base %from %to\r
+        %subject : L0.NamespaceMigrationStep.Prefix        \r
+            L0.NamespaceMigrationStep.Prefix.base %base\r
+            L0.NamespaceMigrationStep.Prefix.from %from\r
+            L0.NamespaceMigrationStep.Prefix.to %to\r
+            \r
+MIGRATION = SYSDYN."from1.6to1.7" : L0.MigrationSequence\r
+    @L0.list\r
+        MIGRATION.Ontologies\r
+        MIGRATION.Spreadsheets\r
+        MIGRATION.SysdynChanges\r
+\r
+MIGRATION.Ontologies : L0.NamespaceMigrationStep\r
+  @L0.list\r
+    _\r
+      @move "http://www.simantics.org/Layer0-" "1.0" "1.1"\r
+    _\r
+      @move "http://www.simantics.org/Layer0X-" "1.0" "1.1"\r
+    _\r
+      @move"http://www.simantics.org/G2D-" "1.0" "1.1"\r
+    _\r
+      @move"http://www.simantics.org/Structural-" "1.0" "1.2"\r
+    _\r
+      @move"http://www.simantics.org/Structural-" "1.1" "1.2"\r
+    _\r
+      @move"http://www.simantics.org/Diagram-" "2.1" "2.2"\r
+    _\r
+      @move"http://www.simantics.org/Simulation-" "1.0" "1.1"\r
+    _\r
+      @move"http://www.simantics.org/Diagram-" "1.0" "1.1"\r
+    _\r
+      @move"http://www.simantics.org/Modeling-" "1.1" "1.2"\r
+    _\r
+      @move"http://www.simantics.org/Project-" "1.1" "1.2"\r
+    _\r
+      @move"http://www.simantics.org/Spreadsheet-" "1.1" "1.2"\r
+    _\r
+      @move"http://www.simantics.org/Viewpoint-" "1.1" "1.2"\r
+    _\r
+      @move"http://www.simantics.org/Image2-" "1.1" "1.2"\r
+    _\r
+      @move"http://www.simantics.org/Color-" "1.0" "1.1"\r
+    _\r
+      @move"http://www.simantics.org/Action-" "1.0" "1.1"\r
+    _\r
+      @move"http://www.simantics.org/Silk-" "1.0" "1.1"\r
+    _\r
+      @move"http://www.simantics.org/Issue-" "1.1" "1.2"\r
+    _\r
+      @move"http://www.simantics.org/User-" "1.0" "1.1"\r
+    _\r
+      @move"http://www.simantics.org/Documentation-" "1.0" "1.1"\r
+    _\r
+      @move"http://www.simantics.org/Document-" "1.1" "1.2"\r
+    _\r
+      @move"http://www.simantics.org/SelectionView-" "1.1" "1.2"\r
+      \r
+MIGRATION.Spreadsheets : L0.NamespaceMigrationStep\r
+  @L0.list\r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "HasContent" "Cell/content"\r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "FitRows" "Dimensions/fitRows"\r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "FitColumns" "Dimensions/fitColumns"\r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "ColumnCount" "Dimensions/columnCount"\r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "ColumnLabels" "Headers/columnLabels"\r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "RowCount" "Dimensions/rowCount"\r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "ColumnWidths" "Headers/columnWidths"      \r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "ContentOf" "Cell/content/Inverse"\r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "FitRowsOf" "Dimensions/fitRows/Inverse"\r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "FitColumnsOf" "Dimensions/fitColumns/Inverse"\r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "ColumnCountOf" "Dimensions/columnCount/Inverse"\r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "ColumnLabelsOf" "Headers/columnLabels/Inverse"\r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "RowCountOf" "Dimensions/rowCount/Inverse"\r
+    _\r
+      @move "http://www.simantics.org/Spreadsheet-1.2/" "ColumnWidthsOf" "Headers/columnWidths/Inverse"        \r
+\r
+MIGRATION.SysdynChanges : L0.NamespaceMigrationStep\r
+  @L0.list\r
+    _\r
+      @move "http://www.simantics.org/Sysdyn-1.1/" "HasTail" "Variable/HasTail"\r
+    _\r
+      @move "http://www.simantics.org/Sysdyn-1.1/" "HasHead" "Variable/HasHead"\r
+    _\r
+      @move "http://www.simantics.org/Sysdyn-1.1/" "SharedModuleOntolofgy" "SharedModuleOntology"\r
index 4c7431052ae573e60b1a6344cdc3b358760e8c9d..9790d620bdd2c718b7b151113ef07ebbe3e3bf93 100644 (file)
@@ -208,9 +208,6 @@ SYSDYN.SysdynModuleLibrary <T L0.Library
 //#####################################################################
 // Connections and Relations
 //#####################################################################
-SYSDYN.HasHead <R STR.IsConnectedTo
-SYSDYN.HasTail <R STR.IsConnectedTo
-
 SYSDYN.Dependency <T STR.Connection
     >-- SYSDYN.Dependency.angle --> L0.Double <R L0.HasProperty : L0.FunctionalRelation
     >-- SYSDYN.Dependency.refersTo --> SYSDYN.Variable <R L0.IsRelatedTo : L0.FunctionalRelation
index 1b13475afee7920f26994d31e928c1663a9cd0ad..35ae91005f7d5da5eeb97ff73190213dc5ac5a6a 100644 (file)
@@ -136,18 +136,14 @@ public class SysdynResource {
     public final Resource GameExperiment_stepLength_Inverse;\r
     public final Resource HasEquationOrEmpty;\r
     public final Resource HasEquationOrEmpty_Inverse;\r
-    public final Resource HasHead;\r
     public final Resource HasHeadTerminal;\r
-    public final Resource HasHead_Inverse;\r
     public final Resource HasRangeEnd;\r
     public final Resource HasRangeEnd_Inverse;\r
     public final Resource HasRangeStart;\r
     public final Resource HasRangeStart_Inverse;\r
     public final Resource HasRangeStep;\r
     public final Resource HasRangeStep_Inverse;\r
-    public final Resource HasTail;\r
     public final Resource HasTailTerminal;\r
-    public final Resource HasTail_Inverse;\r
     public final Resource HasUnit;\r
     public final Resource HasUnit_Inverse;\r
     public final Resource HistoryDataset;\r
@@ -397,6 +393,10 @@ public class SysdynResource {
     public final Resource WithLookupExpression_maxY;\r
     public final Resource WithLookupExpression_minX;\r
     public final Resource WithLookupExpression_minY;\r
+    public final Resource from1$6to1$7;\r
+    public final Resource from1$6to1$7_Ontologies;\r
+    public final Resource from1$6to1$7_Spreadsheets;\r
+    public final Resource from1$6to1$7_SysdynChanges;\r
         \r
     public static class URIs {\r
         public static final String AdditionalSymbols = "http://www.simantics.org/Sysdyn-1.1/AdditionalSymbols";\r
@@ -526,18 +526,14 @@ public class SysdynResource {
         public static final String GameExperiment_stepLength_Inverse = "http://www.simantics.org/Sysdyn-1.1/GameExperiment/stepLength/Inverse";\r
         public static final String HasEquationOrEmpty = "http://www.simantics.org/Sysdyn-1.1/HasEquationOrEmpty";\r
         public static final String HasEquationOrEmpty_Inverse = "http://www.simantics.org/Sysdyn-1.1/HasEquationOrEmpty/Inverse";\r
-        public static final String HasHead = "http://www.simantics.org/Sysdyn-1.1/HasHead";\r
         public static final String HasHeadTerminal = "http://www.simantics.org/Sysdyn-1.1/HasHeadTerminal";\r
-        public static final String HasHead_Inverse = "http://www.simantics.org/Sysdyn-1.1/HasHead/Inverse";\r
         public static final String HasRangeEnd = "http://www.simantics.org/Sysdyn-1.1/HasRangeEnd";\r
         public static final String HasRangeEnd_Inverse = "http://www.simantics.org/Sysdyn-1.1/HasRangeEnd/Inverse";\r
         public static final String HasRangeStart = "http://www.simantics.org/Sysdyn-1.1/HasRangeStart";\r
         public static final String HasRangeStart_Inverse = "http://www.simantics.org/Sysdyn-1.1/HasRangeStart/Inverse";\r
         public static final String HasRangeStep = "http://www.simantics.org/Sysdyn-1.1/HasRangeStep";\r
         public static final String HasRangeStep_Inverse = "http://www.simantics.org/Sysdyn-1.1/HasRangeStep/Inverse";\r
-        public static final String HasTail = "http://www.simantics.org/Sysdyn-1.1/HasTail";\r
         public static final String HasTailTerminal = "http://www.simantics.org/Sysdyn-1.1/HasTailTerminal";\r
-        public static final String HasTail_Inverse = "http://www.simantics.org/Sysdyn-1.1/HasTail/Inverse";\r
         public static final String HasUnit = "http://www.simantics.org/Sysdyn-1.1/HasUnit";\r
         public static final String HasUnit_Inverse = "http://www.simantics.org/Sysdyn-1.1/HasUnit/Inverse";\r
         public static final String HistoryDataset = "http://www.simantics.org/Sysdyn-1.1/HistoryDataset";\r
@@ -787,6 +783,10 @@ public class SysdynResource {
         public static final String WithLookupExpression_maxY = "http://www.simantics.org/Sysdyn-1.1/WithLookupExpression/maxY";\r
         public static final String WithLookupExpression_minX = "http://www.simantics.org/Sysdyn-1.1/WithLookupExpression/minX";\r
         public static final String WithLookupExpression_minY = "http://www.simantics.org/Sysdyn-1.1/WithLookupExpression/minY";\r
+        public static final String from1$6to1$7 = "http://www.simantics.org/Sysdyn-1.1/from1.6to1.7";\r
+        public static final String from1$6to1$7_Ontologies = "http://www.simantics.org/Sysdyn-1.1/from1.6to1.7/Ontologies";\r
+        public static final String from1$6to1$7_Spreadsheets = "http://www.simantics.org/Sysdyn-1.1/from1.6to1.7/Spreadsheets";\r
+        public static final String from1$6to1$7_SysdynChanges = "http://www.simantics.org/Sysdyn-1.1/from1.6to1.7/SysdynChanges";\r
     }\r
     \r
     public static Resource getResourceOrNull(ReadGraph graph, String uri) {\r
@@ -926,18 +926,14 @@ public class SysdynResource {
         GameExperiment_stepLength_Inverse = getResourceOrNull(graph, URIs.GameExperiment_stepLength_Inverse);\r
         HasEquationOrEmpty = getResourceOrNull(graph, URIs.HasEquationOrEmpty);\r
         HasEquationOrEmpty_Inverse = getResourceOrNull(graph, URIs.HasEquationOrEmpty_Inverse);\r
-        HasHead = getResourceOrNull(graph, URIs.HasHead);\r
         HasHeadTerminal = getResourceOrNull(graph, URIs.HasHeadTerminal);\r
-        HasHead_Inverse = getResourceOrNull(graph, URIs.HasHead_Inverse);\r
         HasRangeEnd = getResourceOrNull(graph, URIs.HasRangeEnd);\r
         HasRangeEnd_Inverse = getResourceOrNull(graph, URIs.HasRangeEnd_Inverse);\r
         HasRangeStart = getResourceOrNull(graph, URIs.HasRangeStart);\r
         HasRangeStart_Inverse = getResourceOrNull(graph, URIs.HasRangeStart_Inverse);\r
         HasRangeStep = getResourceOrNull(graph, URIs.HasRangeStep);\r
         HasRangeStep_Inverse = getResourceOrNull(graph, URIs.HasRangeStep_Inverse);\r
-        HasTail = getResourceOrNull(graph, URIs.HasTail);\r
         HasTailTerminal = getResourceOrNull(graph, URIs.HasTailTerminal);\r
-        HasTail_Inverse = getResourceOrNull(graph, URIs.HasTail_Inverse);\r
         HasUnit = getResourceOrNull(graph, URIs.HasUnit);\r
         HasUnit_Inverse = getResourceOrNull(graph, URIs.HasUnit_Inverse);\r
         HistoryDataset = getResourceOrNull(graph, URIs.HistoryDataset);\r
@@ -1187,6 +1183,10 @@ public class SysdynResource {
         WithLookupExpression_maxY = getResourceOrNull(graph, URIs.WithLookupExpression_maxY);\r
         WithLookupExpression_minX = getResourceOrNull(graph, URIs.WithLookupExpression_minX);\r
         WithLookupExpression_minY = getResourceOrNull(graph, URIs.WithLookupExpression_minY);\r
+        from1$6to1$7 = getResourceOrNull(graph, URIs.from1$6to1$7);\r
+        from1$6to1$7_Ontologies = getResourceOrNull(graph, URIs.from1$6to1$7_Ontologies);\r
+        from1$6to1$7_Spreadsheets = getResourceOrNull(graph, URIs.from1$6to1$7_Spreadsheets);\r
+        from1$6to1$7_SysdynChanges = getResourceOrNull(graph, URIs.from1$6to1$7_SysdynChanges);\r
     }\r
     \r
     public static SysdynResource getInstance(ReadGraph graph) {\r
index e4769c299af2bb964f5feea2599b705d2f881d2e..72a0bd005791758fa3d756757155760a4e96cb13 100644 (file)
@@ -44,19 +44,21 @@ public class SysdynVariableRemover implements Remover {
             Layer0 L0 = Layer0.getInstance(graph);\r
             ProjectResource PROJ = ProjectResource.getInstance(graph);\r
             Resource configuration = graph.getPossibleObject(resource, L0.PartOf);\r
-            Resource moduleType =  graph.getPossibleObject(configuration, L0.PartOf);\r
-            if(graph.isInheritedFrom(moduleType, SR.Module)) {\r
-                Resource project = moduleType;\r
-                do {\r
-                    project = graph.getPossibleObject(project, L0.PartOf);\r
-                } while (project != null && !graph.isInstanceOf(project, PROJ.Project));\r
+            if(configuration != null) {\r
+                Resource moduleType =  graph.getPossibleObject(configuration, L0.PartOf);\r
+                if(graph.isInheritedFrom(moduleType, SR.Module)) {\r
+                    Resource project = moduleType;\r
+                    do {\r
+                        project = graph.getPossibleObject(project, L0.PartOf);\r
+                    } while (project != null && !graph.isInstanceOf(project, PROJ.Project));\r
 \r
-                if(project != null) {\r
-                    for(Resource model : graph.syncRequest(new ObjectsWithType(project, L0.ConsistsOf, SR.SysdynModel))) {\r
-                        removeOutputReferencesFromModel(graph, model, resource, moduleType);\r
+                    if(project != null) {\r
+                        for(Resource model : graph.syncRequest(new ObjectsWithType(project, L0.ConsistsOf, SR.SysdynModel))) {\r
+                            removeOutputReferencesFromModel(graph, model, resource, moduleType);\r
+                        }\r
                     }\r
-                }\r
 \r
+                }\r
             }\r
         }\r
         EntityRemover.remove(graph, resource);\r
index 9057470fd1515290bfa708317bc1b616df639e9e..99cbbeaa37b9e1da229a64d96497514e91046709 100644 (file)
@@ -23,13 +23,17 @@ import java.util.Set;
 import org.eclipse.core.commands.AbstractHandler;\r
 import org.eclipse.core.commands.ExecutionEvent;\r
 import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
 import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.core.runtime.Status;\r
 import org.eclipse.core.runtime.SubMonitor;\r
+import org.eclipse.core.runtime.jobs.Job;\r
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.widgets.FileDialog;\r
-import org.eclipse.swt.widgets.MessageBox;\r
 import org.eclipse.swt.widgets.Shell;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.DatabaseJob;\r
 import org.simantics.Simantics;\r
 import org.simantics.databoard.binding.Binding;\r
 import org.simantics.databoard.container.DataContainer;\r
@@ -42,11 +46,15 @@ import org.simantics.db.Resource;
 import org.simantics.db.Session;\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.request.WriteResultRequest;\r
 import org.simantics.db.common.utils.NameUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
+import org.simantics.db.layer0.migration.MigrationState;\r
+import org.simantics.db.layer0.migration.MigrationStateKeys;\r
+import org.simantics.db.layer0.migration.MigrationStep;\r
+import org.simantics.db.layer0.migration.MigrationUtils;\r
 import org.simantics.db.layer0.util.RemoverUtil;\r
 import org.simantics.db.request.Read;\r
 import org.simantics.graph.representation.TransferableGraph1;\r
@@ -71,180 +79,233 @@ import org.simantics.ui.SimanticsUI;
  */\r
 public class ImportModelHandler extends AbstractHandler {\r
 \r
-       public static String IMPORTMODELTPATH = "IMPORT_MODEL_PATH";\r
-\r
-       @Override\r
-       public Object execute(ExecutionEvent event) throws ExecutionException {\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
-               String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMODELTPATH);\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(IMPORTMODELTPATH, (new File(selected)).getParent());\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
-                       @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
-                               } catch (Exception e) {\r
-                                       e.printStackTrace();\r
-                                       return null;\r
-                               }\r
-                       }\r
-               });\r
-               \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
-               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
-                                               } else {\r
-                                                       fixHasTailAndHasHead(graph, ModelRoot);\r
-                                                       addDependencies(graph, ModelRoot);\r
-                                                       removeIssueSourcesFromModules(graph, ModelRoot);\r
-                                                       addEnumerationIssueSource(graph, ModelRoot);\r
-                                                       \r
-                                                       activateModel(graph, ModelRoot);\r
-                                               }\r
-                                       }\r
-\r
-                               });\r
-                               \r
-                               \r
-                               Collection<Resource> confs = SimanticsUI.getSession().syncRequest(new Read<Collection<Resource>>() {\r
+    public static String IMPORTMODELTPATH = "IMPORT_MODEL_PATH";\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\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
+        String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMODELTPATH);\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
+        final String selected = fd.open();\r
+        if(selected == null) return null;\r
+\r
+        Job job = new DatabaseJob("Import model") {\r
+\r
+            @Override\r
+            protected IStatus run(IProgressMonitor monitor) {\r
+                IStatus status = importFile(selected, shell, monitor);\r
+                return status;\r
+            }\r
+        };\r
+        job.setUser(true);\r
+        job.schedule();\r
+\r
+        return null;\r
+\r
+    }\r
+\r
+    private IStatus importFile(String path, final Shell shell, final IProgressMonitor monitor) {\r
+        final Resource project = SimanticsUI.getProject().get();\r
+        if(project == null) return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model: project not found", null);\r
+\r
+        monitor.beginTask("Import model", 7);\r
+        \r
+        Activator.getDefault().getPreferenceStore().setValue(IMPORTMODELTPATH, (new File(path)).getParent());\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
+            @Override\r
+            public Object process(DataContainer container) throws DataFormatException {\r
+                DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(project);\r
+\r
+                try {\r
+                    monitor.subTask("Migrate");\r
+                    Session session = Simantics.getSession();\r
+                    TransferableGraph1 tg = (TransferableGraph1)container.content.getValue();\r
+\r
+                    MigrationStep step = MigrationUtils.getStep(session, SysdynResource.URIs.from1$6to1$7);\r
+                    MigrationState state = MigrationUtils.newState();\r
+                    state.setProperty(MigrationStateKeys.CURRENT_TG, tg);\r
+                    step.applyTo(session, state);\r
+                    tg = state.getProperty(MigrationStateKeys.CURRENT_TG);\r
+\r
+                    monitor.worked(1);\r
+                    monitor.subTask("Import");\r
+                    DefaultPasteHandler.defaultExecute(tg, project, ia);\r
+                    monitor.worked(1);\r
+                    return ia.getRoot();\r
+\r
+                } catch (DatabaseException e) {\r
+                    e.printStackTrace();\r
+                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed", e);\r
+                } catch (Exception e) {\r
+                    e.printStackTrace();\r
+                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed", e);\r
+                }\r
+            }\r
+        });\r
+\r
+        Object result = null;\r
+        try {\r
+            File file = new File(path);\r
+            if(!file.isFile()) {\r
+//                     displayErrorMessage(shell, "File " + path + " not found");\r
+                return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: File " + path + " not found", null);\r
+            } else {    \r
+                result = DataContainers.readFile(file, handlers);\r
+            }\r
+        } catch(DataFormatException e) {\r
+            e.printStackTrace();\r
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", e);                 \r
+        } catch(IOException e) {\r
+            e.printStackTrace();\r
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", e); \r
+        } catch(Exception e) {\r
+            if(e instanceof RuntimeException) {\r
+                return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", e); \r
+//                     throw (RuntimeException)e;\r
+            } else {\r
+                return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", e); \r
+//                     throw new RuntimeException(e);\r
+            }\r
+        }\r
+\r
+        if(result == null || !(result instanceof Resource)) {\r
+//                 displayErrorMessage(shell, "Model could not be imported.");\r
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", null);\r
+        } else {\r
+            try {\r
+                final Resource ModelRoot = (Resource) result;\r
+                IStatus status = SimanticsUI.getSession().syncRequest(new WriteResultRequest<IStatus>()  {\r
+\r
+                    @Override\r
+                    public IStatus 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
+                            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "The imported file is not of type: System Dynamics Model (" + ft +")", null);\r
+//                                     }\r
+//                                 });\r
+\r
+                        } else {\r
+                            monitor.subTask("Add required dependencies");\r
+                            addDependencies(graph, ModelRoot);\r
+                            monitor.worked(1);\r
+                            monitor.subTask("Remove unnecessary issue sources");\r
+                            removeIssueSourcesFromModules(graph, ModelRoot);\r
+                            monitor.worked(1);\r
+                            monitor.subTask("Add enumeration issue source");\r
+                            addEnumerationIssueSource(graph, ModelRoot);\r
+                            monitor.worked(1);\r
+                            monitor.subTask("Activate model");\r
+                            activateModel(graph, ModelRoot);\r
+                            monitor.worked(1);\r
+                        }\r
+                        return null;\r
+\r
+                    }\r
+\r
+                });\r
+\r
+                if(status != null)\r
+                    return status;\r
+\r
+                monitor.subTask("Validate model");\r
+                Collection<Resource> confs = SimanticsUI.getSession().syncRequest(new Read<Collection<Resource>>() {\r
 \r
                     @Override\r
                     public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {\r
                         return findAllConfigurations(graph, ModelRoot);\r
                     }\r
 \r
-                               });\r
-\r
-\r
-                               // Batch validate all issue sources after import\r
-                               try {\r
-                                   for(Resource conf : confs) {\r
-                                       /*\r
-                                        * This section is copied from BatchValidations.runAll. runAll did\r
-                                        * not find module types to validate.\r
-                                        */\r
-//                                     BatchValidations.runAll(null, mod);\r
-\r
-                                       final Session session = Simantics.getSession();\r
-                                       final Collection<BatchIssueSource> validations = session.sync( new AllBatchIssueSources(ModelRoot) );\r
-                                       SubMonitor progress = SubMonitor.convert(null, "Validate Model", 100);\r
-                                       Collection<Resource> contexts = Collections.singletonList(conf);\r
-                                       for(BatchIssueSource bis : validations) {\r
-                                           Map<Resource, Set<Issue>> is = BatchValidations.validate(progress.newChild(90, SubMonitor.SUPPRESS_NONE), bis, contexts);\r
-                                           BatchValidations.store(progress.newChild(10, SubMonitor.SUPPRESS_NONE), bis.getResource(), is);\r
-                                       }\r
-                                   }\r
-                               } catch(Throwable t) {\r
-                                   t.printStackTrace();\r
-                               \r
-\r
-\r
-                               \r
-                       } catch (DatabaseException e) {\r
-                               e.printStackTrace();\r
-                       }\r
-               }\r
-\r
-               return null;\r
-\r
-       }\r
-       \r
-       protected void removeIssueSourcesFromModules(WriteGraph graph, Resource modelRoot) throws DatabaseException {\r
+                });\r
+\r
+\r
+                // Batch validate all issue sources after import\r
+                try {\r
+                    for(Resource conf : confs) {\r
+                        /*\r
+                         * This section is copied from BatchValidations.runAll. runAll did\r
+                         * not find module types to validate.\r
+                         */\r
+//                           BatchValidations.runAll(null, mod);\r
+\r
+                        final Session session = Simantics.getSession();\r
+                        final Collection<BatchIssueSource> validations = session.sync( new AllBatchIssueSources(ModelRoot) );\r
+                        SubMonitor progress = SubMonitor.convert(monitor, "Validate Model", 100);\r
+                        Collection<Resource> contexts = Collections.singletonList(conf);\r
+                        for(BatchIssueSource bis : validations) {\r
+                            Map<Resource, Set<Issue>> is = BatchValidations.validate(progress.newChild(90, SubMonitor.SUPPRESS_NONE), bis, contexts);\r
+                            BatchValidations.store(progress.newChild(10, SubMonitor.SUPPRESS_NONE), bis.getResource(), is);\r
+                        }\r
+                    }\r
+                } catch(Throwable t) {\r
+                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", t);\r
+                } \r
+\r
+                monitor.worked(1);\r
+\r
+            } catch (DatabaseException e) {\r
+                e.printStackTrace();\r
+                return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", e);\r
+            }\r
+\r
+            return Status.OK_STATUS;\r
+        }\r
+    }\r
+\r
+    protected void removeIssueSourcesFromModules(WriteGraph graph, Resource modelRoot) throws DatabaseException {\r
         Layer0 L0 = Layer0.getInstance(graph);\r
         IssueResource ISSUE = IssueResource.getInstance(graph);\r
-        \r
+\r
         for(Resource m : findAllModelsAndModules(graph, modelRoot)) {\r
             if(m.equals(modelRoot))\r
                 continue;\r
-            \r
+\r
             // Module may contain issue sources due to legacy reasons. Remove these. \r
             for(Resource issueSource : graph.syncRequest(new ObjectsWithType(m, L0.ConsistsOf, ISSUE.IssueSource))) {\r
                 // Issues are created to virtual graphs, so they should not need to be removed. Just in case they are created, remove them first\r
                 for(Resource issue : graph.getObjects(issueSource, ISSUE.IssueSource_Manages)) {\r
                     RemoverUtil.remove(graph, issue);\r
                 }\r
-                \r
+\r
                 // finally remove the issue source\r
                 RemoverUtil.remove(graph, issueSource);\r
             }\r
         }\r
-           \r
+\r
     }\r
 \r
     protected void activateModel(WriteGraph graph, Resource modelRoot) throws DatabaseException {\r
@@ -259,31 +320,37 @@ public class ImportModelHandler extends AbstractHandler {
     }\r
 \r
     protected void addDependencies(WriteGraph graph, Resource modelRoot) throws DatabaseException {\r
-               Layer0 L0 = Layer0.getInstance(graph);\r
-\r
-               ArrayList<String> links = new ArrayList<String>();\r
-               for(Resource link : graph.getObjects(modelRoot, L0.IsLinkedTo))\r
-                       links.add(graph.getURI(link));\r
-               \r
-               \r
-               if(!links.contains("http://www.simantics.org/Documentation-1.0")) {\r
-                       Resource documentation = graph.getPossibleResource("http://www.simantics.org/Documentation-1.0");\r
-                       if(documentation != null)\r
-                               graph.claim(modelRoot, L0.IsLinkedTo, documentation);\r
-               }\r
-               \r
-               if(!links.contains("http://www.simantics.org/SelectionView-1.1")) {\r
-                       Resource selectionView = graph.getPossibleResource("http://www.simantics.org/SelectionView-1.1");\r
-                       if(selectionView != null)\r
-                               graph.claim(modelRoot, L0.IsLinkedTo, selectionView);\r
-               }\r
-       }\r
-\r
-       protected void addEnumerationIssueSource(WriteGraph graph, Resource modelRoot) throws DatabaseException {\r
-           Layer0 L0 = Layer0.getInstance(graph);\r
-           Layer0X L0X = Layer0X.getInstance(graph); \r
-           SysdynResource sr = SysdynResource.getInstance(graph);\r
-           Collection<Resource> enumerationIssueSources = graph.syncRequest(new ObjectsWithType(modelRoot, L0.ConsistsOf, sr.Validations_Enumerations_EnumerationIssueSource));\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+\r
+        ArrayList<String> links = new ArrayList<String>();\r
+        for(Resource link : graph.getObjects(modelRoot, L0.IsLinkedTo))\r
+            links.add(graph.getURI(link));\r
+\r
+\r
+        if(!links.contains("http://www.simantics.org/Documentation-1.1")) {\r
+            Resource documentation = graph.getPossibleResource("http://www.simantics.org/Documentation-1.1");\r
+            if(documentation != null)\r
+                graph.claim(modelRoot, L0.IsLinkedTo, documentation);\r
+        }\r
+\r
+        if(!links.contains("http://www.simantics.org/SelectionView-1.2")) {\r
+            Resource selectionView = graph.getPossibleResource("http://www.simantics.org/SelectionView-1.2");\r
+            if(selectionView != null)\r
+                graph.claim(modelRoot, L0.IsLinkedTo, selectionView);\r
+        }\r
+\r
+        if(!links.contains("http://www.simantics.org/DocumentWorkbench-1.0")) {\r
+            Resource documentWb = graph.getPossibleResource("http://www.simantics.org/DocumentWorkbench-1.0");\r
+            if(documentWb != null)\r
+                graph.claim(modelRoot, L0.IsLinkedTo, documentWb);\r
+        }\r
+    }\r
+\r
+    protected void addEnumerationIssueSource(WriteGraph graph, Resource modelRoot) throws DatabaseException {\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        Layer0X L0X = Layer0X.getInstance(graph); \r
+        SysdynResource sr = SysdynResource.getInstance(graph);\r
+        Collection<Resource> enumerationIssueSources = graph.syncRequest(new ObjectsWithType(modelRoot, L0.ConsistsOf, sr.Validations_Enumerations_EnumerationIssueSource));\r
         if(enumerationIssueSources.isEmpty()) {\r
             GraphUtils.create2(graph,\r
                     sr.Validations_Enumerations_EnumerationIssueSource,\r
@@ -292,64 +359,42 @@ public class ImportModelHandler extends AbstractHandler {
                     );  \r
         }      }\r
 \r
-       protected void fixHasTailAndHasHead(WriteGraph graph, Resource modelRoot) throws DatabaseException {\r
-               Layer0 L0 = Layer0.getInstance(graph);\r
-               SysdynResource sr = SysdynResource.getInstance(graph);\r
-               for(Resource configuration : findAllConfigurations(graph, modelRoot)) {\r
-                       Collection<Resource> connections = graph.syncRequest(new ObjectsWithType(configuration, L0.ConsistsOf, sr.Dependency));\r
-                       connections.addAll(graph.syncRequest(new ObjectsWithType(configuration, L0.ConsistsOf, sr.Flow)));\r
-\r
-                       for(Resource connection : connections) {\r
-                               Resource head = graph.getPossibleObject(connection, sr.HasHead);\r
-                               Resource tail = graph.getPossibleObject(connection, sr.HasTail);\r
-\r
-                               if(head != null && tail != null) {\r
-                                       graph.deny(connection, sr.HasHead, head);\r
-                                       graph.deny(connection, sr.HasTail, tail);\r
-\r
-                                       graph.claim(connection, sr.Variable_HasHead, head);\r
-                                       graph.claim(connection, sr.Variable_HasTail, tail);\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-       \r
-       private Collection<Resource> findAllConfigurations(ReadGraph graph, Resource modelRoot) throws DatabaseException {\r
-               SysdynResource sr = SysdynResource.getInstance(graph);\r
-               StructuralResource2 SR2 = StructuralResource2.getInstance(graph);\r
-               Collection<Resource> modulesAndModels = findAllModelsAndModules(graph, modelRoot);\r
-               Collection<Resource> configurations = new ArrayList<Resource>();\r
-\r
-               for(Resource r : modulesAndModels) {\r
-                       if(graph.isInheritedFrom(r, sr.Module)) {\r
-                               configurations.add(graph.getPossibleObject(r, SR2.IsDefinedBy));\r
-                       } else if(graph.isInstanceOf(r, sr.SysdynModel)) {\r
-                        configurations.add(graph.getPossibleObject(modelRoot, SimulationResource.getInstance(graph).HasConfiguration));\r
-                       }\r
-               }\r
-               \r
-               return configurations;\r
-       }\r
-       \r
-          private Collection<Resource> findAllModelsAndModules(ReadGraph graph, Resource modelRoot) throws DatabaseException {\r
-               Layer0 L0 = Layer0.getInstance(graph);\r
-               SysdynResource sr = SysdynResource.getInstance(graph);\r
-               Collection<Resource> modelsAndModules = new ArrayList<Resource>();\r
-               modelsAndModules.add(modelRoot);\r
-\r
-               for(Resource r : graph.getObjects(modelRoot, L0.ConsistsOf)) {\r
-                   if(graph.isInheritedFrom(r, sr.Module)) {\r
-                       modelsAndModules.add(r);\r
-                   }\r
-               }\r
-               \r
-               return modelsAndModules;\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
+    private Collection<Resource> findAllConfigurations(ReadGraph graph, Resource modelRoot) throws DatabaseException {\r
+        SysdynResource sr = SysdynResource.getInstance(graph);\r
+        StructuralResource2 SR2 = StructuralResource2.getInstance(graph);\r
+        Collection<Resource> modulesAndModels = findAllModelsAndModules(graph, modelRoot);\r
+        Collection<Resource> configurations = new ArrayList<Resource>();\r
+\r
+        for(Resource r : modulesAndModels) {\r
+            if(graph.isInheritedFrom(r, sr.Module)) {\r
+                configurations.add(graph.getPossibleObject(r, SR2.IsDefinedBy));\r
+            } else if(graph.isInstanceOf(r, sr.SysdynModel)) {\r
+                configurations.add(graph.getPossibleObject(modelRoot, SimulationResource.getInstance(graph).HasConfiguration));\r
+            }\r
+        }\r
+\r
+        return configurations;\r
+    }\r
+\r
+    private Collection<Resource> findAllModelsAndModules(ReadGraph graph, Resource modelRoot) throws DatabaseException {\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        SysdynResource sr = SysdynResource.getInstance(graph);\r
+        Collection<Resource> modelsAndModules = new ArrayList<Resource>();\r
+        modelsAndModules.add(modelRoot);\r
+\r
+        for(Resource r : graph.getObjects(modelRoot, L0.ConsistsOf)) {\r
+            if(graph.isInheritedFrom(r, sr.Module)) {\r
+                modelsAndModules.add(r);\r
+            }\r
+        }\r
+\r
+        return modelsAndModules;\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
index 393d5ae889083bc9e21b43fb3dcf5fa17624747c..d60a1854252bccbdfca255bcc40a17729174db26 100644 (file)
@@ -18,6 +18,7 @@ import org.eclipse.core.runtime.IProgressMonitor;
 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.DatabaseJob;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.common.request.WriteRequest;\r
 import org.simantics.db.exception.DatabaseException;\r
@@ -37,7 +38,7 @@ public class NewModelHandler extends AbstractHandler {
     public Object execute(ExecutionEvent event) throws ExecutionException {\r
         \r
         \r
-        Job job = new Job("Creating System Dynamics Model") {\r
+        Job job = new DatabaseJob("Creating System Dynamics Model") {\r
             @Override\r
             protected IStatus run(IProgressMonitor monitor) {\r
                 try {\r
@@ -54,7 +55,7 @@ public class NewModelHandler extends AbstractHandler {
                 }\r
             }\r
         };\r
-        job.setUser(true);\r
+//        job.setUser(true);\r
         job.schedule();\r
 \r
         return null;\r