1 package org.simantics.diagram.profile.function;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
9 import java.util.TreeMap;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.widgets.Event;
13 import org.eclipse.swt.widgets.Tree;
14 import org.eclipse.swt.widgets.TreeItem;
15 import org.simantics.Simantics;
16 import org.simantics.browsing.ui.BuiltinKeys;
17 import org.simantics.browsing.ui.GraphExplorer;
18 import org.simantics.browsing.ui.NodeContext;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.common.utils.ListUtils;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.layer0.adapter.Instances;
27 import org.simantics.db.layer0.variable.Variable;
28 import org.simantics.db.service.VirtualGraphSupport;
29 import org.simantics.diagram.profile.view.ResourcePair;
30 import org.simantics.diagram.stubs.DiagramResource;
31 import org.simantics.layer0.Layer0;
32 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;
33 import org.simantics.scenegraph.profile.ProfileUtils;
34 import org.simantics.scl.reflection.annotations.SCLValue;
35 import org.simantics.scl.runtime.function.FunctionImpl1;
36 import org.simantics.scl.runtime.tuple.Tuple;
37 import org.simantics.scl.runtime.tuple.Tuple2;
38 import org.simantics.simulation.ontology.SimulationResource;
39 import org.simantics.utils.strings.StringUtils;
43 @SCLValue(type = "ReadGraph -> Resource -> Resource -> [Resource]")
44 public static List<Resource> profileChildren(ReadGraph graph, Resource resource, Resource context) throws DatabaseException {
45 Layer0 L0 = Layer0.getInstance(graph);
46 List<Resource> listedChildren = ListUtils.toList(graph, context);
47 if(listedChildren.isEmpty()) {
48 DiagramResource DIA = DiagramResource.getInstance(graph);
49 TreeMap<Double,Resource> priorityChildren = new TreeMap<Double,Resource>();
50 for(Resource child : graph.getObjects(context, L0.IsComposedOf)) {
51 Double p = graph.getPossibleRelatedValue(child, DIA.Profile_priority, Bindings.DOUBLE);
53 priorityChildren.put(p, child);
55 return new ArrayList<Resource>(priorityChildren.values());
57 return listedChildren;
61 @SCLValue(type = "ReadGraph -> Resource -> Variable -> [(String, Resource)]")
62 public static List<Tuple> availableProfiles(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
64 Resource runtimeDiagram = ScenegraphLoaderUtils.getPossibleResourceSelection(graph, context);
65 if(runtimeDiagram == null) return Collections.emptyList();
67 Layer0 L0 = Layer0.getInstance(graph);
68 DiagramResource DIA = DiagramResource.getInstance(graph);
70 String modelURI = graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasModelURI);
72 return Collections.emptyList();
74 Resource model = graph.getPossibleResource(modelURI);
76 return Collections.emptyList();
78 Instances query = graph.adapt(DIA.Profile, Instances.class);
80 ArrayList<Tuple> result = new ArrayList<Tuple>();
81 for(Resource profile : query.find(graph, model)) {
82 if(!graph.hasStatement(profile, L0.Abstract)) {
83 String name = graph.getRelatedValue(profile, L0.HasName, Bindings.STRING);
84 result.add(new Tuple2(name, profile));
91 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
92 public static Object profileEntrySelected(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
94 return new FunctionImpl1<Object, Boolean>() {
96 public void processRecursively(WriteGraph graph, Resource runtimeProfile, Resource entry, boolean checked) throws DatabaseException {
98 DiagramResource DIA = DiagramResource.getInstance(graph);
100 if(graph.isInstanceOf(entry, DIA.Profile)) {
101 Set<Resource> singleSelGroups = new HashSet<Resource>();
103 for(Resource child : ProfileUtils.getProfileChildren(graph, entry)) {
104 if(checked && graph.isInstanceOf(child, DIA.ProfileEntry)) {
105 //enable only one item from single selection groups.
106 Resource group = graph.getPossibleObject(child, DIA.ProfileEntry_HasGroup);
107 if (group != null && graph.isInstanceOf(group, DIA.SingleSelectionGroup)) {
108 if (singleSelGroups.contains(group))
110 singleSelGroups.add(group);
113 processRecursively(graph, runtimeProfile, child, checked);
116 } else if(graph.isInstanceOf(entry, DIA.ProfileEntry)) {
117 Resource group = graph.getPossibleObject(entry, DIA.ProfileEntry_HasGroup);
118 if (group != null && graph.isInstanceOf(group, DIA.SingleSelectionGroup)) {
120 //enable selected item from single selection groups, disable the rest.
121 Collection<Resource> entries = graph.getObjects(group, DIA.ProfileEntry_HasGroup_Inverse);
122 for (Resource e : entries) {
123 graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, e);
125 graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, entry);
127 graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, entry);
131 graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, entry);
133 graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, entry);
142 public Boolean apply(Object _event) {
144 Event event = (Event)_event;
146 if(event.detail == SWT.CHECK) {
148 final TreeItem item = (TreeItem)event.item;
149 Tree tree = item.getParent();
150 GraphExplorer explorer = (GraphExplorer)tree.getData("GraphExplorer");
151 final Resource runtimeDiagram = (Resource)explorer.getRoot().getConstant(BuiltinKeys.INPUT);
152 final boolean checked = item.getChecked();
153 NodeContext context = (NodeContext)item.getData();
154 final ResourcePair entry = (ResourcePair)context.getConstant(BuiltinKeys.INPUT);
157 VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);
158 Simantics.getSession().syncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {
161 public void perform(WriteGraph graph) throws DatabaseException {
163 DiagramResource DIA = DiagramResource.getInstance(graph);
164 Resource runtimeProfile = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);
165 processRecursively(graph, runtimeProfile, entry.getSecond(), checked);
170 } catch (DatabaseException e) {
183 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
184 public static Object selectedProfile(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
186 Resource runtimeDiagram = ScenegraphLoaderUtils.getResourceSelection(graph, context);
187 if(runtimeDiagram == null) return "";
189 Layer0 L0 = Layer0.getInstance(graph);
190 DiagramResource DIA = DiagramResource.getInstance(graph);
191 Resource profile = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);
192 if(profile == null) return null;
193 String name = graph.getPossibleRelatedValue(profile, L0.HasName, Bindings.STRING);
194 return StringUtils.safeString(name);
198 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
199 public static Object activeProfileModifier(ReadGraph graph, final Resource resource, final Variable context) throws DatabaseException {
201 return new FunctionImpl1<String, String>() {
204 public String apply(final String key) {
206 VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);
207 Simantics.getSession().asyncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {
209 public Resource selected(List<Tuple> avail, String text) {
210 for(Tuple a : avail) if(a.get(0).equals(text)) return (Resource)a.get(1);
215 public void perform(WriteGraph graph) throws DatabaseException {
217 Resource runtimeDiagram = ScenegraphLoaderUtils.getResourceSelection(graph, context);
219 List<Tuple> avail = (List<Tuple>)availableProfiles(graph, resource, context);
221 final Resource profile = selected(avail, key);
223 DiagramResource DIA = DiagramResource.getInstance(graph);
224 Resource current = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);
225 if(profile.equals(current)) return;
227 graph.deny(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);
228 graph.claim(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile, null, profile);
230 // Set this profile as the default profile for this model
231 String modelURI = graph.getRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasModelURI);
232 Resource model = graph.getResource(modelURI);
233 graph.deny(model, DIA.HasActiveProfile);
234 graph.claim(model, DIA.HasActiveProfile, profile);
236 // Set this profile as the default profile for this diagram
237 Resource configuration = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);
238 graph.deny(configuration, DIA.HasActiveProfile);
239 graph.claim(configuration, DIA.HasActiveProfile, profile);
253 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Resource")
254 public static Resource singleResourceSelection(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
255 return ScenegraphLoaderUtils.getPossibleResourceSelection(graph, context);