]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
675d2e903588aceb9067421f2a705e74d11fc363
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 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.editor;\r
13 \r
14 import gnu.trove.TIntIntHashMap;\r
15 \r
16 import java.util.Collection;\r
17 import java.util.HashMap;\r
18 \r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.common.request.ObjectsWithType;\r
22 import org.simantics.db.common.utils.NameUtils;\r
23 import org.simantics.db.common.utils.URIStringUtils;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.layer0.Layer0;\r
26 import org.simantics.layer0.utils.direct.GraphUtils;\r
27 import org.simantics.mapping.constraint.instructions.TypedBracketInstruction.CreationInstruction;\r
28 import org.simantics.modeling.services.ComponentNamingUtil;\r
29 import org.simantics.modeling.services.NamingException;\r
30 import org.simantics.operation.Layer0X;\r
31 import org.simantics.project.IProject;\r
32 import org.simantics.sysdyn.SysdynResource;\r
33 import org.simantics.sysdyn.ui.elements.ShadowVariableReferenceDialogRunnable;\r
34 import org.simantics.utils.threads.SWTThread;\r
35 \r
36 public class SysdynCreationInstruction extends CreationInstruction {\r
37 \r
38     IProject project;\r
39     Resource configurationRoot;\r
40     int lComponentType;\r
41     int lConfiguration;\r
42 \r
43     public SysdynCreationInstruction(IProject project, Resource configurationRoot, int variableId, int componentType,\r
44             int configuration) {\r
45         super(variableId);\r
46         this.project = project;\r
47         this.configurationRoot = configurationRoot;\r
48         lComponentType = componentType;\r
49         lConfiguration = configuration;\r
50     }\r
51 \r
52     @Override\r
53     public Resource create(WriteGraph g, Object[] bindings) throws DatabaseException {\r
54         Resource componentType = (Resource) bindings[lComponentType];\r
55         Resource configuration = (Resource) bindings[lConfiguration];\r
56 \r
57         Layer0 l0 = Layer0.getInstance(g);\r
58         Layer0X L0X = Layer0X.getInstance(g);\r
59         \r
60         try {\r
61             \r
62             // Naming strategy\r
63             String proposition = URIStringUtils.escape(ComponentNamingUtil.findFreshInstanceName(g, project, configuration, configuration, componentType));\r
64             Resource result = GraphUtils.create(g,\r
65                     l0.HasName, proposition\r
66             );\r
67             g.claim(result, L0X.Represents, result);\r
68             \r
69             \r
70             \r
71             // Async reference dialog for shadow variables\r
72             SysdynResource SR = SysdynResource.getInstance(g);\r
73             if(SR.Shadow.equals(componentType)) {\r
74                 Layer0 L0 = Layer0.getInstance(g);\r
75                 Collection<Resource> referrableVariables = g.syncRequest(new ObjectsWithType(configuration, L0.ConsistsOf, SR.IndependentVariable));\r
76                 referrableVariables.addAll( g.syncRequest(new ObjectsWithType(configuration, L0.ConsistsOf, SR.Input)));\r
77                 HashMap<String, Resource> names = new  HashMap<String, Resource>();\r
78                 for(Resource var : referrableVariables) {\r
79                     String name = NameUtils.getSafeName(g, var);\r
80                     names.put(name, var);\r
81                 }\r
82                 SWTThread.getThreadAccess().asyncExec(new ShadowVariableReferenceDialogRunnable(result, names));\r
83             }\r
84             \r
85             return result;\r
86         } catch (NamingException e1) {\r
87             throw new DatabaseException(e1);\r
88         }\r
89     }\r
90     \r
91     @Override\r
92     public void mapVariables(TIntIntHashMap map) {\r
93         super.mapVariables(map);\r
94         lComponentType = map.get(lComponentType);\r
95         lConfiguration = map.get(lConfiguration);\r
96     }\r
97 \r
98 }\r