ss: History trees can define their own node types
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core.tests / stubs / org / eclipse / tracecompass / statesystem / core / tests / stubs / backend / HistoryTreeClassicStub.java
index b46894d47819559ce90702ba1513b8f93a77e9f8..99a08f21560405e7c3c940035168f27a8d98aee6 100644 (file)
@@ -16,14 +16,17 @@ import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.nio.channels.ClosedChannelException;
 import java.util.List;
 
-import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.CoreNode;
+import org.eclipse.tracecompass.internal.statesystem.core.Activator;
 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTConfig;
 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTNode;
-import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HistoryTreeClassic;
 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.IHistoryTree;
+import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.ParentNode;
+import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.classic.CoreNode;
+import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.classic.HistoryTreeClassic;
 
 import com.google.common.collect.Iterables;
 
@@ -71,8 +74,13 @@ public class HistoryTreeClassicStub extends HistoryTreeClassic {
         super(existingStateFile, expProviderVersion);
     }
 
+    // ------------------------------------------------------------------------
+    // Extra test accessors
+    // ------------------------------------------------------------------------
+
     @Override
     public List<HTNode> getLatestBranch() {
+        /* Super method is not public */
         return checkNotNull(super.getLatestBranch());
     }
 
@@ -108,7 +116,115 @@ public class HistoryTreeClassicStub extends HistoryTreeClassic {
         return getLatestBranch().size();
     }
 
-    private void assertChildrenIntegrity(CoreNode node) {
+    // ------------------------------------------------------------------------
+    // Debug printing methods
+    // ------------------------------------------------------------------------
+
+    /**
+     * Print out the full tree for debugging purposes
+     *
+     * @param writer
+     *            PrintWriter in which to write the output
+     * @param printIntervals
+     *            Flag to enable full output of the interval information
+     * @param ts
+     *            The timestamp that nodes have to intersect for intervals to be
+     *            printed. A negative value will print intervals for all nodes.
+     *            The timestamp only applies if printIntervals is true.
+     */
+    public void debugPrintFullTree(PrintWriter writer, boolean printIntervals, long ts) {
+        preOrderPrint(writer, false, getLatestBranch().get(0), 0, ts);
+
+        if (printIntervals) {
+            preOrderPrint(writer, true, getLatestBranch().get(0), 0, ts);
+        }
+        writer.println('\n');
+    }
+
+    /**
+     * Start at currentNode and print the contents of all its children, in
+     * pre-order. Give the root node in parameter to visit the whole tree, and
+     * have a nice overview.
+     */
+    private void preOrderPrint(PrintWriter writer, boolean printIntervals,
+            HTNode currentNode, int curDepth, long ts) {
+
+        writer.println(currentNode.toString());
+        /*
+         * Print intervals only if timestamp is negative or within the range of
+         * the node
+         */
+        if (printIntervals &&
+                (ts <= 0 ||
+                        (ts >= currentNode.getNodeStart() && ts <= currentNode.getNodeEnd()))) {
+            currentNode.debugPrintIntervals(writer);
+        }
+
+        switch (currentNode.getNodeType()) {
+        case LEAF:
+            /* Stop if it's the leaf node */
+            return;
+
+        case CORE:
+            try {
+                final CoreNode node = (CoreNode) currentNode;
+                /* If node extensions were used, they would be printed here. */
+
+                /* Print the child nodes */
+                for (int i = 0; i < node.getNbChildren(); i++) {
+                    HTNode nextNode = getTreeIO().readNode(node.getChild(i));
+                    for (int j = 0; j < curDepth; j++) {
+                        writer.print("  ");
+                    }
+                    writer.print("+-");
+                    preOrderPrint(writer, printIntervals, nextNode, curDepth + 1, ts);
+                }
+            } catch (ClosedChannelException e) {
+                Activator.getDefault().logError(e.getMessage());
+            }
+            break;
+
+        default:
+            break;
+        }
+    }
+
+    // ------------------------------------------------------------------------
+    // Assertion methods, for use with JUnit tests
+    // ------------------------------------------------------------------------
+
+    /**
+     * Check the integrity of all the nodes in the tree. Calls
+     * {@link #assertNodeIntegrity} for every node in the tree.
+     */
+    public void assertIntegrity() {
+        try {
+            for (int i = 0; i < getNodeCount(); i++) {
+                assertNodeIntegrity(getNode(i));
+            }
+        } catch (ClosedChannelException e) {
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Debugging method to make sure all intervals contained in the given node
+     * have valid start and end times.
+     *
+     * @param node
+     *            The node to check
+     */
+    private void assertNodeIntegrity(HTNode node) {
+        if (node instanceof ParentNode) {
+            assertChildrenIntegrity((ParentNode) node);
+        }
+
+        /* Check that all intervals are within the node's range */
+        // TODO: Get the intervals of a node
+
+    }
+
+    private void assertChildrenIntegrity(ParentNode node) {
         try {
             /*
              * Test that this node's start and end times match the start of the
@@ -148,35 +264,4 @@ public class HistoryTreeClassicStub extends HistoryTreeClassic {
         }
     }
 
-    /**
-     * Debugging method to make sure all intervals contained in the given node
-     * have valid start and end times.
-     *
-     * @param node
-     *            The node to check
-     */
-    private void assertNodeIntegrity(HTNode node) {
-        if (node instanceof CoreNode) {
-            assertChildrenIntegrity((CoreNode) node);
-        }
-
-        /* Check that all intervals are within the node's range */
-        // TODO: Get the intervals of a node
-
-    }
-
-    /**
-     * Check the integrity of all the nodes in the tree. Calls
-     * {@link #assertNodeIntegrity} for every node in the tree.
-     */
-    public void assertIntegrity() {
-        try {
-            for (int i = 0; i < getNodeCount(); i++) {
-                assertNodeIntegrity(getNode(i));
-            }
-        } catch (ClosedChannelException e) {
-            fail(e.getMessage());
-        }
-    }
-
 }
This page took 0.027489 seconds and 5 git commands to generate.