1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.sysdyn.ui.browser.actions.newActions;
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;
33 * Creates a new module type
34 * @author Teemu Lempinen
37 public class NewModuleTypeAction implements ActionFactory{
40 public Runnable create(Object target) {
41 if(!(target instanceof Resource))
43 final Resource model = (Resource)target;
45 return new Runnable() {
48 Simantics.getSession().asyncRequest(new WriteRequest() {
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);
58 String name = NameUtils.findFreshName(g, "ModuleType", model, l0.ConsistsOf, "%s%d");
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);
67 Resource configuration = GraphUtils.create2(g,
69 l0.HasName, name + "Configuration",
70 l0.PartOf, moduleType);
72 g.claim(moduleType, sr2.IsDefinedBy , configuration);
74 Resource diagram = g.newResource();
75 g.adapt(sr.ConfigurationDiagramTemplate, Template.class).apply(g,
77 .keys("", "diagram", "name")
78 .values(configuration, diagram, "Diagrammi")
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);
89 Resource mapping = g.newResource();
90 g.claim(mapping, l0.InstanceOf, null, sr.DiagramToCompositeMapping);
91 g.claim(diagram, L0X.HasTrigger, mapping);
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);
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);
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);
111 g.claim(moduleSymbol, sr2.IsDefinedBy, OrderedSetUtils.create(g, sr2.Composite, terminal, terminal2));
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);
126 Resource configurationRelationBaseInverse = graph.getSingleObject(configurationRelationBase, L0.InverseOf);
128 Resource configurationRelation = GraphUtils.create(graph,
129 L0.SubrelationOf, configurationRelationBase,
131 L0.HasName, NameUtils.getSafeName(graph, configurationRelationBase)
134 Resource configurationRelationInverse = GraphUtils.create(graph,
135 L0.PartOf, configurationRelation,
136 L0.SubrelationOf, configurationRelationBaseInverse,
137 L0.HasName, "Inverse",
138 L0.InverseOf, configurationRelation);
140 Resource terminalRelation = null;
141 terminalRelation = GraphUtils.create(graph,
142 MOD.DiagramConnectionRelationToConnectionRelation, configurationRelation,
144 L0.HasName, NameUtils.getSafeName(graph, connectionRelation)
147 graph.claim(terminalRelation, L0.SubrelationOf, null, connectionRelation);
148 Resource inverse = GraphUtils.create(graph,
149 L0.PartOf, terminalRelation,
150 L0.HasName, "Inverse");
152 graph.claim(inverse, L0.SubrelationOf, null, STR.Connects);
153 graph.claim(terminalRelation, L0.InverseOf, inverse);
155 return terminalRelation;