From: Patrick Tasse Date: Tue, 12 Jun 2012 11:56:32 +0000 (-0400) Subject: Fix for bug 381411: Implement ranked location in experiment. X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=9a7f542f431487b7a6fa90dc56dc01de67e9737a;p=deliverable%2Ftracecompass.git Fix for bug 381411: Implement ranked location in experiment. --- diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfExperimentCheckpointIndexTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfExperimentCheckpointIndexTest.java index c880feef09..ac207a8a6b 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfExperimentCheckpointIndexTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfExperimentCheckpointIndexTest.java @@ -131,8 +131,8 @@ public class TmfExperimentCheckpointIndexTest extends TestCase { TmfExperimentLocation expLocation = (TmfExperimentLocation) checkpoint.getLocation(); TmfLocationArray locations = expLocation.getLocation(); ITmfContext[] trcContexts = new ITmfContext[2]; - trcContexts[0] = new TmfContext(locations.getLocations()[0], (i * pageSize) / 2); - trcContexts[1] = new TmfContext(locations.getLocations()[1], (i * pageSize) / 2); + trcContexts[0] = new TmfContext(locations.getLocations()[0].getLocation(), (i * pageSize) / 2); + trcContexts[1] = new TmfContext(locations.getLocations()[1].getLocation(), (i * pageSize) / 2); TmfExperimentContext expContext = new TmfExperimentContext(trcContexts); expContext.getEvents()[0] = fTestTraces[0].getNext(fTestTraces[0].seekEvent((i * pageSize) / 2)); expContext.getEvents()[1] = fTestTraces[1].getNext(fTestTraces[1].seekEvent((i * pageSize) / 2)); @@ -188,8 +188,8 @@ public class TmfExperimentCheckpointIndexTest extends TestCase { TmfExperimentLocation expLocation = (TmfExperimentLocation) checkpoint.getLocation(); TmfLocationArray locations = expLocation.getLocation(); ITmfContext[] trcContexts = new ITmfContext[2]; - trcContexts[0] = new TmfContext(locations.getLocations()[0], (i * pageSize) / 2); - trcContexts[1] = new TmfContext(locations.getLocations()[1], (i * pageSize) / 2); + trcContexts[0] = new TmfContext(locations.getLocations()[0].getLocation(), (i * pageSize) / 2); + trcContexts[1] = new TmfContext(locations.getLocations()[1].getLocation(), (i * pageSize) / 2); TmfExperimentContext expContext = new TmfExperimentContext(trcContexts); expContext.getEvents()[0] = testTraces[0].getNext(testTraces[0].seekEvent((i * pageSize) / 2)); expContext.getEvents()[1] = testTraces[1].getNext(testTraces[1].seekEvent((i * pageSize) / 2)); diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfExperimentTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfExperimentTest.java index b2b4a595ac..d44208df98 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfExperimentTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfExperimentTest.java @@ -244,23 +244,17 @@ public class TmfExperimentTest extends TestCase { // First event ITmfContext context = fExperiment.seekEvent((ITmfLocation) null); double ratio = fExperiment.getLocationRatio(context.getLocation()); - context = fExperiment.seekEvent(ratio); - double ratio2 = fExperiment.getLocationRatio(context.getLocation()); - assertEquals("getLocationRatio", ratio, ratio2); + assertEquals("getLocationRatio", 0.0, ratio); // Middle event context = fExperiment.seekEvent(NB_EVENTS / 2); ratio = fExperiment.getLocationRatio(context.getLocation()); - context = fExperiment.seekEvent(ratio); - ratio2 = fExperiment.getLocationRatio(context.getLocation()); - assertEquals("getLocationRatio", ratio, ratio2); + assertEquals("getLocationRatio", (double) (NB_EVENTS / 2) / NB_EVENTS, ratio); // Last event context = fExperiment.seekEvent(NB_EVENTS - 1); ratio = fExperiment.getLocationRatio(context.getLocation()); - context = fExperiment.seekEvent(ratio); - ratio2 = fExperiment.getLocationRatio(context.getLocation()); - assertEquals("getLocationRatio", ratio, ratio2); + assertEquals("getLocationRatio", (double) (NB_EVENTS - 1) / NB_EVENTS, ratio); } // @SuppressWarnings({ "unchecked", "rawtypes" }) @@ -698,4 +692,4 @@ public class TmfExperimentTest extends TestCase { assertTrue("isCancelled", request.isCancelled()); } -} \ No newline at end of file +} diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfExperimentContext.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfExperimentContext.java index dfe716fedd..981e8c213d 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfExperimentContext.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfExperimentContext.java @@ -15,7 +15,6 @@ package org.eclipse.linuxtools.internal.tmf.core.trace; import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; -import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; import org.eclipse.linuxtools.tmf.core.trace.TmfContext; /** @@ -62,20 +61,17 @@ public class TmfExperimentContext extends TmfContext implements Cloneable { super(); fContexts = contexts; fEvents = new ITmfEvent[fContexts.length]; - final ITmfLocation[] locations = new ITmfLocation[fContexts.length]; + final TmfRankedLocation[] locations = new TmfRankedLocation[fContexts.length]; - setLocation(new TmfExperimentLocation(new TmfLocationArray(locations.clone()))); - - final long[] ranks = new long[fContexts.length]; long rank = 0; - for (int i = 0; i < fContexts.length; i++) + for (int i = 0; i < fContexts.length; i++) { if (contexts[i] != null) { - locations[i] = contexts[i].getLocation(); - ranks[i] = contexts[i].getRank(); + locations[i] = new TmfRankedLocation(contexts[i]); rank += contexts[i].getRank(); } + } -// setLocation(new TmfExperimentLocation(new TmfLocationArray(locations))); + setLocation(new TmfExperimentLocation(new TmfLocationArray(locations))); setRank(rank); fLastTraceRead = NO_TRACE; } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfExperimentLocation.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfExperimentLocation.java index 22e4357a9b..0fae162287 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfExperimentLocation.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfExperimentLocation.java @@ -13,7 +13,6 @@ package org.eclipse.linuxtools.internal.tmf.core.trace; -import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; /** @@ -64,7 +63,6 @@ public class TmfExperimentLocation extends TmfLocation impleme */ @Override public TmfExperimentLocation clone() { -// super.clone(); // To keep FindBugs happy TmfLocationArray array = (TmfLocationArray) getLocation(); TmfLocationArray clones = array.clone(); return new TmfExperimentLocation(clones); @@ -81,8 +79,8 @@ public class TmfExperimentLocation extends TmfLocation impleme @SuppressWarnings("nls") public String toString() { StringBuilder result = new StringBuilder("[TmfExperimentLocation"); - ITmfLocation>[] locations = ((TmfLocationArray) getLocation()).getLocations(); - for (ITmfLocation location : locations) { + TmfRankedLocation[] locations = ((TmfLocationArray) getLocation()).getLocations(); + for (TmfRankedLocation location : locations) { result.append("[" + location + "]"); } result.append("]"); diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfLocationArray.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfLocationArray.java index 06a324528a..8720caf6c0 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfLocationArray.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfLocationArray.java @@ -15,8 +15,6 @@ package org.eclipse.linuxtools.internal.tmf.core.trace; import java.util.Arrays; -import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; - /** * A convenience class to store trace location arrays. The main purpose is to * provide a Comparable implementation for TmfExperimentLocation. @@ -30,7 +28,7 @@ public class TmfLocationArray implements Comparable, Cloneable // Attributes // ------------------------------------------------------------------------ - private ITmfLocation>[] fLocations; + private TmfRankedLocation[] fLocations; // ------------------------------------------------------------------------ // Constructors @@ -41,7 +39,7 @@ public class TmfLocationArray implements Comparable, Cloneable * * @param locations the locations */ - public TmfLocationArray(ITmfLocation>[] locations) { + public TmfLocationArray(TmfRankedLocation[] locations) { fLocations = locations; } @@ -54,7 +52,7 @@ public class TmfLocationArray implements Comparable, Cloneable * * @param locations the locations */ - public ITmfLocation>[] getLocations() { + public TmfRankedLocation[] getLocations() { return fLocations; } @@ -67,9 +65,9 @@ public class TmfLocationArray implements Comparable, Cloneable */ @Override public TmfLocationArray clone() { - ITmfLocation>[] clones = (ITmfLocation>[]) new ITmfLocation[fLocations.length]; + TmfRankedLocation[] clones = new TmfRankedLocation[fLocations.length]; for (int i = 0; i < fLocations.length; i++) { - ITmfLocation location = fLocations[i]; + TmfRankedLocation location = fLocations[i]; clones[i] = (location != null) ? location.clone() : null; } return new TmfLocationArray(clones); @@ -80,12 +78,11 @@ public class TmfLocationArray implements Comparable, Cloneable // ------------------------------------------------------------------------ @Override - @SuppressWarnings({ "unchecked", "rawtypes" }) public int compareTo(TmfLocationArray o) { for (int i = 0; i < fLocations.length; i++) { - ITmfLocation l1 = (ITmfLocation) fLocations[i].getLocation(); - ITmfLocation l2 = (ITmfLocation) o.fLocations[i].getLocation(); - int result = l1.getLocation().compareTo(l2.getLocation()); + TmfRankedLocation l1 = fLocations[i]; + TmfRankedLocation l2 = o.fLocations[i]; + int result = l1.compareTo(l2); if (result != 0) { return result; } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfRankedLocation.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfRankedLocation.java new file mode 100644 index 0000000000..c72da5efb7 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfRankedLocation.java @@ -0,0 +1,158 @@ +/******************************************************************************* + * Copyright (c) 2012 Ericsson + * + * 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 + * + * Contributors: + * Patrick Tasse - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.internal.tmf.core.trace; + +import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; +import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; + +/** + * A pair of trace location and trace rank. + * + * @version 1.0 + * @author Patrick Tasse + */ +public class TmfRankedLocation implements Comparable, Cloneable { + + // ------------------------------------------------------------------------ + // Attributes + // ------------------------------------------------------------------------ + + private ITmfLocation> fLocation; + private long fRank; + + // ------------------------------------------------------------------------ + // Constructors + // ------------------------------------------------------------------------ + + /** + * The standard constructor + * + * @param context a trace context + */ + public TmfRankedLocation(ITmfContext context) { + fLocation = context.getLocation().clone(); + fRank = context.getRank(); + } + + /** + * Private constructor + * + * @param location the trace location + * @param rank the trace rank + */ + private TmfRankedLocation(ITmfLocation> location, long rank) { + fLocation = location; + fRank = rank; + } + + // ------------------------------------------------------------------------ + // Getters + // ------------------------------------------------------------------------ + + /** + * Get the trace location + * + * @return the trace location + */ + public ITmfLocation> getLocation() { + return fLocation; + } + + /** + * Get the trace rank + * + * @return the trace rank + */ + public long getRank() { + return fRank; + } + + // ------------------------------------------------------------------------ + // Cloneable + // ------------------------------------------------------------------------ + + /* (non-Javadoc) + * @see java.lang.Object#clone() + */ + @Override + public TmfRankedLocation clone() { + return new TmfRankedLocation(fLocation.clone(), fRank); + } + + // ------------------------------------------------------------------------ + // Comparable + // ------------------------------------------------------------------------ + + /* + * (non-Javadoc) + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ + @Override + public int compareTo(TmfRankedLocation o) { + return Long.valueOf(fRank).compareTo(Long.valueOf(o.fRank)); + } + + // ------------------------------------------------------------------------ + // Object + // ------------------------------------------------------------------------ + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((fLocation == null) ? 0 : fLocation.hashCode()); + result = prime * result + (int) (fRank ^ (fRank >>> 32)); + return result; + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + TmfRankedLocation other = (TmfRankedLocation) obj; + if (fLocation == null) { + if (other.fLocation != null) { + return false; + } + } else if (!fLocation.equals(other.fLocation)) { + return false; + } + if (fRank != other.fRank) { + return false; + } + return true; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + @SuppressWarnings("nls") + public String toString() { + return fLocation + "," + fRank; + } + +} diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfExperiment.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfExperiment.java index a5d0716f3a..0a065173c8 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfExperiment.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfExperiment.java @@ -19,6 +19,7 @@ import org.eclipse.core.resources.IResource; import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext; import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentLocation; import org.eclipse.linuxtools.internal.tmf.core.trace.TmfLocationArray; +import org.eclipse.linuxtools.internal.tmf.core.trace.TmfRankedLocation; import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; @@ -283,24 +284,29 @@ public class TmfExperiment extends TmfTrace implements I // Instantiate the location final TmfExperimentLocation expLocation = (location == null) - ? new TmfExperimentLocation(new TmfLocationArray(new ITmfLocation[fTraces.length])) + ? new TmfExperimentLocation(new TmfLocationArray(new TmfRankedLocation[fTraces.length])) : (TmfExperimentLocation) location.clone(); // Create and populate the context's traces contexts final TmfExperimentContext context = new TmfExperimentContext(new ITmfContext[fTraces.length]); + long rank = 0; for (int i = 0; i < fTraces.length; i++) { // Get the relevant trace attributes - final ITmfLocation trcLocation = expLocation.getLocation().getLocations()[i]; - context.getContexts()[i] = fTraces[i].seekEvent(trcLocation); - expLocation.getLocation().getLocations()[i] = context.getContexts()[i].getLocation().clone(); + final TmfRankedLocation rankedLocation = expLocation.getLocation().getLocations()[i]; + final ITmfLocation traceLocation = (rankedLocation == null) ? null : rankedLocation.getLocation(); + final long traceRank = (rankedLocation == null) ? 0 : rankedLocation.getRank(); + context.getContexts()[i] = fTraces[i].seekEvent(traceLocation); + context.getContexts()[i].setRank(traceRank); + expLocation.getLocation().getLocations()[i] = new TmfRankedLocation(context.getContexts()[i]); context.getEvents()[i] = fTraces[i].getNext(context.getContexts()[i]); + rank += traceRank; } // Finalize context context.setLocation(expLocation); context.setLastTrace(TmfExperimentContext.NO_TRACE); - context.setRank(ITmfContext.UNKNOWN_RANK); + context.setRank(rank); fExperimentContext = context; return (ITmfContext) context; @@ -325,7 +331,11 @@ public class TmfExperiment extends TmfTrace implements I @Override public double getLocationRatio(final ITmfLocation location) { if (location instanceof TmfExperimentLocation) { - return (double) seekEvent(location).getRank() / getNbEvents(); + long rank = 0; + for (TmfRankedLocation rankedLocation : ((TmfExperimentLocation) location).getLocation().getLocations()) { + rank += rankedLocation.getRank(); + } + return (double) rank / getNbEvents(); } return 0.0; } @@ -335,9 +345,9 @@ public class TmfExperiment extends TmfTrace implements I */ @Override public ITmfLocation getCurrentLocation() { - ITmfLocation[] locations = new ITmfLocation[fTraces.length]; + TmfRankedLocation[] locations = new TmfRankedLocation[fTraces.length]; for (int i = 0; i < fTraces.length; i++) { - locations[i] = fTraces[i].getCurrentLocation(); + locations[i] = new TmfRankedLocation(new TmfContext(fTraces[i].getCurrentLocation(), ITmfContext.UNKNOWN_RANK)); } return new TmfExperimentLocation(new TmfLocationArray(locations)); } @@ -404,9 +414,9 @@ public class TmfExperiment extends TmfTrace implements I updateAttributes(expContext, event.getTimestamp()); expContext.increaseRank(); expContext.setLastTrace(trace); - final TmfExperimentLocation location = (TmfExperimentLocation) expContext.getLocation(); + final TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation(); final ITmfContext traceContext = expContext.getContexts()[trace]; - location.getLocation().getLocations()[trace] = traceContext.getLocation().clone(); + expLocation.getLocation().getLocations()[trace] = new TmfRankedLocation(traceContext); fExperimentContext = expContext; processEvent(event); }