1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.profile;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.databoard.binding.mutable.Variant;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.RequestProcessor;
24 import org.simantics.db.Resource;
25 import org.simantics.db.VirtualGraph;
26 import org.simantics.db.WriteGraph;
27 import org.simantics.db.WriteOnlyGraph;
28 import org.simantics.db.common.request.WriteRequest;
29 import org.simantics.db.common.utils.ListUtils;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.layer0.adapter.impl.DefaultCopyHandler;
32 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
33 import org.simantics.db.layer0.util.ClipboardUtils;
34 import org.simantics.db.layer0.util.SimanticsClipboard;
35 import org.simantics.db.layer0.util.SimanticsClipboardImpl;
36 import org.simantics.db.layer0.util.SimanticsKeys;
37 import org.simantics.db.layer0.variable.Variable;
38 import org.simantics.db.service.VirtualGraphSupport;
39 import org.simantics.diagram.profile.ProfileActivityBean.Profile;
40 import org.simantics.diagram.stubs.DiagramResource;
41 import org.simantics.graph.db.TransferableGraphs;
42 import org.simantics.graph.representation.Root;
43 import org.simantics.graph.representation.TransferableGraph1;
44 import org.simantics.layer0.Layer0;
45 import org.simantics.modeling.ModelingResources;
46 import org.simantics.simulation.ontology.SimulationResource;
47 import org.simantics.utils.datastructures.Arrays;
50 * @author Tuukka Lehtonen
52 public class Profiles {
54 public static Resource createProfile(WriteGraph graph, String profileName, Resource... entries)
55 throws DatabaseException {
56 Layer0 L0 = Layer0.getInstance(graph);
57 DiagramResource DIA = DiagramResource.getInstance(graph);
59 ArrayList<Resource> ee = new ArrayList<Resource>();
60 Arrays.addAll(ee, entries);
61 Resource list = ListUtils.create(graph, DIA.Profile, ee);
63 // TODO : DIA.Profile is a list, but why it is used in the container?
64 //Resource profile = graph.newResource();
65 //graph.claim(profile, L0.InstanceOf, null, DIA.Profile);
67 Resource profile = ListUtils.create(graph, DIA.Profile);
69 graph.claimLiteral(profile, L0.HasName, profileName);
70 graph.claim(profile, DIA.HasEntries, null, list);
75 public static Resource createEntry(WriteGraph graph, String name, Resource style, Resource group)
76 throws DatabaseException {
77 Layer0 L0 = Layer0.getInstance(graph);
78 DiagramResource DIA = DiagramResource.getInstance(graph);
80 Resource entry = graph.newResource();
81 graph.claim(entry, L0.InstanceOf, null, DIA.GroupStyleProfileEntry);
82 graph.claimLiteral(entry, L0.HasName, name);
83 graph.claimLiteral(entry, L0.HasLabel, name);
84 graph.claim(entry, DIA.ProfileEntry_HasStyle, style);
85 graph.claim(entry, DIA.ProfileEntry_HasGroup, group);
90 public static Resource createContainerProfile(WriteGraph graph, String name, Resource... children)
91 throws DatabaseException {
92 Layer0 L0 = Layer0.getInstance(graph);
93 DiagramResource DIA = DiagramResource.getInstance(graph);
95 Resource profile = graph.newResource();
96 graph.claim(profile, L0.InstanceOf, null, DIA.Profile);
97 graph.claim(profile, L0.Abstract, profile);
98 graph.claimLiteral(profile, L0.HasName, name);
99 graph.claimLiteral(profile, L0.HasLabel, name);
100 ArrayList<Resource> cd = new ArrayList<Resource>();
101 Arrays.addAll(cd, children);
102 graph.claim(profile, DIA.HasEntries, null, ListUtils.create(graph, DIA.Profile, cd));
107 public static Variable getMappedVariable(ReadGraph graph, Resource runtimeDiagram, Resource element) throws DatabaseException {
109 DiagramResource DIA = DiagramResource.getInstance(graph);
110 ModelingResources MOD = ModelingResources.getInstance(graph);
112 String variableURI = graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasVariable, Bindings.STRING);
113 Variable activeVariable = org.simantics.db.layer0.variable.Variables.getVariable(graph, variableURI);
115 Resource module = graph.getPossibleObject(element, MOD.ElementToComponent);
119 return activeVariable.browse(graph, module);
123 public static Resource[] getProfileEntries(ReadGraph graph, Resource profile) throws DatabaseException {
124 DiagramResource DIA = DiagramResource.getInstance(graph);
126 Resource list = graph.getSingleObject(profile, DIA.HasEntries);
127 List<Resource> entries = ListUtils.toList(graph, list);
128 return entries.toArray(new Resource[entries.size()]);
132 * Copies profiles from an ontology definition to a model.
136 * @param sourceProfile
137 * @throws DatabaseException
139 public static void addProfiles(WriteGraph graph, Resource model, Resource sourceProfile) throws DatabaseException {
140 Layer0 L0 = Layer0.getInstance(graph);
141 DiagramResource DIA = DiagramResource.getInstance(graph);
142 Resource profile = null;
144 if (graph.isInstanceOf(sourceProfile, DIA.ConfigurableProfile))
145 profile = copyProfiles(graph, model, sourceProfile);
147 profile = initProfiles(graph, model, sourceProfile);
149 // connect profile to model
150 graph.claim(model, DIA.HasProfile, profile);
151 graph.claim(model, L0.ConsistsOf, profile);
152 graph.claim(model, DIA.HasActiveProfile, profile);
156 public static void addProfiles(WriteGraph graph, Resource model, String name, Resource... profileEntries) throws DatabaseException {
157 Layer0 L0 = Layer0.getInstance(graph);
158 DiagramResource DIA = DiagramResource.getInstance(graph);
159 Resource profile = null;
161 profile = initProfiles(graph, model, name, profileEntries);
163 // connect profile to model
164 graph.claim(model, DIA.HasProfile, profile);
165 graph.claim(model, L0.ConsistsOf, profile);
166 graph.claim(model, DIA.HasActiveProfile, profile);
170 private static Resource initProfiles(WriteGraph graph, Resource model, Resource profile) throws DatabaseException {
171 Layer0 L0 = Layer0.getInstance(graph);
173 Resource entries[] = getProfileEntries(graph, profile);
175 String name = "Profile";
176 String label = graph.getPossibleRelatedValue(profile, L0.HasLabel, Bindings.STRING);
177 if (label != null && label.length() > 0)
180 return initProfiles(graph, model, name, entries);
183 private static Resource initProfiles(WriteGraph graph, Resource model, String name, Resource entries[])
184 throws DatabaseException {
185 DiagramResource DIA = DiagramResource.getInstance(graph);
186 for (int i = 0; i < entries.length; i++) {
187 Resource entry = entries[i];
188 if (graph.isInstanceOf(entry, DIA.ConfigurableProfile)) {
189 entries[i] = copyProfiles(graph, model, entry);
193 return createProfile2(graph, name, entries);
196 private static Resource createProfile2(WriteGraph graph, String name, Resource[] entries) throws DatabaseException {
197 DiagramResource DIA = DiagramResource.getInstance(graph);
199 Resource profile = Profiles.createProfile(graph, name, entries);
201 final List<Resource> enabled = new ArrayList<Resource>();
202 for (Resource r : entries) {
203 if (graph.hasStatement(r, DIA.Profile_defaultEnabled)) {
208 if (enabled.size() > 0) {
209 final Resource p = profile;
210 VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);
211 graph.syncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {
214 public void perform(WriteGraph graph) throws DatabaseException {
215 SimulationResource SIM = SimulationResource.getInstance(graph);
216 for (Resource r : enabled) {
217 graph.claim(p, SIM.IsActive, r);
226 private static Resource copyProfiles(WriteGraph graph, Resource model, Resource profile) throws DatabaseException {
227 Layer0 L0 = Layer0.getInstance(graph);
228 DiagramResource DIA = DiagramResource.getInstance(graph);
230 Resource entries[] = getProfileEntries(graph, profile);
232 Map<Resource, Resource> groupMap = new HashMap<Resource, Resource>();
233 for (int i = 0; i < entries.length; i++) {
234 Resource entry = entries[i];
235 if (graph.isInstanceOf(entry, DIA.Profile)) {
236 entries[i] = initProfiles(graph, model, entry);
237 } else if (graph.isInstanceOf(entry, DIA.ConfigurableProfile)) {
238 entries[i] = copyProfiles(graph, model, entry);
239 } else if (graph.isInstanceOf(entry, DIA.ProfileEntry)) {
240 entries[i] = copyProfileEntry(graph, entry, groupMap);
243 String name = "Profile";
244 String label = graph.getPossibleRelatedValue(profile, L0.HasLabel, Bindings.STRING);
245 if (label != null && label.length() > 0)
248 return createProfile2(graph, name, entries);
251 private static Resource copyProfileEntry(WriteGraph graph, Resource entry, Map<Resource, Resource> groupMap)
252 throws DatabaseException {
253 Layer0 L0 = Layer0.getInstance(graph);
254 DiagramResource DIA = DiagramResource.getInstance(graph);
256 String name = graph.getPossibleRelatedValue(entry, L0.HasLabel, Bindings.STRING);
257 Resource group = graph.getSingleObject(entry, DIA.ProfileEntry_HasGroup);
258 Resource copyGroup = groupMap.get(group);
259 if (copyGroup == null) {
260 copyGroup = copyProfileGroup(graph, group);
261 groupMap.put(group, copyGroup);
263 Resource style = graph.getSingleObject(entry, DIA.ProfileEntry_HasStyle);
264 if (!graph.isInstanceOf(style, DIA.Style)) {
265 style = createProfileStyle(graph, style);
267 Resource instance = Profiles.createEntry(graph, name, style, copyGroup);
268 graph.claim(instance, L0.InstanceOf, entry);
269 Double priority = graph.getPossibleRelatedValue(entry, DIA.ProfileEntry_HasPriority, Bindings.DOUBLE);
270 if (priority != null) {
271 graph.claimLiteral(instance, DIA.ProfileEntry_HasPriority, priority, Bindings.DOUBLE);
273 for (Resource template : graph.getObjects(entry, DIA.HasTemplate)) {
274 SimanticsClipboardImpl builder = new SimanticsClipboardImpl();
275 DefaultCopyHandler handler = new DefaultCopyHandler(template);
276 DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(instance) {
278 public Resource createRoot(WriteOnlyGraph graph, Root root, Resource resource)
279 throws DatabaseException {
280 Layer0 l0 = graph.getService(Layer0.class);
281 DiagramResource DIA = graph.getService(DiagramResource.class);
283 if(resource == null) resource = graph.newResource();
285 graph.claim(library, DIA.HasTemplate, DIA.HasTemplate_Inverse, resource);
287 String newName = getName(root);
288 graph.addLiteral(resource, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);
290 addRootInfo(root, newName, resource);
295 handler.copyToClipboard(graph, builder);
296 for(Set<SimanticsClipboard.Representation> object : builder.getContents()) {
297 TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
298 TransferableGraphs.importGraph1(graph, tg, advisor);
305 private static Resource copyProfileGroup(WriteGraph graph, Resource group) throws DatabaseException {
306 Layer0 L0 = Layer0.getInstance(graph);
307 DiagramResource DIA = DiagramResource.getInstance(graph);
309 Resource instance = graph.newResource();
310 for (Resource type : graph.getObjects(group, L0.InstanceOf)) {
311 graph.claim(instance, L0.InstanceOf, type);
313 Resource referredType = graph.getPossibleObject(group, DIA.TypeGroup_HasType);
314 if (referredType != null)
315 graph.claim(instance, DIA.TypeGroup_HasType, referredType);
320 private static Resource createProfileStyle(WriteGraph graph, Resource style) throws DatabaseException {
321 Layer0 L0 = Layer0.getInstance(graph);
323 Resource instance = graph.newResource();
324 graph.claim(instance, L0.InstanceOf, style);
328 public static ProfileActivityBean readProfileActivity(
329 RequestProcessor processor,
331 throws DatabaseException
333 return processor.syncRequest(new ProfileActivityBeanRequest(root));
336 public static ProfileActivityBean readProfileActivity(
337 RequestProcessor processor,
339 Map<String, Variant> writeToMap)
340 throws DatabaseException
342 ProfileActivityBean pab = readProfileActivity(processor, root);
343 if (pab != null && writeToMap != null)
344 writeToMap.put(ProfileActivityBean.EXTENSION_KEY, new Variant(ProfileActivityBean.BINDING, pab));
348 public static void writeProfileActivity(
349 RequestProcessor processor,
351 ProfileActivityBean bean)
352 throws DatabaseException
354 VirtualGraph vg = processor.getService(VirtualGraphSupport.class).getWorkspacePersistent("profiles");
355 processor.syncRequest(new WriteRequest(vg) {
357 public void perform(WriteGraph graph) throws DatabaseException {
358 writeProfileActivity(graph, root, bean);
363 private static void writeProfileActivity(
366 ProfileActivityBean bean)
367 throws DatabaseException
369 String rootUri = graph.getPossibleURI(root);
373 SimulationResource SIMU = SimulationResource.getInstance(graph);
374 for (Profile p : bean.topLevelProfiles.values()) {
375 Resource pr = resolveRelativeUri(graph, rootUri, p.relativeUri);
378 for (String active : p.activeEntries) {
379 Resource ar = resolveRelativeUri(graph, rootUri, active);
381 graph.claim(pr, SIMU.IsActive, ar);
385 Resource activeProfile = resolveRelativeUri(graph, rootUri, bean.activeProfile);
386 if (activeProfile != null) {
387 DiagramResource DIA = DiagramResource.getInstance(graph);
388 graph.claim(root, DIA.HasActiveProfile, DIA.HasActiveProfile_Inverse, activeProfile);
392 static String possiblyRelativeUri(ReadGraph graph, String rootUri, Resource r) throws DatabaseException {
395 String uri = graph.getPossibleURI(r);
396 if (rootUri != null && uri != null && uri.startsWith(rootUri))
397 return uri.substring(rootUri.length());
401 static Resource resolveRelativeUri(ReadGraph graph, String rootUri, String possiblyRelativeUri) throws DatabaseException {
402 return possiblyRelativeUri != null
403 ? graph.getPossibleResource( resolveRelativeUri(rootUri, possiblyRelativeUri) )
407 private static String resolveRelativeUri(String rootUri, String possiblyRelativeUri) {
408 return possiblyRelativeUri.startsWith("http:/")
409 ? possiblyRelativeUri
410 : rootUri + possiblyRelativeUri;