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
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
35 * @author Tuukka Lehtonen
\r
37 public class Profiles {
\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
44 ArrayList<Resource> ee = new ArrayList<Resource>();
\r
45 Arrays.addAll(ee, entries);
\r
46 Resource list = ListUtils.create(graph, DIA.Profile, ee);
\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
52 Resource profile = ListUtils.create(graph, DIA.Profile);
\r
54 graph.claimLiteral(profile, L0.HasName, profileName);
\r
55 graph.claim(profile, DIA.HasEntries, null, list);
\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
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
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
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
92 public static Variable getMappedVariable(ReadGraph graph, Resource runtimeDiagram, Resource element) throws DatabaseException {
\r
94 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
95 ModelingResources MOD = ModelingResources.getInstance(graph);
\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
100 Resource module = graph.getPossibleObject(element, MOD.ElementToComponent);
\r
101 if (module == null)
\r
104 return activeVariable.browse(graph, module);
\r
108 public static Resource[] getProfileEntries(ReadGraph graph, Resource profile) throws DatabaseException {
\r
109 DiagramResource DIA = DiagramResource.getInstance(graph);
\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
117 * Copies profiles from an ontology definition to a model.
\r
121 * @param sourceProfile
\r
122 * @throws DatabaseException
\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
129 if (graph.isInstanceOf(sourceProfile, DIA.ConfigurableProfile))
\r
130 profile = copyProfiles(graph, model, sourceProfile);
\r
132 profile = initProfiles(graph, model, sourceProfile);
\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
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
146 profile = initProfiles(graph, model, name, profileEntries);
\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
155 private static Resource initProfiles(WriteGraph graph, Resource model, Resource profile) throws DatabaseException {
\r
156 Layer0 L0 = Layer0.getInstance(graph);
\r
158 Resource entries[] = getProfileEntries(graph, profile);
\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
165 return initProfiles(graph, model, name, entries);
\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
178 return createProfile2(graph, name, entries);
\r
181 private static Resource createProfile2(WriteGraph graph, String name, Resource[] entries) throws DatabaseException {
\r
182 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
184 Resource profile = Profiles.createProfile(graph, name, entries);
\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
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
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
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
216 Resource entries[] = getProfileEntries(graph, profile);
\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
229 String name = "Profile";
\r
230 String label = graph.getPossibleRelatedValue(profile, L0.HasLabel, Bindings.STRING);
\r
231 if (label != null && label.length() > 0)
\r
234 return createProfile2(graph, name, entries);
\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
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
249 Resource style = graph.getSingleObject(entry, DIA.ProfileEntry_HasStyle);
\r
250 if (!graph.isInstanceOf(style, DIA.Style)) {
\r
251 style = createProfileStyle(graph, style);
\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
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
266 Resource instance = graph.newResource();
\r
267 for (Resource type : graph.getObjects(group, L0.InstanceOf)) {
\r
268 graph.claim(instance, L0.InstanceOf, type);
\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
277 private static Resource createProfileStyle(WriteGraph graph, Resource style) throws DatabaseException {
\r
278 Layer0 L0 = Layer0.getInstance(graph);
\r
280 Resource instance = graph.newResource();
\r
281 graph.claim(instance, L0.InstanceOf, style);
\r