]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.loader/src/org/simantics/scenegraph/loader/ScenegraphLoaderUtils.java
Replace System.err and System.out with SLF4J Logging
[simantics/platform.git] / bundles / org.simantics.scenegraph.loader / src / org / simantics / scenegraph / loader / ScenegraphLoaderUtils.java
1 package org.simantics.scenegraph.loader;
2
3 import java.lang.reflect.Field;
4 import java.lang.reflect.Method;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.Map;
10
11 import org.simantics.Simantics;
12 import org.simantics.databoard.Bindings;
13 import org.simantics.databoard.binding.Binding;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.RequestProcessor;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.NamedResource;
18 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
19 import org.simantics.db.common.request.BinaryRead;
20 import org.simantics.db.common.request.ParametrizedPrimitiveRead;
21 import org.simantics.db.common.request.ResourceRead;
22 import org.simantics.db.common.request.UnaryRead;
23 import org.simantics.db.exception.AssumptionException;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.exception.VariableException;
26 import org.simantics.db.layer0.request.VariableName;
27 import org.simantics.db.layer0.request.VariableURI;
28 import org.simantics.db.layer0.variable.Variable;
29 import org.simantics.db.layer0.variable.VariableBuilder;
30 import org.simantics.db.layer0.variable.Variables;
31 import org.simantics.db.procedure.Listener;
32 import org.simantics.db.procedure.Procedure;
33 import org.simantics.layer0.Layer0;
34 import org.simantics.scenegraph.INode;
35 import org.simantics.scenegraph.LoaderNode;
36 import org.simantics.scenegraph.ParentNode;
37 import org.simantics.scenegraph.ontology.ScenegraphResources;
38 import org.simantics.scenegraph.utils.NodeUtil;
39 import org.simantics.scl.runtime.function.Function1;
40 import org.simantics.scl.runtime.function.FunctionImpl2;
41 import org.simantics.utils.DataContainer;
42 import org.simantics.utils.datastructures.Pair;
43 import org.simantics.utils.threads.IThreadWorkQueue;
44 import org.simantics.utils.threads.ThreadUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class ScenegraphLoaderUtils {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(ScenegraphLoaderUtils.class);
51         static Map<Pair<Variable, String>, Collection<Procedure<Object>>> externalMap = new HashMap<Pair<Variable, String>, Collection<Procedure<Object>>>(); 
52         static Map<Pair<Variable, String>, Object> externalValueMap = new HashMap<Pair<Variable, String>, Object>(); 
53
54         final public static class ScenegraphPropertyReference<T> {
55
56                 static class ExternalRead<T> extends ParametrizedPrimitiveRead<Pair<Variable, String>, T> {
57                         
58                         public ExternalRead(Variable base, String path) {
59                                 super(Pair.make(base, path));
60                         }
61                         
62                         @Override
63                         public void register(ReadGraph graph, final Listener<T> procedure) {
64                                 Object value = externalValueMap.get(parameter);
65                                 procedure.execute((T)value);
66                                 Collection<Procedure<Object>> listeners = externalMap.get(parameter);
67                                 if(listeners == null) {
68                                         listeners = new ArrayList<Procedure<Object>>();
69                                         externalMap.put(parameter, listeners);
70                                 }
71                                 listeners.add(new ProcedureAdapter<Object>() {
72                                         
73                                         @Override
74                                         public void execute(Object result) {
75                                                 procedure.execute((T)result);
76                                         }
77                                         
78                                 });
79                         }
80                         
81                         @Override
82                         public void unregistered() {
83                                 externalMap.remove(parameter);
84                         }
85                         
86                 }
87                 
88                 Variable baseVariable;
89                 IThreadWorkQueue thread;
90                 INode root;
91                 String reference;
92                 T value = null;
93                 
94                 public ScenegraphPropertyReference(IThreadWorkQueue thread, INode root, String reference, Variable baseVariable) {
95                         assert(root != null);
96                         this.thread = thread;
97                         this.root = root;
98                         this.reference = reference;
99                         this.baseVariable = baseVariable;
100                 }
101                 
102                 public T getExternalValue(RequestProcessor processor) throws DatabaseException {
103                         return processor.sync(new ExternalRead<T>(baseVariable, reference));
104                 }
105                 
106                 public T getValue() {
107
108                         final Pair<INode, String> ref = NodeUtil.browsePossibleReference(root, reference);
109                         
110                         final DataContainer<T> result = new DataContainer<T>();
111                         
112                         ThreadUtils.syncExec(thread, new Runnable() {
113
114                                 @Override
115                                 public void run() {
116                                         T value = ScenegraphLoaderUtils.getNodeProperty((LoaderNode)ref.first, ref.second);
117                                         result.set(value);
118                                 }
119                                 
120                         });
121                         
122                         return result.get();
123                         
124                 }
125
126                 public void setValue(final T value) {
127                         
128                         final Pair<INode, String> ref = NodeUtil.browsePossibleReference(root, reference);
129                         if(ref != null) {
130                                 ThreadUtils.asyncExec(thread, new Runnable() {
131
132                                         @Override
133                                         public void run() {
134                                                 Function1<Object, Boolean> function = ScenegraphLoaderUtils.getPropertyFunction((LoaderNode)ref.first, ref.second);
135                                                 if(function != null) {
136                                                         function.apply(value);
137                                                 } else {
138                                                         new Exception("no function for ref " + ref).printStackTrace();
139                                                 }
140                                         }
141                                         
142                                 });
143                         } else {
144                                 //new Exception("no reference for " + root + " " + reference).printStackTrace();
145                         }
146                         
147                         
148                 }               
149                 
150         }       
151         public static Collection<Variable> computeChildren(ReadGraph graph, Variable configuration) throws DatabaseException {
152                 ScenegraphResources SG = ScenegraphResources.getInstance(graph);
153                 Resource represents = configuration.getRepresents(graph);
154                 Collection<Resource> children = graph.getPossibleRelatedValue2(represents, SG.Node_children, configuration);
155                 if(children == null) return Collections.emptyList();
156                 ArrayList<Variable> result = new ArrayList<Variable>();
157                 for(Resource item : children) {
158                         VariableBuilder variableBuilder = graph.adapt(item, VariableBuilder.class);
159                         Variable child = variableBuilder.buildChild(graph, configuration, null, item);
160                         if(child != null) result.add(child);
161                 }
162                 return result;
163         }
164         
165         public static Collection<Variable> getChildren(RequestProcessor processor, Variable configuration) throws DatabaseException {
166                 
167                 return processor.sync(new UnaryRead<Variable, Collection<Variable>>(configuration) {
168
169                         @Override
170                         public Collection<Variable> perform(ReadGraph graph) throws DatabaseException {
171                                 return parameter.browseChildren(graph);
172                         }
173
174                 });
175                 
176         }
177
178         public static Collection<Variable> getChildren(Resource configuration) throws DatabaseException {
179                 
180                 return Simantics.getSession().sync(new ResourceRead<Collection<Variable>>(configuration) {
181
182                         @Override
183                         public Collection<Variable> perform(ReadGraph graph) throws DatabaseException {
184                                 return computeChildren(graph, Variables.getVariable(graph, resource));
185                         }
186
187                 });
188                 
189         }
190
191         public static Collection<NamedResource> getProperties(RequestProcessor processor, Resource configuration) throws DatabaseException {
192                 
193                 return processor.sync(new ResourceRead<Collection<NamedResource>>(configuration) {
194
195                         @Override
196                         public Collection<NamedResource> perform(ReadGraph graph) throws DatabaseException {
197                                 Layer0 L0 = Layer0.getInstance(graph);
198                                 ScenegraphResources SG = ScenegraphResources.getInstance(graph);
199                                 ArrayList<NamedResource> result = new ArrayList<NamedResource>(); 
200                                 for(Resource predicate : graph.getPredicates(resource)) {
201                                         if(graph.isSubrelationOf(predicate, SG.Node_HasProperty)) {
202                                                 String name = graph.getRelatedValue(predicate, L0.HasName, Bindings.STRING);
203                                                 result.add(new NamedResource(name, predicate));
204                                         }
205                                 }
206                                 return result;
207                         }
208
209                 });
210                 
211         }
212
213         /**
214      * A custom exception for indicating that the a (runtime) resource has been
215      * disposed of (i.e. its statements have been removed). Optimized by
216      * nullifying {@link #fillInStackTrace()} since this is only used customly
217      * by
218      * {@link ScenegraphLoaderUtils#listen(RequestProcessor, Variable, String, Function1)}
219      * to dispose of the DB listeners it creates.
220      * 
221      * @author Tuukka Lehtonen
222      */
223         static class DisposedRuntimeException extends AssumptionException {
224
225         private static final long serialVersionUID = 5213099691410928157L;
226
227         public DisposedRuntimeException(String message) {
228             super(message);
229         }
230
231         @Override
232         public synchronized Throwable fillInStackTrace() {
233             return this;
234         }
235
236         }
237
238         public static <T> void listen(RequestProcessor processor, final ScenegraphLoaderProcess process, final Variable context, final String property, final Function1<T, Boolean> function) throws DatabaseException {
239
240                 try {
241                 
242                         processor.syncRequest(new BinaryRead<Variable, String, T> (context, property) {
243
244                                 @SuppressWarnings("unchecked")
245                                 @Override
246                                 public T perform(ReadGraph graph) throws DatabaseException {
247                                         // FIXME: this must throw a dedicated exception in the case where the runtime variable has been deleted which implies this listener should be disposed of
248                                         SceneGraphContext vc = getContext(graph, context);
249                                         if (vc == null)
250                                                 throw new DisposedRuntimeException("No scene graph context");
251                                         Resource runtime = vc.getRuntime();
252                                         if (runtime == null || !graph.hasStatement(runtime))
253                                                 throw new DisposedRuntimeException("Scene graph runtime disposed");
254
255                                         return (T)parameter.getPropertyValue(graph, parameter2); 
256                                 }
257
258                         }, new Listener<T>() {
259
260                                 private boolean disposed = false;
261
262                                 @Override
263                                 public void exception(Throwable t) {
264                                         if (t instanceof DisposedRuntimeException) {
265                                                 //System.out.println("ScenegraphLoaderUtils(" + this + ").listen: runtime disposed");
266                                                 disposed = true;
267                                         } else {
268                                                 //t.printStackTrace();
269                                         }
270                                 }
271
272                                 @Override
273                                 public void execute(T result) {
274                                         if (!disposed)
275                                                 disposed = function.apply(result);
276                                 }
277
278                                 @Override
279                                 public boolean isDisposed() {
280                                         return process.isDisposed() | disposed;
281                                 }
282                                 
283                                 @Override
284                                 public String toString() {
285                                         return "Scenegraph Property Listener for " + process;
286                                 }
287
288                         });
289                 
290                 } catch (DatabaseException e) {
291                         
292                 }
293     
294         }
295         
296     public static Resource getRuntime(ReadGraph graph, Variable context) throws DatabaseException {
297         SceneGraphContext vc = getContext(graph, context);
298         if(vc != null) return vc.getRuntime();
299         Variable parent = context.getParent(graph);
300         if(parent == null) throw new DatabaseException("Runtime resource was not found from context Variable.");
301         return getRuntime(graph, parent);
302     }
303     
304     public static SceneGraphContext getContext(ReadGraph graph, Variable context) throws DatabaseException {
305         SceneGraphContext vc = context.adaptPossible(graph, SceneGraphContext.class);
306         if(vc != null) return vc;
307         else {
308                 Variable parent = context.getParent(graph);
309                 if(parent != null) return getContext(graph, parent);
310                 else return null;
311         }
312     }
313     
314     public static Variable getRuntimeVariable(ReadGraph graph, Variable context) throws DatabaseException {
315         SceneGraphContext vc = getContext(graph, context);
316         if(vc == null) return null;
317         else return vc.getRuntimeVariable();
318     }
319     
320     public static Variable getBaseVariable(ReadGraph graph, Variable context) throws DatabaseException {
321         
322         Variable parent = context.getParent(graph);
323         if(parent == null) return null;
324         if(context instanceof ScenegraphVariable && !(parent instanceof ScenegraphVariable)) return context;
325         else return getBaseVariable(graph, parent);
326         
327     }
328
329     static class ScenegraphReference extends UnaryRead<Variable, Pair<Variable, String>> {
330
331         public ScenegraphReference(Variable var) {
332                 super(var);
333                 assert(var != null);
334         }
335
336         @Override
337         public Pair<Variable, String> perform(ReadGraph graph) throws DatabaseException {
338                 Variable base = getBaseVariable(graph, parameter);
339                 return Pair.make(base, Variables.getRVI(graph, base, parameter));
340         }
341
342     }
343     
344     public static INode create(RequestProcessor processor, ScenegraphLoaderProcess process, ParentNode<?> parent, Resource configuration, final Variable context, Class<?> clazz) throws DatabaseException {
345
346         final String name = processor.sync(new VariableName(context));
347         
348         final String uri  = processor.sync(new VariableURI(context));
349         
350         LoaderNode node = (LoaderNode)parent.addNode(name, clazz);
351
352         final Pair<Variable, String> reference = processor.sync(new ScenegraphReference(context));
353
354         node.setPropertyCallback(new FunctionImpl2<String, Object, Boolean>() {
355
356                 @Override
357                 public Boolean apply(String property, Object value) {
358                         Pair<Variable, String> key = Pair.make(reference.first, reference.second + "#" + property);
359                         externalValueMap.put(key, value);
360                         Collection<Procedure<Object>> listeners = externalMap.get(key);
361                         if(listeners != null) {
362                                 for(Procedure<Object> listener : listeners) listener.execute(value);
363                         }
364                         return true;
365                 }
366                 
367                 });
368
369         for(NamedResource property : ScenegraphLoaderUtils.getProperties(processor, configuration)) {
370                 try {
371                         Function1<Object, Boolean> func = node.getPropertyFunction(property.getName());
372                         if (func != null)
373                             ScenegraphLoaderUtils.listen(processor, process, context, property.getName(), func);
374                         //else
375                         //    System.out.println("NO FUNCTION FOR PROPERTY: " + property.getName() + " (" + node + ")");
376                 } catch (Exception e) {
377                         e.printStackTrace();
378                 }
379         }
380
381         return node;
382         
383     }
384         
385         public static Variable getVariableSelection(ReadGraph graph, Variable context) throws DatabaseException {
386                 
387                 Variable runtimeVariable = getRuntimeVariable(graph, context);
388                 if (runtimeVariable == null)
389                         throw new VariableException("no runtime variable for context " + context.getURI(graph));
390                 return runtimeVariable.getPropertyValue(graph, "variable");
391                 
392         }
393
394         public static Variable getPossibleVariableSelection(ReadGraph graph, Variable context) throws DatabaseException {
395             Variable runtimeVariable = getRuntimeVariable(graph, context);
396             return runtimeVariable == null ? null : (Variable) runtimeVariable.getPossiblePropertyValue(graph, "variable");
397         }
398
399         public static Resource getResourceSelection(ReadGraph graph, Variable context) throws DatabaseException {
400
401                 Variable runtimeVariable = getRuntimeVariable(graph, context);
402                 if (runtimeVariable == null)
403             throw new VariableException("no runtime variable for context " + context.getURI(graph));
404                 Resource sel = runtimeVariable.getPropertyValue(graph, "resource"); 
405                 return sel;
406
407         }
408         
409         public static Resource getPossibleResourceSelection(ReadGraph graph, Variable context) throws DatabaseException {
410
411             Variable runtimeVariable = getRuntimeVariable(graph, context);
412             return runtimeVariable == null ? null : (Resource) runtimeVariable.getPossiblePropertyValue(graph, "resource"); 
413
414         }
415
416         public static INode getNode(ReadGraph graph, Variable location) throws DatabaseException {
417                 Variable runtime = getRuntimeVariable(graph, location);
418                 INode root = runtime.adapt(graph, INode.class);
419                 Variable base = getBaseVariable(graph, location);
420                 String rvi = Variables.getRVI(graph, base, location);
421                 return NodeUtil.browsePossible(root, rvi);
422         }
423         
424 //      public static <T> ScenegraphPropertyReference<T> getPropertyReference(final IThreadWorkQueue thread, final Variable context, final String path) throws DatabaseException {
425 //              return Simantics.getSession().sync(new UniqueRead<ScenegraphPropertyReference<T>>() {
426 //
427 //                      @Override
428 //                      public ScenegraphPropertyReference<T> perform(ReadGraph graph) throws DatabaseException {
429 //                              return getRelativePropertyReference(thread, graph, context, path);
430 //                      }
431 //
432 //              });
433 //      }
434
435 //      public static <T> T getRelativeProperty(final IThreadWorkQueue thread, ReadGraph graph, Variable context, String path) throws DatabaseException {
436 //              ScenegraphPropertyReference<T> ref = getRelativePropertyReference(thread, graph, context, path);
437 //              return ref.getExternalValue(graph);
438 //      }
439
440         public static <T> T getProperty(final IThreadWorkQueue thread, INode _root, String reference) {
441                 
442                 INode root = ((ParentNode<INode>)_root).getNodes().iterator().next();
443                 
444                 final Pair<INode, String> ref = NodeUtil.browsePossibleReference(root, reference);
445                 
446                 final DataContainer<T> result = new DataContainer<T>();
447                 
448                 ThreadUtils.syncExec(thread, new Runnable() {
449
450                         @Override
451                         public void run() {
452                                 T value = ScenegraphLoaderUtils.getNodeProperty((LoaderNode)ref.first, ref.second);
453                                 result.set(value);
454                         }
455                         
456                 });
457                 
458                 return result.get();
459                 
460         }
461
462         public static <T> ScenegraphPropertyReference<T> getRelativePropertyReference(final IThreadWorkQueue thread, ReadGraph graph, Variable context, String path) throws DatabaseException {
463                 
464                 Variable runtime = getRuntimeVariable(graph, context);
465                 INode root = runtime.adapt(graph, INode.class);
466                 Variable base = getBaseVariable(graph, context);
467                 INode baseNode = NodeUtil.findChildById((ParentNode)root, base.getName(graph));
468                 String contextRVI = Variables.getRVI(graph, base, context);
469                 String rvi = Variables.getRVI(contextRVI, path);
470                 return new ScenegraphPropertyReference<T>(thread, baseNode, rvi, base);
471                 
472         }       
473
474         public static <T> ScenegraphPropertyReference<T> getPropertyReference(final IThreadWorkQueue thread, ReadGraph graph, Variable context, String path) throws DatabaseException {
475                 
476                 Variable runtime = getRuntimeVariable(graph, context);
477                 INode root = runtime.adapt(graph, INode.class);
478                 Variable base = getBaseVariable(graph, context);
479                 return new ScenegraphPropertyReference<T>(thread, root, path, base);
480                 
481         }       
482         
483         public static Method getSynchronizeMethod(INode node, String propertyName) {
484                 try {
485                         String methodName = "synchronize" + propertyName.substring(0,1).toUpperCase() + propertyName.substring(1);
486                         for(Method m : node.getClass().getMethods()) {
487                                 if(m.getName().equals(methodName)) return m;
488                         }
489                         return null;
490                 } catch (SecurityException e) {
491                         e.printStackTrace();
492                 }
493                 return null;
494         }
495
496         public static Method getReadMethod(INode node, String propertyName) {
497                 try {
498                         String methodName = "read" + propertyName.substring(0,1).toUpperCase() + propertyName.substring(1);
499                         return node.getClass().getMethod(methodName);
500                 } catch (SecurityException e) {
501                         e.printStackTrace();
502                 } catch (NoSuchMethodException e) {
503                         e.printStackTrace();
504                 }
505                 return null;
506         }
507         
508         public static Field getPropertyField(INode node, String propertyName) {
509                 try {
510                         return node.getClass().getField(propertyName);
511                 } catch (SecurityException e) {
512                         LOGGER.error("node: " + node, e);
513                 } catch (NoSuchFieldException e) {
514                         LOGGER.error("node: " + node, e);
515                 }
516                 return null;
517         }
518         
519         public static Class<?> getPropertyType(Field field) {
520                 return field.getType();
521         }
522         
523         public static Class<?> getArgumentType(Method method) {
524                 return (Class<?>)method.getGenericParameterTypes()[0];
525         }
526         
527         public static Class<?> getReturnType(Method method) {
528                 return method.getReturnType();
529         }
530
531         public static Binding getPropertyBinding(Class<?> clazz) {
532                 try {
533                         return Bindings.getBindingUnchecked(clazz);
534                 } catch (Throwable t) {
535                         return null;
536                 }
537         }
538         
539         public static Binding getGenericPropertyBinding(Binding binding) {
540                 try {
541                         return Bindings.getBinding(binding.type());
542                 } catch (Throwable t) {
543                         return null;
544                 }
545         }
546         
547         public static Function1<Object, Boolean> getPropertyFunction(final LoaderNode node, final String propertyName) {
548                 return node.getPropertyFunction(propertyName);
549         }
550
551         public static <T> T getNodeProperty(final LoaderNode node, final String propertyName) {
552                 return node.getProperty(propertyName);
553         }
554         
555         public static String getPath(ReadGraph graph, Variable context) throws DatabaseException {
556                 Variable base = getBaseVariable(graph, context);
557                 return Variables.getRVI(graph, base, context);
558         }
559         
560 }