]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
b5939cb726d4ea9a92a1080a4cff87ca821af6cf
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.handlers.newComponents;\r
13 \r
14 import org.eclipse.core.commands.AbstractHandler;\r
15 import org.eclipse.core.commands.ExecutionEvent;\r
16 import org.eclipse.core.commands.ExecutionException;\r
17 import org.eclipse.jface.viewers.ISelection;\r
18 import org.eclipse.ui.handlers.HandlerUtil;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.common.request.WriteRequest;\r
22 import org.simantics.db.common.utils.NameUtils;\r
23 import org.simantics.db.common.utils.OrderedSetUtils;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.layer0.adapter.Template;\r
26 import org.simantics.db.layer0.util.Layer0Utils;\r
27 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;\r
28 import org.simantics.layer0.Layer0;\r
29 import org.simantics.layer0.utils.direct.GraphUtils;\r
30 import org.simantics.modeling.ModelingResources;\r
31 import org.simantics.operation.Layer0X;\r
32 import org.simantics.structural.stubs.StructuralResource2;\r
33 import org.simantics.sysdyn.SysdynResource;\r
34 import org.simantics.sysdyn.ui.browser.actions.newActions.NewModuleTypeAction;\r
35 import org.simantics.sysdyn.ui.browser.nodes.ModulesNode;\r
36 import org.simantics.ui.SimanticsUI;\r
37 import org.simantics.utils.datastructures.ArrayMap;\r
38 import org.simantics.utils.ui.AdaptionUtils;\r
39 \r
40 /**\r
41  * Creates a new module node for a model.\r
42  * \r
43  * @author Teemu Lempinen\r
44  *\r
45  */\r
46 public class NewModuleNodeHandler extends AbstractHandler {\r
47 \r
48     /**\r
49      * Assumes to be called from a node that has a SysDyn model as its resource.\r
50      */\r
51     @Override\r
52     public Object execute(ExecutionEvent event) throws ExecutionException {\r
53         \r
54         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
55         \r
56         ModulesNode node = AdaptionUtils.adaptToSingle(sel, ModulesNode.class);\r
57         if (node == null)\r
58             return null;\r
59 \r
60         final Resource model = node.data;        \r
61         \r
62         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
63             \r
64             @Override\r
65             public void perform(WriteGraph g) throws DatabaseException {\r
66                 g.markUndoPoint();\r
67                 SysdynResource sr = SysdynResource.getInstance(g);\r
68                 Layer0 l0 = Layer0.getInstance(g);\r
69                 Layer0X L0X = Layer0X.getInstance(g);\r
70                 ModelingResources mr = ModelingResources.getInstance(g);\r
71                 StructuralResource2 sr2 = StructuralResource2.getInstance(g);\r
72                 \r
73                 String name = NameUtils.findFreshName(g, "ModuleType", model, l0.ConsistsOf, "%s%d");\r
74                 \r
75                 Resource moduleType = g.newResource();\r
76                 g.claimLiteral(moduleType, l0.HasName, name);\r
77                 g.claim(moduleType, l0.Inherits, sr.Module);\r
78                 g.claim(moduleType, l0.PartOf, model);\r
79                 \r
80                 \r
81                 \r
82                 Resource configuration = GraphUtils.create2(g, \r
83                         sr.Configuration,\r
84                         l0.HasName, name + "Configuration",\r
85                         l0.PartOf, moduleType);\r
86                 \r
87                 g.claim(moduleType, sr2.IsDefinedBy , configuration);\r
88                 \r
89                 Resource diagram = g.newResource();\r
90                 g.adapt(sr.ConfigurationDiagramTemplate, Template.class).apply(g,\r
91                         ArrayMap\r
92                         .keys("", "diagram", "name")\r
93                         .values(configuration, diagram, "Diagrammi")\r
94                 );\r
95                 \r
96                 \r
97                 // Remove default mapping and add sysdyn mapping\r
98                 for(Resource trigger : g.getObjects(diagram, L0X.HasTrigger)) {\r
99                         if(g.isInstanceOf(trigger, mr.DiagramToCompositeMapping)) {\r
100                                 g.deny(diagram, L0X.HasTrigger, trigger);\r
101                         }\r
102                 }\r
103                 \r
104                 Resource mapping = g.newResource();\r
105                 g.claim(mapping, l0.InstanceOf, null, sr.DiagramToCompositeMapping);\r
106                 g.claim(diagram, L0X.HasTrigger, mapping);\r
107                 \r
108                 Resource moduleSymbol = g.newResource();\r
109                 g.claimLiteral(moduleSymbol, l0.HasName, name + " Symbol");\r
110                 g.claimLiteral(moduleSymbol, l0.HasLabel, name + " Symbol");\r
111                 g.claim(moduleSymbol, l0.Inherits, sr.ModuleSymbol);\r
112                 g.claim(moduleSymbol, mr.SymbolToComponentType, moduleType);\r
113                 g.claim(moduleSymbol, l0.PartOf, moduleType);\r
114                 \r
115                 Resource terminal = g.newResource();\r
116                 g.claim(terminal, l0.InstanceOf, sr.SysdynTerminal);\r
117                 Resource relation = NewModuleTypeAction.createTerminalRelation(g, moduleSymbol, sr.IsHeadOfTerminal, sr.Variable_isHeadOf);\r
118                 DiagramGraphUtil.addConnectionPoint(g, moduleSymbol, terminal, relation);               \r
119                 \r
120                 Resource terminal2 = g.newResource();\r
121                 g.claim(terminal2, l0.InstanceOf, sr.SysdynTerminal);\r
122                 relation = NewModuleTypeAction.createTerminalRelation(g, moduleSymbol, sr.IsTailOfTerminal, sr.Variable_isTailOf);\r
123                 DiagramGraphUtil.addConnectionPoint(g, moduleSymbol, terminal2, relation);\r
124                                 \r
125                 g.claim(moduleSymbol, sr2.IsDefinedBy, OrderedSetUtils.create(g, sr2.Composite, terminal, terminal2));\r
126             \r
127                 Layer0Utils.addCommentMetadata(g, "Created new Module Type " + name + " " + moduleType.toString());\r
128             }\r
129         });\r
130         return null;\r
131     }\r
132     \r
133 \r
134 }\r