From 9508f064ec1afcdc1919eab4ff586091c9cb84d6 Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 23 Jul 2010 11:28:30 +0000 Subject: [PATCH] Creation of new modules git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16801 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/plugin.xml | 18 ++++- .../ui/browser/contributions/Model.java | 34 ++++---- .../browser/contributions/ModelChildren.java | 34 -------- .../browser/contributions/ModuleLabeler.java | 3 - .../contributions/ModuleTypeLabeler.java | 16 ++++ .../ui/browser/contributions/Modules.java | 4 +- .../ui/browser/contributions/Project.java | 40 +++++++++ .../ui/handlers/NewModuleNodeHandler.java | 81 ++++++++++++++++++- .../sysdyn/modelica/ModelicaWriter.java | 12 +-- .../sysdyn/representation/Module.java | 8 -- sysdyn_ontologies/sysdyn.graph | 14 ++-- 11 files changed, 180 insertions(+), 84 deletions(-) delete mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModelChildren.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleTypeLabeler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Project.java diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index 90babd61..4ae354c9 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -244,6 +244,16 @@ id="org.simantics.sysdyn.ui.browser.newModule" label="Module" style="push"> + + + + + + + + { +public class Model extends ViewpointContributor { @Override - public Collection getContribution(ReadGraph graph, Resource project) + public Collection getContribution(ReadGraph graph, ModelNode model) throws DatabaseException { - ArrayList result = new ArrayList(); - Builtins b = graph.getBuiltins(); - SysdynResource sr = SysdynResource.getInstance(graph); - for(Resource r : graph.syncRequest(new ObjectsWithType(project, b.ConsistsOf, sr.SysdynModel))) { - try { - result.add(graph.adapt(r, AbstractNode.class)); - } catch(DatabaseException e) { - e.printStackTrace(); - } - } - result.add(new ModulesNode(project)); + result.add(new ConfigurationNode( + graph.getSingleObject( + model.resource, + graph.getBuiltins().HasConfiguration)) + ); + result.add(new ExperimentsFolder(model.resource)); + result.add(new OperatingInterfacesFolder(model.resource)); + result.add(new ModulesNode(model.resource)); return result; - } @Override public String getViewpointId() { return "Standard"; } - } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModelChildren.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModelChildren.java deleted file mode 100644 index d5dc8636..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModelChildren.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.simantics.sysdyn.ui.browser.contributions; - -import java.util.ArrayList; -import java.util.Collection; -import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; -import org.simantics.db.ReadGraph; -import org.simantics.db.exception.DatabaseException; -import org.simantics.sysdyn.ui.browser.nodes.AbstractNode; -import org.simantics.sysdyn.ui.browser.nodes.ConfigurationNode; -import org.simantics.sysdyn.ui.browser.nodes.ExperimentsFolder; -import org.simantics.sysdyn.ui.browser.nodes.ModelNode; -import org.simantics.sysdyn.ui.browser.nodes.OperatingInterfacesFolder; - -public class ModelChildren extends ViewpointContributor { - - @Override - public Collection getContribution(ReadGraph graph, ModelNode model) - throws DatabaseException { - ArrayList result = new ArrayList(); - result.add(new ConfigurationNode( - graph.getSingleObject( - model.resource, - graph.getBuiltins().HasConfiguration)) - ); - result.add(new ExperimentsFolder(model.resource)); - result.add(new OperatingInterfacesFolder(model.resource)); - return result; - } - - @Override - public String getViewpointId() { - return "Standard"; - } -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleLabeler.java index fdc289c6..78e02ffe 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleLabeler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleLabeler.java @@ -25,9 +25,6 @@ public class ModuleLabeler extends LabelerContributor{ Builtins b = graph.getBuiltins(); Resource resource = module.resource; StringBuilder sb = new StringBuilder(); - for(Resource r : graph.getObjects(resource, b.HasLabel)) - sb.append(graph.getValue(r)); - sb.append(" : "); for(Resource r : graph.getObjects(resource, b.HasName)) sb.append(graph.getValue(r)); sb.append(" : "); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleTypeLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleTypeLabeler.java new file mode 100644 index 00000000..cefdd32b --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleTypeLabeler.java @@ -0,0 +1,16 @@ +package org.simantics.sysdyn.ui.browser.contributions; + +import org.simantics.browsing.ui.graph.contributor.labeler.LabelerContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.browser.nodes.ModuleTypeNode; + +public class ModuleTypeLabeler extends LabelerContributor{ + + @Override + public String getLabel(ReadGraph graph, ModuleTypeNode moduleType) throws DatabaseException { + String label = graph.getPossibleRelatedValue(moduleType.resource, graph.getBuiltins().HasLabel); + return label == null ? "Model (no name)" : label; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Modules.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Modules.java index 8b6111fc..5281330f 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Modules.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Modules.java @@ -17,13 +17,13 @@ import org.simantics.sysdyn.ui.browser.nodes.ModulesNode; public class Modules extends ViewpointContributor { @Override - public Collection getContribution(ReadGraph graph, ModulesNode project) + public Collection getContribution(ReadGraph graph, ModulesNode model) throws DatabaseException { ArrayList result = new ArrayList(); Builtins b = graph.getBuiltins(); StructuralResource2 st = StructuralResource2.getInstance(graph); - for(Resource r : graph.syncRequest(new ObjectsWithType(project.resource, b.IsRelatedTo, st.ComponentType))) { + for(Resource r : graph.syncRequest(new ObjectsWithType(model.resource, b.ConsistsOf, st.ComponentType))) { result.add(new ModuleTypeNode(r)); } return result; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Project.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Project.java new file mode 100644 index 00000000..83ef90d0 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Project.java @@ -0,0 +1,40 @@ +package org.simantics.sysdyn.ui.browser.contributions; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; +import org.simantics.db.Builtins; +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.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.browser.nodes.AbstractNode; + +public class Project extends ViewpointContributor { + + @Override + public Collection getContribution(ReadGraph graph, Resource project) + throws DatabaseException { + + ArrayList result = new ArrayList(); + Builtins b = graph.getBuiltins(); + SysdynResource sr = SysdynResource.getInstance(graph); + for(Resource r : graph.syncRequest(new ObjectsWithType(project, b.ConsistsOf, sr.SysdynModel))) { + try { + result.add(graph.adapt(r, AbstractNode.class)); + } catch(DatabaseException e) { + e.printStackTrace(); + } + } + return result; + + } + + @Override + public String getViewpointId() { + return "Standard"; + } + +} 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 54d75f87..e178b0b5 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 @@ -3,12 +3,91 @@ package org.simantics.sysdyn.ui.handlers; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.DiagramConstants; +import org.simantics.diagram.synchronization.graph.layer.GraphLayer; +import org.simantics.diagram.synchronization.graph.layer.GraphLayerUtil; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.modeling.ModelingUtils; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.utils.ResourceAdaptionUtils; public class NewModuleNodeHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { - System.out.println("New module!"); + + ISelection sel = HandlerUtil.getCurrentSelection(event); + Resource[] resources = ResourceAdaptionUtils.toResources(sel); + if (resources.length != 1) + return null; + + final Resource model = resources[0]; + + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph g) throws DatabaseException { + // Same as in SysdynProject.java. Should use the same code, not copy. + SysdynResource sr = SysdynResource.getInstance(g); + ModelingUtils mu = new ModelingUtils(g); + + Resource moduleType = g.newResource(); + g.claimValue(moduleType, mu.b.HasName, "ModuleType"); + g.claimValue(moduleType, mu.b.HasLabel, "ModuleTypeLabel"); + g.claim(moduleType, mu.b.Inherits, sr.Module); + g.claim(moduleType, mu.b.PartOf, model); + + Resource diagram = OrderedSetUtils.create(g, sr.ConfigurationDiagram); + GraphLayer l = new GraphLayerUtil(g).createLayer(DiagramConstants.DEFAULT_LAYER_NAME, true); + g.claim(diagram, mu.dr.HasLayer, l.getLayer()); + Resource mapping = g.newResource(); + g.claim(mapping, mu.b.InstanceOf, null, sr.DiagramToCompositeMapping); + g.claim(diagram, mu.b.HasTrigger, mapping); + + + Resource configuration = GraphUtils.create2(g, + sr.Configuration, + mu.mr.CompositeToDiagram, diagram); + + g.claim(moduleType, mu.sr.IsDefinedBy , configuration); + + + Resource moduleSymbol = g.newResource(); + g.claimValue(moduleSymbol, mu.b.HasName, "ModuuliSymboli"); + g.claimValue(moduleSymbol, mu.b.HasLabel, "ModuuliSymboliLabel"); + g.claim(moduleSymbol, mu.b.Inherits, sr.ModuleSymbol); + g.claim(moduleSymbol, mu.mr.SymbolToComponentType, moduleType); + + Resource terminal = g.newResource(); + g.claim(terminal, mu.b.InstanceOf, sr.SysdynTerminal); + Resource connectionVariable = g.newResource(); + g.claim(connectionVariable, mu.b.InstanceOf, mu.sr.ConnectionVariable); + g.claim(connectionVariable, mu.sr.Binds, sr.IsHeadOfTerminal); + g.claim(connectionVariable, mu.sr.IsParameterOf, sr.ModuleSymbol); + g.claim(terminal, mu.dr.HasConnectionVariable, connectionVariable); + + + Resource terminal2 = g.newResource(); + g.claim(terminal2, mu.b.InstanceOf, sr.SysdynTerminal); + Resource connectionVariable2 = g.newResource(); + g.claim(connectionVariable2, mu.b.InstanceOf, mu.sr.ConnectionVariable); + g.claim(connectionVariable2, mu.sr.Binds, sr.IsTailOfTerminal); + g.claim(connectionVariable2, mu.sr.IsParameterOf, sr.ModuleSymbol); + g.claim(terminal2, mu.dr.HasConnectionVariable, connectionVariable2); + + g.claim(moduleSymbol, mu.sr.IsDefinedBy, OrderedSetUtils.create(g, mu.sr.Composite, terminal, terminal2)); + + + } + }); return null; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java index d9cba57d..f3abe32e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java @@ -78,10 +78,10 @@ public class ModelicaWriter { } else if (element instanceof Module) { Module m = (Module)element; modules.add(m); - moduleInputs.put(m.getLabel(), new ArrayList()); + moduleInputs.put(m.getName(), new ArrayList()); for(IElement e : m.getType().getConfiguration().getElements()) if(e instanceof Input && !((Input)e).isHeadOfDependency()) { - moduleInputs.get(m.getLabel()).add((Input)e); + moduleInputs.get(m.getName()).add((Input)e); } } else if (element instanceof Input) { inputs.add((Input)element); @@ -112,7 +112,7 @@ public class ModelicaWriter { if(!modules.isEmpty()) { b.append("// Module definitions\n"); for(Module m : modules) { - b.append(" " + m.getType().getName() + " " + m.getLabel() + ";\n"); + b.append(" " + m.getType().getName() + " " + m.getName() + ";\n"); } } @@ -155,7 +155,7 @@ public class ModelicaWriter { Input variable = (Input)dependency.getHead(); Module module = (Module)dependency.getTail(); Variable reference = (Variable)dependency.refersTo(); - b.append(" " + variable.getName() + " = " + module.getLabel() + "." + reference.getName() + ";\n"); + b.append(" " + variable.getName() + " = " + module.getName() + "." + reference.getName() + ";\n"); } b.append("// Outputs\n"); @@ -163,8 +163,8 @@ public class ModelicaWriter { Variable variable = (Variable)dependency.getTail(); Module module = (Module)dependency.getHead(); Input reference = (Input)dependency.refersTo(); - b.append(" " + module.getLabel() + "." + reference.getName() + " = " + variable.getName() + ";\n"); - moduleInputs.get(module.getLabel()).remove(reference); + b.append(" " + module.getName() + "." + reference.getName() + " = " + variable.getName() + ";\n"); + moduleInputs.get(module.getName()).remove(reference); } b.append("// Default values for inputs in modules\n"); 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 7aed35d3..3af65a54 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java @@ -12,9 +12,6 @@ public class Module implements IElement { @RelatedValue("http://www.simantics.org/Layer0-1.0/HasName") private String name; - @RelatedValue("http://www.simantics.org/Layer0-1.0/HasLabel") - private String label; - @RelatedElement("http://www.simantics.org/Layer0-1.0/PartOf") private Configuration parentConfiguration; @@ -47,11 +44,6 @@ public class Module implements IElement { public String getName() { return name; } - - - public String getLabel() { - return label; - } public ModuleType getType() { return type; diff --git a/sysdyn_ontologies/sysdyn.graph b/sysdyn_ontologies/sysdyn.graph index 25f1dcd1..3869fac1 100644 --- a/sysdyn_ontologies/sysdyn.graph +++ b/sysdyn_ontologies/sysdyn.graph @@ -604,10 +604,13 @@ CapacitySymbol