Fix for bug 381411: Implement ranked location in experiment.
authorPatrick Tasse <patrick.tasse@gmail.com>
Tue, 12 Jun 2012 11:56:32 +0000 (07:56 -0400)
committerPatrick Tasse <patrick.tasse@gmail.com>
Tue, 12 Jun 2012 11:56:32 +0000 (07:56 -0400)
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfExperimentCheckpointIndexTest.java
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfExperimentTest.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfExperimentContext.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfExperimentLocation.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfLocationArray.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/trace/TmfRankedLocation.java [new file with mode: 0644]
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfExperiment.java

index c880feef09ae71af305f5648c90d7a9f720ff226..ac207a8a6bfac081659b2ed56a0b332bee1d5926 100644 (file)
@@ -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));
index b2b4a595acbac717efe74cc543370d77b7dc38ed..d44208df986756762b82b027966831eb975a3d6c 100644 (file)
@@ -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
+}
index dfe716fedd891f487693185f75616efba5985bdf..981e8c213d521a540795b24be0f8b1182aaeb869 100644 (file)
@@ -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;
     }
index 22e4357a9b62e8f64416269ef65a4e122f2d53cb..0fae16228717ee0d2961e0cb43dabb9fa5b921bb 100644 (file)
@@ -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<TmfLocationArray> 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<TmfLocationArray> impleme
     @SuppressWarnings("nls")
     public String toString() {
         StringBuilder result = new StringBuilder("[TmfExperimentLocation");
-        ITmfLocation<? extends Comparable<?>>[] locations = ((TmfLocationArray) getLocation()).getLocations();
-        for (ITmfLocation<?> location : locations) {
+        TmfRankedLocation[] locations = ((TmfLocationArray) getLocation()).getLocations();
+        for (TmfRankedLocation location : locations) {
             result.append("[" + location + "]");
         }
         result.append("]");
index 06a324528abf723604a632fe8a92eefb7e030889..8720caf6c091a79209ca3f82585ca09a94d2a0e8 100644 (file)
@@ -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<TmfLocationArray>, Cloneable
     // Attributes
     // ------------------------------------------------------------------------
 
-    private ITmfLocation<? extends Comparable<?>>[] fLocations;
+    private TmfRankedLocation[] fLocations;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -41,7 +39,7 @@ public class TmfLocationArray implements Comparable<TmfLocationArray>, Cloneable
      * 
      * @param locations the locations
      */
-    public TmfLocationArray(ITmfLocation<? extends Comparable<?>>[] locations) {
+    public TmfLocationArray(TmfRankedLocation[] locations) {
         fLocations = locations;
     }
 
@@ -54,7 +52,7 @@ public class TmfLocationArray implements Comparable<TmfLocationArray>, Cloneable
      * 
      * @param locations the locations
      */
-    public ITmfLocation<? extends Comparable<?>>[] getLocations() {
+    public TmfRankedLocation[] getLocations() {
         return fLocations;
     }
 
@@ -67,9 +65,9 @@ public class TmfLocationArray implements Comparable<TmfLocationArray>, Cloneable
      */
     @Override
     public TmfLocationArray clone() {
-        ITmfLocation<? extends Comparable<?>>[] clones = (ITmfLocation<? extends Comparable<?>>[]) 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<TmfLocationArray>, Cloneable
     // ------------------------------------------------------------------------
 
     @Override
-    @SuppressWarnings({ "unchecked", "rawtypes" })
     public int compareTo(TmfLocationArray o) {
         for (int i = 0; i < fLocations.length; i++) {
-            ITmfLocation<? extends Comparable> l1 = (ITmfLocation<? extends Comparable>) fLocations[i].getLocation();
-            ITmfLocation<? extends Comparable> l2 = (ITmfLocation<? extends Comparable>) 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 (file)
index 0000000..c72da5e
--- /dev/null
@@ -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<TmfRankedLocation>, Cloneable {
+
+    // ------------------------------------------------------------------------
+    // Attributes
+    // ------------------------------------------------------------------------
+
+    private ITmfLocation<? extends Comparable<?>> 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<? extends Comparable<?>> location, long rank) {
+        fLocation = location;
+        fRank = rank;
+    }
+
+    // ------------------------------------------------------------------------
+    // Getters
+    // ------------------------------------------------------------------------
+
+    /**
+     * Get the trace location
+     * 
+     * @return the trace location
+     */
+    public ITmfLocation<? extends Comparable<?>> 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;
+    }
+
+}
index a5d0716f3af7664b95e89eee7c02368a7c6fb893..0a065173c8e0008f958a01bc6fb16920e14b57c3 100644 (file)
@@ -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<T extends ITmfEvent> extends TmfTrace<T> 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<T extends ITmfEvent> extends TmfTrace<T> 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<T extends ITmfEvent> extends TmfTrace<T> 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<T extends ITmfEvent> extends TmfTrace<T> 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);
             }
This page took 0.031375 seconds and 5 git commands to generate.