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.db.layer0;
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.binding.Binding;
23 import org.simantics.databoard.binding.VariantBinding;
24 import org.simantics.databoard.binding.error.BindingConstructionException;
25 import org.simantics.databoard.binding.error.BindingException;
26 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
27 import org.simantics.databoard.binding.mutable.Variant;
28 import org.simantics.databoard.type.Datatype;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.simulator.variable.NodeManager;
31 import org.simantics.simulator.variable.Realm;
32 import org.simantics.simulator.variable.exceptions.NoSuchNodeException;
33 import org.simantics.simulator.variable.exceptions.NodeManagerException;
34 import org.simantics.simulator.variable.exceptions.NotInRealmException;
36 import gnu.trove.map.hash.THashMap;
37 import gnu.trove.procedure.TObjectProcedure;
38 import gnu.trove.set.hash.THashSet;
41 * StandardNodeManager gives default implementations to some methods
44 * @author Antti Villberg
46 public abstract class StandardNodeManager<Node,Engine extends StandardEngine<Node>> implements NodeManager<Node> {
48 final private Node root;
49 final private StandardRealm<Node,Engine> realm;
51 final static Binding NO_BINDING = new VariantBinding() {
54 public Object getContent(Object variant, Binding contentBinding) throws BindingException {
59 public Object getContent(Object variant) throws BindingException {
64 public Datatype getContentType(Object variant) throws BindingException {
69 public Binding getContentBinding(Object variant) throws BindingException {
74 public Object create(Binding contentBinding, Object content) throws BindingException {
79 public void setContent(Object variant, Binding contentBinding, Object content) throws BindingException {
84 public boolean isInstance(Object obj) {
89 public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
94 public int compare(Object o1, Object o2) throws org.simantics.databoard.binding.error.RuntimeBindingException {
99 return - System.identityHashCode(o2);
103 return System.identityHashCode(o1);
105 if(o1.equals(o2)) return 0;
106 return System.identityHashCode(o1) - System.identityHashCode(o2);
113 THashMap<Node, Object> valueCache = new THashMap<Node, Object>();
114 protected THashMap<Node, THashSet<Runnable>> listeners = new THashMap<Node, THashSet<Runnable>>();
116 AtomicBoolean fireNodeListenersScheduled = new AtomicBoolean(false);
117 Runnable fireNodeListeners = new Runnable() {
120 fireNodeListenersScheduled.set(false);
121 final TObjectProcedure<Runnable> procedure = new TObjectProcedure<Runnable>() {
123 public boolean execute(Runnable object) {
128 synchronized(listeners) {
129 listeners.forEachValue(new TObjectProcedure<THashSet<Runnable>>() {
131 public boolean execute(THashSet<Runnable> object) {
132 object.forEach(procedure);
140 Runnable clearValueCache = new Runnable() {
147 public StandardNodeManager(StandardRealm<Node,Engine> realm, Node root) {
153 public List<String> getChildNames(Node node) throws NodeManagerException {
154 List<Node> children = getChildren(node);
155 ArrayList<String> names = new ArrayList<String>(children.size());
156 for(Node child : children)
157 names.add(getName(child));
162 public List<String> getPropertyNames(Node node) throws NodeManagerException {
163 List<Node> properties = getProperties(node);
164 ArrayList<String> names = new ArrayList<String>(properties.size());
165 for(Node property : properties)
166 names.add(getName(property));
171 public Object getValue(Node node, String propertyName, Binding binding)
172 throws NodeManagerException, BindingException {
173 Node property = getProperty(node, propertyName);
175 throw new NoSuchNodeException("Didn't find a property " + propertyName);
176 return getValue(property, binding);
180 public void setValue(Node node, String propertyName, Object value,
181 Binding binding) throws NodeManagerException, BindingException {
182 Node property = getProperty(node, propertyName);
184 throw new NoSuchNodeException("Didn't find a property " + propertyName);
185 setValue(property, value, binding);
189 public Variant getValue(Node node) throws NodeManagerException {
190 Object value = getEngineValueOrCached(node);
191 if (value instanceof Variant)
192 return (Variant) value;
194 Binding binding = Bindings.getBinding(value.getClass());
195 return new Variant(binding, value);
196 } catch (BindingConstructionException e) {
203 public Variant getValue(Node node, String propertyName)
204 throws NodeManagerException {
205 Node property = getProperty(node, propertyName);
207 throw new NoSuchNodeException("Didn't find a property " + propertyName);
208 return getValue(property);
212 public String getPropertyURI(Node parent, Node property) {
217 public Realm getRealm() {
221 public StandardRealm<Node, Engine> getStandardRealm() {
225 protected String getRealmId() {
229 public Node getRoot() {
233 protected boolean isRoot(Node node) {
234 return root.equals(node);
238 public void addNodeListener(Node node, Runnable listener) {
239 synchronized(listeners) {
240 THashSet<Runnable> l = listeners.get(node);
242 l = new THashSet<Runnable>();
243 listeners.put(node, l);
247 getRealm().asyncExec(listener);
251 public void removeNodeListener(Node node, Runnable listener) {
252 synchronized(listeners) {
253 THashSet<Runnable> l = listeners.get(node);
257 listeners.remove(node);
262 public void fireNodeListeners() {
263 if(!fireNodeListenersScheduled.getAndSet(true))
264 realm.asyncExec(fireNodeListeners);
267 public void fireNodeListenersSync() {
269 realm.syncExec(fireNodeListeners);
270 } catch (InterruptedException e) {
275 public void refreshVariables() {
276 realm.asyncExec(clearValueCache);
280 public void refreshVariablesSync() {
282 realm.syncExec(clearValueCache);
283 } catch (InterruptedException e) {
286 fireNodeListenersSync();
289 protected Object getEngineValueOrCached(Node node) throws NodeManagerException {
290 Object value = valueCache.get(node);
292 value = realm.getEngine().getValue(node);
293 valueCache.put(node, value);
300 public Object getValue(Node node, Binding binding) throws NodeManagerException {
302 return getEngineValueOrCached(node);
305 protected void checkThreadAccess() throws NodeManagerException {
306 if(Thread.currentThread() != realm.getThread())
307 throw new NotInRealmException();
310 protected Datatype getDatatypeForValue(Object value) throws DatabaseException {
311 Binding binding = Bindings.getBindingUnchecked(value.getClass());
312 if(binding == null) return null;
313 else return binding.type();
317 public void setValue(Node node, Object value, Binding binding)
318 throws NodeManagerException {
320 valueCache.put(node, value);
321 realm.getEngine().setValue(node, value);
322 realm.nodeManager.valueCache.put(node, value);
327 public String getName(Node node) {
329 String id = getRealmId();
330 int lastSlash = id.lastIndexOf("/");
331 if(lastSlash == -1) throw new IllegalStateException("Invalid realm id " + id);
332 String name = id.substring(lastSlash+1);
335 return realm.getEngine().getName(node);
341 public Node getNode(String path) throws NodeManagerException {
343 throw new UnsupportedOperationException();
347 public Node getChild(Node node, String name) throws NodeManagerException {
349 Map<String,Node> map = realm.getEngine().getChildren(node);
350 return map.get(name);
354 public Node getProperty(Node node, String name) throws NodeManagerException {
356 Map<String,Node> map = realm.getEngine().getProperties(node);
357 return map.get(name);
361 public List<Node> getChildren(Node node) throws NodeManagerException {
363 return new ArrayList<Node>(realm.getEngine().getChildren(node).values());
367 public List<Node> getProperties(Node node) throws NodeManagerException {
369 return new ArrayList<Node>(realm.getEngine().getProperties(node).values());
373 public Datatype getDatatype(Node node) throws NodeManagerException {
376 Datatype type = getDatatypeForValue(getEngineValueOrCached(node));
378 } catch (DatabaseException e) {
380 } catch (RuntimeBindingConstructionException e) {
381 // There is no datatype for all values