]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulator.variable/src/org/simantics/simulator/variable/DebugNodeManager.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.simulator.variable / src / org / simantics / simulator / variable / DebugNodeManager.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.simulator.variable;
13
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Set;
17
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;
27
28 /**
29  * A proxy implementation of NodeManager for debugging purposes.
30  * <p>
31  * It prints time-stamped enter + exit/return strings when methods are entered
32  * and exited.
33  * 
34  * TODO: Implement optional extra delays for different methods.
35  * 
36  * @author Tuukka Lehtonen
37  */
38 public class DebugNodeManager<Node> implements NodeManager<Node> {
39
40         protected NodeManager<Node> delegate;
41
42         public DebugNodeManager(NodeManager<Node> delegate) {
43                 this.delegate = delegate;
44         }
45
46         // --- Do not require a realm access ---
47
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);
52                 return ts;
53         }
54
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);
61                 return ts;
62         }
63
64         protected void format(String msg, Object... args) {
65                 println(String.format(msg, args));
66         }
67
68         /**
69          * The realm of the node manager. Almost all other methods
70          * of this class must be called inside this realm.
71          */
72         @Override
73         public Realm getRealm() {
74                 println("getRealm");
75                 return delegate.getRealm();
76         }
77
78         @Override
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);
83                 return name;
84         }
85
86         /**
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.
91          */
92         @Override
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 + ")");
97         }
98         
99         /**
100          * Removes previously added listener. This can be called outside of
101          * the realm.
102          */
103         @Override
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 + ")");
108         }
109         
110         // --- Require a realm access ---
111         
112         /**
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
116          */
117         @Override
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);
122                 return node;
123         }
124         /**
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
127          */
128         @Override
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);
133                 return child;
134         }
135         @Override
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);
140                 return property;
141         }
142         @Override
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);
147                 return childNames;
148         }
149         @Override
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;
155         }
156         @Override
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);
161                 return children;
162         }
163         @Override
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);
168                 return properties;
169         }
170
171         /**
172          * @throws NoValueException if the node has no value (and therefore no datatype)
173          * @throws NotInRealmException if not synchronized to the realm
174          */
175         @Override
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);
180                 return datatype;
181         }
182         /**
183          * @throws NoValueException if the node has no value
184          * @throws NotInRealmException if not synchronized to the realm
185          */
186         @Override
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);
191                 return value;
192         }
193         /**
194          * A variant of {@link #getValue(Object, Binding)} that uses
195          * a binding chosen by the node manager.
196          */
197         @Override
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);
202                 return value;
203         }
204         /**
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
209          */
210         @Override
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);
215                 return value;
216         }
217         /**
218          * A variant of {@link #getValue(Object, String, Binding)} that uses
219          * a binding chosen by the node manager.
220          */
221         @Override
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);
226                 return value;
227         }
228         
229         /**
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
233          */
234         @Override
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 + ")");
239         }
240         /**
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
245          */
246         @Override
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 + ")");
251         }
252
253         /**
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.
256          */
257         @Override
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);
262                 return result;
263         }
264
265         /**
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.
271          * 
272          * @param node the node to classify
273          * @return classifications of the node, empty set if the node has no classifications
274          * @throws NodeManagerException 
275          */
276         @Override
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);
281                 return result;
282         }
283
284 }