--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.profile;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.ListUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.service.VirtualGraphSupport;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.utils.datastructures.Arrays;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class Profiles {\r
+\r
+ public static Resource createProfile(WriteGraph graph, String profileName, Resource... entries)\r
+ throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+ ArrayList<Resource> ee = new ArrayList<Resource>();\r
+ Arrays.addAll(ee, entries);\r
+ Resource list = ListUtils.create(graph, DIA.Profile, ee);\r
+\r
+ // TODO : DIA.Profile is a list, but why it is used in the container?\r
+ //Resource profile = graph.newResource();\r
+ //graph.claim(profile, L0.InstanceOf, null, DIA.Profile);\r
+ \r
+ Resource profile = ListUtils.create(graph, DIA.Profile);\r
+ \r
+ graph.claimLiteral(profile, L0.HasName, profileName);\r
+ graph.claim(profile, DIA.HasEntries, null, list);\r
+\r
+ return profile;\r
+ }\r
+\r
+ public static Resource createEntry(WriteGraph graph, String name, Resource style, Resource group)\r
+ throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+ Resource entry = graph.newResource();\r
+ graph.claim(entry, L0.InstanceOf, null, DIA.GroupStyleProfileEntry);\r
+ graph.claimLiteral(entry, L0.HasName, name);\r
+ graph.claimLiteral(entry, L0.HasLabel, name);\r
+ graph.claim(entry, DIA.ProfileEntry_HasStyle, style);\r
+ graph.claim(entry, DIA.ProfileEntry_HasGroup, group);\r
+\r
+ return entry;\r
+ }\r
+\r
+ public static Resource createContainerProfile(WriteGraph graph, String name, Resource... children)\r
+ throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+ Resource profile = graph.newResource();\r
+ graph.claim(profile, L0.InstanceOf, null, DIA.Profile);\r
+ graph.claim(profile, L0.Abstract, profile);\r
+ graph.claimLiteral(profile, L0.HasName, name);\r
+ graph.claimLiteral(profile, L0.HasLabel, name);\r
+ ArrayList<Resource> cd = new ArrayList<Resource>();\r
+ Arrays.addAll(cd, children);\r
+ graph.claim(profile, DIA.HasEntries, null, ListUtils.create(graph, DIA.Profile, cd));\r
+\r
+ return profile;\r
+ }\r
+ \r
+ public static Variable getMappedVariable(ReadGraph graph, Resource runtimeDiagram, Resource element) throws DatabaseException {\r
+ \r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ ModelingResources MOD = ModelingResources.getInstance(graph);\r
+\r
+ String variableURI = graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasVariable, Bindings.STRING);\r
+ Variable activeVariable = org.simantics.db.layer0.variable.Variables.getVariable(graph, variableURI);\r
+ \r
+ Resource module = graph.getPossibleObject(element, MOD.ElementToComponent);\r
+ if (module == null)\r
+ return null;\r
+\r
+ return activeVariable.browse(graph, module);\r
+ \r
+ }\r
+ \r
+ public static Resource[] getProfileEntries(ReadGraph graph, Resource profile) throws DatabaseException {\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+ Resource list = graph.getSingleObject(profile, DIA.HasEntries);\r
+ List<Resource> entries = ListUtils.toList(graph, list);\r
+ return entries.toArray(new Resource[entries.size()]);\r
+ }\r
+ \r
+ /**\r
+ * Copies profiles from an ontology definition to a model.\r
+ * \r
+ * @param graph\r
+ * @param model\r
+ * @param sourceProfile\r
+ * @throws DatabaseException\r
+ */\r
+ public static void addProfiles(WriteGraph graph, Resource model, Resource sourceProfile) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ Resource profile = null;\r
+\r
+ if (graph.isInstanceOf(sourceProfile, DIA.ConfigurableProfile))\r
+ profile = copyProfiles(graph, model, sourceProfile);\r
+ else\r
+ profile = initProfiles(graph, model, sourceProfile);\r
+\r
+ // connect profile to model\r
+ graph.claim(model, DIA.HasProfile, profile);\r
+ graph.claim(model, L0.ConsistsOf, profile);\r
+ graph.claim(model, DIA.HasActiveProfile, profile);\r
+\r
+ }\r
+ \r
+ public static void addProfiles(WriteGraph graph, Resource model, String name, Resource... profileEntries) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ Resource profile = null;\r
+\r
+ profile = initProfiles(graph, model, name, profileEntries);\r
+\r
+ // connect profile to model\r
+ graph.claim(model, DIA.HasProfile, profile);\r
+ graph.claim(model, L0.ConsistsOf, profile);\r
+ graph.claim(model, DIA.HasActiveProfile, profile);\r
+\r
+ }\r
+\r
+ private static Resource initProfiles(WriteGraph graph, Resource model, Resource profile) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+\r
+ Resource entries[] = getProfileEntries(graph, profile);\r
+\r
+ String name = "Profile";\r
+ String label = graph.getPossibleRelatedValue(profile, L0.HasLabel, Bindings.STRING);\r
+ if (label != null && label.length() > 0)\r
+ name = label;\r
+\r
+ return initProfiles(graph, model, name, entries);\r
+ }\r
+\r
+ private static Resource initProfiles(WriteGraph graph, Resource model, String name, Resource entries[])\r
+ throws DatabaseException {\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ for (int i = 0; i < entries.length; i++) {\r
+ Resource entry = entries[i];\r
+ if (graph.isInstanceOf(entry, DIA.ConfigurableProfile)) {\r
+ entries[i] = copyProfiles(graph, model, entry);\r
+ }\r
+ }\r
+\r
+ return createProfile2(graph, name, entries);\r
+ }\r
+\r
+ private static Resource createProfile2(WriteGraph graph, String name, Resource[] entries) throws DatabaseException {\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+ Resource profile = Profiles.createProfile(graph, name, entries);\r
+\r
+ final List<Resource> enabled = new ArrayList<Resource>();\r
+ for (Resource r : entries) {\r
+ if (graph.hasStatement(r, DIA.Profile_defaultEnabled)) {\r
+ enabled.add(r);\r
+ }\r
+ }\r
+\r
+ if (enabled.size() > 0) {\r
+ final Resource p = profile;\r
+ VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);\r
+ graph.syncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ SimulationResource SIM = SimulationResource.getInstance(graph);\r
+ for (Resource r : enabled) {\r
+ graph.claim(p, SIM.IsActive, r);\r
+ }\r
+\r
+ }\r
+ });\r
+ }\r
+\r
+ return profile;\r
+ }\r
+\r
+ private static Resource copyProfiles(WriteGraph graph, Resource model, Resource profile) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+ Resource entries[] = getProfileEntries(graph, profile);\r
+\r
+ Map<Resource, Resource> groupMap = new HashMap<Resource, Resource>();\r
+ for (int i = 0; i < entries.length; i++) {\r
+ Resource entry = entries[i];\r
+ if (graph.isInstanceOf(entry, DIA.Profile)) {\r
+ entries[i] = initProfiles(graph, model, entry);\r
+ } else if (graph.isInstanceOf(entry, DIA.ConfigurableProfile)) {\r
+ entries[i] = copyProfiles(graph, model, entry);\r
+ } else if (graph.isInstanceOf(entry, DIA.ProfileEntry)) {\r
+ entries[i] = copyProfileEntry(graph, entry, groupMap);\r
+ }\r
+ }\r
+ String name = "Profile";\r
+ String label = graph.getPossibleRelatedValue(profile, L0.HasLabel, Bindings.STRING);\r
+ if (label != null && label.length() > 0)\r
+ name = label;\r
+\r
+ return createProfile2(graph, name, entries);\r
+ }\r
+\r
+ private static Resource copyProfileEntry(WriteGraph graph, Resource entry, Map<Resource, Resource> groupMap)\r
+ throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+ String name = graph.getPossibleRelatedValue(entry, L0.HasLabel, Bindings.STRING);\r
+ Resource group = graph.getSingleObject(entry, DIA.ProfileEntry_HasGroup);\r
+ Resource copyGroup = groupMap.get(group);\r
+ if (copyGroup == null) {\r
+ copyGroup = copyProfileGroup(graph, group);\r
+ groupMap.put(group, copyGroup);\r
+ }\r
+ Resource style = graph.getSingleObject(entry, DIA.ProfileEntry_HasStyle);\r
+ if (!graph.isInstanceOf(style, DIA.Style)) {\r
+ style = createProfileStyle(graph, style);\r
+ }\r
+ Resource instance = Profiles.createEntry(graph, name, style, copyGroup);\r
+ graph.claim(instance, L0.InstanceOf, entry);\r
+ Double priority = graph.getPossibleRelatedValue(entry, DIA.ProfileEntry_HasPriority, Bindings.DOUBLE);\r
+ if (priority != null) {\r
+ graph.claimLiteral(instance, DIA.ProfileEntry_HasPriority, priority, Bindings.DOUBLE);\r
+ }\r
+ return instance;\r
+ }\r
+\r
+ private static Resource copyProfileGroup(WriteGraph graph, Resource group) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+ Resource instance = graph.newResource();\r
+ for (Resource type : graph.getObjects(group, L0.InstanceOf)) {\r
+ graph.claim(instance, L0.InstanceOf, type);\r
+ }\r
+ Resource referredType = graph.getPossibleObject(group, DIA.TypeGroup_HasType);\r
+ if (referredType != null)\r
+ graph.claim(instance, DIA.TypeGroup_HasType, referredType);\r
+\r
+ return instance;\r
+ }\r
+\r
+ private static Resource createProfileStyle(WriteGraph graph, Resource style) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+\r
+ Resource instance = graph.newResource();\r
+ graph.claim(instance, L0.InstanceOf, style);\r
+ return instance;\r
+ }\r
+\r
+}\r