]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/profile/Profiles.java
9baa067f783294da74d63c8ba02c5e339230e277
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / Profiles.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 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.diagram.profile;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.HashMap;\r
16 import java.util.List;\r
17 import java.util.Map;\r
18 \r
19 import org.simantics.databoard.Bindings;\r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.WriteGraph;\r
23 import org.simantics.db.common.request.WriteRequest;\r
24 import org.simantics.db.common.utils.ListUtils;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.db.layer0.variable.Variable;\r
27 import org.simantics.db.service.VirtualGraphSupport;\r
28 import org.simantics.diagram.stubs.DiagramResource;\r
29 import org.simantics.layer0.Layer0;\r
30 import org.simantics.modeling.ModelingResources;\r
31 import org.simantics.simulation.ontology.SimulationResource;\r
32 import org.simantics.utils.datastructures.Arrays;\r
33 \r
34 /**\r
35  * @author Tuukka Lehtonen\r
36  */\r
37 public class Profiles {\r
38 \r
39     public static Resource createProfile(WriteGraph graph, String profileName, Resource... entries)\r
40             throws DatabaseException {\r
41         Layer0 L0 = Layer0.getInstance(graph);\r
42         DiagramResource DIA = DiagramResource.getInstance(graph);\r
43 \r
44         ArrayList<Resource> ee = new ArrayList<Resource>();\r
45         Arrays.addAll(ee, entries);\r
46         Resource list = ListUtils.create(graph, DIA.Profile, ee);\r
47 \r
48         // TODO : DIA.Profile is a list, but why it is used in the container?\r
49         //Resource profile = graph.newResource();\r
50         //graph.claim(profile, L0.InstanceOf, null, DIA.Profile);\r
51         \r
52         Resource profile = ListUtils.create(graph, DIA.Profile);\r
53         \r
54         graph.claimLiteral(profile, L0.HasName, profileName);\r
55         graph.claim(profile, DIA.HasEntries, null, list);\r
56 \r
57         return profile;\r
58     }\r
59 \r
60     public static Resource createEntry(WriteGraph graph, String name, Resource style, Resource group)\r
61             throws DatabaseException {\r
62         Layer0 L0 = Layer0.getInstance(graph);\r
63         DiagramResource DIA = DiagramResource.getInstance(graph);\r
64 \r
65         Resource entry = graph.newResource();\r
66         graph.claim(entry, L0.InstanceOf, null, DIA.GroupStyleProfileEntry);\r
67         graph.claimLiteral(entry, L0.HasName, name);\r
68         graph.claimLiteral(entry, L0.HasLabel, name);\r
69         graph.claim(entry, DIA.ProfileEntry_HasStyle, style);\r
70         graph.claim(entry, DIA.ProfileEntry_HasGroup, group);\r
71 \r
72         return entry;\r
73     }\r
74 \r
75     public static Resource createContainerProfile(WriteGraph graph, String name, Resource... children)\r
76             throws DatabaseException {\r
77         Layer0 L0 = Layer0.getInstance(graph);\r
78         DiagramResource DIA = DiagramResource.getInstance(graph);\r
79 \r
80         Resource profile = graph.newResource();\r
81         graph.claim(profile, L0.InstanceOf, null, DIA.Profile);\r
82         graph.claim(profile, L0.Abstract, profile);\r
83         graph.claimLiteral(profile, L0.HasName, name);\r
84         graph.claimLiteral(profile, L0.HasLabel, name);\r
85         ArrayList<Resource> cd = new ArrayList<Resource>();\r
86         Arrays.addAll(cd, children);\r
87         graph.claim(profile, DIA.HasEntries, null, ListUtils.create(graph, DIA.Profile, cd));\r
88 \r
89         return profile;\r
90     }\r
91     \r
92     public static Variable getMappedVariable(ReadGraph graph, Resource runtimeDiagram, Resource element) throws DatabaseException {\r
93         \r
94                 DiagramResource DIA = DiagramResource.getInstance(graph);\r
95                 ModelingResources MOD = ModelingResources.getInstance(graph);\r
96 \r
97                 String variableURI = graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasVariable, Bindings.STRING);\r
98                 Variable activeVariable = org.simantics.db.layer0.variable.Variables.getVariable(graph, variableURI);\r
99                 \r
100                 Resource module = graph.getPossibleObject(element, MOD.ElementToComponent);\r
101                 if (module == null)\r
102                         return null;\r
103 \r
104                 return activeVariable.browse(graph, module);\r
105         \r
106     }\r
107     \r
108     public static Resource[] getProfileEntries(ReadGraph graph, Resource profile) throws DatabaseException {\r
109                 DiagramResource DIA = DiagramResource.getInstance(graph);\r
110 \r
111                 Resource list = graph.getSingleObject(profile, DIA.HasEntries);\r
112                 List<Resource> entries = ListUtils.toList(graph, list);\r
113                 return entries.toArray(new Resource[entries.size()]);\r
114         }\r
115         \r
116         /**\r
117          * Copies profiles from an ontology definition to a model.\r
118          * \r
119          * @param graph\r
120          * @param model\r
121          * @param sourceProfile\r
122          * @throws DatabaseException\r
123          */\r
124         public static void addProfiles(WriteGraph graph, Resource model, Resource sourceProfile) throws DatabaseException {\r
125                 Layer0 L0 = Layer0.getInstance(graph);\r
126                 DiagramResource DIA = DiagramResource.getInstance(graph);\r
127                 Resource profile = null;\r
128 \r
129                 if (graph.isInstanceOf(sourceProfile, DIA.ConfigurableProfile))\r
130                         profile = copyProfiles(graph, model, sourceProfile);\r
131                 else\r
132                         profile = initProfiles(graph, model, sourceProfile);\r
133 \r
134                 // connect profile to model\r
135                 graph.claim(model, DIA.HasProfile, profile);\r
136                 graph.claim(model, L0.ConsistsOf, profile);\r
137                 graph.claim(model, DIA.HasActiveProfile, profile);\r
138 \r
139         }\r
140         \r
141         public static void addProfiles(WriteGraph graph, Resource model, String name, Resource... profileEntries) throws DatabaseException {\r
142                 Layer0 L0 = Layer0.getInstance(graph);\r
143                 DiagramResource DIA = DiagramResource.getInstance(graph);\r
144                 Resource profile = null;\r
145 \r
146                 profile = initProfiles(graph, model, name, profileEntries);\r
147 \r
148                 // connect profile to model\r
149                 graph.claim(model, DIA.HasProfile, profile);\r
150                 graph.claim(model, L0.ConsistsOf, profile);\r
151                 graph.claim(model, DIA.HasActiveProfile, profile);\r
152 \r
153         }\r
154 \r
155         private static Resource initProfiles(WriteGraph graph, Resource model, Resource profile) throws DatabaseException {\r
156                 Layer0 L0 = Layer0.getInstance(graph);\r
157 \r
158                 Resource entries[] = getProfileEntries(graph, profile);\r
159 \r
160                 String name = "Profile";\r
161                 String label = graph.getPossibleRelatedValue(profile, L0.HasLabel, Bindings.STRING);\r
162                 if (label != null && label.length() > 0)\r
163                         name = label;\r
164 \r
165                 return initProfiles(graph, model, name, entries);\r
166         }\r
167 \r
168         private static Resource initProfiles(WriteGraph graph, Resource model, String name, Resource entries[])\r
169                         throws DatabaseException {\r
170                 DiagramResource DIA = DiagramResource.getInstance(graph);\r
171                 for (int i = 0; i < entries.length; i++) {\r
172                         Resource entry = entries[i];\r
173                         if (graph.isInstanceOf(entry, DIA.ConfigurableProfile)) {\r
174                                 entries[i] = copyProfiles(graph, model, entry);\r
175                         }\r
176                 }\r
177 \r
178                 return createProfile2(graph, name, entries);\r
179         }\r
180 \r
181         private static Resource createProfile2(WriteGraph graph, String name, Resource[] entries) throws DatabaseException {\r
182                 DiagramResource DIA = DiagramResource.getInstance(graph);\r
183 \r
184                 Resource profile = Profiles.createProfile(graph, name, entries);\r
185 \r
186                 final List<Resource> enabled = new ArrayList<Resource>();\r
187                 for (Resource r : entries) {\r
188                         if (graph.hasStatement(r, DIA.Profile_defaultEnabled)) {\r
189                                 enabled.add(r);\r
190                         }\r
191                 }\r
192 \r
193                 if (enabled.size() > 0) {\r
194                         final Resource p = profile;\r
195                         VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);\r
196                         graph.syncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {\r
197 \r
198                                 @Override\r
199                                 public void perform(WriteGraph graph) throws DatabaseException {\r
200                                         SimulationResource SIM = SimulationResource.getInstance(graph);\r
201                                         for (Resource r : enabled) {\r
202                                                 graph.claim(p, SIM.IsActive, r);\r
203                                         }\r
204 \r
205                                 }\r
206                         });\r
207                 }\r
208 \r
209                 return profile;\r
210         }\r
211 \r
212         private static Resource copyProfiles(WriteGraph graph, Resource model, Resource profile) throws DatabaseException {\r
213                 Layer0 L0 = Layer0.getInstance(graph);\r
214                 DiagramResource DIA = DiagramResource.getInstance(graph);\r
215 \r
216                 Resource entries[] = getProfileEntries(graph, profile);\r
217 \r
218                 Map<Resource, Resource> groupMap = new HashMap<Resource, Resource>();\r
219                 for (int i = 0; i < entries.length; i++) {\r
220                         Resource entry = entries[i];\r
221                         if (graph.isInstanceOf(entry, DIA.Profile)) {\r
222                                 entries[i] = initProfiles(graph, model, entry);\r
223                         } else if (graph.isInstanceOf(entry, DIA.ConfigurableProfile)) {\r
224                                 entries[i] = copyProfiles(graph, model, entry);\r
225                         } else if (graph.isInstanceOf(entry, DIA.ProfileEntry)) {\r
226                                 entries[i] = copyProfileEntry(graph, entry, groupMap);\r
227                         }\r
228                 }\r
229                 String name = "Profile";\r
230                 String label = graph.getPossibleRelatedValue(profile, L0.HasLabel, Bindings.STRING);\r
231                 if (label != null && label.length() > 0)\r
232                         name = label;\r
233 \r
234                 return createProfile2(graph, name, entries);\r
235         }\r
236 \r
237         private static Resource copyProfileEntry(WriteGraph graph, Resource entry, Map<Resource, Resource> groupMap)\r
238                         throws DatabaseException {\r
239                 Layer0 L0 = Layer0.getInstance(graph);\r
240                 DiagramResource DIA = DiagramResource.getInstance(graph);\r
241 \r
242                 String name = graph.getPossibleRelatedValue(entry, L0.HasLabel, Bindings.STRING);\r
243                 Resource group = graph.getSingleObject(entry, DIA.ProfileEntry_HasGroup);\r
244                 Resource copyGroup = groupMap.get(group);\r
245                 if (copyGroup == null) {\r
246                         copyGroup = copyProfileGroup(graph, group);\r
247                         groupMap.put(group, copyGroup);\r
248                 }\r
249                 Resource style = graph.getSingleObject(entry, DIA.ProfileEntry_HasStyle);\r
250                 if (!graph.isInstanceOf(style, DIA.Style)) {\r
251                         style = createProfileStyle(graph, style);\r
252                 }\r
253                 Resource instance = Profiles.createEntry(graph, name, style, copyGroup);\r
254                 graph.claim(instance, L0.InstanceOf, entry);\r
255                 Double priority = graph.getPossibleRelatedValue(entry, DIA.ProfileEntry_HasPriority, Bindings.DOUBLE);\r
256                 if (priority != null) {\r
257                         graph.claimLiteral(instance, DIA.ProfileEntry_HasPriority, priority, Bindings.DOUBLE);\r
258                 }\r
259                 return instance;\r
260         }\r
261 \r
262         private static Resource copyProfileGroup(WriteGraph graph, Resource group) throws DatabaseException {\r
263                 Layer0 L0 = Layer0.getInstance(graph);\r
264                 DiagramResource DIA = DiagramResource.getInstance(graph);\r
265 \r
266                 Resource instance = graph.newResource();\r
267                 for (Resource type : graph.getObjects(group, L0.InstanceOf)) {\r
268                         graph.claim(instance, L0.InstanceOf, type);\r
269                 }\r
270                 Resource referredType = graph.getPossibleObject(group, DIA.TypeGroup_HasType);\r
271                 if (referredType != null)\r
272                         graph.claim(instance, DIA.TypeGroup_HasType, referredType);\r
273 \r
274                 return instance;\r
275         }\r
276 \r
277         private static Resource createProfileStyle(WriteGraph graph, Resource style) throws DatabaseException {\r
278                 Layer0 L0 = Layer0.getInstance(graph);\r
279 \r
280                 Resource instance = graph.newResource();\r
281                 graph.claim(instance, L0.InstanceOf, style);\r
282                 return instance;\r
283         }\r
284 \r
285 }\r