--- /dev/null
+package org.simantics.sysdyn.ui.actions;\r
+\r
+import java.util.Map;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.ObjectsWithSupertype;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.Instances;\r
+import org.simantics.db.layer0.adapter.Remover;\r
+import org.simantics.db.layer0.adapter.impl.EntityRemover;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.project.ontology.ProjectResource;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.sysdyn.SysdynResource;\r
+\r
+/**\r
+ * Remover for sysdyn variables. The main purpose is to take care that input and output references\r
+ * are removed \r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class SysdynVariableRemover implements Remover {\r
+ \r
+ private Resource resource;\r
+ \r
+ public SysdynVariableRemover(Resource resource) {\r
+ this.resource = resource;\r
+ }\r
+\r
+ @Override\r
+ public String canRemove(ReadGraph graph, Map<Object, Object> aux) throws DatabaseException {\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public void remove(WriteGraph graph) throws DatabaseException {\r
+ SysdynResource SR = SysdynResource.getInstance(graph);\r
+ if(graph.hasStatement(resource, SR.IsOutput) || graph.isInstanceOf(resource, SR.Input)) {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ ProjectResource PROJ = ProjectResource.getInstance(graph);\r
+ Resource configuration = graph.getPossibleObject(resource, L0.PartOf);\r
+ Resource moduleType = graph.getPossibleObject(configuration, L0.PartOf);\r
+ if(graph.isInheritedFrom(moduleType, SR.Module)) {\r
+ Resource project = moduleType;\r
+ do {\r
+ project = graph.getPossibleObject(project, L0.PartOf);\r
+ } while (project != null && !graph.isInstanceOf(project, PROJ.Project));\r
+\r
+ if(project != null) {\r
+ for(Resource model : graph.syncRequest(new ObjectsWithType(project, L0.ConsistsOf, SR.SysdynModel))) {\r
+ removeOutputReferencesFromModel(graph, model, resource, moduleType);\r
+ }\r
+ }\r
+\r
+ }\r
+ }\r
+ EntityRemover.remove(graph, resource);\r
+ }\r
+\r
+ private void removeOutputReferencesFromModel(WriteGraph graph, Resource model, Resource reference, Resource moduleType) throws DatabaseException {\r
+ SysdynResource SR = SysdynResource.getInstance(graph);\r
+ StructuralResource2 SR2 = StructuralResource2.getInstance(graph);\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ \r
+ Instances moduleInstanceFinder = graph.adapt(moduleType, Instances.class);\r
+ \r
+ denyReferences(graph, moduleInstanceFinder, graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration), reference);\r
+ \r
+ for(Resource type : graph.syncRequest(new ObjectsWithSupertype(model, L0.ConsistsOf, SR.Module))) {\r
+ Resource configuration = graph.getPossibleObject(type, SR2.IsDefinedBy);\r
+ denyReferences(graph, moduleInstanceFinder, configuration, reference);\r
+\r
+ }\r
+ \r
+ }\r
+ \r
+ private void denyReferences(WriteGraph graph, Instances moduleInstanceFinder, Resource indexRoot, Resource reference) throws DatabaseException {\r
+ StructuralResource2 SR2 = StructuralResource2.getInstance(graph);\r
+ SysdynResource SR = SysdynResource.getInstance(graph);\r
+\r
+ for(Resource module : moduleInstanceFinder.find(graph, indexRoot)) {\r
+ for(Statement stm : graph.getStatements(module, SR2.IsConnectedTo)) {\r
+ Resource connection = stm.getObject();\r
+ Resource ref = graph.getPossibleObject(connection, SR.Dependency_refersTo);\r
+ if(ref != null && ref.equals(reference)) {\r
+ graph.deny(connection, SR.Dependency_refersTo);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+}\r
package org.simantics.sysdyn.ui.handlers.exports;\r
\r
import java.io.File;\r
-import java.io.IOException;\r
-import java.util.ArrayList;\r
\r
import org.eclipse.core.commands.AbstractHandler;\r
import org.eclipse.core.commands.ExecutionEvent;\r
import org.eclipse.swt.widgets.Shell;\r
import org.eclipse.ui.handlers.HandlerUtil;\r
import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
-import org.simantics.databoard.binding.mutable.Variant;\r
-import org.simantics.databoard.container.DataContainer;\r
-import org.simantics.databoard.container.DataContainers;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
import org.simantics.db.common.request.ReadRequest;\r
import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.util.TransferableGraphRequest2;\r
+import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;\r
+import org.simantics.db.layer0.util.TransferableGraphConfiguration2;\r
import org.simantics.db.request.Read;\r
-import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.graph.db.TransferableGraphSource;\r
+import org.simantics.graph.db.TransferableGraphs;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.ui.Activator;\r
import org.simantics.sysdyn.ui.handlers.imports.ImportModelHandler;\r
import org.simantics.ui.SimanticsUI;\r
import org.simantics.ui.utils.ResourceAdaptionUtils;\r
-import org.simantics.utils.datastructures.Pair;\r
\r
/**\r
* Exports a selected model\r
fd.setFilterExtensions(filterExt);\r
final String selected = fd.open();\r
if(selected == null) return null;\r
- \r
- // Save location to preference store\r
+\r
+ // Save location to preference store\r
Activator.getDefault().getPreferenceStore().setValue(ImportModelHandler.IMPORTMODELTPATH, (new File(selected)).getParent());\r
- \r
- // Asynchronously create the file using transferable graph\r
- SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
- \r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- \r
- Layer0 l0 = Layer0.getInstance(graph);\r
- String name = graph.syncRequest(new PossibleRelatedValue<String>(model, l0.HasName, Bindings.STRING ));\r
- ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();\r
- roots.add(Pair.make(model, name));\r
- TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, model));\r
\r
- try {\r
- DataContainers.writeFile(new File(selected), new DataContainer("sysdynModel", 1, new Variant(TransferableGraph1.BINDING, tg)));\r
- } catch (RuntimeBindingConstructionException e) {\r
- e.printStackTrace();\r
- } catch (IOException e) {\r
- e.printStackTrace();\r
- }\r
+ // Asynchronously create the file using transferable graph\r
+ SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
\r
- \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, model);\r
+ TransferableGraphSource s = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));\r
+ try {\r
+ TransferableGraphs.writeTransferableGraph(graph, "sysdynModel", 1, s,new File(selected));\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
}\r
});\r
\r