]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulator.toolkit/src/org/simantics/simulator/toolkit/StandardNodeManager.java
Support pending values in NodeManager
[simantics/platform.git] / bundles / org.simantics.simulator.toolkit / src / org / simantics / simulator / toolkit / StandardNodeManager.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *     Semantum Oy - initial API and implementation
12  *******************************************************************************/
13 package org.simantics.simulator.toolkit;
14
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.atomic.AtomicBoolean;
21
22 import org.simantics.databoard.Bindings;
23 import org.simantics.databoard.adapter.AdaptException;
24 import org.simantics.databoard.adapter.Adapter;
25 import org.simantics.databoard.adapter.AdapterConstructionException;
26 import org.simantics.databoard.binding.Binding;
27 import org.simantics.databoard.binding.VariantBinding;
28 import org.simantics.databoard.binding.error.BindingException;
29 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
30 import org.simantics.databoard.binding.mutable.Variant;
31 import org.simantics.databoard.type.Datatype;
32 import org.simantics.simulator.variable.NodeManager;
33 import org.simantics.simulator.variable.Realm;
34 import org.simantics.simulator.variable.exceptions.NoSuchNodeException;
35 import org.simantics.simulator.variable.exceptions.NodeManagerException;
36 import org.simantics.simulator.variable.exceptions.NotInRealmException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import gnu.trove.map.hash.THashMap;
41 import gnu.trove.procedure.TObjectProcedure;
42 import gnu.trove.set.hash.THashSet;
43
44 /**
45  * StandardNodeManager gives default implementations to some methods
46  * of NodeManager.
47  *
48  * @author Antti Villberg
49  */
50 public class StandardNodeManager<Node, Engine extends StandardNodeManagerSupport<Node>> implements NodeManager<Node> {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(StandardNodeManager.class);
53
54     protected final Node root;
55     protected final StandardRealm<Node,Engine> realm;
56
57     static final Binding NO_BINDING = new VariantBinding() {
58
59         @Override
60         public Object getContent(Object variant, Binding contentBinding) throws BindingException {
61             throw new Error();
62         }
63
64         @Override
65         public Object getContent(Object variant) throws BindingException {
66             throw new Error();
67         }
68
69         @Override
70         public Datatype getContentType(Object variant) throws BindingException {
71             throw new Error();
72         }
73
74         @Override
75         public Binding getContentBinding(Object variant) throws BindingException {
76             throw new Error();
77         }
78
79         @Override
80         public Object create(Binding contentBinding, Object content) throws BindingException {
81             throw new Error();
82         }
83
84         @Override
85         public void setContent(Object variant, Binding contentBinding, Object content) throws BindingException {
86             throw new Error();
87         }
88
89         @Override
90         public boolean isInstance(Object obj) {
91             return true;
92         }
93
94         @Override
95         public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
96             throw new Error();
97         }
98
99         @Override
100         public int compare(Object o1, Object o2) throws org.simantics.databoard.binding.error.RuntimeBindingException {
101             if(o1 == null) {
102                 if(o2 == null) {
103                     return 0;
104                 } else {
105                     return  - System.identityHashCode(o2);
106                 }
107             } else {
108                 if(o2 == null) {
109                     return  System.identityHashCode(o1);
110                 } else {
111                     if(o1.equals(o2)) return 0;
112                     return System.identityHashCode(o1) - System.identityHashCode(o2);
113                 }
114             }
115         }
116
117     };
118
119     protected THashMap<Node, Variant> valueCache = new THashMap<>();
120     protected THashMap<Node, THashSet<Runnable>> listeners = new THashMap<>();
121
122     AtomicBoolean fireNodeListenersScheduled = new AtomicBoolean(false);
123     Runnable fireNodeListeners = new Runnable() {
124         @Override
125         public void run() {
126             fireNodeListenersScheduled.set(false);
127             TObjectProcedure<Runnable> procedure = r -> {
128                 r.run();
129                 return true;
130             };
131             synchronized(listeners) {
132                 listeners.forEachValue(set -> {
133                     set.forEach(procedure);
134                     return true;
135                 });
136             }
137         }
138     };
139
140     Runnable clearValueCache = () -> valueCache.clear();
141
142     public StandardNodeManager(StandardRealm<Node,Engine> realm, Node root) {
143         assert(realm != null);
144         assert(root != null);
145         this.realm = realm;
146         this.root = root;
147     }
148
149     @Override
150     public List<String> getChildNames(Node node) throws NodeManagerException {
151         List<Node> children = getChildren(node);
152         ArrayList<String> names = new ArrayList<>(children.size());
153         for(Node child : children)
154             names.add(getName(child));
155         return names;
156     }
157
158     @Override
159     public List<String> getPropertyNames(Node node) throws NodeManagerException {
160         List<Node> properties = getProperties(node);
161         ArrayList<String> names = new ArrayList<>(properties.size());
162         for(Node property : properties)
163             names.add(getName(property));
164         return names;
165     }
166
167     @Override
168     public Object getValue(Node node, String propertyName, Binding binding)
169             throws NodeManagerException, BindingException {
170         Node property = getProperty(node, propertyName);
171         if(property == null)
172             throw new NoSuchNodeException("Didn't find a property " + propertyName);
173         return getValue(property, binding);
174     }
175
176     @Override
177     public void setValue(Node node, String propertyName, Object value,
178             Binding binding) throws NodeManagerException, BindingException {
179         Node property = getProperty(node, propertyName);
180         if(property == null)
181             throw new NoSuchNodeException("Didn't find a property " + propertyName);
182         setValue(property, value, binding);
183     }
184
185     @Override
186     public Variant getValue(Node node, String propertyName)
187             throws NodeManagerException {
188         Node property = getProperty(node, propertyName);
189         if(property == null)
190             throw new NoSuchNodeException("Didn't find a property " + propertyName);
191         return getValue(property);
192     }
193
194     @Override
195     public Object getValue(Node node, Binding binding) throws NodeManagerException, BindingException {
196         try {
197             Variant value = getValue(node);
198             if(NodeManager.PENDING_NODE_VALUE == value)
199                 return value;
200             return value.getValue(binding);
201         } catch (AdaptException e) {
202             throw new BindingException(e);
203         }
204     }
205
206     @Override
207     public String getPropertyURI(Node parent, Node property) {
208         return null;
209     }
210
211     @Override
212     public Realm getRealm() {
213         return realm;
214     }
215
216     public StandardRealm<Node, Engine> getStandardRealm() {
217         return realm;
218     }
219
220     protected String getRealmId() {
221         return realm.getId();
222     }
223
224     public Node getRoot() {
225         return root;
226     }
227
228     protected boolean isRoot(Node node) {
229         return root.equals(node);
230     }
231
232     @Override
233     public void addNodeListener(Node node, Runnable listener) {
234         synchronized(listeners) {
235             THashSet<Runnable> l = listeners.get(node);
236             if(l == null) {
237                 l = new THashSet<>();
238                 listeners.put(node, l);
239             }
240             l.add(listener);
241         }
242         getRealm().asyncExec(listener);
243     }
244
245     @Override
246     public void removeNodeListener(Node node, Runnable listener) {
247         synchronized(listeners) {
248             THashSet<Runnable> l = listeners.get(node);
249             if(l != null) {
250                 l.remove(listener);
251                 if(l.isEmpty())
252                     listeners.remove(node);
253             }
254         }
255     }
256
257     public void fireNodeListeners() {
258         if(!fireNodeListenersScheduled.getAndSet(true))
259             realm.asyncExec(fireNodeListeners);
260     }
261
262     public void fireNodeListenersSync() {
263         try {
264             realm.syncExec(fireNodeListeners);
265         } catch (InterruptedException e) {
266             LOGGER.error("Synchronous node listener firing was interrupted.", e);
267         }
268     }
269
270     public void refreshVariable(Node node) {
271         realm.asyncExec(() -> {
272             valueCache.remove(node);
273             synchronized(listeners) {
274                 THashSet<Runnable> runnables = listeners.get(node);
275                 if (runnables != null) {
276                     for (Runnable r : runnables) {
277                         r.run();
278                     }
279                 }
280             }
281         });
282     }
283
284     public void refreshVariables() {
285         realm.asyncExec(clearValueCache);
286         fireNodeListeners();
287     }
288
289     public void refreshVariablesSync() {
290         try {
291             realm.syncExec(clearValueCache);
292         } catch (InterruptedException e) {
293             LOGGER.error("Synchronous value cache refresh was interrupted.", e);
294         }
295         fireNodeListenersSync();
296     }
297
298     protected Variant getEngineVariantOrCached(Node node) throws NodeManagerException {
299         Variant variant = valueCache.get(node);
300         if(variant == null) {
301             Object value = realm.getEngine().getEngineValue(node);
302             if(NodeManager.PENDING_NODE_VALUE == value)
303                 return (Variant)value;
304             Binding binding = realm.getEngine().getEngineBinding(node);
305             variant = new Variant(binding, value);
306             valueCache.put(node, variant);
307         }
308         return variant;
309     }
310
311     @Override
312     public Variant getValue(Node node) throws NodeManagerException {
313         checkThreadAccess();
314         return getEngineVariantOrCached(node);
315     }
316
317     protected void checkThreadAccess() throws NodeManagerException {
318         if(Thread.currentThread() != realm.getThread())
319             throw new NotInRealmException();
320     }
321
322     protected Datatype getDatatypeForValue(Object value) {
323         Binding binding = Bindings.getBindingUnchecked(value.getClass());
324         if(binding == null) return null;
325         else return binding.type();
326     }
327
328     @Override
329     public void setValue(Node node, Object value, Binding binding)
330             throws NodeManagerException {
331         updateValueInner(node, value, binding);
332         refreshVariable(node);
333     }
334
335     //Update the value of the node and remove from valueCache only the references nodes
336     public void setValueAndFireSelectedListeners(Node node, Object value, Binding binding, Set<Node> references) throws NodeManagerException {
337         if(references.size() > 0) {
338                 for(Node n : references) {
339                         valueCache.remove(n);
340                 }
341         }
342         updateValueInner(node, value, binding);
343         fireNodeListenersSync();
344     }
345     
346     //Update the value of the node helper method
347     private void updateValueInner(Node node, Object value, Binding binding) throws NodeManagerException {
348         checkThreadAccess();
349         Binding targetBinding = realm.getEngine().getEngineBinding(node);
350         if(binding.equals(targetBinding)) {
351             Variant variant = new Variant(binding, value);
352             valueCache.put(node, variant);
353             realm.getEngine().setEngineValue(node, value);
354         } else {
355             try {
356                 Adapter adapter = Bindings.getAdapter(binding, targetBinding);
357                 Object targetValue = adapter.adapt(value);
358                 Variant variant = new Variant(targetBinding, targetValue);
359                 valueCache.put(node, variant);
360                 realm.getEngine().setEngineValue(node, targetValue);
361             } catch (AdapterConstructionException e) {
362                 throw new NodeManagerException(e);
363             } catch (AdaptException e) {
364                 throw new NodeManagerException(e);
365             }
366         }
367     }
368
369     @Override
370     public String getName(Node node) {
371         if(isRoot(node)) {
372             String id = getRealmId();
373             int lastSlash = id.lastIndexOf("/");
374             if(lastSlash == -1) throw new IllegalStateException("Invalid realm id " + id);
375             String name = id.substring(lastSlash+1);
376             return name;
377         } else {
378             return realm.getEngine().getName(node);
379         }
380     }
381
382     @Override
383     public Node getNode(String path) throws NodeManagerException {
384         checkThreadAccess();
385         throw new UnsupportedOperationException();
386     }
387
388     @Override
389     public Node getChild(Node node, String name) throws NodeManagerException {
390         checkThreadAccess();
391         Map<String,Node> map = realm.getEngine().getChildren(node);
392         return map.get(name);
393     }
394
395     @Override
396     public Node getProperty(Node node, String name) throws NodeManagerException {
397         checkThreadAccess();
398         Map<String,Node> map = realm.getEngine().getProperties(node);
399         return map.get(name);
400     }
401
402     @Override
403     public List<Node> getChildren(Node node) throws NodeManagerException {
404         checkThreadAccess();
405         return new ArrayList<Node>(realm.getEngine().getChildren(node).values());
406     }
407
408     @Override
409     public List<Node> getProperties(Node node) throws NodeManagerException {
410         checkThreadAccess();
411         return new ArrayList<Node>(realm.getEngine().getProperties(node).values());
412     }
413
414     @Override
415     public Datatype getDatatype(Node node) throws NodeManagerException {
416         checkThreadAccess();
417         try {
418             Variant v = getEngineVariantOrCached(node);
419             Binding b = v.getBinding();
420             if(b == null) return null;
421             return b.type();
422         } catch (RuntimeBindingConstructionException e) {
423             // There is no datatype for all values
424         }
425         return null;
426     }
427
428     public void clear() {
429         valueCache.clear();
430         listeners.clear();
431     }
432
433     @Override
434     public Set<String> getClassifications(Node node) throws NodeManagerException {
435         return Collections.emptySet();
436     }
437
438 }