]> gerrit.simantics Code Review - simantics/platform.git/blob
87ae4f32f3b7394d1e8168c705c2584ba0ae3f8c
[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 refreshVariables() {
267         realm.asyncExec(clearValueCache);
268         fireNodeListeners();
269     }
270
271     public void refreshVariablesSync() {
272         try {
273             realm.syncExec(clearValueCache);
274         } catch (InterruptedException e) {
275             LOGGER.error("Synchronous value cache refresh was interrupted.", e);
276         }
277         fireNodeListenersSync();
278     }
279
280     protected Variant getEngineVariantOrCached(Node node) throws NodeManagerException {
281         Variant variant = valueCache.get(node);
282         if(variant == null) {
283             Object value = realm.getEngine().getEngineValue(node);
284             Binding binding = realm.getEngine().getEngineBinding(node);
285             variant = new Variant(binding, value);
286             valueCache.put(node, variant);
287         }
288         return variant;
289     }
290
291     @Override
292     public Variant getValue(Node node) throws NodeManagerException {
293         checkThreadAccess();
294         return getEngineVariantOrCached(node);
295     }
296
297     protected void checkThreadAccess() throws NodeManagerException {
298         if(Thread.currentThread() != realm.getThread())
299             throw new NotInRealmException();
300     }
301
302     protected Datatype getDatatypeForValue(Object value) {
303         Binding binding = Bindings.getBindingUnchecked(value.getClass());
304         if(binding == null) return null;
305         else return binding.type();
306     }
307
308     @Override
309     public void setValue(Node node, Object value, Binding binding)
310             throws NodeManagerException {
311         checkThreadAccess();
312         Binding targetBinding = realm.getEngine().getEngineBinding(node);
313         if(binding.equals(targetBinding)) {
314             Variant variant = new Variant(binding, value);
315             valueCache.put(node, variant);
316             realm.getEngine().setEngineValue(node, value);
317         } else {
318             try {
319                 Adapter adapter = Bindings.getAdapter(binding, targetBinding);
320                 Object targetValue = adapter.adapt(value);
321                 Variant variant = new Variant(targetBinding, targetValue);
322                 valueCache.put(node, variant);
323                 realm.getEngine().setEngineValue(node, targetValue);
324             } catch (AdapterConstructionException e) {
325                 throw new NodeManagerException(e);
326             } catch (AdaptException e) {
327                 throw new NodeManagerException(e);
328             }
329         }
330         refreshVariables();
331     }
332
333     @Override
334     public String getName(Node node) {
335         if(isRoot(node)) {
336             String id = getRealmId();
337             int lastSlash = id.lastIndexOf("/");
338             if(lastSlash == -1) throw new IllegalStateException("Invalid realm id " + id);
339             String name = id.substring(lastSlash+1); 
340             return name;
341         } else {
342             return realm.getEngine().getName(node);
343         }
344     }
345
346     @Override
347     public Node getNode(String path) throws NodeManagerException {
348         checkThreadAccess();
349         throw new UnsupportedOperationException();
350     }
351
352     @Override
353     public Node getChild(Node node, String name) throws NodeManagerException {
354         checkThreadAccess();
355         Map<String,Node> map = realm.getEngine().getChildren(node);
356         return map.get(name);
357     }
358
359     @Override
360     public Node getProperty(Node node, String name) throws NodeManagerException {
361         checkThreadAccess();
362         Map<String,Node> map = realm.getEngine().getProperties(node);
363         return map.get(name);
364     }
365
366     @Override
367     public List<Node> getChildren(Node node) throws NodeManagerException {
368         checkThreadAccess();
369         return new ArrayList<Node>(realm.getEngine().getChildren(node).values());
370     }
371
372     @Override
373     public List<Node> getProperties(Node node) throws NodeManagerException {
374         checkThreadAccess();
375         return new ArrayList<Node>(realm.getEngine().getProperties(node).values());
376     }
377
378     @Override
379     public Datatype getDatatype(Node node) throws NodeManagerException {
380         checkThreadAccess();
381         try {
382             Variant v = getEngineVariantOrCached(node);
383             Binding b = v.getBinding();
384             if(b == null) return null;
385             return b.type();
386         } catch (RuntimeBindingConstructionException e) {
387             // There is no datatype for all values
388         }
389         return null;
390     }
391
392     public void clear() {
393         valueCache.clear();
394         listeners.clear();
395     }
396
397 }