]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
1d503d025d6c3dc6b57ab5c02030192108fecd15
[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                         Resource mapping = g.newResource();\r
90                         g.claim(mapping, l0.InstanceOf, null, sr.DiagramToCompositeMapping);\r
91                         g.claim(diagram, L0X.HasTrigger, mapping);\r
92 \r
93                         Resource moduleSymbol = g.newResource();\r
94                         g.claimLiteral(moduleSymbol, l0.HasName, name + " Symbol");\r
95                         g.claimLiteral(moduleSymbol, l0.HasLabel, name + " Symbol");\r
96                         g.claim(moduleSymbol, l0.Inherits, sr.ModuleSymbol);\r
97                         g.claim(moduleSymbol, mr.SymbolToComponentType, moduleType);\r
98                         g.claim(moduleSymbol, l0.PartOf, moduleType);\r
99 \r
100                         Resource terminal = g.newResource();\r
101                         g.claim(terminal, l0.InstanceOf, sr.SysdynTerminal);\r
102                         Resource relation = createTerminalRelation(g, moduleSymbol, sr.IsHeadOfTerminal, sr.Variable_isHeadOf);\r
103                         DiagramGraphUtil.addConnectionPoint(g, moduleSymbol, terminal, relation);               \r
104 \r
105                         Resource terminal2 = g.newResource();\r
106                         g.claim(terminal2, l0.InstanceOf, sr.SysdynTerminal);\r
107                         relation = createTerminalRelation(g, moduleSymbol, sr.IsTailOfTerminal, sr.Variable_isTailOf);\r
108                         DiagramGraphUtil.addConnectionPoint(g, moduleSymbol, terminal2, relation);\r
109 \r
110                         g.claim(moduleSymbol, sr2.IsDefinedBy, OrderedSetUtils.create(g, sr2.Composite, terminal, terminal2));\r
111 \r
112 \r
113 \r
114                     }\r
115                 });\r
116             }\r
117         };\r
118     }\r
119 \r
120     public static Resource createTerminalRelation(WriteGraph graph, Resource symbol, Resource connectionRelation, Resource configurationRelation) throws DatabaseException {\r
121         StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
122         ModelingResources MOD = ModelingResources.getInstance(graph);\r
123         Layer0 L0 = Layer0.getInstance(graph);\r
124 \r
125 \r
126         Resource terminalRelation = null;\r
127         terminalRelation = GraphUtils.create(graph,\r
128                 MOD.DiagramConnectionRelationToConnectionRelation, configurationRelation,\r
129                 L0.PartOf, symbol,\r
130                 L0.HasName, NameUtils.getSafeName(graph, connectionRelation)\r
131                 );\r
132 \r
133         graph.claim(terminalRelation, L0.SubrelationOf, null, connectionRelation);\r
134         Resource inverse = GraphUtils.create(graph,\r
135                 L0.PartOf, terminalRelation, \r
136                 L0.HasName, "Inverse");\r
137 \r
138         graph.claim(inverse, L0.SubrelationOf, null, STR.Connects);\r
139         graph.claim(terminalRelation, L0.InverseOf, inverse);\r
140 \r
141         return terminalRelation;\r
142     }\r
143 \r
144 }\r