From: Antti Villberg Date: Tue, 24 Oct 2017 09:43:27 +0000 (+0300) Subject: Support dependency embedding in generic model export/import X-Git-Tag: v1.31.0~98^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=f0f518eabb008b3438a309c080eef9545d604d55;p=simantics%2Fplatform.git Support dependency embedding in generic model export/import refs #7572 Change-Id: Ia839aaecccf1a100b2141f518ad787d3f8d19884 --- diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelDependenciesBean.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelDependenciesBean.java index b42e46581..f448340dd 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelDependenciesBean.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelDependenciesBean.java @@ -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 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 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 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 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; + } } diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ExportPlan.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ExportPlan.java index f8cd67a8a..e70fc426f 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ExportPlan.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ExportPlan.java @@ -39,6 +39,7 @@ public class ExportPlan { * true if the model should be overwritten without a warning. */ boolean overwrite; + boolean includeDependencies; boolean tgAndPgraph; ExportPlan(ISessionContext sessionContext, Deque recentLocations) { diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ImportPlan.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ImportPlan.java index 7627db397..5233936dd 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ImportPlan.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ImportPlan.java @@ -30,6 +30,8 @@ public class ImportPlan { // Output File importLocation; + + boolean includeDependencies; public ImportPlan(ISessionContext sessionContext, Deque recentLocations) { this.sessionContext = sessionContext; diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExportPage.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExportPage.java index 8dc11477e..2a9e6f2b4 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExportPage.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExportPage.java @@ -59,6 +59,7 @@ public class ModelExportPage extends WizardPage { List 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 + "."); diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExporter.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExporter.java index 68e01297b..7211c7903 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExporter.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExporter.java @@ -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); } }); diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportPage.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportPage.java index f2c8423eb..f3ee014b7 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportPage.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportPage.java @@ -60,6 +60,9 @@ public class ModelImportPage extends WizardPage { CCombo importLocation; List 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 { diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportWizard.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportWizard.java index 4b7039258..fd8aaff3c 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportWizard.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportWizard.java @@ -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 resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);