1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 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.diagram.profile;
\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
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
46 * @author Tuukka Lehtonen
\r
48 public class Profiles {
\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
55 ArrayList<Resource> ee = new ArrayList<Resource>();
\r
56 Arrays.addAll(ee, entries);
\r
57 Resource list = ListUtils.create(graph, DIA.Profile, ee);
\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
63 Resource profile = ListUtils.create(graph, DIA.Profile);
\r
65 graph.claimLiteral(profile, L0.HasName, profileName);
\r
66 graph.claim(profile, DIA.HasEntries, null, list);
\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
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
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
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
103 public static Variable getMappedVariable(ReadGraph graph, Resource runtimeDiagram, Resource element) throws DatabaseException {
\r
105 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
106 ModelingResources MOD = ModelingResources.getInstance(graph);
\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
111 Resource module = graph.getPossibleObject(element, MOD.ElementToComponent);
\r
112 if (module == null)
\r
115 return activeVariable.browse(graph, module);
\r
119 public static Resource[] getProfileEntries(ReadGraph graph, Resource profile) throws DatabaseException {
\r
120 DiagramResource DIA = DiagramResource.getInstance(graph);
\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
128 * Copies profiles from an ontology definition to a model.
\r
132 * @param sourceProfile
\r
133 * @throws DatabaseException
\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
140 if (graph.isInstanceOf(sourceProfile, DIA.ConfigurableProfile))
\r
141 profile = copyProfiles(graph, model, sourceProfile);
\r
143 profile = initProfiles(graph, model, sourceProfile);
\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
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
157 profile = initProfiles(graph, model, name, profileEntries);
\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
166 private static Resource initProfiles(WriteGraph graph, Resource model, Resource profile) throws DatabaseException {
\r
167 Layer0 L0 = Layer0.getInstance(graph);
\r
169 Resource entries[] = getProfileEntries(graph, profile);
\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
176 return initProfiles(graph, model, name, entries);
\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
189 return createProfile2(graph, name, entries);
\r
192 private static Resource createProfile2(WriteGraph graph, String name, Resource[] entries) throws DatabaseException {
\r
193 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
195 Resource profile = Profiles.createProfile(graph, name, entries);
\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
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
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
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
226 Resource entries[] = getProfileEntries(graph, profile);
\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
239 String name = "Profile";
\r
240 String label = graph.getPossibleRelatedValue(profile, L0.HasLabel, Bindings.STRING);
\r
241 if (label != null && label.length() > 0)
\r
244 return createProfile2(graph, name, entries);
\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
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
259 Resource style = graph.getSingleObject(entry, DIA.ProfileEntry_HasStyle);
\r
260 if (!graph.isInstanceOf(style, DIA.Style)) {
\r
261 style = createProfileStyle(graph, style);
\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
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
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
279 if(resource == null) resource = graph.newResource();
\r
281 graph.claim(library, DIA.HasTemplate, DIA.HasTemplate_Inverse, resource);
\r
283 String newName = getName(root);
\r
284 graph.addLiteral(resource, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);
\r
286 addRootInfo(root, newName, resource);
\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
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
305 Resource instance = graph.newResource();
\r
306 for (Resource type : graph.getObjects(group, L0.InstanceOf)) {
\r
307 graph.claim(instance, L0.InstanceOf, type);
\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
316 private static Resource createProfileStyle(WriteGraph graph, Resource style) throws DatabaseException {
\r
317 Layer0 L0 = Layer0.getInstance(graph);
\r
319 Resource instance = graph.newResource();
\r
320 graph.claim(instance, L0.InstanceOf, style);
\r