From e6ef90c0db8ca20f622e43ffe0a0cf3fb859e356 Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Sat, 28 Oct 2017 02:26:31 +0300 Subject: [PATCH] Still minor cleanup for generic model import/export code refs #7572 Change-Id: I012071bcf94fd5e35485918c0ee6ca4096e47f1d --- .../db/layer0/util/ModelDependenciesBean.java | 25 ++-- .../sharedontology/wizard/ModelExporter.java | 28 +++-- .../wizard/ModelImportWizard.java | 99 ++-------------- .../sharedontology/wizard/ModelImporter.java | 108 ++++++++++++++++++ 4 files changed, 138 insertions(+), 122 deletions(-) create mode 100644 bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImporter.java 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 8b8e86b52..afeba2fb9 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,5 +1,6 @@ package org.simantics.db.layer0.util; +import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; @@ -46,7 +47,7 @@ public class ModelDependenciesBean { collectDependencies(graph, resource, order, new HashSet<>()); return order; } - + private static void collectDependencies(ReadGraph graph, Resource resource, LinkedList order, Set visited) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); for(Resource library : graph.syncRequest(new ObjectsWithType(resource, L0.IsLinkedTo, L0.SharedOntology))) { @@ -57,12 +58,12 @@ public class ModelDependenciesBean { order.addFirst(library); } } - + private static List collectModelDependencies(ReadGraph graph, Resource resource) throws DatabaseException { List order = collectDependencies(graph, resource); Collections.reverse(order); - List modelDependencies = new LinkedList<>(); + List modelDependencies = new ArrayList<>(order.size()); for (Resource library : order) { String uri = graph.getPossibleURI(library); @@ -78,27 +79,17 @@ public class ModelDependenciesBean { } } return modelDependencies; - } public static ModelDependenciesBean create(ReadGraph graph, Resource resource) throws DatabaseException { - List dependencies = collectModelDependencies(graph, resource); return new ModelDependenciesBean(dependencies.toArray(new ModelDependency[dependencies.size()])); - } - public static ModelDependenciesBean fromMigrationState(MigrationState state) throws DatabaseException { + public static ModelDependenciesBean fromMigrationState(MigrationState state) throws DatabaseException, AdaptException { 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; - } - + return variant != null ? (ModelDependenciesBean) variant.getValue(ModelDependenciesBean.BINDING) : null; + } + } 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 7211c7903..8d3b124fd 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 @@ -71,21 +71,19 @@ public class ModelExporter implements IRunnableWithProgress { mon.setWorkRemaining(0); } } - - 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 { - 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); - } - - }); + 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 { + 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/ModelImportWizard.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportWizard.java index fd8aaff3c..bb0d6f68a 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 @@ -11,18 +11,11 @@ *******************************************************************************/ package org.simantics.modeling.ui.sharedontology.wizard; -import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.util.Collection; import java.util.Deque; -import java.util.HashMap; -import java.util.Map; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.SubMonitor; import org.eclipse.core.runtime.preferences.InstanceScope; -import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.preference.IPersistentPreferenceStore; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.viewers.IStructuredSelection; @@ -33,25 +26,11 @@ import org.eclipse.ui.IImportWizard; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.preferences.ScopedPreferenceStore; import org.simantics.Simantics; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.container.DataContainer; -import org.simantics.databoard.container.DataContainers; -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; -import org.simantics.graph.representation.TransferableGraph1; import org.simantics.modeling.ui.Activator; import org.simantics.modeling.ui.utils.NoProjectPage; import org.simantics.project.IProject; @@ -135,18 +114,15 @@ public class ModelImportWizard extends Wizard implements IImportWizard { try { MigratedImportResult[] result = { null }; - getContainer().run(true, true, new IRunnableWithProgress() { - @Override - public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { - try { - Resource target = Simantics.getProjectResource(); - importModel.sessionContext.getSession().markUndoPoint(); - result[0] = doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target, importModel.includeDependencies); - } catch (Exception e) { - throw new InvocationTargetException(e); - } finally { - monitor.done(); - } + getContainer().run(true, true, monitor -> { + try { + Resource target = Simantics.getProjectResource(); + importModel.sessionContext.getSession().markUndoPoint(); + result[0] = ModelImporter.doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target, importModel.includeDependencies); + } catch (Exception e) { + throw new InvocationTargetException(e); + } finally { + monitor.done(); } }); @@ -180,61 +156,4 @@ public class ModelImportWizard extends Wizard implements IImportWizard { return true; } - public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target, boolean includeDependencies) - throws Exception - { - SubMonitor mon = SubMonitor.convert(monitor); - mon.beginTask("Loading model from disk", 1000); - - FormatHandler handler1 = new FormatHandler() { - @Override - public Binding getBinding() { - return TransferableGraph1.BINDING; - } - - @Override - public MigratedImportResult process(DataContainer container) throws Exception { - mon.worked(100); - mon.setTaskName("Importing model into database"); - - MigrationState state = MigrationUtils.newState(); - state.setProperty(MigrationStateKeys.UPDATE_DEPENDENCIES, false); - state.setProperty(MigrationStateKeys.MODEL_FILE, modelFile); - 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); - ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT); - return new MigratedImportResult(resultRoots, result); - - } - }; - - Map> handlers = new HashMap<>(); - handlers.put(":1", handler1); - handlers.put(Constants.MODEL_FORMAT_V1, handler1); - - MigratedImportResult result = DataContainers.readFile(modelFile, handlers); - - mon.setTaskName("Postprocessing"); - mon.subTask(""); - mon.newChild(50).done(); - - return result; - } - } diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImporter.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImporter.java new file mode 100644 index 000000000..6a8d6718d --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImporter.java @@ -0,0 +1,108 @@ +/******************************************************************************* + * Copyright (c) 2017 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.ui.sharedontology.wizard; + +import java.io.File; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.SubMonitor; +import org.simantics.databoard.adapter.AdaptException; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.container.DataContainer; +import org.simantics.databoard.container.DataContainers; +import org.simantics.databoard.container.FormatHandler; +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.graph.db.ImportResult; +import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.modeling.ui.Activator; + +/** + * @author Tuukka Lehtonen + * @since 1.31.0 + */ +public class ModelImporter { + + public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target, boolean includeDependencies) + throws Exception + { + SubMonitor mon = SubMonitor.convert(monitor); + mon.beginTask("Loading model from disk", 1000); + + FormatHandler handler1 = new FormatHandler() { + @Override + public Binding getBinding() { + return TransferableGraph1.BINDING; + } + + @Override + public MigratedImportResult process(DataContainer container) throws Exception { + mon.worked(100); + mon.setTaskName("Importing model into database"); + + MigrationState state = MigrationUtils.newState(); + state.setProperty(MigrationStateKeys.UPDATE_DEPENDENCIES, false); + state.setProperty(MigrationStateKeys.MODEL_FILE, modelFile); + state.setProperty(MigrationStateKeys.SESSION, session); + state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor); + + if (includeDependencies) { + try { + 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); + } + } + } + } catch (AdaptException e) { + Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not read model dependencies bean.", e)); + } + } + + MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target); + + Collection resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES); + ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT); + return new MigratedImportResult(resultRoots, result); + } + }; + + Map> handlers = new HashMap<>(); + handlers.put(":1", handler1); + handlers.put(Constants.MODEL_FORMAT_V1, handler1); + + MigratedImportResult result = DataContainers.readFile(modelFile, handlers); + + mon.setTaskName("Postprocessing"); + mon.subTask(""); + mon.newChild(50).done(); + + return result; + } + +} -- 2.47.0