From b319eabaffc25701904a8e24c6543b1691db0e50 Mon Sep 17 00:00:00 2001 From: lempinen Date: Wed, 21 Nov 2012 13:14:14 +0000 Subject: [PATCH] Added remover to remove references from dependency arrows when the referred variables are removed (fixes #3580) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26382 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/adapters.xml | 2 +- .../ui/actions/SysdynVariableRemover.java | 97 +++++++++++++++++++ .../handlers/exports/ExportModelHandler.java | 48 ++++----- 3 files changed, 115 insertions(+), 32 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/SysdynVariableRemover.java diff --git a/org.simantics.sysdyn.ui/adapters.xml b/org.simantics.sysdyn.ui/adapters.xml index e5efdca7..d2ce6b7a 100644 --- a/org.simantics.sysdyn.ui/adapters.xml +++ b/org.simantics.sysdyn.ui/adapters.xml @@ -10,5 +10,5 @@ VTT Technical Research Centre of Finland - initial API and implementation --> - none 0 fill 1 + none 0 fill 1 \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/SysdynVariableRemover.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/SysdynVariableRemover.java new file mode 100644 index 00000000..e4769c29 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/SysdynVariableRemover.java @@ -0,0 +1,97 @@ +package org.simantics.sysdyn.ui.actions; + +import java.util.Map; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Statement; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ObjectsWithSupertype; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.Instances; +import org.simantics.db.layer0.adapter.Remover; +import org.simantics.db.layer0.adapter.impl.EntityRemover; +import org.simantics.layer0.Layer0; +import org.simantics.project.ontology.ProjectResource; +import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.SysdynResource; + +/** + * Remover for sysdyn variables. The main purpose is to take care that input and output references + * are removed + * @author Teemu Lempinen + * + */ +public class SysdynVariableRemover implements Remover { + + private Resource resource; + + public SysdynVariableRemover(Resource resource) { + this.resource = resource; + } + + @Override + public String canRemove(ReadGraph graph, Map aux) throws DatabaseException { + return null; + } + + @Override + public void remove(WriteGraph graph) throws DatabaseException { + SysdynResource SR = SysdynResource.getInstance(graph); + if(graph.hasStatement(resource, SR.IsOutput) || graph.isInstanceOf(resource, SR.Input)) { + Layer0 L0 = Layer0.getInstance(graph); + ProjectResource PROJ = ProjectResource.getInstance(graph); + Resource configuration = graph.getPossibleObject(resource, L0.PartOf); + Resource moduleType = graph.getPossibleObject(configuration, L0.PartOf); + if(graph.isInheritedFrom(moduleType, SR.Module)) { + Resource project = moduleType; + do { + project = graph.getPossibleObject(project, L0.PartOf); + } while (project != null && !graph.isInstanceOf(project, PROJ.Project)); + + if(project != null) { + for(Resource model : graph.syncRequest(new ObjectsWithType(project, L0.ConsistsOf, SR.SysdynModel))) { + removeOutputReferencesFromModel(graph, model, resource, moduleType); + } + } + + } + } + EntityRemover.remove(graph, resource); + } + + private void removeOutputReferencesFromModel(WriteGraph graph, Resource model, Resource reference, Resource moduleType) throws DatabaseException { + SysdynResource SR = SysdynResource.getInstance(graph); + StructuralResource2 SR2 = StructuralResource2.getInstance(graph); + Layer0 L0 = Layer0.getInstance(graph); + + Instances moduleInstanceFinder = graph.adapt(moduleType, Instances.class); + + denyReferences(graph, moduleInstanceFinder, graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration), reference); + + for(Resource type : graph.syncRequest(new ObjectsWithSupertype(model, L0.ConsistsOf, SR.Module))) { + Resource configuration = graph.getPossibleObject(type, SR2.IsDefinedBy); + denyReferences(graph, moduleInstanceFinder, configuration, reference); + + } + + } + + private void denyReferences(WriteGraph graph, Instances moduleInstanceFinder, Resource indexRoot, Resource reference) throws DatabaseException { + StructuralResource2 SR2 = StructuralResource2.getInstance(graph); + SysdynResource SR = SysdynResource.getInstance(graph); + + for(Resource module : moduleInstanceFinder.find(graph, indexRoot)) { + for(Statement stm : graph.getStatements(module, SR2.IsConnectedTo)) { + Resource connection = stm.getObject(); + Resource ref = graph.getPossibleObject(connection, SR.Dependency_refersTo); + if(ref != null && ref.equals(reference)) { + graph.deny(connection, SR.Dependency_refersTo); + } + } + } + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java index 7434b851..f67d8594 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java @@ -12,8 +12,6 @@ package org.simantics.sysdyn.ui.handlers.exports; import java.io.File; -import java.io.IOException; -import java.util.ArrayList; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; @@ -25,24 +23,21 @@ import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.databoard.Bindings; -import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; -import org.simantics.databoard.binding.mutable.Variant; -import org.simantics.databoard.container.DataContainer; -import org.simantics.databoard.container.DataContainers; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.primitiverequest.PossibleRelatedValue; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.util.TransferableGraphRequest2; +import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest; +import org.simantics.db.layer0.util.TransferableGraphConfiguration2; import org.simantics.db.request.Read; -import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.graph.db.TransferableGraphSource; +import org.simantics.graph.db.TransferableGraphs; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.ui.Activator; import org.simantics.sysdyn.ui.handlers.imports.ImportModelHandler; import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.ResourceAdaptionUtils; -import org.simantics.utils.datastructures.Pair; /** * Exports a selected model @@ -94,31 +89,22 @@ public class ExportModelHandler extends AbstractHandler { fd.setFilterExtensions(filterExt); final String selected = fd.open(); if(selected == null) return null; - - // Save location to preference store + + // Save location to preference store Activator.getDefault().getPreferenceStore().setValue(ImportModelHandler.IMPORTMODELTPATH, (new File(selected)).getParent()); - - // Asynchronously create the file using transferable graph - SimanticsUI.getSession().asyncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - - Layer0 l0 = Layer0.getInstance(graph); - String name = graph.syncRequest(new PossibleRelatedValue(model, l0.HasName, Bindings.STRING )); - ArrayList> roots = new ArrayList>(); - roots.add(Pair.make(model, name)); - TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, model)); - try { - DataContainers.writeFile(new File(selected), new DataContainer("sysdynModel", 1, new Variant(TransferableGraph1.BINDING, tg))); - } catch (RuntimeBindingConstructionException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } + // Asynchronously create the file using transferable graph + SimanticsUI.getSession().asyncRequest(new ReadRequest() { - + @Override + public void run(ReadGraph graph) throws DatabaseException { + TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, model); + TransferableGraphSource s = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf)); + try { + TransferableGraphs.writeTransferableGraph(graph, "sysdynModel", 1, s,new File(selected)); + } catch (Exception e) { + e.printStackTrace(); + } } }); -- 2.47.1