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