]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.simulator.variable/src/org/simantics/simulator/variable/DebugNodeManager.java b/bundles/org.simantics.simulator.variable/src/org/simantics/simulator/variable/DebugNodeManager.java
new file mode 100644 (file)
index 0000000..c092966
--- /dev/null
@@ -0,0 +1,284 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.simulator.variable;\r
+\r
+import java.util.Collections;\r
+import java.util.List;\r
+import java.util.Set;\r
+\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.simulator.variable.exceptions.InvalidPathException;\r
+import org.simantics.simulator.variable.exceptions.NoSuchNodeException;\r
+import org.simantics.simulator.variable.exceptions.NoValueException;\r
+import org.simantics.simulator.variable.exceptions.NodeManagerException;\r
+import org.simantics.simulator.variable.exceptions.NotInRealmException;\r
+\r
+/**\r
+ * A proxy implementation of NodeManager for debugging purposes.\r
+ * <p>\r
+ * It prints time-stamped enter + exit/return strings when methods are entered\r
+ * and exited.\r
+ * \r
+ * TODO: Implement optional extra delays for different methods.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class DebugNodeManager<Node> implements NodeManager<Node> {\r
+\r
+       protected NodeManager<Node> delegate;\r
+\r
+       public DebugNodeManager(NodeManager<Node> delegate) {\r
+               this.delegate = delegate;\r
+       }\r
+\r
+       // --- Do not require a realm access ---\r
+\r
+       protected long println(String msg) {\r
+               long ts = System.nanoTime();\r
+               double time = (double) ts * 1e-6;\r
+               System.out.format("[%s @%f] %s\n", delegate.getClass().getSimpleName(), time, msg);\r
+               return ts;\r
+       }\r
+\r
+       protected long println(long previousTime, String msg) {\r
+               long ts = System.nanoTime();\r
+               long dt = ts - previousTime;\r
+               double time = (double) ts * 1e-6;\r
+               double dtime = (double) dt * 1e-6;\r
+               System.out.format("[%s @%f, took %f ms] %s\n", delegate.getClass().getSimpleName(), time, dtime, msg);\r
+               return ts;\r
+       }\r
+\r
+       protected void format(String msg, Object... args) {\r
+               println(String.format(msg, args));\r
+       }\r
+\r
+       /**\r
+        * The realm of the node manager. Almost all other methods\r
+        * of this class must be called inside this realm.\r
+        */\r
+       @Override\r
+       public Realm getRealm() {\r
+               println("getRealm");\r
+               return delegate.getRealm();\r
+       }\r
+\r
+       @Override\r
+       public String getName(Node node) {\r
+               long ts = println("enter getName(" + node + ")");\r
+               String name = delegate.getName(node);\r
+               println(ts, "return getName(" + node + ") = " + name);\r
+               return name;\r
+       }\r
+\r
+       /**\r
+        * Adds a listener to a certain node. The new listener is called as\r
+        * soon as possible (for example before simulator takes the next simulation\r
+        * step). After the first call, it is called always the node value \r
+        * or structure may have changed. This can be called outside of the realm.\r
+        */\r
+       @Override\r
+       public void addNodeListener(Node node, Runnable listener) {\r
+               long ts = println("enter addNodeListener(" + node + ", " + listener + ")");\r
+               delegate.addNodeListener(node, listener);\r
+               println(ts, "exit addNodeListener(" + node + ", " + listener + ")");\r
+       }\r
+       \r
+       /**\r
+        * Removes previously added listener. This can be called outside of\r
+        * the realm.\r
+        */\r
+       @Override\r
+       public void removeNodeListener(Node node, Runnable listener) {\r
+               long ts = println("enter removeNodeListener(" + node + ", " + listener + ")");\r
+               delegate.removeNodeListener(node, listener);\r
+               println(ts, "exit removeNodeListener(" + node + ", " + listener + ")");\r
+       }\r
+       \r
+       // --- Require a realm access ---\r
+       \r
+       /**\r
+        * @return {@code null} if node cannot be found, otherwise a node with the given path\r
+        * @throws InvalidPathException if the path is not in a valid path format\r
+        * @throws NotInRealmException if not synchronized to the realm\r
+        */\r
+       @Override\r
+       public Node getNode(String path) throws NodeManagerException {\r
+               long ts = println("enter getNode(" + path + ")");\r
+               Node node = getNode(path);\r
+               println(ts, "return getNode(" + path + ") = " + node);\r
+               return node;\r
+       }\r
+       /**\r
+        * @return {@code null} if node cannot be found, otherwise a child node with the given name\r
+        * @throws NotInRealmException if not synchronized to the realm\r
+        */\r
+       @Override\r
+       public Node getChild(Node node, String name) throws NodeManagerException {\r
+               long ts = println("enter getChild(" + node + ", " + name + ")");\r
+               Node child = getChild(node, name);\r
+               println(ts, "return getChild(" + node + ", " + name + ") = " + child);\r
+               return child;\r
+       }\r
+       @Override\r
+       public Node getProperty(Node node, String name) throws NodeManagerException {\r
+               long ts = println("enter getProperty(" + node + ", " + name + ")");\r
+               Node property = delegate.getProperty(node, name);\r
+               println(ts, "return getProperty(" + node + ", " + name + ") = " + property);\r
+               return property;\r
+       }\r
+       @Override\r
+       public List<String> getChildNames(Node node) throws NodeManagerException {\r
+               long ts = println("enter getChildNames(" + node + ")");\r
+               List<String> childNames = delegate.getChildNames(node);\r
+               println(ts, "return getChildNames(" + node + ") = " + childNames);\r
+               return childNames;\r
+       }\r
+       @Override\r
+       public List<String> getPropertyNames(Node node) throws NodeManagerException {\r
+               long ts = println("enter getPropertyNames(" + node + ")");\r
+               List<String> propertyNames = delegate.getPropertyNames(node);\r
+               println(ts, "return getPropertyNames(" + node + ") = " + propertyNames);\r
+               return propertyNames;\r
+       }\r
+       @Override\r
+       public List<Node> getChildren(Node node) throws NodeManagerException {\r
+               long ts = println("enter getChildren(" + node + ")");\r
+               List<Node> children = delegate.getChildren(node);\r
+               println(ts, "return getChildren(" + node + ") = " + children);\r
+               return children;\r
+       }\r
+       @Override\r
+       public List<Node> getProperties(Node node) throws NodeManagerException {\r
+               long ts = println("enter getProperties(" + node + ")");\r
+               List<Node> properties = delegate.getProperties(node);\r
+               println(ts, "return getProperties(" + node + ") = " + properties);\r
+               return properties;\r
+       }\r
+\r
+       /**\r
+        * @throws NoValueException if the node has no value (and therefore no datatype)\r
+        * @throws NotInRealmException if not synchronized to the realm\r
+        */\r
+       @Override\r
+       public Datatype getDatatype(Node node) throws NodeManagerException {\r
+               long ts = println("enter getValue(" + node + ")");\r
+               Datatype datatype = delegate.getDatatype(node);\r
+               println(ts, "return getValue(" + node + ") = " + datatype);\r
+               return datatype;\r
+       }\r
+       /**\r
+        * @throws NoValueException if the node has no value\r
+        * @throws NotInRealmException if not synchronized to the realm\r
+        */\r
+       @Override\r
+       public Object getValue(Node node, Binding binding) throws NodeManagerException, BindingException {\r
+               long ts = println("enter getValue(" + node + ", " + binding + ")");\r
+               Object value = delegate.getValue(node, binding);\r
+               println(ts, "return getValue(" + node + ", " + binding + ") = " + value);\r
+               return value;\r
+       }\r
+       /**\r
+        * A variant of {@link #getValue(Object, Binding)} that uses\r
+        * a binding chosen by the node manager.\r
+        */\r
+       @Override\r
+       public Variant getValue(Node node) throws NodeManagerException {\r
+               long ts = println("enter getValue(" + node + ")");\r
+               Variant value = delegate.getValue(node);\r
+               println(ts, "return getValue(" + node + ") = " + value);\r
+               return value;\r
+       }\r
+       /**\r
+        * @throws NoSuchNodeException if the property does not exist\r
+        * @throws NoValueException if the property has no value\r
+        * @throws NotInRealmException if not synchronized to the realm\r
+        * @throws BindingException if the value can not be bound to the given binding\r
+        */\r
+       @Override\r
+       public Object getValue(Node node, String property, Binding binding) throws NodeManagerException, BindingException {\r
+               long ts = println("enter getValue(" + node + ", " + property + ", " + binding + ")");\r
+               Object value = delegate.getValue(node, property, binding);\r
+               println(ts, "return getValue(" + node + ", " + property + ", " + binding + ") = " + value);\r
+               return value;\r
+       }\r
+       /**\r
+        * A variant of {@link #getValue(Object, String, Binding)} that uses\r
+        * a binding chosen by the node manager.\r
+        */\r
+       @Override\r
+       public Variant getValue(Node node, String property) throws NodeManagerException {\r
+               long ts = println("enter getValue(" + node + ", " + property + ")");\r
+               Variant value = delegate.getValue(node, property);\r
+               println(ts, "return getValue(" + node + ", " + property + ") = " + value);\r
+               return value;\r
+       }\r
+       \r
+       /**\r
+        * @throws BindingException if the value can not be bound to the given binding\r
+        * @throws NoValueException if the property has no value that could be assigned\r
+        * @throws NotInRealmException if not synchronized to the realm\r
+        */\r
+       @Override\r
+       public void setValue(Node node, Object value, Binding binding) throws NodeManagerException, BindingException {\r
+               long ts = println("enter setValue(" + node + ", " + value + ", " + binding + ")");\r
+               delegate.setValue(node, value, binding);\r
+               println(ts, "exit setValue(" + node + ", " + value + ", " + binding + ")");\r
+       }\r
+       /**\r
+        * @throws BindingException if the value can not be bound to the given binding\r
+        * @throws NoSuchNodeException if the property does not exist\r
+        * @throws NoValueException if the property has no value that could be assigned\r
+        * @throws NotInRealmException if not synchronized to the realm\r
+        */\r
+       @Override\r
+       public void setValue(Node node, String property, Object value, Binding binding) throws NodeManagerException, BindingException {\r
+               long ts = println("enter setValue(" + node + ", " + property + ", " + value + ", " + binding + ")");\r
+               delegate.setValue(node, property, value, binding);\r
+               println(ts, "exit setValue(" + node + ", " + property + ", " + value + ", " + binding + ")");\r
+       }\r
+\r
+       /**\r
+        * Asks the full URI of a property node. The parent of the property is also given as a parameter.\r
+        * This is an optional method, NodeManager does not have to implement it for all nodes.\r
+        */\r
+       @Override\r
+       public String getPropertyURI(Node parent, Node property) {\r
+               long ts = println("enter getPropertyURI(" + parent + ", " + property + ")");\r
+               String result = delegate.getPropertyURI(parent, property);\r
+               println(ts, "return getPropertyURI(" + parent + ", " + property + ") = " + result);\r
+               return result;\r
+       }\r
+\r
+       /**\r
+        * Asks for the classifications of a property node.\r
+        * This is an optional method, NodeManager does not have to implement it for all nodes.\r
+        * A default implementation should just return {@link Collections#emptySet()}.\r
+        * Classifications can be any strings, however a recommended interpretation is to return\r
+        * the URIs of the primary ontological types that this node corresponds to.\r
+        * \r
+        * @param node the node to classify\r
+        * @return classifications of the node, empty set if the node has no classifications\r
+        * @throws NodeManagerException \r
+        */\r
+       @Override\r
+       public Set<String> getClassifications(Node node) throws NodeManagerException {\r
+               long ts = println("enter getClassifications(" + node + ")");\r
+               Set<String> result = delegate.getClassifications(node);\r
+               println(ts, "return getClassifications(" + node + ") = " + result);\r
+               return result;\r
+       }\r
+\r
+}
\ No newline at end of file