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 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.simulator.variable;
14 import java.util.Collections;
15 import java.util.List;
18 import org.simantics.databoard.binding.Binding;
19 import org.simantics.databoard.binding.error.BindingException;
20 import org.simantics.databoard.binding.mutable.Variant;
21 import org.simantics.databoard.type.Datatype;
22 import org.simantics.simulator.variable.exceptions.InvalidPathException;
23 import org.simantics.simulator.variable.exceptions.NoSuchNodeException;
24 import org.simantics.simulator.variable.exceptions.NoValueException;
25 import org.simantics.simulator.variable.exceptions.NodeManagerException;
26 import org.simantics.simulator.variable.exceptions.NotInRealmException;
29 * A proxy implementation of NodeManager for debugging purposes.
31 * It prints time-stamped enter + exit/return strings when methods are entered
34 * TODO: Implement optional extra delays for different methods.
36 * @author Tuukka Lehtonen
38 public class DebugNodeManager<Node> implements NodeManager<Node> {
40 protected NodeManager<Node> delegate;
42 public DebugNodeManager(NodeManager<Node> delegate) {
43 this.delegate = delegate;
46 // --- Do not require a realm access ---
48 protected long println(String msg) {
49 long ts = System.nanoTime();
50 double time = (double) ts * 1e-6;
51 System.out.format("[%s @%f] %s\n", delegate.getClass().getSimpleName(), time, msg);
55 protected long println(long previousTime, String msg) {
56 long ts = System.nanoTime();
57 long dt = ts - previousTime;
58 double time = (double) ts * 1e-6;
59 double dtime = (double) dt * 1e-6;
60 System.out.format("[%s @%f, took %f ms] %s\n", delegate.getClass().getSimpleName(), time, dtime, msg);
64 protected void format(String msg, Object... args) {
65 println(String.format(msg, args));
69 * The realm of the node manager. Almost all other methods
70 * of this class must be called inside this realm.
73 public Realm getRealm() {
75 return delegate.getRealm();
79 public String getName(Node node) {
80 long ts = println("enter getName(" + node + ")");
81 String name = delegate.getName(node);
82 println(ts, "return getName(" + node + ") = " + name);
87 * Adds a listener to a certain node. The new listener is called as
88 * soon as possible (for example before simulator takes the next simulation
89 * step). After the first call, it is called always the node value
90 * or structure may have changed. This can be called outside of the realm.
93 public void addNodeListener(Node node, Runnable listener) {
94 long ts = println("enter addNodeListener(" + node + ", " + listener + ")");
95 delegate.addNodeListener(node, listener);
96 println(ts, "exit addNodeListener(" + node + ", " + listener + ")");
100 * Removes previously added listener. This can be called outside of
104 public void removeNodeListener(Node node, Runnable listener) {
105 long ts = println("enter removeNodeListener(" + node + ", " + listener + ")");
106 delegate.removeNodeListener(node, listener);
107 println(ts, "exit removeNodeListener(" + node + ", " + listener + ")");
110 // --- Require a realm access ---
113 * @return {@code null} if node cannot be found, otherwise a node with the given path
114 * @throws InvalidPathException if the path is not in a valid path format
115 * @throws NotInRealmException if not synchronized to the realm
118 public Node getNode(String path) throws NodeManagerException {
119 long ts = println("enter getNode(" + path + ")");
120 Node node = getNode(path);
121 println(ts, "return getNode(" + path + ") = " + node);
125 * @return {@code null} if node cannot be found, otherwise a child node with the given name
126 * @throws NotInRealmException if not synchronized to the realm
129 public Node getChild(Node node, String name) throws NodeManagerException {
130 long ts = println("enter getChild(" + node + ", " + name + ")");
131 Node child = getChild(node, name);
132 println(ts, "return getChild(" + node + ", " + name + ") = " + child);
136 public Node getProperty(Node node, String name) throws NodeManagerException {
137 long ts = println("enter getProperty(" + node + ", " + name + ")");
138 Node property = delegate.getProperty(node, name);
139 println(ts, "return getProperty(" + node + ", " + name + ") = " + property);
143 public List<String> getChildNames(Node node) throws NodeManagerException {
144 long ts = println("enter getChildNames(" + node + ")");
145 List<String> childNames = delegate.getChildNames(node);
146 println(ts, "return getChildNames(" + node + ") = " + childNames);
150 public List<String> getPropertyNames(Node node) throws NodeManagerException {
151 long ts = println("enter getPropertyNames(" + node + ")");
152 List<String> propertyNames = delegate.getPropertyNames(node);
153 println(ts, "return getPropertyNames(" + node + ") = " + propertyNames);
154 return propertyNames;
157 public List<Node> getChildren(Node node) throws NodeManagerException {
158 long ts = println("enter getChildren(" + node + ")");
159 List<Node> children = delegate.getChildren(node);
160 println(ts, "return getChildren(" + node + ") = " + children);
164 public List<Node> getProperties(Node node) throws NodeManagerException {
165 long ts = println("enter getProperties(" + node + ")");
166 List<Node> properties = delegate.getProperties(node);
167 println(ts, "return getProperties(" + node + ") = " + properties);
172 * @throws NoValueException if the node has no value (and therefore no datatype)
173 * @throws NotInRealmException if not synchronized to the realm
176 public Datatype getDatatype(Node node) throws NodeManagerException {
177 long ts = println("enter getValue(" + node + ")");
178 Datatype datatype = delegate.getDatatype(node);
179 println(ts, "return getValue(" + node + ") = " + datatype);
183 * @throws NoValueException if the node has no value
184 * @throws NotInRealmException if not synchronized to the realm
187 public Object getValue(Node node, Binding binding) throws NodeManagerException, BindingException {
188 long ts = println("enter getValue(" + node + ", " + binding + ")");
189 Object value = delegate.getValue(node, binding);
190 println(ts, "return getValue(" + node + ", " + binding + ") = " + value);
194 * A variant of {@link #getValue(Object, Binding)} that uses
195 * a binding chosen by the node manager.
198 public Variant getValue(Node node) throws NodeManagerException {
199 long ts = println("enter getValue(" + node + ")");
200 Variant value = delegate.getValue(node);
201 println(ts, "return getValue(" + node + ") = " + value);
205 * @throws NoSuchNodeException if the property does not exist
206 * @throws NoValueException if the property has no value
207 * @throws NotInRealmException if not synchronized to the realm
208 * @throws BindingException if the value can not be bound to the given binding
211 public Object getValue(Node node, String property, Binding binding) throws NodeManagerException, BindingException {
212 long ts = println("enter getValue(" + node + ", " + property + ", " + binding + ")");
213 Object value = delegate.getValue(node, property, binding);
214 println(ts, "return getValue(" + node + ", " + property + ", " + binding + ") = " + value);
218 * A variant of {@link #getValue(Object, String, Binding)} that uses
219 * a binding chosen by the node manager.
222 public Variant getValue(Node node, String property) throws NodeManagerException {
223 long ts = println("enter getValue(" + node + ", " + property + ")");
224 Variant value = delegate.getValue(node, property);
225 println(ts, "return getValue(" + node + ", " + property + ") = " + value);
230 * @throws BindingException if the value can not be bound to the given binding
231 * @throws NoValueException if the property has no value that could be assigned
232 * @throws NotInRealmException if not synchronized to the realm
235 public void setValue(Node node, Object value, Binding binding) throws NodeManagerException, BindingException {
236 long ts = println("enter setValue(" + node + ", " + value + ", " + binding + ")");
237 delegate.setValue(node, value, binding);
238 println(ts, "exit setValue(" + node + ", " + value + ", " + binding + ")");
241 * @throws BindingException if the value can not be bound to the given binding
242 * @throws NoSuchNodeException if the property does not exist
243 * @throws NoValueException if the property has no value that could be assigned
244 * @throws NotInRealmException if not synchronized to the realm
247 public void setValue(Node node, String property, Object value, Binding binding) throws NodeManagerException, BindingException {
248 long ts = println("enter setValue(" + node + ", " + property + ", " + value + ", " + binding + ")");
249 delegate.setValue(node, property, value, binding);
250 println(ts, "exit setValue(" + node + ", " + property + ", " + value + ", " + binding + ")");
254 * Asks the full URI of a property node. The parent of the property is also given as a parameter.
255 * This is an optional method, NodeManager does not have to implement it for all nodes.
258 public String getPropertyURI(Node parent, Node property) {
259 long ts = println("enter getPropertyURI(" + parent + ", " + property + ")");
260 String result = delegate.getPropertyURI(parent, property);
261 println(ts, "return getPropertyURI(" + parent + ", " + property + ") = " + result);
266 * Asks for the classifications of a property node.
267 * This is an optional method, NodeManager does not have to implement it for all nodes.
268 * A default implementation should just return {@link Collections#emptySet()}.
269 * Classifications can be any strings, however a recommended interpretation is to return
270 * the URIs of the primary ontological types that this node corresponds to.
272 * @param node the node to classify
273 * @return classifications of the node, empty set if the node has no classifications
274 * @throws NodeManagerException
277 public Set<String> getClassifications(Node node) throws NodeManagerException {
278 long ts = println("enter getClassifications(" + node + ")");
279 Set<String> result = delegate.getClassifications(node);
280 println(ts, "return getClassifications(" + node + ") = " + result);