]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
0bc575095aee9c58f57ed5a95da7b69b6a2da0d8
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 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.browser.actions.newActions;\r
13 \r
14 import org.simantics.db.Resource;\r
15 import org.simantics.db.WriteGraph;\r
16 import org.simantics.db.common.request.WriteRequest;\r
17 import org.simantics.db.common.utils.NameUtils;\r
18 import org.simantics.db.common.utils.OrderedSetUtils;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.db.layer0.adapter.ActionFactory;\r
21 import org.simantics.db.layer0.adapter.Template;\r
22 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;\r
23 import org.simantics.layer0.Layer0;\r
24 import org.simantics.layer0.utils.direct.GraphUtils;\r
25 import org.simantics.modeling.ModelingResources;\r
26 import org.simantics.operation.Layer0X;\r
27 import org.simantics.structural.stubs.StructuralResource2;\r
28 import org.simantics.sysdyn.SysdynResource;\r
29 import org.simantics.ui.SimanticsUI;\r
30 import org.simantics.utils.datastructures.ArrayMap;\r
31 \r
32 /**\r
33  * Creates a new module type\r
34  * @author Teemu Lempinen\r
35  *\r
36  */\r
37 public class NewModuleTypeAction implements ActionFactory{\r
38 \r
39     @Override\r
40     public Runnable create(Object target) {\r
41         if(!(target instanceof Resource))\r
42             return null;\r
43         final Resource model = (Resource)target;\r
44 \r
45         return new Runnable() {\r
46             @Override\r
47             public void run() {\r
48                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
49 \r
50                     @Override\r
51                     public void perform(WriteGraph g) throws DatabaseException {\r
52                         SysdynResource sr = SysdynResource.getInstance(g);\r
53                         Layer0 l0 = Layer0.getInstance(g);\r
54                         Layer0X L0X = Layer0X.getInstance(g);\r
55                         ModelingResources mr = ModelingResources.getInstance(g);\r
56                         StructuralResource2 sr2 = StructuralResource2.getInstance(g);\r
57 \r
58                         String name = NameUtils.findFreshName(g, "ModuleType", model, l0.ConsistsOf, "%s%d");\r
59 \r
60                         Resource moduleType = g.newResource();\r
61                         g.claimLiteral(moduleType, l0.HasName, name);\r
62                         g.claim(moduleType, l0.Inherits, sr.Module);\r
63                         g.claim(moduleType, l0.PartOf, model);\r
64 \r
65 \r
66 \r
67                         Resource configuration = GraphUtils.create2(g, \r
68                                 sr.Configuration,\r
69                                 l0.HasName, name + "Configuration",\r
70                                 l0.PartOf, moduleType);\r
71 \r
72                         g.claim(moduleType, sr2.IsDefinedBy , configuration);\r
73 \r
74                         Resource diagram = g.newResource();\r
75                         g.adapt(sr.ConfigurationDiagramTemplate, Template.class).apply(g,\r
76                                 ArrayMap\r
77                                 .keys("", "diagram", "name")\r
78                                 .values(configuration, diagram, "Diagrammi")\r
79                                 );\r
80 \r
81 \r
82                         // Remove default mapping and add sysdyn mapping\r
83                         for(Resource trigger : g.getObjects(diagram, L0X.HasTrigger)) {\r
84                             if(g.isInstanceOf(trigger, mr.DiagramToCompositeMapping)) {\r
85                                 g.deny(diagram, L0X.HasTrigger, trigger);\r
86                             }\r
87                         }\r
88 \r
89                         GraphUtils.create2(g,\r
90                                 sr.Validations_Dependencies_MissingDependencyConnectionsIssueSource,\r
91                                 L0X.IsActivatedBy, model,\r
92                                 l0.PartOf, moduleType\r
93                                 );\r
94 \r
95                         GraphUtils.create2(g,\r
96                                 sr.Validations_Dependencies_DependencyConnectionsIssueSource,\r
97                                 L0X.IsActivatedBy, model,\r
98                                 l0.PartOf, moduleType\r
99                                 );\r
100 \r
101                         GraphUtils.create2(g,\r
102                                 sr.Validations_Expressions_ExpressionIssueSource,\r
103                                 L0X.IsActivatedBy, model,\r
104                                 l0.PartOf, moduleType\r
105                                 );\r
106 \r
107                         Resource mapping = g.newResource();\r
108                         g.claim(mapping, l0.InstanceOf, null, sr.DiagramToCompositeMapping);\r
109                         g.claim(diagram, L0X.HasTrigger, mapping);\r
110 \r
111                         Resource moduleSymbol = g.newResource();\r
112                         g.claimLiteral(moduleSymbol, l0.HasName, name + " Symbol");\r
113                         g.claimLiteral(moduleSymbol, l0.HasLabel, name + " Symbol");\r
114                         g.claim(moduleSymbol, l0.Inherits, sr.ModuleSymbol);\r
115                         g.claim(moduleSymbol, mr.SymbolToComponentType, moduleType);\r
116                         g.claim(moduleSymbol, l0.PartOf, moduleType);\r
117 \r
118                         Resource terminal = g.newResource();\r
119                         g.claim(terminal, l0.InstanceOf, sr.SysdynTerminal);\r
120                         Resource relation = createTerminalRelation(g, moduleSymbol, sr.IsHeadOfTerminal, sr.Variable_isHeadOf);\r
121                         DiagramGraphUtil.addConnectionPoint(g, moduleSymbol, terminal, relation);               \r
122 \r
123                         Resource terminal2 = g.newResource();\r
124                         g.claim(terminal2, l0.InstanceOf, sr.SysdynTerminal);\r
125                         relation = createTerminalRelation(g, moduleSymbol, sr.IsTailOfTerminal, sr.Variable_isTailOf);\r
126                         DiagramGraphUtil.addConnectionPoint(g, moduleSymbol, terminal2, relation);\r
127 \r
128                         g.claim(moduleSymbol, sr2.IsDefinedBy, OrderedSetUtils.create(g, sr2.Composite, terminal, terminal2));\r
129 \r
130 \r
131 \r
132                     }\r
133                 });\r
134             }\r
135         };\r
136     }\r
137 \r
138     public static Resource createTerminalRelation(WriteGraph graph, Resource symbol, Resource connectionRelation, Resource configurationRelation) throws DatabaseException {\r
139         StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
140         ModelingResources MOD = ModelingResources.getInstance(graph);\r
141         Layer0 L0 = Layer0.getInstance(graph);\r
142 \r
143 \r
144         Resource terminalRelation = null;\r
145         terminalRelation = GraphUtils.create(graph,\r
146                 MOD.DiagramConnectionRelationToConnectionRelation, configurationRelation,\r
147                 L0.PartOf, symbol,\r
148                 L0.HasName, NameUtils.getSafeName(graph, connectionRelation)\r
149                 );\r
150 \r
151         graph.claim(terminalRelation, L0.SubrelationOf, null, connectionRelation);\r
152         Resource inverse = GraphUtils.create(graph,\r
153                 L0.PartOf, terminalRelation, \r
154                 L0.HasName, "Inverse");\r
155 \r
156         graph.claim(inverse, L0.SubrelationOf, null, STR.Connects);\r
157         graph.claim(terminalRelation, L0.InverseOf, inverse);\r
158 \r
159         return terminalRelation;\r
160     }\r
161 \r
162 }\r