From da4232b4bd5e394cd5f1f29e14777e28f107994b Mon Sep 17 00:00:00 2001 From: Matthew Khouzam Date: Fri, 23 Oct 2015 22:23:10 -0400 Subject: [PATCH] analysis: Clean up critical path algorithm a bit Change-Id: I8c798fc612a28640b29292a6e51af046ebe50d97 Signed-off-by: Matthew Khouzam Reviewed-on: https://git.eclipse.org/r/58872 Reviewed-by: Hudson CI Reviewed-by: Bernd Hufmann Tested-by: Bernd Hufmann --- .../TmfCriticalPathAlgoBoundedTest.java | 10 +++- .../CriticalPathAlgorithmException.java | 28 ++++++++++ .../core/criticalpath/CriticalPathModule.java | 10 ++-- .../criticalpath/ICriticalPathAlgorithm.java | 4 +- .../CriticalPathAlgorithmBounded.java | 51 ++++++------------- 5 files changed, 62 insertions(+), 41 deletions(-) create mode 100644 analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/analysis/graph/core/criticalpath/CriticalPathAlgorithmException.java diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/analysis/criticalpath/TmfCriticalPathAlgoBoundedTest.java b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/analysis/criticalpath/TmfCriticalPathAlgoBoundedTest.java index 43db003da7..03eb26369f 100644 --- a/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/analysis/criticalpath/TmfCriticalPathAlgoBoundedTest.java +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/analysis/criticalpath/TmfCriticalPathAlgoBoundedTest.java @@ -10,9 +10,11 @@ package org.eclipse.tracecompass.analysis.graph.core.tests.analysis.criticalpath; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; 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.criticalpath.CriticalPathAlgorithmException; import org.eclipse.tracecompass.analysis.graph.core.criticalpath.ICriticalPathAlgorithm; import org.eclipse.tracecompass.analysis.graph.core.tests.stubs.GraphBuilder; import org.eclipse.tracecompass.internal.analysis.graph.core.criticalpath.CriticalPathAlgorithmBounded; @@ -29,8 +31,12 @@ public class TmfCriticalPathAlgoBoundedTest extends TmfCriticalPathAlgorithmTest protected TmfGraph computeCriticalPath(TmfGraph graph, TmfVertex start) { assertNotNull(graph); ICriticalPathAlgorithm cp = new CriticalPathAlgorithmBounded(graph); - TmfGraph bounded = cp.compute(start, null); - return bounded; + try { + return cp.compute(start, null); + } catch (CriticalPathAlgorithmException e) { + fail(e.getMessage()); + } + return null; } @Override diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/analysis/graph/core/criticalpath/CriticalPathAlgorithmException.java b/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/analysis/graph/core/criticalpath/CriticalPathAlgorithmException.java new file mode 100644 index 0000000000..92c2c6ebd5 --- /dev/null +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/analysis/graph/core/criticalpath/CriticalPathAlgorithmException.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * 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.criticalpath; + +/** + * Critical Path Algorithm Exception + * @author Matthew Khouzam + */ +public class CriticalPathAlgorithmException extends Exception { + /** + * Serial ID for serialization + */ + private static final long serialVersionUID = -919020777158527567L; + + /** + * Constructor + * @param message message + */ + public CriticalPathAlgorithmException(String message){ + super(message); + } +} \ No newline at end of file diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/analysis/graph/core/criticalpath/CriticalPathModule.java b/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/analysis/graph/core/criticalpath/CriticalPathModule.java index 549bc6c653..6064f3b345 100644 --- a/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/analysis/graph/core/criticalpath/CriticalPathModule.java +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/analysis/graph/core/criticalpath/CriticalPathModule.java @@ -106,9 +106,13 @@ public class CriticalPathModule extends TmfAbstractAnalysisModule { return true; } ICriticalPathAlgorithm cp = getAlgorithm(graph); - fCriticalPath = cp.compute(start, null); - - return true; + try { + fCriticalPath = cp.compute(start, null); + return true; + } catch (CriticalPathAlgorithmException e) { + Activator.getInstance().logError(NonNullUtils.nullToEmptyString(e.getMessage()), e); + } + return false; } @Override diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/analysis/graph/core/criticalpath/ICriticalPathAlgorithm.java b/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/analysis/graph/core/criticalpath/ICriticalPathAlgorithm.java index 090b792ec3..562288cb00 100644 --- a/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/analysis/graph/core/criticalpath/ICriticalPathAlgorithm.java +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/analysis/graph/core/criticalpath/ICriticalPathAlgorithm.java @@ -28,8 +28,10 @@ public interface ICriticalPathAlgorithm { * @param end * The end vertex * @return The graph of the critical path + * @throws CriticalPathAlgorithmException + * an exception in the calculation occurred */ - public TmfGraph compute(TmfVertex start, @Nullable TmfVertex end); + public TmfGraph compute(TmfVertex start, @Nullable TmfVertex end) throws CriticalPathAlgorithmException; /** * Unique ID of this algorithm diff --git a/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/internal/analysis/graph/core/criticalpath/CriticalPathAlgorithmBounded.java b/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/internal/analysis/graph/core/criticalpath/CriticalPathAlgorithmBounded.java index 4179b92b17..3649de80bf 100644 --- a/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/internal/analysis/graph/core/criticalpath/CriticalPathAlgorithmBounded.java +++ b/analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/internal/analysis/graph/core/criticalpath/CriticalPathAlgorithmBounded.java @@ -9,6 +9,8 @@ package org.eclipse.tracecompass.internal.analysis.graph.core.criticalpath; +import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; + import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -20,6 +22,7 @@ import org.eclipse.tracecompass.analysis.graph.core.base.TmfEdge; 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.criticalpath.CriticalPathAlgorithmException; /** * Critical path bounded algorithm: backward resolution of blocking limited to @@ -45,7 +48,7 @@ public class CriticalPathAlgorithmBounded extends AbstractCriticalPathAlgorithm } @Override - public TmfGraph compute(TmfVertex start, @Nullable TmfVertex end) { + public TmfGraph compute(TmfVertex start, @Nullable TmfVertex end) throws CriticalPathAlgorithmException { /* Create new graph for the critical path result */ TmfGraph criticalPath = new TmfGraph(); @@ -55,10 +58,7 @@ public class CriticalPathAlgorithmBounded extends AbstractCriticalPathAlgorithm /* * Calculate path starting from the object the start vertex belongs to */ - IGraphWorker parent = graph.getParentOf(start); - if (parent == null) { - throw new NullPointerException(); - } + IGraphWorker parent = checkNotNull(graph.getParentOf(start)); criticalPath.add(parent, new TmfVertex(start)); TmfVertex currentVertex = start; TmfEdge nextEdge = currentVertex.getEdge(EdgeDirection.OUTGOING_HORIZONTAL_EDGE); @@ -92,12 +92,9 @@ public class CriticalPathAlgorithmBounded extends AbstractCriticalPathAlgorithm * TODO: Normally, the parent of the link's vertex to should be * the object itself, verify if that is true */ - IGraphWorker parentTo = graph.getParentOf(nextEdge.getVertexTo()); - if (parentTo == null) { - throw new NullPointerException(); - } + IGraphWorker parentTo = checkNotNull(graph.getParentOf(nextEdge.getVertexTo())); if (parentTo != parent) { - System.err.println("no, the parents of horizontal edges are not always identical... shouldn't they be?"); //$NON-NLS-1$ + throw new CriticalPathAlgorithmException("no, the parents of horizontal edges are not always identical... shouldn't they be?"); //$NON-NLS-1$ } criticalPath.append(parentTo, new TmfVertex(nextEdge.getVertexTo()), nextEdge.getType()); break; @@ -109,11 +106,11 @@ public class CriticalPathAlgorithmBounded extends AbstractCriticalPathAlgorithm break; case EPS: if (nextEdge.getDuration() != 0) { - throw new RuntimeException("epsilon duration is not zero " + nextEdge); //$NON-NLS-1$ + throw new CriticalPathAlgorithmException("epsilon duration is not zero " + nextEdge); //$NON-NLS-1$ } break; case DEFAULT: - throw new RuntimeException("Illegal link type " + nextEdge.getType()); //$NON-NLS-1$ + throw new CriticalPathAlgorithmException("Illegal link type " + nextEdge.getType()); //$NON-NLS-1$ case UNKNOWN: default: break; @@ -126,10 +123,7 @@ public class CriticalPathAlgorithmBounded extends AbstractCriticalPathAlgorithm /** Add the links to the critical path, with currentVertex to glue to */ private void appendPathComponent(TmfGraph criticalPath, TmfGraph graph, TmfVertex currentVertex, List links) { - IGraphWorker currentActor = graph.getParentOf(currentVertex); - if (currentActor == null) { - throw new NullPointerException(); - } + IGraphWorker currentActor = checkNotNull(graph.getParentOf(currentVertex)); if (links.isEmpty()) { /* * The next vertex should not be null, since we glue only after @@ -144,21 +138,14 @@ public class CriticalPathAlgorithmBounded extends AbstractCriticalPathAlgorithm } // FIXME: assert last link.to actor == currentActor - // attach subpath to b1 and b2 - TmfVertex b1 = criticalPath.getTail(currentActor); - if (b1 == null) { - throw new NullPointerException(); - } - // TmfVertex b2 = new TmfVertex(curr.neighbor(TmfVertex.OUTH)); + // attach subpath to b1 + TmfVertex b1 = checkNotNull(criticalPath.getTail(currentActor)); // glue head TmfEdge lnk = links.get(0); TmfVertex anchor = null; - IGraphWorker objSrc = graph.getParentOf(lnk.getVertexFrom()); - if (objSrc == null) { - throw new NullPointerException(); - } - if (objSrc == currentActor) { + IGraphWorker objSrc = checkNotNull(graph.getParentOf(lnk.getVertexFrom())); + if (objSrc.equals( currentActor)) { anchor = b1; } else { anchor = new TmfVertex(currentVertex); @@ -167,10 +154,7 @@ public class CriticalPathAlgorithmBounded extends AbstractCriticalPathAlgorithm /* fill any gap with UNKNOWN */ if (lnk.getVertexFrom().compareTo(anchor) > 0) { anchor = new TmfVertex(lnk.getVertexFrom()); - TmfEdge edge = criticalPath.append(objSrc, anchor); - if (edge == null) { - throw new NullPointerException(); - } + TmfEdge edge = checkNotNull(criticalPath.append(objSrc, anchor)); edge.setType(TmfEdge.EdgeType.UNKNOWN); } } @@ -212,10 +196,7 @@ public class CriticalPathAlgorithmBounded extends AbstractCriticalPathAlgorithm return subPath; } - TmfEdge down = junction.getEdge(EdgeDirection.INCOMING_VERTICAL_EDGE); - if (down == null) { - throw new NullPointerException(); - } + TmfEdge down = checkNotNull(junction.getEdge(EdgeDirection.INCOMING_VERTICAL_EDGE)); subPath.add(down); TmfVertex vertexFrom = down.getVertexFrom(); -- 2.34.1