From 16cdcbfb20a094750a64f10c45b0447c764983c1 Mon Sep 17 00:00:00 2001 From: lempinen Date: Thu, 5 May 2011 11:04:45 +0000 Subject: [PATCH] Import and export of modules that have no dependencies to other modules git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20778 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/plugin.xml | 46 +++- .../contributions/FunctionLibraries.java | 4 +- .../sysdyn/ui/elements2/InputFactory.java | 4 +- .../ui/handlers/ExportModuleHandler.java | 244 ++++++++++++++++++ .../ui/handlers/ImportFunctionLibrary.java | 15 +- .../ui/handlers/ImportModuleHandler.java | 82 ++++++ .../ui/handlers/NewModuleNodeHandler.java | 6 +- .../sysdyn/representation/Module.java | 2 +- 8 files changed, 387 insertions(+), 16 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportModuleHandler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModuleHandler.java diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index 83a652e3..cb235536 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -455,8 +455,8 @@ @@ -483,8 +483,8 @@ @@ -644,6 +644,14 @@ id="org.simantics.sysdyn.ui.exportFunctionLibrary" name="Export Function Library"> + + + + @@ -852,6 +860,36 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/FunctionLibraries.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/FunctionLibraries.java index cf4b0ce8..754510d6 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/FunctionLibraries.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/FunctionLibraries.java @@ -44,8 +44,8 @@ public class FunctionLibraries extends ViewpointContributor { } // Find model function libraries - for(Resource modelLibrary : graph.syncRequest(new ObjectsWithType(functionsFolder.data, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary))) { - new FunctionLibraryNode(modelLibrary); + for(Resource functionLibrary : graph.syncRequest(new ObjectsWithType(functionsFolder.data, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary))) { + result.add(new FunctionLibraryNode(functionLibrary)); } Resource sysdyn = graph.getPossibleResource("http://www.simantics.org/Sysdyn-1.0"); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java index 96caea96..4b5aac48 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java @@ -146,7 +146,9 @@ public class InputFactory extends SysdynElementFactory { parent = ((ChildVariable)v).getParent(graph); if(parent != null && !(parent instanceof RunVariable) && !(parent instanceof ConfigurationVariable)) { String parentURI = parent.getURI(graph); - moduleName = (String) graph.getPossibleRelatedValue(graph.getResource(parentURI), l0.HasName); + Resource p = graph.getPossibleResource(parentURI); + if(p != null) + moduleName = (String) graph.getPossibleRelatedValue(p, l0.HasName); } break; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportModuleHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportModuleHandler.java new file mode 100644 index 00000000..17b5bd05 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportModuleHandler.java @@ -0,0 +1,244 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 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: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.handlers; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.runtime.Platform; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.MessageBox; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.databoard.Bindings; +import org.simantics.databoard.Files; +import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.primitiverequest.PossibleRelatedValue; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.TransferableGraphRequest2; +import org.simantics.db.request.Read; +import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.layer0.Layer0; +import org.simantics.modeling.ModelingResources; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.utils.ResourceAdaptionUtils; +import org.simantics.utils.datastructures.Pair; + +public class ExportModuleHandler extends AbstractHandler { + + /** + * Temporary exception. At this phase, the system will not support exporting modules + * that depend on other modules. + * + * @author TLTEEMU + * + */ + class ContainsDependenciesException extends DatabaseException { + private static final long serialVersionUID = -1533706136673146020L; + + private Collection dependencies; + + ContainsDependenciesException(Collection dependencies) { + this.dependencies = dependencies; + } + + public Collection getDependencies() { + return this.dependencies; + } + + } + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + + ISelection sel = HandlerUtil.getCurrentSelection(event); + final Resource modulesymbol = ResourceAdaptionUtils.toSingleResource(sel); + if(modulesymbol == null) return null; + + String name = null; + try { + name = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + ModelingResources mr = ModelingResources.getInstance(graph); + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + + Resource component = graph.getPossibleObject(modulesymbol, mr.SymbolToComponentType); + if (component == null || !graph.hasStatement(component, Layer0.getInstance(graph).PartOf)) + return null; + + Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy); + if (configuration == null) + return null; + + ArrayList dependencies = null; + for(Resource r : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Module))) { + if(dependencies == null) + dependencies = new ArrayList(); + String name = NameUtils.getSafeName(graph, r); + String instanceOf = NameUtils.getSafeName(graph, graph.getSingleObject(r, l0.InstanceOf)); + dependencies.add(name + " : " + instanceOf); + } + if(dependencies != null && !dependencies.isEmpty()) + throw new ContainsDependenciesException(dependencies); + + String name = graph.getPossibleRelatedValue(component, l0.HasName, Bindings.STRING); + return name; + + } + + }); + } catch (ContainsDependenciesException e1) { + Shell shell = HandlerUtil.getActiveShellChecked(event); + MessageBox mb = new MessageBox(shell, SWT.OK); + StringBuilder sb = new StringBuilder(); + sb.append("This version does not support exporting modules with other module instances.\n\n"); + sb.append("Dependencies:\n"); + for(String s : e1.getDependencies()) + sb.append(" " + s + "\n"); + mb.setMessage(sb.toString()); + mb.setText("Module contains dependencies."); + mb.open(); + return null; + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + if(name == null) return null; + + Shell shell = HandlerUtil.getActiveShellChecked(event); + FileDialog fd = new FileDialog(shell, SWT.SAVE); + fd.setText("Export.."); + fd.setFileName(name); + fd.setFilterPath(Platform.getLocation().toOSString()); + String[] filterExt = {"*.tg"}; + fd.setFilterExtensions(filterExt); + final String selected = fd.open(); + if(selected == null) return null; + + + SimanticsUI.getSession().asyncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + ModelingResources mr = ModelingResources.getInstance(graph); + + final Resource component = graph.getPossibleObject(modulesymbol, mr.SymbolToComponentType); + if (component == null || !graph.hasStatement(component, Layer0.getInstance(graph).PartOf)) + return; + String name = graph.syncRequest(new PossibleRelatedValue(component, l0.HasName, Bindings.STRING )); + final ArrayList> roots = new ArrayList>(); + roots.add(Pair.make(component, name)); + + graph.asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + + Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy); + if (!graph.hasStatement(configuration, l0.PartOf, component)&& + !graph.hasStatement(modulesymbol, l0.PartOf, component)) { + // Make sure that configuration and symbol are included. + // In old versions, they were attached to model, not to module. + Resource previousPartof = graph.getSingleObject(configuration, l0.PartOf); + + graph.deny(configuration, l0.PartOf); + graph.deny(modulesymbol, l0.PartOf); + graph.claim(configuration, l0.PartOf, l0.ConsistsOf, component); + graph.claim(modulesymbol, l0.PartOf, l0.ConsistsOf, component); + + export(graph, selected, roots, component); + + graph.deny(configuration, l0.PartOf); + graph.deny(modulesymbol, l0.PartOf); + graph.claim(configuration, l0.PartOf, l0.ConsistsOf, previousPartof); + graph.claim(modulesymbol, l0.PartOf, l0.ConsistsOf, previousPartof); + } else { + // Normal export + export(graph, selected, roots, component); + } + } + }); + + } + }); + + return null; + } + + /** + * Export module (without dependencies to other modules) and write it to file. + * Disable existing enumeration replacement for during export. + * + * @param graph WriteGraph + * @param path Path for the exported file + * @param roots + * @param component Module + */ + private void export(WriteGraph graph, String path, ArrayList> roots, Resource component) { + + // FIXME: Enumeration replacement handling like this is not suitable. + try { + Layer0 l0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + + Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy); + ArrayList> replacements = new ArrayList>(); + + for(Resource enumeration : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Enumeration))) { + if(graph.hasStatement(enumeration, sr.ReplacedEnumeration_Inverse)) { + for(Resource replacement : graph.getObjects(enumeration, sr.ReplacedEnumeration_Inverse)) { + replacements.add(new Pair(enumeration, replacement)); + } + } + } + + for(Pair replacement : replacements) + graph.deny(replacement.first, sr.ReplacedEnumeration_Inverse, replacement.second); + + TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, component)); + Files.createFile(new File(path), Bindings.getBindingUnchecked(TransferableGraph1.class), tg); + + for(Pair replacement : replacements) + graph.claim(replacement.first, sr.ReplacedEnumeration_Inverse, replacement.second); + + } catch (RuntimeBindingConstructionException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (DatabaseException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportFunctionLibrary.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportFunctionLibrary.java index bf16f6c9..3193d416 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportFunctionLibrary.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportFunctionLibrary.java @@ -43,19 +43,24 @@ import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.manager.FunctionUtils; +import org.simantics.sysdyn.ui.browser.nodes.FunctionsFolder; import org.simantics.ui.SimanticsUI; +import org.simantics.ui.utils.AdaptionUtils; import org.simantics.ui.utils.ResourceAdaptionUtils; public class ImportFunctionLibrary extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { - Resource project = SimanticsUI.getProject().get(); - if(project == null) return null; - ISelection sel = HandlerUtil.getCurrentSelection(event); - final Resource functionLibrary = ResourceAdaptionUtils.toSingleResource(sel); - if(functionLibrary == null) return null; + Resource r = ResourceAdaptionUtils.toSingleResource(sel); + if(r == null) { + FunctionsFolder mn = AdaptionUtils.adaptToSingle(sel, FunctionsFolder.class); + r = mn.data; + } + if(r == null) return null; + + final Resource functionLibrary = r; Shell shell = HandlerUtil.getActiveShellChecked(event); FileDialog fd = new FileDialog(shell, SWT.OPEN); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModuleHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModuleHandler.java new file mode 100644 index 00000000..c973385b --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModuleHandler.java @@ -0,0 +1,82 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 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: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.handlers; + +import java.io.File; +import java.io.IOException; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.runtime.Platform; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.browsing.ui.common.node.AbstractNode; +import org.simantics.databoard.Bindings; +import org.simantics.databoard.Files; +import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; +import org.simantics.db.Resource; +import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; +import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; +import org.simantics.graph.db.MissingDependencyException; +import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.ui.utils.AdaptionUtils; + +public class ImportModuleHandler extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + ISelection sel = HandlerUtil.getCurrentSelection(event); + + @SuppressWarnings("unchecked") + AbstractNode node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class); + if (node == null) + return null; + + final Resource model = node.data; + + Shell shell = HandlerUtil.getActiveShellChecked(event); + FileDialog fd = new FileDialog(shell, SWT.OPEN); + fd.setText("Import Module"); + fd.setFilterPath(Platform.getLocation().toOSString()); + String[] filterExt = {"*.tg"}; + fd.setFilterExtensions(filterExt); + String selected = fd.open(); + if(selected == null) return null; + + TransferableGraph1 tg = null; + try { + tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class)); + } catch (RuntimeBindingConstructionException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + if(tg == null) return null; + + + DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(model); + try { + DefaultPasteHandler.defaultExecute(tg, model, ia); + } catch (MissingDependencyException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModuleNodeHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModuleNodeHandler.java index 5f8a66fb..a6c4e6c0 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModuleNodeHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModuleNodeHandler.java @@ -68,7 +68,7 @@ public class NewModuleNodeHandler extends AbstractHandler { Resource configuration = GraphUtils.create2(g, sr.Configuration, l0.HasName, name + "Configuration", - l0.PartOf, model); + l0.PartOf, moduleType); g.claim(moduleType, sr2.IsDefinedBy , configuration); @@ -92,10 +92,10 @@ public class NewModuleNodeHandler extends AbstractHandler { Resource moduleSymbol = g.newResource(); g.claimLiteral(moduleSymbol, l0.HasName, name + " Symbol"); - g.claimLiteral(moduleSymbol, l0.HasLabel, name); + g.claimLiteral(moduleSymbol, l0.HasLabel, name + " Symbol"); g.claim(moduleSymbol, l0.Inherits, sr.ModuleSymbol); g.claim(moduleSymbol, mr.SymbolToComponentType, moduleType); - g.claim(moduleSymbol, l0.PartOf, model); + g.claim(moduleSymbol, l0.PartOf, moduleType); Resource terminal = g.newResource(); g.claim(terminal, l0.InstanceOf, sr.SysdynTerminal); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java index 02fa9e8d..d04bc240 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java @@ -52,7 +52,7 @@ public class Module implements IElement { /** * Returns the declaration of a module with possible redeclared enumerations * - * Module m(redeclare type E1 = enumeration(i1, i2, i3), redeclare type E2 = enumeration(i4, i5, i6)); + * Module m(redeclared.size = enumeration.size, redeclared.elements = enumeration.elements); * * @return Declaration of a module instance */ -- 2.47.1