]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/function/All.java
0a3245b52268fb79e4c0a180a80dabae15dda206
[simantics/platform.git] / bundles / org.simantics.diagram.profile / src / org / simantics / diagram / profile / function / All.java
1 package org.simantics.diagram.profile.function;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Set;
9 import java.util.TreeMap;
10
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;
40
41 public class All {
42
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);
52                         if(p != null)
53                                 priorityChildren.put(p, child);
54                 }
55                         return new ArrayList<Resource>(priorityChildren.values());
56         } else {
57                 return listedChildren;
58         }
59     }
60
61     @SCLValue(type = "ReadGraph -> Resource -> Variable -> [(String, Resource)]")
62     public static List<Tuple> availableProfiles(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
63         
64         Resource runtimeDiagram = ScenegraphLoaderUtils.getPossibleResourceSelection(graph, context);
65         if(runtimeDiagram == null) return Collections.emptyList();
66
67         Layer0 L0 = Layer0.getInstance(graph);
68         DiagramResource DIA = DiagramResource.getInstance(graph);
69
70         String modelURI = graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasModelURI);
71         if (modelURI == null)
72             return Collections.emptyList();
73
74         Resource model = graph.getPossibleResource(modelURI);
75         if (model == null)
76             return Collections.emptyList();
77
78         Instances query = graph.adapt(DIA.Profile, Instances.class);
79
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));
85             }
86         }
87         return result;
88         
89     }
90
91     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
92     public static Object profileEntrySelected(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
93         
94         return new FunctionImpl1<Object, Boolean>() {
95
96                 public void processRecursively(WriteGraph graph, Resource runtimeProfile, Resource entry, boolean checked) throws DatabaseException {
97
98                 DiagramResource DIA = DiagramResource.getInstance(graph);
99                         
100                 if(graph.isInstanceOf(entry, DIA.Profile)) {
101                         Set<Resource> singleSelGroups = new HashSet<Resource>();
102                         
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))
109                                                 continue;
110                                         singleSelGroups.add(group);
111                                 }
112                                 }
113                                 processRecursively(graph, runtimeProfile, child, checked);
114                         }
115                         
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)) {
119                                  if(checked) {
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);
124                                          }
125                                          graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, entry);
126                                  } else {
127                                          graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, entry);
128                                  }
129                         } else {
130                                     if(checked) {
131                                         graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, entry);
132                                     } else {
133                                         graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, entry);
134                                     }
135                         }
136
137                         }
138                         
139                 }
140                 
141             @Override
142             public Boolean apply(Object _event) {
143                 
144                 Event event = (Event)_event;
145                 
146                 if(event.detail == SWT.CHECK) {
147                         
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);
155                     try {
156                         
157                         VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);
158                         Simantics.getSession().syncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {
159
160                             @Override
161                             public void perform(WriteGraph graph) throws DatabaseException {
162                                 
163                                 DiagramResource DIA = DiagramResource.getInstance(graph);
164                                 Resource runtimeProfile = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);
165                                 processRecursively(graph, runtimeProfile, entry.getSecond(), checked);
166                                 
167                             }
168
169                         });
170                     } catch (DatabaseException e) {
171                         e.printStackTrace();
172                     }
173                 }
174                 
175                 return null;
176                 
177             }
178                 
179         };
180         
181     }
182     
183     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
184     public static Object selectedProfile(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
185         
186         Resource runtimeDiagram = ScenegraphLoaderUtils.getResourceSelection(graph, context);
187         if(runtimeDiagram == null) return "";
188
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);
195
196     }
197
198     @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
199     public static Object activeProfileModifier(ReadGraph graph, final Resource resource, final Variable context) throws DatabaseException {
200         
201         return new FunctionImpl1<String, String>() {
202
203                 @Override
204                 public String apply(final String key) {
205
206                         VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);
207                         Simantics.getSession().asyncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {
208
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);
211                                                         return null;
212                                 }
213
214                                 @Override
215                                 public void perform(WriteGraph graph) throws DatabaseException {
216
217                                         Resource runtimeDiagram = ScenegraphLoaderUtils.getResourceSelection(graph, context);
218
219                                         List<Tuple> avail = (List<Tuple>)availableProfiles(graph, resource, context);
220
221                                         final Resource profile = selected(avail, key);
222
223                                         DiagramResource DIA = DiagramResource.getInstance(graph);
224                                         Resource current = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);
225                                         if(profile.equals(current)) return;
226
227                                         graph.deny(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);
228                                         graph.claim(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile, null, profile);
229
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);
235
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);
240
241                                 }
242
243                         });
244                         
245                         return null;
246
247                 }
248                 
249         };
250         
251     }
252
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);
256     }
257     
258 }