]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
9c2e98e4ad5b670a7f10df901b7c248ea37d9c6f
[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.diagram.stubs.DiagramResource;\r
27 import org.simantics.layer0.Layer0;\r
28 import org.simantics.layer0.utils.direct.GraphUtils;\r
29 import org.simantics.modeling.ModelingResources;\r
30 import org.simantics.operation.Layer0X;\r
31 import org.simantics.structural.stubs.StructuralResource2;\r
32 import org.simantics.sysdyn.SysdynResource;\r
33 import org.simantics.sysdyn.ui.browser.nodes.ModulesNode;\r
34 import org.simantics.ui.SimanticsUI;\r
35 import org.simantics.ui.utils.AdaptionUtils;\r
36 import org.simantics.utils.datastructures.ArrayMap;\r
37 \r
38 /**\r
39  * Creates a new module node for a model.\r
40  * \r
41  * @author Teemu Lempinen\r
42  *\r
43  */\r
44 public class NewModuleNodeHandler extends AbstractHandler {\r
45 \r
46     /**\r
47      * Assumes to be called from a node that has a SysDyn model as its resource.\r
48      */\r
49     @Override\r
50     public Object execute(ExecutionEvent event) throws ExecutionException {\r
51         \r
52         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
53         \r
54         ModulesNode node = AdaptionUtils.adaptToSingle(sel, ModulesNode.class);\r
55         if (node == null)\r
56             return null;\r
57 \r
58         final Resource model = node.data;        \r
59         \r
60         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
61             \r
62             @Override\r
63             public void perform(WriteGraph g) throws DatabaseException {\r
64                 SysdynResource sr = SysdynResource.getInstance(g);\r
65                 Layer0 l0 = Layer0.getInstance(g);\r
66                 Layer0X L0X = Layer0X.getInstance(g);\r
67                 ModelingResources mr = ModelingResources.getInstance(g);\r
68                 StructuralResource2 sr2 = StructuralResource2.getInstance(g);\r
69                 DiagramResource dr = DiagramResource.getInstance(g);\r
70                 \r
71                 String name = NameUtils.findFreshName(g, "ModuleType", model, l0.ConsistsOf, "%s%d");\r
72                 \r
73                 Resource moduleType = g.newResource();\r
74                 g.claimLiteral(moduleType, l0.HasName, name);\r
75                 g.claim(moduleType, l0.Inherits, sr.Module);\r
76                 g.claim(moduleType, l0.PartOf, model);\r
77                 \r
78                 \r
79                 \r
80                 Resource configuration = GraphUtils.create2(g, \r
81                         sr.Configuration,\r
82                         l0.HasName, name + "Configuration",\r
83                         l0.PartOf, moduleType);\r
84                 \r
85                 g.claim(moduleType, sr2.IsDefinedBy , configuration);\r
86                 \r
87                 Resource diagram = g.newResource();\r
88                 g.adapt(sr.ConfigurationDiagramTemplate, Template.class).apply(g,\r
89                         ArrayMap\r
90                         .keys("", "diagram", "name")\r
91                         .values(configuration, diagram, "Diagrammi")\r
92                 );\r
93                 \r
94                 \r
95                 // Remove default mapping and add sysdyn mapping\r
96                 for(Resource trigger : g.getObjects(diagram, L0X.HasTrigger)) {\r
97                         if(g.isInstanceOf(trigger, mr.DiagramToCompositeMapping)) {\r
98                                 g.deny(diagram, L0X.HasTrigger, trigger);\r
99                         }\r
100                 }\r
101                 \r
102                 GraphUtils.create2(g,\r
103                         sr.Validations_Dependencies_MissingDependencyConnectionsIssueSource,\r
104                         L0X.IsActivatedBy, model,\r
105                         l0.PartOf, moduleType\r
106                         );\r
107                 \r
108                 GraphUtils.create2(g,\r
109                         sr.Validations_Dependencies_DependencyConnectionsIssueSource,\r
110                         L0X.IsActivatedBy, model,\r
111                         l0.PartOf, moduleType\r
112                         );\r
113                 \r
114                 GraphUtils.create2(g,\r
115                         sr.Validations_Expressions_ExpressionIssueSource,\r
116                         L0X.IsActivatedBy, model,\r
117                         l0.PartOf, moduleType\r
118                         );\r
119                 \r
120                 Resource mapping = g.newResource();\r
121                 g.claim(mapping, l0.InstanceOf, null, sr.DiagramToCompositeMapping);\r
122                 g.claim(diagram, L0X.HasTrigger, mapping);\r
123                 \r
124                 Resource moduleSymbol = g.newResource();\r
125                 g.claimLiteral(moduleSymbol, l0.HasName, name + " Symbol");\r
126                 g.claimLiteral(moduleSymbol, l0.HasLabel, name + " Symbol");\r
127                 g.claim(moduleSymbol, l0.Inherits, sr.ModuleSymbol);\r
128                 g.claim(moduleSymbol, mr.SymbolToComponentType, moduleType);\r
129                 g.claim(moduleSymbol, l0.PartOf, moduleType);\r
130                 \r
131                 Resource terminal = g.newResource();\r
132                 g.claim(terminal, l0.InstanceOf, sr.SysdynTerminal);\r
133                 Resource connectionVariable = g.newResource();\r
134                 g.claim(connectionVariable, l0.InstanceOf, sr2.ConnectionVariable);\r
135                 g.claim(connectionVariable, sr2.Binds, sr.IsHeadOfTerminal);\r
136                 g.claim(connectionVariable, sr2.IsParameterOf, moduleSymbol);\r
137                 g.claim(terminal, dr.HasConnectionVariable, connectionVariable);\r
138                 \r
139                 \r
140                 Resource terminal2 = g.newResource();\r
141                 g.claim(terminal2, l0.InstanceOf, sr.SysdynTerminal);\r
142                 Resource connectionVariable2 = g.newResource();\r
143                 g.claim(connectionVariable2, l0.InstanceOf, sr2.ConnectionVariable);\r
144                 g.claim(connectionVariable2, sr2.Binds, sr.IsTailOfTerminal);\r
145                 g.claim(connectionVariable2, sr2.IsParameterOf, moduleSymbol);\r
146                 g.claim(terminal2, dr.HasConnectionVariable, connectionVariable2);\r
147                 \r
148                 g.claim(moduleSymbol, sr2.IsDefinedBy, OrderedSetUtils.create(g, sr2.Composite, terminal, terminal2));\r
149             \r
150                 \r
151             }\r
152         });\r
153         return null;\r
154     }\r
155 \r
156 \r
157 }\r