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