]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulator.variable/src/org/simantics/simulator/variable/DebugNodeManager.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.simulator.variable / src / org / simantics / simulator / variable / DebugNodeManager.java
1 /*******************************************************************************\r
2  * Copyright (c) 2013 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.simulator.variable;\r
13 \r
14 import java.util.Collections;\r
15 import java.util.List;\r
16 import java.util.Set;\r
17 \r
18 import org.simantics.databoard.binding.Binding;\r
19 import org.simantics.databoard.binding.error.BindingException;\r
20 import org.simantics.databoard.binding.mutable.Variant;\r
21 import org.simantics.databoard.type.Datatype;\r
22 import org.simantics.simulator.variable.exceptions.InvalidPathException;\r
23 import org.simantics.simulator.variable.exceptions.NoSuchNodeException;\r
24 import org.simantics.simulator.variable.exceptions.NoValueException;\r
25 import org.simantics.simulator.variable.exceptions.NodeManagerException;\r
26 import org.simantics.simulator.variable.exceptions.NotInRealmException;\r
27 \r
28 /**\r
29  * A proxy implementation of NodeManager for debugging purposes.\r
30  * <p>\r
31  * It prints time-stamped enter + exit/return strings when methods are entered\r
32  * and exited.\r
33  * \r
34  * TODO: Implement optional extra delays for different methods.\r
35  * \r
36  * @author Tuukka Lehtonen\r
37  */\r
38 public class DebugNodeManager<Node> implements NodeManager<Node> {\r
39 \r
40         protected NodeManager<Node> delegate;\r
41 \r
42         public DebugNodeManager(NodeManager<Node> delegate) {\r
43                 this.delegate = delegate;\r
44         }\r
45 \r
46         // --- Do not require a realm access ---\r
47 \r
48         protected long println(String msg) {\r
49                 long ts = System.nanoTime();\r
50                 double time = (double) ts * 1e-6;\r
51                 System.out.format("[%s @%f] %s\n", delegate.getClass().getSimpleName(), time, msg);\r
52                 return ts;\r
53         }\r
54 \r
55         protected long println(long previousTime, String msg) {\r
56                 long ts = System.nanoTime();\r
57                 long dt = ts - previousTime;\r
58                 double time = (double) ts * 1e-6;\r
59                 double dtime = (double) dt * 1e-6;\r
60                 System.out.format("[%s @%f, took %f ms] %s\n", delegate.getClass().getSimpleName(), time, dtime, msg);\r
61                 return ts;\r
62         }\r
63 \r
64         protected void format(String msg, Object... args) {\r
65                 println(String.format(msg, args));\r
66         }\r
67 \r
68         /**\r
69          * The realm of the node manager. Almost all other methods\r
70          * of this class must be called inside this realm.\r
71          */\r
72         @Override\r
73         public Realm getRealm() {\r
74                 println("getRealm");\r
75                 return delegate.getRealm();\r
76         }\r
77 \r
78         @Override\r
79         public String getName(Node node) {\r
80                 long ts = println("enter getName(" + node + ")");\r
81                 String name = delegate.getName(node);\r
82                 println(ts, "return getName(" + node + ") = " + name);\r
83                 return name;\r
84         }\r
85 \r
86         /**\r
87          * Adds a listener to a certain node. The new listener is called as\r
88          * soon as possible (for example before simulator takes the next simulation\r
89          * step). After the first call, it is called always the node value \r
90          * or structure may have changed. This can be called outside of the realm.\r
91          */\r
92         @Override\r
93         public void addNodeListener(Node node, Runnable listener) {\r
94                 long ts = println("enter addNodeListener(" + node + ", " + listener + ")");\r
95                 delegate.addNodeListener(node, listener);\r
96                 println(ts, "exit addNodeListener(" + node + ", " + listener + ")");\r
97         }\r
98         \r
99         /**\r
100          * Removes previously added listener. This can be called outside of\r
101          * the realm.\r
102          */\r
103         @Override\r
104         public void removeNodeListener(Node node, Runnable listener) {\r
105                 long ts = println("enter removeNodeListener(" + node + ", " + listener + ")");\r
106                 delegate.removeNodeListener(node, listener);\r
107                 println(ts, "exit removeNodeListener(" + node + ", " + listener + ")");\r
108         }\r
109         \r
110         // --- Require a realm access ---\r
111         \r
112         /**\r
113          * @return {@code null} if node cannot be found, otherwise a node with the given path\r
114          * @throws InvalidPathException if the path is not in a valid path format\r
115          * @throws NotInRealmException if not synchronized to the realm\r
116          */\r
117         @Override\r
118         public Node getNode(String path) throws NodeManagerException {\r
119                 long ts = println("enter getNode(" + path + ")");\r
120                 Node node = getNode(path);\r
121                 println(ts, "return getNode(" + path + ") = " + node);\r
122                 return node;\r
123         }\r
124         /**\r
125          * @return {@code null} if node cannot be found, otherwise a child node with the given name\r
126          * @throws NotInRealmException if not synchronized to the realm\r
127          */\r
128         @Override\r
129         public Node getChild(Node node, String name) throws NodeManagerException {\r
130                 long ts = println("enter getChild(" + node + ", " + name + ")");\r
131                 Node child = getChild(node, name);\r
132                 println(ts, "return getChild(" + node + ", " + name + ") = " + child);\r
133                 return child;\r
134         }\r
135         @Override\r
136         public Node getProperty(Node node, String name) throws NodeManagerException {\r
137                 long ts = println("enter getProperty(" + node + ", " + name + ")");\r
138                 Node property = delegate.getProperty(node, name);\r
139                 println(ts, "return getProperty(" + node + ", " + name + ") = " + property);\r
140                 return property;\r
141         }\r
142         @Override\r
143         public List<String> getChildNames(Node node) throws NodeManagerException {\r
144                 long ts = println("enter getChildNames(" + node + ")");\r
145                 List<String> childNames = delegate.getChildNames(node);\r
146                 println(ts, "return getChildNames(" + node + ") = " + childNames);\r
147                 return childNames;\r
148         }\r
149         @Override\r
150         public List<String> getPropertyNames(Node node) throws NodeManagerException {\r
151                 long ts = println("enter getPropertyNames(" + node + ")");\r
152                 List<String> propertyNames = delegate.getPropertyNames(node);\r
153                 println(ts, "return getPropertyNames(" + node + ") = " + propertyNames);\r
154                 return propertyNames;\r
155         }\r
156         @Override\r
157         public List<Node> getChildren(Node node) throws NodeManagerException {\r
158                 long ts = println("enter getChildren(" + node + ")");\r
159                 List<Node> children = delegate.getChildren(node);\r
160                 println(ts, "return getChildren(" + node + ") = " + children);\r
161                 return children;\r
162         }\r
163         @Override\r
164         public List<Node> getProperties(Node node) throws NodeManagerException {\r
165                 long ts = println("enter getProperties(" + node + ")");\r
166                 List<Node> properties = delegate.getProperties(node);\r
167                 println(ts, "return getProperties(" + node + ") = " + properties);\r
168                 return properties;\r
169         }\r
170 \r
171         /**\r
172          * @throws NoValueException if the node has no value (and therefore no datatype)\r
173          * @throws NotInRealmException if not synchronized to the realm\r
174          */\r
175         @Override\r
176         public Datatype getDatatype(Node node) throws NodeManagerException {\r
177                 long ts = println("enter getValue(" + node + ")");\r
178                 Datatype datatype = delegate.getDatatype(node);\r
179                 println(ts, "return getValue(" + node + ") = " + datatype);\r
180                 return datatype;\r
181         }\r
182         /**\r
183          * @throws NoValueException if the node has no value\r
184          * @throws NotInRealmException if not synchronized to the realm\r
185          */\r
186         @Override\r
187         public Object getValue(Node node, Binding binding) throws NodeManagerException, BindingException {\r
188                 long ts = println("enter getValue(" + node + ", " + binding + ")");\r
189                 Object value = delegate.getValue(node, binding);\r
190                 println(ts, "return getValue(" + node + ", " + binding + ") = " + value);\r
191                 return value;\r
192         }\r
193         /**\r
194          * A variant of {@link #getValue(Object, Binding)} that uses\r
195          * a binding chosen by the node manager.\r
196          */\r
197         @Override\r
198         public Variant getValue(Node node) throws NodeManagerException {\r
199                 long ts = println("enter getValue(" + node + ")");\r
200                 Variant value = delegate.getValue(node);\r
201                 println(ts, "return getValue(" + node + ") = " + value);\r
202                 return value;\r
203         }\r
204         /**\r
205          * @throws NoSuchNodeException if the property does not exist\r
206          * @throws NoValueException if the property has no value\r
207          * @throws NotInRealmException if not synchronized to the realm\r
208          * @throws BindingException if the value can not be bound to the given binding\r
209          */\r
210         @Override\r
211         public Object getValue(Node node, String property, Binding binding) throws NodeManagerException, BindingException {\r
212                 long ts = println("enter getValue(" + node + ", " + property + ", " + binding + ")");\r
213                 Object value = delegate.getValue(node, property, binding);\r
214                 println(ts, "return getValue(" + node + ", " + property + ", " + binding + ") = " + value);\r
215                 return value;\r
216         }\r
217         /**\r
218          * A variant of {@link #getValue(Object, String, Binding)} that uses\r
219          * a binding chosen by the node manager.\r
220          */\r
221         @Override\r
222         public Variant getValue(Node node, String property) throws NodeManagerException {\r
223                 long ts = println("enter getValue(" + node + ", " + property + ")");\r
224                 Variant value = delegate.getValue(node, property);\r
225                 println(ts, "return getValue(" + node + ", " + property + ") = " + value);\r
226                 return value;\r
227         }\r
228         \r
229         /**\r
230          * @throws BindingException if the value can not be bound to the given binding\r
231          * @throws NoValueException if the property has no value that could be assigned\r
232          * @throws NotInRealmException if not synchronized to the realm\r
233          */\r
234         @Override\r
235         public void setValue(Node node, Object value, Binding binding) throws NodeManagerException, BindingException {\r
236                 long ts = println("enter setValue(" + node + ", " + value + ", " + binding + ")");\r
237                 delegate.setValue(node, value, binding);\r
238                 println(ts, "exit setValue(" + node + ", " + value + ", " + binding + ")");\r
239         }\r
240         /**\r
241          * @throws BindingException if the value can not be bound to the given binding\r
242          * @throws NoSuchNodeException if the property does not exist\r
243          * @throws NoValueException if the property has no value that could be assigned\r
244          * @throws NotInRealmException if not synchronized to the realm\r
245          */\r
246         @Override\r
247         public void setValue(Node node, String property, Object value, Binding binding) throws NodeManagerException, BindingException {\r
248                 long ts = println("enter setValue(" + node + ", " + property + ", " + value + ", " + binding + ")");\r
249                 delegate.setValue(node, property, value, binding);\r
250                 println(ts, "exit setValue(" + node + ", " + property + ", " + value + ", " + binding + ")");\r
251         }\r
252 \r
253         /**\r
254          * Asks the full URI of a property node. The parent of the property is also given as a parameter.\r
255          * This is an optional method, NodeManager does not have to implement it for all nodes.\r
256          */\r
257         @Override\r
258         public String getPropertyURI(Node parent, Node property) {\r
259                 long ts = println("enter getPropertyURI(" + parent + ", " + property + ")");\r
260                 String result = delegate.getPropertyURI(parent, property);\r
261                 println(ts, "return getPropertyURI(" + parent + ", " + property + ") = " + result);\r
262                 return result;\r
263         }\r
264 \r
265         /**\r
266          * Asks for the classifications of a property node.\r
267          * This is an optional method, NodeManager does not have to implement it for all nodes.\r
268          * A default implementation should just return {@link Collections#emptySet()}.\r
269          * Classifications can be any strings, however a recommended interpretation is to return\r
270          * the URIs of the primary ontological types that this node corresponds to.\r
271          * \r
272          * @param node the node to classify\r
273          * @return classifications of the node, empty set if the node has no classifications\r
274          * @throws NodeManagerException \r
275          */\r
276         @Override\r
277         public Set<String> getClassifications(Node node) throws NodeManagerException {\r
278                 long ts = println("enter getClassifications(" + node + ")");\r
279                 Set<String> result = delegate.getClassifications(node);\r
280                 println(ts, "return getClassifications(" + node + ") = " + result);\r
281                 return result;\r
282         }\r
283 \r
284 }