1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.browser.actions.newActions;
\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
33 * Creates a new module type
\r
34 * @author Teemu Lempinen
\r
37 public class NewModuleTypeAction implements ActionFactory{
\r
40 public Runnable create(Object target) {
\r
41 if(!(target instanceof Resource))
\r
43 final Resource model = (Resource)target;
\r
45 return new Runnable() {
\r
48 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\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
58 String name = NameUtils.findFreshName(g, "ModuleType", model, l0.ConsistsOf, "%s%d");
\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
67 Resource configuration = GraphUtils.create2(g,
\r
69 l0.HasName, name + "Configuration",
\r
70 l0.PartOf, moduleType);
\r
72 g.claim(moduleType, sr2.IsDefinedBy , configuration);
\r
74 Resource diagram = g.newResource();
\r
75 g.adapt(sr.ConfigurationDiagramTemplate, Template.class).apply(g,
\r
77 .keys("", "diagram", "name")
\r
78 .values(configuration, diagram, "Diagrammi")
\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
89 GraphUtils.create2(g,
\r
90 sr.Validations_Dependencies_MissingDependencyConnectionsIssueSource,
\r
91 L0X.IsActivatedBy, model,
\r
92 l0.PartOf, moduleType
\r
95 GraphUtils.create2(g,
\r
96 sr.Validations_Dependencies_DependencyConnectionsIssueSource,
\r
97 L0X.IsActivatedBy, model,
\r
98 l0.PartOf, moduleType
\r
101 GraphUtils.create2(g,
\r
102 sr.Validations_Expressions_ExpressionIssueSource,
\r
103 L0X.IsActivatedBy, model,
\r
104 l0.PartOf, moduleType
\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
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
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
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
128 g.claim(moduleSymbol, sr2.IsDefinedBy, OrderedSetUtils.create(g, sr2.Composite, terminal, terminal2));
\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
144 Resource terminalRelation = null;
\r
145 terminalRelation = GraphUtils.create(graph,
\r
146 MOD.DiagramConnectionRelationToConnectionRelation, configurationRelation,
\r
148 L0.HasName, NameUtils.getSafeName(graph, connectionRelation)
\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
156 graph.claim(inverse, L0.SubrelationOf, null, STR.Connects);
\r
157 graph.claim(terminalRelation, L0.InverseOf, inverse);
\r
159 return terminalRelation;
\r