1 /*******************************************************************************
\r
2 * Copyright (c) 2010 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.handlers;
\r
14 import org.eclipse.core.commands.AbstractHandler;
\r
15 import org.eclipse.core.commands.ExecutionEvent;
\r
16 import org.eclipse.core.commands.ExecutionException;
\r
17 import org.eclipse.jface.viewers.ISelection;
\r
18 import org.eclipse.ui.handlers.HandlerUtil;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.WriteGraph;
\r
21 import org.simantics.db.common.request.WriteRequest;
\r
22 import org.simantics.db.common.utils.NameUtils;
\r
23 import org.simantics.db.common.utils.OrderedSetUtils;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.layer0.adapter.Template;
\r
26 import org.simantics.diagram.stubs.DiagramResource;
\r
27 import org.simantics.layer0.Layer0;
\r
28 import org.simantics.layer0.utils.direct.GraphUtils;
\r
29 import org.simantics.modeling.ModelingResources;
\r
30 import org.simantics.structural.stubs.StructuralResource2;
\r
31 import org.simantics.sysdyn.SysdynResource;
\r
32 import org.simantics.sysdyn.ui.browser.nodes.ModulesNode;
\r
33 import org.simantics.ui.SimanticsUI;
\r
34 import org.simantics.ui.utils.AdaptionUtils;
\r
35 import org.simantics.utils.datastructures.ArrayMap;
\r
37 public class NewModuleNodeHandler extends AbstractHandler {
\r
40 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
42 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
44 ModulesNode node = AdaptionUtils.adaptToSingle(sel, ModulesNode.class);
\r
48 final Resource model = node.data;
\r
50 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
53 public void perform(WriteGraph g) throws DatabaseException {
\r
54 // Same as in SysdynProject.java. Should use the same code, not copy.
\r
55 SysdynResource sr = SysdynResource.getInstance(g);
\r
56 Layer0 l0 = Layer0.getInstance(g);
\r
57 ModelingResources mr = ModelingResources.getInstance(g);
\r
58 StructuralResource2 sr2 = StructuralResource2.getInstance(g);
\r
59 DiagramResource dr = DiagramResource.getInstance(g);
\r
61 String name = NameUtils.findFreshName(g, "ModuleType", model, l0.ConsistsOf, "%s%d");
\r
63 Resource moduleType = g.newResource();
\r
64 g.claimLiteral(moduleType, l0.HasName, name);
\r
65 g.claim(moduleType, l0.Inherits, sr.Module);
\r
66 g.claim(moduleType, l0.PartOf, model);
\r
68 Resource configuration = GraphUtils.create2(g,
\r
70 l0.HasName, name + "Configuration",
\r
73 g.claim(moduleType, sr2.IsDefinedBy , configuration);
\r
75 Resource diagram = g.newResource();
\r
76 g.adapt(sr.ConfigurationDiagramTemplate, Template.class).apply(g,
\r
78 .keys("", "diagram", "name")
\r
79 .values(configuration, diagram, "Diagrammi")
\r
83 // Remove default mapping and add sysdyn mapping
\r
84 for(Resource trigger : g.getObjects(diagram, l0.HasTrigger)) {
\r
85 if(g.isInstanceOf(trigger, mr.DiagramToCompositeMapping)) {
\r
86 g.deny(diagram, l0.HasTrigger, trigger);
\r
89 Resource mapping = g.newResource();
\r
90 g.claim(mapping, l0.InstanceOf, null, sr.DiagramToCompositeMapping);
\r
91 g.claim(diagram, l0.HasTrigger, mapping);
\r
93 Resource moduleSymbol = g.newResource();
\r
94 g.claimLiteral(moduleSymbol, l0.HasName, name + " Symbol");
\r
95 g.claimLiteral(moduleSymbol, l0.HasLabel, name);
\r
96 g.claim(moduleSymbol, l0.Inherits, sr.ModuleSymbol);
\r
97 g.claim(moduleSymbol, mr.SymbolToComponentType, moduleType);
\r
98 g.claim(moduleSymbol, l0.PartOf, model);
\r
100 Resource terminal = g.newResource();
\r
101 g.claim(terminal, l0.InstanceOf, sr.SysdynTerminal);
\r
102 Resource connectionVariable = g.newResource();
\r
103 g.claim(connectionVariable, l0.InstanceOf, sr2.ConnectionVariable);
\r
104 g.claim(connectionVariable, sr2.Binds, sr.IsHeadOfTerminal);
\r
105 g.claim(connectionVariable, sr2.IsParameterOf, moduleSymbol);
\r
106 g.claim(terminal, dr.HasConnectionVariable, connectionVariable);
\r
109 Resource terminal2 = g.newResource();
\r
110 g.claim(terminal2, l0.InstanceOf, sr.SysdynTerminal);
\r
111 Resource connectionVariable2 = g.newResource();
\r
112 g.claim(connectionVariable2, l0.InstanceOf, sr2.ConnectionVariable);
\r
113 g.claim(connectionVariable2, sr2.Binds, sr.IsTailOfTerminal);
\r
114 g.claim(connectionVariable2, sr2.IsParameterOf, moduleSymbol);
\r
115 g.claim(terminal2, dr.HasConnectionVariable, connectionVariable2);
\r
117 g.claim(moduleSymbol, sr2.IsDefinedBy, OrderedSetUtils.create(g, sr2.Composite, terminal, terminal2));
\r