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 return getValue(node).getValue(binding);
198 } catch (AdaptException e) {
199 throw new BindingException(e);
204 public String getPropertyURI(Node parent, Node property) {
209 public Realm getRealm() {
213 public StandardRealm<Node, Engine> getStandardRealm() {
217 protected String getRealmId() {
218 return realm.getId();
221 public Node getRoot() {
225 protected boolean isRoot(Node node) {
226 return root.equals(node);
230 public void addNodeListener(Node node, Runnable listener) {
231 synchronized(listeners) {
232 THashSet<Runnable> l = listeners.get(node);
234 l = new THashSet<>();
235 listeners.put(node, l);
239 getRealm().asyncExec(listener);
243 public void removeNodeListener(Node node, Runnable listener) {
244 synchronized(listeners) {
245 THashSet<Runnable> l = listeners.get(node);
249 listeners.remove(node);
254 public void fireNodeListeners() {
255 if(!fireNodeListenersScheduled.getAndSet(true))
256 realm.asyncExec(fireNodeListeners);
259 public void fireNodeListenersSync() {
261 realm.syncExec(fireNodeListeners);
262 } catch (InterruptedException e) {
263 LOGGER.error("Synchronous node listener firing was interrupted.", e);
267 public void refreshVariable(Node node) {
268 realm.asyncExec(() -> {
269 valueCache.remove(node);
270 synchronized(listeners) {
271 THashSet<Runnable> runnables = listeners.get(node);
272 if (runnables != null) {
273 for (Runnable r : runnables) {
281 public void refreshVariables() {
282 realm.asyncExec(clearValueCache);
286 public void refreshVariablesSync() {
288 realm.syncExec(clearValueCache);
289 } catch (InterruptedException e) {
290 LOGGER.error("Synchronous value cache refresh was interrupted.", e);
292 fireNodeListenersSync();
295 protected Variant getEngineVariantOrCached(Node node) throws NodeManagerException {
296 Variant variant = valueCache.get(node);
297 if(variant == null) {
298 Object value = realm.getEngine().getEngineValue(node);
299 Binding binding = realm.getEngine().getEngineBinding(node);
300 variant = new Variant(binding, value);
301 valueCache.put(node, variant);
307 public Variant getValue(Node node) throws NodeManagerException {
309 return getEngineVariantOrCached(node);
312 protected void checkThreadAccess() throws NodeManagerException {
313 if(Thread.currentThread() != realm.getThread())
314 throw new NotInRealmException();
317 protected Datatype getDatatypeForValue(Object value) {
318 Binding binding = Bindings.getBindingUnchecked(value.getClass());
319 if(binding == null) return null;
320 else return binding.type();
324 public void setValue(Node node, Object value, Binding binding)
325 throws NodeManagerException {
326 updateValueInner(node, value, binding);
327 refreshVariable(node);
330 //Update the value of the node and remove from valueCache only the references nodes
331 public void setValueAndFireSelectedListeners(Node node, Object value, Binding binding, Set<Node> references) throws NodeManagerException {
332 if(references.size() > 0) {
333 for(Node n : references) {
334 valueCache.remove(n);
337 updateValueInner(node, value, binding);
338 fireNodeListenersSync();
341 //Update the value of the node helper method
342 private void updateValueInner(Node node, Object value, Binding binding) throws NodeManagerException {
344 Binding targetBinding = realm.getEngine().getEngineBinding(node);
345 if(binding.equals(targetBinding)) {
346 Variant variant = new Variant(binding, value);
347 valueCache.put(node, variant);
348 realm.getEngine().setEngineValue(node, value);
351 Adapter adapter = Bindings.getAdapter(binding, targetBinding);
352 Object targetValue = adapter.adapt(value);
353 Variant variant = new Variant(targetBinding, targetValue);
354 valueCache.put(node, variant);
355 realm.getEngine().setEngineValue(node, targetValue);
356 } catch (AdapterConstructionException e) {
357 throw new NodeManagerException(e);
358 } catch (AdaptException e) {
359 throw new NodeManagerException(e);
365 public String getName(Node node) {
367 String id = getRealmId();
368 int lastSlash = id.lastIndexOf("/");
369 if(lastSlash == -1) throw new IllegalStateException("Invalid realm id " + id);
370 String name = id.substring(lastSlash+1);
373 return realm.getEngine().getName(node);
378 public Node getNode(String path) throws NodeManagerException {
380 throw new UnsupportedOperationException();
384 public Node getChild(Node node, String name) throws NodeManagerException {
386 Map<String,Node> map = realm.getEngine().getChildren(node);
387 return map.get(name);
391 public Node getProperty(Node node, String name) throws NodeManagerException {
393 Map<String,Node> map = realm.getEngine().getProperties(node);
394 return map.get(name);
398 public List<Node> getChildren(Node node) throws NodeManagerException {
400 return new ArrayList<Node>(realm.getEngine().getChildren(node).values());
404 public List<Node> getProperties(Node node) throws NodeManagerException {
406 return new ArrayList<Node>(realm.getEngine().getProperties(node).values());
410 public Datatype getDatatype(Node node) throws NodeManagerException {
413 Variant v = getEngineVariantOrCached(node);
414 Binding b = v.getBinding();
415 if(b == null) return null;
417 } catch (RuntimeBindingConstructionException e) {
418 // There is no datatype for all values
423 public void clear() {
429 public Set<String> getClassifications(Node node) throws NodeManagerException {
430 return Collections.emptySet();