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