--- /dev/null
+package org.simantics.diagram.profile.function;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Set;\r
+import java.util.TreeMap;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Event;\r
+import org.eclipse.swt.widgets.Tree;\r
+import org.eclipse.swt.widgets.TreeItem;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.browsing.ui.NodeContext;\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.adapter.Instances;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.service.VirtualGraphSupport;\r
+import org.simantics.diagram.profile.view.ResourcePair;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;\r
+import org.simantics.scenegraph.profile.ProfileUtils;\r
+import org.simantics.scl.reflection.annotations.SCLValue;\r
+import org.simantics.scl.runtime.function.FunctionImpl1;\r
+import org.simantics.scl.runtime.tuple.Tuple;\r
+import org.simantics.scl.runtime.tuple.Tuple2;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.utils.strings.StringUtils;\r
+\r
+public class All {\r
+\r
+ @SCLValue(type = "ReadGraph -> Resource -> Resource -> [Resource]")\r
+ public static List<Resource> profileChildren(ReadGraph graph, Resource resource, Resource context) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ List<Resource> listedChildren = ListUtils.toList(graph, context);\r
+ if(listedChildren.isEmpty()) {\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ TreeMap<Double,Resource> priorityChildren = new TreeMap<Double,Resource>();\r
+ for(Resource child : graph.getObjects(context, L0.IsComposedOf)) {\r
+ Double p = graph.getPossibleRelatedValue(child, DIA.Profile_priority, Bindings.DOUBLE);\r
+ if(p != null)\r
+ priorityChildren.put(p, child);\r
+ }\r
+ return new ArrayList<Resource>(priorityChildren.values());\r
+ } else {\r
+ return listedChildren;\r
+ }\r
+ }\r
+\r
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> [(String, Resource)]")\r
+ public static List<Tuple> availableProfiles(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {\r
+ \r
+ Resource runtimeDiagram = ScenegraphLoaderUtils.getPossibleResourceSelection(graph, context);\r
+ if(runtimeDiagram == null) return Collections.emptyList();\r
+\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+ String modelURI = graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasModelURI);\r
+ if (modelURI == null)\r
+ return Collections.emptyList();\r
+\r
+ Resource model = graph.getPossibleResource(modelURI);\r
+ if (model == null)\r
+ return Collections.emptyList();\r
+\r
+ Instances query = graph.adapt(DIA.Profile, Instances.class);\r
+\r
+ ArrayList<Tuple> result = new ArrayList<Tuple>();\r
+ for(Resource profile : query.find(graph, model)) {\r
+ if(!graph.hasStatement(profile, L0.Abstract)) {\r
+ String name = graph.getRelatedValue(profile, L0.HasName, Bindings.STRING);\r
+ result.add(new Tuple2(name, profile));\r
+ }\r
+ }\r
+ return result;\r
+ \r
+ }\r
+\r
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
+ public static Object profileEntrySelected(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {\r
+ \r
+ return new FunctionImpl1<Object, Boolean>() {\r
+\r
+ public void processRecursively(WriteGraph graph, Resource runtimeProfile, Resource entry, boolean checked) throws DatabaseException {\r
+\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ \r
+ if(graph.isInstanceOf(entry, DIA.Profile)) {\r
+ Set<Resource> singleSelGroups = new HashSet<Resource>();\r
+ \r
+ for(Resource child : ProfileUtils.getProfileChildren(graph, entry)) {\r
+ if(checked && graph.isInstanceOf(child, DIA.ProfileEntry)) {\r
+ //enable only one item from single selection groups.\r
+ Resource group = graph.getPossibleObject(child, DIA.ProfileEntry_HasGroup);\r
+ if (group != null && graph.isInstanceOf(group, DIA.SingleSelectionGroup)) {\r
+ if (singleSelGroups.contains(group))\r
+ continue;\r
+ singleSelGroups.add(group);\r
+ }\r
+ }\r
+ processRecursively(graph, runtimeProfile, child, checked);\r
+ }\r
+ \r
+ } else if(graph.isInstanceOf(entry, DIA.ProfileEntry)) {\r
+ Resource group = graph.getPossibleObject(entry, DIA.ProfileEntry_HasGroup);\r
+ if (group != null && graph.isInstanceOf(group, DIA.SingleSelectionGroup)) {\r
+ if(checked) {\r
+ //enable selected item from single selection groups, disable the rest.\r
+ Collection<Resource> entries = graph.getObjects(group, DIA.ProfileEntry_HasGroup_Inverse);\r
+ for (Resource e : entries) {\r
+ graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, e);\r
+ }\r
+ graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, entry);\r
+ } else {\r
+ graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, entry);\r
+ }\r
+ } else {\r
+ if(checked) {\r
+ graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, entry);\r
+ } else {\r
+ graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, entry);\r
+ }\r
+ }\r
+\r
+ }\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public Boolean apply(Object _event) {\r
+ \r
+ Event event = (Event)_event;\r
+ \r
+ if(event.detail == SWT.CHECK) {\r
+ \r
+ final TreeItem item = (TreeItem)event.item;\r
+ Tree tree = item.getParent();\r
+ GraphExplorer explorer = (GraphExplorer)tree.getData("GraphExplorer");\r
+ final Resource runtimeDiagram = (Resource)explorer.getRoot().getConstant(BuiltinKeys.INPUT);\r
+ final boolean checked = item.getChecked();\r
+ NodeContext context = (NodeContext)item.getData();\r
+ final ResourcePair entry = (ResourcePair)context.getConstant(BuiltinKeys.INPUT);\r
+ try {\r
+ \r
+ VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);\r
+ Simantics.getSession().syncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ \r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ Resource runtimeProfile = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);\r
+ processRecursively(graph, runtimeProfile, entry.getSecond(), checked);\r
+ \r
+ }\r
+\r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ return null;\r
+ \r
+ }\r
+ \r
+ };\r
+ \r
+ }\r
+ \r
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
+ public static Object selectedProfile(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {\r
+ \r
+ Resource runtimeDiagram = ScenegraphLoaderUtils.getResourceSelection(graph, context);\r
+ if(runtimeDiagram == null) return "";\r
+\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ Resource profile = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);\r
+ if(profile == null) return null;\r
+ String name = graph.getPossibleRelatedValue(profile, L0.HasName, Bindings.STRING);\r
+ return StringUtils.safeString(name);\r
+\r
+ }\r
+\r
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")\r
+ public static Object activeProfileModifier(ReadGraph graph, final Resource resource, final Variable context) throws DatabaseException {\r
+ \r
+ return new FunctionImpl1<String, String>() {\r
+\r
+ @Override\r
+ public String apply(final String key) {\r
+\r
+ VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);\r
+ Simantics.getSession().asyncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {\r
+\r
+ public Resource selected(List<Tuple> avail, String text) {\r
+ for(Tuple a : avail) if(a.get(0).equals(text)) return (Resource)a.get(1);\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+\r
+ Resource runtimeDiagram = ScenegraphLoaderUtils.getResourceSelection(graph, context);\r
+\r
+ List<Tuple> avail = (List<Tuple>)availableProfiles(graph, resource, context);\r
+\r
+ final Resource profile = selected(avail, key);\r
+\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ Resource current = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);\r
+ if(profile.equals(current)) return;\r
+\r
+ graph.deny(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);\r
+ graph.claim(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile, null, profile);\r
+\r
+ // Set this profile as the default profile for this model\r
+ String modelURI = graph.getRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasModelURI);\r
+ Resource model = graph.getResource(modelURI);\r
+ graph.deny(model, DIA.HasActiveProfile);\r
+ graph.claim(model, DIA.HasActiveProfile, profile);\r
+\r
+ // Set this profile as the default profile for this diagram\r
+ Resource configuration = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);\r
+ graph.deny(configuration, DIA.HasActiveProfile);\r
+ graph.claim(configuration, DIA.HasActiveProfile, profile);\r
+\r
+ }\r
+\r
+ });\r
+ \r
+ return null;\r
+\r
+ }\r
+ \r
+ };\r
+ \r
+ }\r
+\r
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> Resource")\r
+ public static Resource singleResourceSelection(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {\r
+ return ScenegraphLoaderUtils.getPossibleResourceSelection(graph, context);\r
+ }\r
+ \r
+}\r