--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2015 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.analysis.graph.core.tests.graph;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import java.util.List;
+
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.tracecompass.analysis.graph.core.base.TmfGraph;
+import org.eclipse.tracecompass.analysis.graph.core.base.TmfVertex;
+import org.eclipse.tracecompass.analysis.graph.core.base.TmfVertex.EdgeDirection;
+import org.eclipse.tracecompass.analysis.graph.core.building.TmfGraphBuilderModule;
+import org.eclipse.tracecompass.analysis.graph.core.tests.Activator;
+import org.eclipse.tracecompass.analysis.graph.core.tests.stubs.TestGraphWorker;
+import org.eclipse.tracecompass.analysis.graph.core.tests.stubs.module.GraphBuilderModuleStub;
+import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
+import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
+import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
+import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
+import org.junit.Test;
+
+/**
+ * Test suite for the {@link TmfGraphBuilderModule} class
+ *
+ * @author Geneviève Bastien
+ * @author Francis Giraldeau
+ */
+public class TmfGraphBuilderModuleTest {
+
+ private static final String STUB_TRACE_FILE = "testfiles/stubtrace.xml";
+
+ /**
+ * With this trace, the resulting graph should look like this:
+ *
+ * <pre>
+ * 0 1 2 3 4 5 6 7 8 9 10 11 12 13
+ * Player 1 *--* *-----* *
+ * | | |
+ * Player 2 *--------* *------*
+ * </pre>
+ *
+ * @return
+ */
+ private TmfGraphBuilderModule getModule() {
+ ITmfTrace trace = new TmfXmlTraceStub();
+ IPath filePath = Activator.getAbsoluteFilePath(STUB_TRACE_FILE);
+ IStatus status = trace.validate(null, filePath.toOSString());
+ if (!status.isOK()) {
+ fail(status.getException().getMessage());
+ }
+ try {
+ trace.initTrace(null, filePath.toOSString(), TmfEvent.class);
+ } catch (TmfTraceException e) {
+ fail(e.getMessage());
+ }
+ ((TmfTrace) trace).traceOpened(new TmfTraceOpenedSignal(this, trace, null));
+ GraphBuilderModuleStub module = null;
+ for (GraphBuilderModuleStub mod : TmfTraceUtils.getAnalysisModulesOfClass(trace, GraphBuilderModuleStub.class)) {
+ module = mod;
+ }
+ assertNotNull(module);
+
+ return module;
+ }
+
+ /**
+ * Test the graph builder execution
+ */
+ @Test
+ public void testBuildGraph() {
+
+ TmfGraphBuilderModule module = getModule();
+ module.schedule();
+ module.waitForCompletion();
+
+ TmfGraph graph = module.getGraph();
+ assertNotNull(graph);
+
+ assertEquals(2, graph.getWorkers().size());
+ assertEquals(9, graph.size());
+
+ List<TmfVertex> vertices = graph.getNodesOf(new TestGraphWorker(1));
+ assertEquals(5, vertices.size());
+
+ long timestamps1[] = { 1, 2, 5, 7, 12 };
+ boolean hasEdges1[][] = {
+ { false, true, false, false },
+ { true, false, false, true },
+ { false, true, true, false },
+ { true, false, false, false},
+ { false, false, true, false} };
+ for (int i = 0; i < vertices.size(); i++) {
+ TmfVertex v = vertices.get(i);
+ assertEquals(timestamps1[i], v.getTs());
+ assertEquals(hasEdges1[i][0], v.getEdge(EdgeDirection.INCOMING_HORIZONTAL_EDGE) != null);
+ assertEquals(hasEdges1[i][1], v.getEdge(EdgeDirection.OUTGOING_HORIZONTAL_EDGE) != null);
+ assertEquals(hasEdges1[i][2], v.getEdge(EdgeDirection.INCOMING_VERTICAL_EDGE) != null);
+ assertEquals(hasEdges1[i][3], v.getEdge(EdgeDirection.OUTGOING_VERTICAL_EDGE) != null);
+ }
+
+ vertices = graph.getNodesOf(new TestGraphWorker(2));
+ assertEquals(4, vertices.size());
+
+ long timestamps2[] = { 2, 5, 10, 12 };
+ boolean hasEdges2[][] = {
+ { false, true, true, false },
+ { true, false, false, true },
+ { false, true, false, false },
+ { true, false, false, true} };
+ for (int i = 0; i < vertices.size(); i++) {
+ TmfVertex v = vertices.get(i);
+ assertEquals(timestamps2[i], v.getTs());
+ assertEquals(hasEdges2[i][0], v.getEdge(EdgeDirection.INCOMING_HORIZONTAL_EDGE) != null);
+ assertEquals(hasEdges2[i][1], v.getEdge(EdgeDirection.OUTGOING_HORIZONTAL_EDGE) != null);
+ assertEquals(hasEdges2[i][2], v.getEdge(EdgeDirection.INCOMING_VERTICAL_EDGE) != null);
+ assertEquals(hasEdges2[i][3], v.getEdge(EdgeDirection.OUTGOING_VERTICAL_EDGE) != null);
+ }
+
+ }
+
+}
--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2015 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.analysis.graph.core.tests.stubs.module;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.analysis.graph.core.base.IGraphWorker;
+import org.eclipse.tracecompass.analysis.graph.core.base.TmfEdge.EdgeType;
+import org.eclipse.tracecompass.analysis.graph.core.base.TmfGraph;
+import org.eclipse.tracecompass.analysis.graph.core.base.TmfVertex;
+import org.eclipse.tracecompass.analysis.graph.core.building.AbstractTmfGraphProvider;
+import org.eclipse.tracecompass.analysis.graph.core.building.AbstractTraceEventHandler;
+import org.eclipse.tracecompass.analysis.graph.core.tests.stubs.TestGraphWorker;
+import org.eclipse.tracecompass.common.core.NonNullUtils;
+import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+
+/**
+ * Simple graph provider stub
+ *
+ * @author Geneviève Bastien
+ * @author Francis Giraldeau
+ */
+public class GraphProviderStub extends AbstractTmfGraphProvider {
+
+ /**
+ * Constructor
+ *
+ * @param trace
+ * The trace
+ */
+ public GraphProviderStub(@NonNull ITmfTrace trace) {
+ super(trace, "Graph Provider Stub");
+ registerHandler(new StubEventHandler());
+ }
+
+ private class StubEventHandler extends AbstractTraceEventHandler {
+
+ @Override
+ public void handleEvent(ITmfEvent event) {
+ String evname = event.getType().getName();
+
+ TmfGraph graph = getAssignedGraph();
+ if (graph == null) {
+ throw new IllegalStateException();
+ }
+
+ switch (evname) {
+ case "take": {
+ IGraphWorker player = new TestGraphWorker(NonNullUtils.checkNotNull((Integer) event.getContent().getField("player").getValue()));
+ TmfVertex v = new TmfVertex(event.getTimestamp().getValue());
+ graph.add(player, v);
+ break;
+ }
+ case "pass": {
+ IGraphWorker playerFrom = new TestGraphWorker(NonNullUtils.checkNotNull((Integer) event.getContent().getField("from").getValue()));
+ IGraphWorker playerTo = new TestGraphWorker(NonNullUtils.checkNotNull((Integer) event.getContent().getField("to").getValue()));
+ TmfVertex vFrom = new TmfVertex(event.getTimestamp().getValue());
+ graph.append(playerFrom, vFrom);
+ TmfVertex vTo = new TmfVertex(event.getTimestamp().getValue());
+ graph.add(playerTo, vTo);
+ graph.link(vFrom, vTo, EdgeType.NETWORK);
+ break;
+ }
+ case "kick": {
+ IGraphWorker player = new TestGraphWorker(NonNullUtils.checkNotNull((Integer) event.getContent().getField("player").getValue()));
+ TmfVertex v = new TmfVertex(event.getTimestamp().getValue());
+ graph.append(player, v);
+ break;
+ }
+ default:
+ break;
+ }
+ }
+
+ }
+
+}