1 /*******************************************************************************
2 * Copyright (c) 2013 Association for Decentralized Information Management
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
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;
15 import java.util.ArrayList;
16 import java.util.List;
19 import java.util.concurrent.atomic.AtomicBoolean;
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;
39 import gnu.trove.map.hash.THashMap;
40 import gnu.trove.procedure.TObjectProcedure;
41 import gnu.trove.set.hash.THashSet;
44 * StandardNodeManager gives default implementations to some methods
47 * @author Antti Villberg
49 public abstract class StandardNodeManager<Node, Engine extends StandardNodeManagerSupport<Node>> implements NodeManager<Node> {
51 private static final Logger LOGGER = LoggerFactory.getLogger(StandardNodeManager.class);
53 private final Node root;
54 private final StandardRealm<Node,Engine> realm;
56 static final Binding NO_BINDING = new VariantBinding() {
59 public Object getContent(Object variant, Binding contentBinding) throws BindingException {
64 public Object getContent(Object variant) throws BindingException {
69 public Datatype getContentType(Object variant) throws BindingException {
74 public Binding getContentBinding(Object variant) throws BindingException {
79 public Object create(Binding contentBinding, Object content) throws BindingException {
84 public void setContent(Object variant, Binding contentBinding, Object content) throws BindingException {
89 public boolean isInstance(Object obj) {
94 public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
99 public int compare(Object o1, Object o2) throws org.simantics.databoard.binding.error.RuntimeBindingException {
104 return - System.identityHashCode(o2);
108 return System.identityHashCode(o1);
110 if(o1.equals(o2)) return 0;
111 return System.identityHashCode(o1) - System.identityHashCode(o2);
118 THashMap<Node, Variant> valueCache = new THashMap<>();
119 protected THashMap<Node, THashSet<Runnable>> listeners = new THashMap<>();
121 AtomicBoolean fireNodeListenersScheduled = new AtomicBoolean(false);
122 Runnable fireNodeListeners = new Runnable() {
125 fireNodeListenersScheduled.set(false);
126 TObjectProcedure<Runnable> procedure = r -> {
130 synchronized(listeners) {
131 listeners.forEachValue(set -> {
132 set.forEach(procedure);
139 Runnable clearValueCache = () -> valueCache.clear();
141 public StandardNodeManager(StandardRealm<Node,Engine> realm, Node root) {
142 assert(realm != null);
143 assert(root != null);
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));
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));
167 public Object getValue(Node node, String propertyName, Binding binding)
168 throws NodeManagerException, BindingException {
169 Node property = getProperty(node, propertyName);
171 throw new NoSuchNodeException("Didn't find a property " + propertyName);
172 return getValue(property, binding);
176 public void setValue(Node node, String propertyName, Object value,
177 Binding binding) throws NodeManagerException, BindingException {
178 Node property = getProperty(node, propertyName);
180 throw new NoSuchNodeException("Didn't find a property " + propertyName);
181 setValue(property, value, binding);
185 public Variant getValue(Node node, String propertyName)
186 throws NodeManagerException {
187 Node property = getProperty(node, propertyName);
189 throw new NoSuchNodeException("Didn't find a property " + propertyName);
190 return getValue(property);
194 public Object getValue(Node node, Binding binding) throws NodeManagerException, BindingException {
196 return getValue(node).getValue(binding);
197 } catch (AdaptException e) {
198 throw new BindingException(e);
203 public String getPropertyURI(Node parent, Node property) {
208 public Realm getRealm() {
212 public StandardRealm<Node, Engine> getStandardRealm() {
216 protected String getRealmId() {
217 return realm.getId();
220 public Node getRoot() {
224 protected boolean isRoot(Node node) {
225 return root.equals(node);
229 public void addNodeListener(Node node, Runnable listener) {
230 synchronized(listeners) {
231 THashSet<Runnable> l = listeners.get(node);
233 l = new THashSet<>();
234 listeners.put(node, l);
238 getRealm().asyncExec(listener);
242 public void removeNodeListener(Node node, Runnable listener) {
243 synchronized(listeners) {
244 THashSet<Runnable> l = listeners.get(node);
248 listeners.remove(node);
253 public void fireNodeListeners() {
254 if(!fireNodeListenersScheduled.getAndSet(true))
255 realm.asyncExec(fireNodeListeners);
258 public void fireNodeListenersSync() {
260 realm.syncExec(fireNodeListeners);
261 } catch (InterruptedException e) {
262 LOGGER.error("Synchronous node listener firing was interrupted.", e);
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) {
280 public void refreshVariables() {
281 realm.asyncExec(clearValueCache);
285 public void refreshVariablesSync() {
287 realm.syncExec(clearValueCache);
288 } catch (InterruptedException e) {
289 LOGGER.error("Synchronous value cache refresh was interrupted.", e);
291 fireNodeListenersSync();
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);
306 public Variant getValue(Node node) throws NodeManagerException {
308 return getEngineVariantOrCached(node);
311 protected void checkThreadAccess() throws NodeManagerException {
312 if(Thread.currentThread() != realm.getThread())
313 throw new NotInRealmException();
316 protected Datatype getDatatypeForValue(Object value) {
317 Binding binding = Bindings.getBindingUnchecked(value.getClass());
318 if(binding == null) return null;
319 else return binding.type();
323 public void setValue(Node node, Object value, Binding binding)
324 throws NodeManagerException {
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);
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);
344 refreshVariable(node);
348 public String getName(Node 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);
356 return realm.getEngine().getName(node);
361 public Node getNode(String path) throws NodeManagerException {
363 throw new UnsupportedOperationException();
367 public Node getChild(Node node, String name) throws NodeManagerException {
369 Map<String,Node> map = realm.getEngine().getChildren(node);
370 return map.get(name);
374 public Node getProperty(Node node, String name) throws NodeManagerException {
376 Map<String,Node> map = realm.getEngine().getProperties(node);
377 return map.get(name);
381 public List<Node> getChildren(Node node) throws NodeManagerException {
383 return new ArrayList<Node>(realm.getEngine().getChildren(node).values());
387 public List<Node> getProperties(Node node) throws NodeManagerException {
389 return new ArrayList<Node>(realm.getEngine().getProperties(node).values());
393 public Datatype getDatatype(Node node) throws NodeManagerException {
396 Variant v = getEngineVariantOrCached(node);
397 Binding b = v.getBinding();
398 if(b == null) return null;
400 } catch (RuntimeBindingConstructionException e) {
401 // There is no datatype for all values
406 public void clear() {