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