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.Collections;
17 import java.util.List;
20 import java.util.concurrent.atomic.AtomicBoolean;
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;
40 import gnu.trove.map.hash.THashMap;
41 import gnu.trove.procedure.TObjectProcedure;
42 import gnu.trove.set.hash.THashSet;
45 * StandardNodeManager gives default implementations to some methods
48 * @author Antti Villberg
50 public class StandardNodeManager<Node, Engine extends StandardNodeManagerSupport<Node>> implements NodeManager<Node> {
52 private static final Logger LOGGER = LoggerFactory.getLogger(StandardNodeManager.class);
54 protected final Node root;
55 protected final StandardRealm<Node,Engine> realm;
57 static final Binding NO_BINDING = new VariantBinding() {
60 public Object getContent(Object variant, Binding contentBinding) throws BindingException {
65 public Object getContent(Object variant) throws BindingException {
70 public Datatype getContentType(Object variant) throws BindingException {
75 public Binding getContentBinding(Object variant) throws BindingException {
80 public Object create(Binding contentBinding, Object content) throws BindingException {
85 public void setContent(Object variant, Binding contentBinding, Object content) throws BindingException {
90 public boolean isInstance(Object obj) {
95 public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
100 public int compare(Object o1, Object o2) throws org.simantics.databoard.binding.error.RuntimeBindingException {
105 return - System.identityHashCode(o2);
109 return System.identityHashCode(o1);
111 if(o1.equals(o2)) return 0;
112 return System.identityHashCode(o1) - System.identityHashCode(o2);
119 protected THashMap<Node, Variant> valueCache = new THashMap<>();
120 protected THashMap<Node, THashSet<Runnable>> listeners = new THashMap<>();
122 AtomicBoolean fireNodeListenersScheduled = new AtomicBoolean(false);
123 Runnable fireNodeListeners = new Runnable() {
126 fireNodeListenersScheduled.set(false);
127 TObjectProcedure<Runnable> procedure = r -> {
131 synchronized(listeners) {
132 listeners.forEachValue(set -> {
133 set.forEach(procedure);
140 Runnable clearValueCache = () -> valueCache.clear();
142 public StandardNodeManager(StandardRealm<Node,Engine> realm, Node root) {
143 assert(realm != null);
144 assert(root != null);
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));
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));
168 public Object getValue(Node node, String propertyName, Binding binding)
169 throws NodeManagerException, BindingException {
170 Node property = getProperty(node, propertyName);
172 throw new NoSuchNodeException("Didn't find a property " + propertyName);
173 return getValue(property, binding);
177 public void setValue(Node node, String propertyName, Object value,
178 Binding binding) throws NodeManagerException, BindingException {
179 Node property = getProperty(node, propertyName);
181 throw new NoSuchNodeException("Didn't find a property " + propertyName);
182 setValue(property, value, binding);
186 public Variant getValue(Node node, String propertyName)
187 throws NodeManagerException {
188 Node property = getProperty(node, propertyName);
190 throw new NoSuchNodeException("Didn't find a property " + propertyName);
191 return getValue(property);
195 public Object getValue(Node node, Binding binding) throws NodeManagerException, BindingException {
197 Variant value = getValue(node);
198 if(NodeManager.PENDING_NODE_VALUE == value)
200 return value.getValue(binding);
201 } catch (AdaptException e) {
202 throw new BindingException(e);
207 public String getPropertyURI(Node parent, Node property) {
212 public Realm getRealm() {
216 public StandardRealm<Node, Engine> getStandardRealm() {
220 protected String getRealmId() {
221 return realm.getId();
224 public Node getRoot() {
228 protected boolean isRoot(Node node) {
229 return root.equals(node);
233 public void addNodeListener(Node node, Runnable listener) {
234 synchronized(listeners) {
235 THashSet<Runnable> l = listeners.get(node);
237 l = new THashSet<>();
238 listeners.put(node, l);
242 getRealm().asyncExec(listener);
246 public void removeNodeListener(Node node, Runnable listener) {
247 synchronized(listeners) {
248 THashSet<Runnable> l = listeners.get(node);
252 listeners.remove(node);
257 public void fireNodeListeners() {
258 if(!fireNodeListenersScheduled.getAndSet(true))
259 realm.asyncExec(fireNodeListeners);
262 public void fireNodeListenersSync() {
264 realm.syncExec(fireNodeListeners);
265 } catch (InterruptedException e) {
266 LOGGER.error("Synchronous node listener firing was interrupted.", e);
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) {
284 public void refreshVariables() {
285 realm.asyncExec(clearValueCache);
289 public void refreshVariablesSync() {
291 realm.syncExec(clearValueCache);
292 } catch (InterruptedException e) {
293 LOGGER.error("Synchronous value cache refresh was interrupted.", e);
295 fireNodeListenersSync();
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);
312 public Variant getValue(Node node) throws NodeManagerException {
314 return getEngineVariantOrCached(node);
317 protected void checkThreadAccess() throws NodeManagerException {
318 if(Thread.currentThread() != realm.getThread())
319 throw new NotInRealmException();
322 protected Datatype getDatatypeForValue(Object value) {
323 Binding binding = Bindings.getBindingUnchecked(value.getClass());
324 if(binding == null) return null;
325 else return binding.type();
329 public void setValue(Node node, Object value, Binding binding)
330 throws NodeManagerException {
331 updateValueInner(node, value, binding);
332 refreshVariable(node);
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);
342 updateValueInner(node, value, binding);
343 fireNodeListenersSync();
346 //Update the value of the node helper method
347 private void updateValueInner(Node node, Object value, Binding binding) throws NodeManagerException {
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);
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);
370 public String getName(Node 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);
378 return realm.getEngine().getName(node);
383 public Node getNode(String path) throws NodeManagerException {
385 throw new UnsupportedOperationException();
389 public Node getChild(Node node, String name) throws NodeManagerException {
391 Map<String,Node> map = realm.getEngine().getChildren(node);
392 return map.get(name);
396 public Node getProperty(Node node, String name) throws NodeManagerException {
398 Map<String,Node> map = realm.getEngine().getProperties(node);
399 return map.get(name);
403 public List<Node> getChildren(Node node) throws NodeManagerException {
405 return new ArrayList<Node>(realm.getEngine().getChildren(node).values());
409 public List<Node> getProperties(Node node) throws NodeManagerException {
411 return new ArrayList<Node>(realm.getEngine().getProperties(node).values());
415 public Datatype getDatatype(Node node) throws NodeManagerException {
418 Variant v = getEngineVariantOrCached(node);
419 Binding b = v.getBinding();
420 if(b == null) return null;
422 } catch (RuntimeBindingConstructionException e) {
423 // There is no datatype for all values
428 public void clear() {
434 public Set<String> getClassifications(Node node) throws NodeManagerException {
435 return Collections.emptySet();