]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Support dependency embedding in generic model export/import 40/1140/3
authorAntti Villberg <antti.villberg@semantum.fi>
Tue, 24 Oct 2017 09:43:27 +0000 (12:43 +0300)
committerAntti Villberg <antti.villberg@semantum.fi>
Wed, 25 Oct 2017 09:45:45 +0000 (12:45 +0300)
refs #7572

Change-Id: Ia839aaecccf1a100b2141f518ad787d3f8d19884

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelDependenciesBean.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ExportPlan.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ImportPlan.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExportPage.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExporter.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportPage.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportWizard.java

index b42e46581b9fcc3fe895ed026ae78d8542314c94..f448340dd8847300ad3564560e7ab3b3fadaa744 100644 (file)
@@ -1,16 +1,32 @@
 package org.simantics.db.layer0.util;
 
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Set;
+
 import org.simantics.databoard.Bindings;
+import org.simantics.databoard.adapter.AdaptException;
 import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.mutable.Variant;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ObjectsWithType;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.CopyHandler;
+import org.simantics.db.layer0.migration.MigrationState;
+import org.simantics.db.layer0.migration.MigrationStateKeys;
+import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
+import org.simantics.graph.representation.TransferableGraph1;
+import org.simantics.layer0.Layer0;
 
 public class ModelDependenciesBean {
 
        public static final String EXTENSION_KEY = ModelDependenciesBean.class.getSimpleName();
-       
+
        public static final Binding BINDING = Bindings.getBindingUnchecked(ModelDependenciesBean.class);
-       
+
        public ModelDependency[] dependencies;
-       
+
        public ModelDependenciesBean() {
                dependencies = new ModelDependency[0];
        }
@@ -18,5 +34,48 @@ public class ModelDependenciesBean {
        public ModelDependenciesBean(ModelDependency[] dependencies) {
                this.dependencies = dependencies;
        }
+
+       private static void collectDependencies(ReadGraph graph, Resource resource, LinkedList<ModelDependency> modelDependencies) throws DatabaseException {
+
+               Layer0 L0 = Layer0.getInstance(graph);
+               libs: for(Resource library : graph.syncRequest(new ObjectsWithType(resource, L0.IsLinkedTo, L0.SharedOntology))) {
+                       String uri = graph.getPossibleURI(library);
+                       if(uri == null) continue;
+                       for(ModelDependency dep : modelDependencies)
+                               if(dep.uri.equals(uri)) continue libs;
+                       CopyHandler ch = graph.adapt(library, CopyHandler.class);
+                       SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
+                       ch.copyToClipboard(graph, clipboard);
+                       for (Set<Representation> object : clipboard.getContents()) {
+                               TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
+                               if(tg != null) {
+                                       modelDependencies.addFirst(new ModelDependency(uri, tg));
+                               }
+                       }
+                       collectDependencies(graph, library, modelDependencies);
+               }
+
+       }
+
+       public static ModelDependenciesBean create(ReadGraph graph, Resource resource) throws DatabaseException {
+
+               LinkedList<ModelDependency> dependencies = new LinkedList<>();
+               collectDependencies(graph, resource, dependencies);
+               return new ModelDependenciesBean(dependencies.toArray(new ModelDependency[dependencies.size()]));
+
+       }
+
+       public static ModelDependenciesBean fromMigrationState(MigrationState state) throws DatabaseException {
+               Map<String,Variant> extensions = state.getProperty(MigrationStateKeys.TG_EXTENSIONS);
+               final Variant variant = extensions.get(ModelDependenciesBean.EXTENSION_KEY);
+               if (variant != null) {
+                       try {
+                               return (ModelDependenciesBean) variant.getValue(ModelDependenciesBean.BINDING);
+                       } catch (AdaptException e) {
+                               e.printStackTrace();
+                       }
+               }
+               return null;
+       }       
        
 }
index f8cd67a8a415b3bbc4bec8bb68b50204109aefbf..e70fc426f3c6b6f531ba73e0ebc2940f5ecf691b 100644 (file)
@@ -39,6 +39,7 @@ public class ExportPlan {
      * <code>true</code> if the model should be overwritten without a warning.
      */
     boolean         overwrite;
+    boolean         includeDependencies;
     boolean         tgAndPgraph;
 
     ExportPlan(ISessionContext sessionContext, Deque<String> recentLocations) {
index 7627db39756fd8b4732658f07aa6072a0115cdb8..5233936ddd559fedc9dbfc2abf4174c7a8c6a77a 100644 (file)
@@ -30,6 +30,8 @@ public class ImportPlan {
 
     // Output
     File            importLocation;
+    
+    boolean         includeDependencies;
 
     public ImportPlan(ISessionContext sessionContext, Deque<String> recentLocations) {
         this.sessionContext = sessionContext;
index 8dc11477e9db3474b83e3033506f8efbc37bb999..2a9e6f2b4f0ad04c0be6d956dd6325a11629c37b 100644 (file)
@@ -59,6 +59,7 @@ public class ModelExportPage extends WizardPage {
 
     List<LibraryInfo> models = Collections.emptyList();
     private Button      overwrite;
+    private Button      dependencies;
 
     protected ModelExportPage(ExportPlan model) {
         super("Export Model", "Define Export Location", null);
@@ -155,6 +156,17 @@ public class ModelExportPage extends WizardPage {
             }
         });
 
+        dependencies = new Button(container, SWT.CHECK);
+        dependencies.setText("&Export dependencies");
+        dependencies.setSelection(exportModel.includeDependencies);
+        GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(dependencies);
+        dependencies.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                validatePage();
+            }
+        });
+
         try {
             initializeData();
         } catch (DatabaseException e) {
@@ -253,6 +265,7 @@ public class ModelExportPage extends WizardPage {
         }
         exportModel.exportLocation = file;
         exportModel.overwrite = overwrite.getSelection();
+        exportModel.includeDependencies = dependencies.getSelection();
 
         setErrorMessage(null);
         setMessage("Export selected model to " + exportModel.exportLocation + ".");
index 68e01297b06ae224107d02210f55b471e1e09e98..7211c79035fe0ae91180231659f70ff60aa26150 100644 (file)
@@ -20,11 +20,14 @@ import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.simantics.Simantics;
 import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.binding.mutable.Variant;
 import org.simantics.databoard.serialization.SerializationException;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.common.request.ReadRequest;
 import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.ModelDependenciesBean;
+import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
 import org.simantics.modeling.ModelingUtils;
 import org.simantics.modeling.ModelingUtils.LibraryInfo;
 import org.simantics.utils.ui.dialogs.ShowMessage;
@@ -58,8 +61,7 @@ public class ModelExporter implements IRunnableWithProgress {
 
     void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
         try {
-            doExport(mon, exportModel.exportLocation, exportModel.model);
-
+            doExport(mon, exportModel.exportLocation, exportModel.model, exportModel.includeDependencies);
         } catch (DatabaseException e) {
             e.printStackTrace();
             Logger.defaultLogError(e);
@@ -70,12 +72,17 @@ public class ModelExporter implements IRunnableWithProgress {
         }
     }
     
-    public static void doExport(IProgressMonitor monitor, final File location, final LibraryInfo info) throws DatabaseException, IOException {
+    public static void doExport(IProgressMonitor monitor, final File location, final LibraryInfo info, boolean includeDependencies) throws DatabaseException, IOException {
        Simantics.sync(new ReadRequest() {
 
                        @Override
                        public void run(ReadGraph graph) throws DatabaseException {
-                       ModelingUtils.exportModel(graph, info.library.getResource(), location.getAbsolutePath(), "", 1);
+                               TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, info.library.getResource(), true, false);
+                               if(includeDependencies) {
+                                       ModelDependenciesBean bean = ModelDependenciesBean.create(graph, info.library.getResource());
+                                       conf.baseExtensions.put(ModelDependenciesBean.EXTENSION_KEY, new Variant(ModelDependenciesBean.BINDING, bean));
+                               }
+                               ModelingUtils.exportModel(graph, conf, location.getAbsolutePath(), "", 1);
                        }
                
        });
index f2c8423ebadcc2b9977f3f1b9e7baf2478357926..f3ee014b7a3bc87fc573723895a211242f79ee93 100644 (file)
@@ -60,6 +60,9 @@ public class ModelImportPage extends WizardPage {
     CCombo              importLocation;
 
     List<NamedResource> models = Collections.emptyList();
+
+    private Button      dependencies;
+
     Label               author;
     Label               status;
 
@@ -123,6 +126,17 @@ public class ModelImportPage extends WizardPage {
             });
         }
         
+        dependencies = new Button(container, SWT.CHECK);
+        dependencies.setText("&Import dependencies");
+        dependencies.setSelection(importModel.includeDependencies);
+        GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(dependencies);
+        dependencies.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                validatePage();
+            }
+        });
+        
         author = new Label(container, SWT.NONE);
         author.setText("");
         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(author);
@@ -170,6 +184,7 @@ public class ModelImportPage extends WizardPage {
             return;
         }
         importModel.importLocation = file;
+        importModel.includeDependencies = dependencies.getSelection();
 
         try {
 
index 4b7039258153da98ab29fc54bfc4108e9564b129..fd8aaff3cf882023d630130d01d4b6549e38f3c0 100644 (file)
@@ -40,11 +40,14 @@ import org.simantics.databoard.container.FormatHandler;
 import org.simantics.databoard.util.URIStringUtils;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
+import org.simantics.db.common.primitiverequest.PossibleResource;
 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
 import org.simantics.db.layer0.migration.MigratedImportResult;
 import org.simantics.db.layer0.migration.MigrationState;
 import org.simantics.db.layer0.migration.MigrationStateKeys;
 import org.simantics.db.layer0.migration.MigrationUtils;
+import org.simantics.db.layer0.util.ModelDependenciesBean;
+import org.simantics.db.layer0.util.ModelDependency;
 import org.simantics.db.management.ISessionContext;
 import org.simantics.graph.db.ImportResult;
 import org.simantics.graph.db.MissingDependencyException;
@@ -138,7 +141,7 @@ public class ModelImportWizard extends Wizard implements IImportWizard {
                     try {
                         Resource target = Simantics.getProjectResource();
                         importModel.sessionContext.getSession().markUndoPoint();
-                        result[0] = doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target);
+                        result[0] = doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target, importModel.includeDependencies);
                     } catch (Exception e) {
                         throw new InvocationTargetException(e);
                     } finally {
@@ -177,7 +180,7 @@ public class ModelImportWizard extends Wizard implements IImportWizard {
         return true;
     }
 
-    public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target)
+    public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target, boolean includeDependencies)
             throws Exception
     {
         SubMonitor mon = SubMonitor.convert(monitor);
@@ -200,6 +203,18 @@ public class ModelImportWizard extends Wizard implements IImportWizard {
                 state.setProperty(MigrationStateKeys.SESSION, session);
                 state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor);
                 
+                if(includeDependencies) {
+                       final ModelDependenciesBean libraryDependenciesBean = ModelDependenciesBean.fromMigrationState(state);
+                       if(libraryDependenciesBean != null) {
+                           for(ModelDependency dependency : libraryDependenciesBean.dependencies) {
+                               Resource existing = session.sync(new PossibleResource(dependency.uri));
+                               if(existing == null) {
+                                   MigrationUtils.importSharedOntology(session, dependency.tg, false);
+                               }
+                           }
+                       }
+                }
+                
                 MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target);
 
                 Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);