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 refreshVariables() {
267 realm.asyncExec(clearValueCache);
271 public void refreshVariablesSync() {
273 realm.syncExec(clearValueCache);
274 } catch (InterruptedException e) {
275 LOGGER.error("Synchronous value cache refresh was interrupted.", e);
277 fireNodeListenersSync();
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);
292 public Variant getValue(Node node) throws NodeManagerException {
294 return getEngineVariantOrCached(node);
297 protected void checkThreadAccess() throws NodeManagerException {
298 if(Thread.currentThread() != realm.getThread())
299 throw new NotInRealmException();
302 protected Datatype getDatatypeForValue(Object value) {
303 Binding binding = Bindings.getBindingUnchecked(value.getClass());
304 if(binding == null) return null;
305 else return binding.type();
309 public void setValue(Node node, Object value, Binding binding)
310 throws NodeManagerException {
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);
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);
334 public String getName(Node 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);
342 return realm.getEngine().getName(node);
347 public Node getNode(String path) throws NodeManagerException {
349 throw new UnsupportedOperationException();
353 public Node getChild(Node node, String name) throws NodeManagerException {
355 Map<String,Node> map = realm.getEngine().getChildren(node);
356 return map.get(name);
360 public Node getProperty(Node node, String name) throws NodeManagerException {
362 Map<String,Node> map = realm.getEngine().getProperties(node);
363 return map.get(name);
367 public List<Node> getChildren(Node node) throws NodeManagerException {
369 return new ArrayList<Node>(realm.getEngine().getChildren(node).values());
373 public List<Node> getProperties(Node node) throws NodeManagerException {
375 return new ArrayList<Node>(realm.getEngine().getProperties(node).values());
379 public Datatype getDatatype(Node node) throws NodeManagerException {
382 Variant v = getEngineVariantOrCached(node);
383 Binding b = v.getBinding();
384 if(b == null) return null;
386 } catch (RuntimeBindingConstructionException e) {
387 // There is no datatype for all values
392 public void clear() {