tmf: Clean up tmf.core.trace package
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfExperiment.java
index 2a25b96739c92daaabcca12716a9a4b6ecef095d..8ecd7ed5da814b10788fa385d808c6b37ae6626b 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2010, 2012 Ericsson
+ * Copyright (c) 2009, 2013 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -9,6 +9,8 @@
  * Contributors:
  *   Francois Chouinard - Initial API and implementation
  *   Francois Chouinard - Updated as per TMF Trace Model 1.0
+ *   Patrick Tasse - Updated for removal of context clone
+ *   Patrick Tasse - Updated for ranks in experiment location
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.trace;
@@ -16,20 +18,23 @@ package org.eclipse.linuxtools.tmf.core.trace;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
 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.tmf.core.event.ITmfEvent;
-import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
-import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
-import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
-import org.eclipse.linuxtools.tmf.core.signal.TmfClearExperimentSignal;
 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
+import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
+import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
+import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
+import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
+import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
 
 /**
  * TmfExperiment presents a time-ordered, unified view of a set of ITmfTrace:s
@@ -78,9 +83,26 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
      * @param traces the experiment set of traces
      */
     public TmfExperiment(final Class<? extends ITmfEvent> type, final String id, final ITmfTrace[] traces) {
-        this(type, id, traces, DEFAULT_INDEX_PAGE_SIZE);
+        this(type, id, traces, DEFAULT_INDEX_PAGE_SIZE, null);
     }
 
+    /**
+     * Constructor of experiment taking type, path, traces and resource
+     *
+     * @param type
+     *            the event type
+     * @param id
+     *            the experiment id
+     * @param traces
+     *            the experiment set of traces
+     * @param resource
+     *            the resource associated to the experiment
+     */
+    public TmfExperiment(final Class<? extends ITmfEvent> type, final String id, final ITmfTrace[] traces, IResource resource) {
+        this(type, id, traces, DEFAULT_INDEX_PAGE_SIZE, resource);
+    }
+
+
     /**
      * @param type the event type
      * @param path the experiment path
@@ -88,12 +110,31 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
      * @param indexPageSize the experiment index page size
      */
     public TmfExperiment(final Class<? extends ITmfEvent> type, final String path, final ITmfTrace[] traces, final int indexPageSize) {
+        this(type, path, traces, indexPageSize, null);
+    }
+
+    /**
+     * Full constructor of an experiment, taking the type, path, traces,
+     * indexPageSize and resource
+     *
+     * @param type
+     *            the event type
+     * @param path
+     *            the experiment path
+     * @param traces
+     *            the experiment set of traces
+     * @param indexPageSize
+     *            the experiment index page size
+     * @param resource
+     *            the resource associated to the experiment
+     */
+    public TmfExperiment(final Class<? extends ITmfEvent> type, final String path, final ITmfTrace[] traces, final int indexPageSize, IResource resource) {
         setCacheSize(indexPageSize);
         setStreamingInterval(0);
         setIndexer(new TmfCheckpointIndexer(this, indexPageSize));
         setParser(this);
         try {
-            super.initialize(null, path, type);
+            super.initialize(resource, path, type);
         } catch (TmfTraceException e) {
             e.printStackTrace();
         }
@@ -121,32 +162,20 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
         super.dispose();
     }
 
-    /**
-     * @param signal the clear view signal
-     * @since 2.0
-     */
-    @TmfSignalHandler
-    public void handleClearExperimentSignal(TmfClearExperimentSignal signal) {
-        dispose();
-    }
-
     // ------------------------------------------------------------------------
     // ITmfTrace - Initializers
     // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
-     */
     @Override
     public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) {
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
+    /**
+     * @since 2.0
      */
     @Override
-    public boolean validate(final IProject project, final String path) {
-        return true;
+    public IStatus validate(final IProject project, final String path) {
+        return Status.OK_STATUS;
     }
 
     // ------------------------------------------------------------------------
@@ -154,9 +183,9 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     // ------------------------------------------------------------------------
 
     /**
-     * Get the list of traces. Handle with care...
+     * Get the traces contained in this experiment.
      *
-     * @return the experiment traces
+     * @return The array of contained traces
      */
     public ITmfTrace[] getTraces() {
         return fTraces;
@@ -168,6 +197,7 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
      *
      * @param index the event index (rank)
      * @return the corresponding event timestamp
+     * @since 2.0
      */
     public ITmfTimestamp getTimestamp(final int index) {
         final ITmfContext context = seekEvent(index);
@@ -198,11 +228,11 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     // Request management
     // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
+    /**
+     * @since 2.0
      */
     @Override
-    protected synchronized ITmfContext armRequest(final ITmfDataRequest request) {
+    public synchronized ITmfContext armRequest(final ITmfDataRequest request) {
 
         // Make sure we have something to read from
         if (fTraces == null) {
@@ -226,12 +256,8 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     // ITmfTrace trace positioning
     // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     *
-     * Returns a brand new context based on the location provided and
-     * initializes the event queues
-     *
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
+    /**
+     * @since 3.0
      */
     @Override
     public synchronized ITmfContext seekEvent(final ITmfLocation location) {
@@ -244,32 +270,33 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
             return null;
         }
 
+        // Initialize the location array if necessary
+        TmfLocationArray locationArray = ((location == null) ?
+                new TmfLocationArray(fTraces.length) :
+                ((TmfExperimentLocation) location).getLocationInfo());
+
+        ITmfLocation[] locations = locationArray.getLocations();
+        long[] ranks = locationArray.getRanks();
+
         // Create and populate the context's traces contexts
-        final TmfExperimentContext context = new TmfExperimentContext(new ITmfContext[fTraces.length]);
-        ITmfLocation[] expLocations = new ITmfLocation[fTraces.length];
-        if (location != null) {
-            TmfExperimentLocation locations = (TmfExperimentLocation) location;
-            int index = 0;
-            ITmfLocation l = locations.getLocationInfo().getLocation(index);
-            while (index < expLocations.length && l != null) {
-                expLocations[index] = l;
-                l = locations.getLocationInfo().getLocation(++index);
-            }
-        }
+        final TmfExperimentContext context = new TmfExperimentContext(fTraces.length);
 
         // Position the traces
+        long rank = 0;
         for (int i = 0; i < fTraces.length; i++) {
             // Get the relevant trace attributes
-            final ITmfLocation trcLocation = expLocations[i];
-            context.getContexts()[i] = fTraces[i].seekEvent(trcLocation);
-            expLocations[i] = context.getContexts()[i].getLocation();
-            context.getEvents()[i] = fTraces[i].getNext(context.getContexts()[i]);
+            final ITmfContext traceContext = fTraces[i].seekEvent(locations[i]);
+            context.getContexts()[i] = traceContext;
+            traceContext.setRank(ranks[i]);
+            locations[i] = traceContext.getLocation(); // update location after seek
+            context.getEvents()[i] = fTraces[i].getNext(traceContext);
+            rank += ranks[i];
         }
 
         // Finalize context
-        context.setLocation(new TmfExperimentLocation(new TmfLocationArray(expLocations)));
+        context.setLocation(new TmfExperimentLocation(new TmfLocationArray(locations, ranks)));
         context.setLastTrace(TmfExperimentContext.NO_TRACE);
-        context.setRank((location == null) ? 0 : ITmfContext.UNKNOWN_RANK);
+        context.setRank(rank);
 
         return context;
     }
@@ -278,55 +305,48 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     // ITmfTrace - SeekEvent operations (returning a trace context)
     // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(double)
-     */
     @Override
     public ITmfContext seekEvent(final double ratio) {
         final ITmfContext context = seekEvent(Math.round(ratio * getNbEvents()));
         return context;
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getLocationRatio(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
+    /**
+     * @since 3.0
      */
     @Override
     public double getLocationRatio(final ITmfLocation location) {
         if (location instanceof TmfExperimentLocation) {
-            return (double) seekEvent(location).getRank() / getNbEvents();
+            long rank = 0;
+            TmfLocationArray locationArray = ((TmfExperimentLocation) location).getLocationInfo();
+            for (int i = 0; i < locationArray.size(); i++) {
+                rank += locationArray.getRank(i);
+            }
+            return (double) rank / getNbEvents();
         }
         return 0.0;
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
+    /**
+     * @since 3.0
      */
     @Override
     public ITmfLocation getCurrentLocation() {
-        ITmfLocation[] locations = new ITmfLocation[fTraces.length];
-        for (int i = 0; i < fTraces.length; i++) {
-            locations[i] = fTraces[i].getCurrentLocation();
-        }
-        return new TmfExperimentLocation(new TmfLocationArray(locations));
+        // never used
+        return null;
     }
 
     // ------------------------------------------------------------------------
     // ITmfTrace trace positioning
     // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser#parseEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
-     */
     @Override
     public synchronized ITmfEvent parseEvent(final ITmfContext context) {
-        final ITmfContext savedContext = context.clone();
-        final ITmfEvent event = getNext(savedContext);
+        final ITmfContext tmpContext = seekEvent(context.getLocation());
+        final ITmfEvent event = getNext(tmpContext);
         return event;
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
-     */
     @Override
     public synchronized ITmfEvent getNext(ITmfContext context) {
 
@@ -373,9 +393,11 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
                 expContext.setLastTrace(trace);
                 final ITmfContext traceContext = expContext.getContexts()[trace];
 
-                expContext.setLocation(new TmfExperimentLocation(
-                        (TmfExperimentLocation) expContext.getLocation(),
-                        trace, traceContext.getLocation()));
+                // Update the experiment location
+                TmfLocationArray locationArray = new TmfLocationArray(
+                        ((TmfExperimentLocation) expContext.getLocation()).getLocationInfo(),
+                        trace, traceContext.getLocation(), traceContext.getRank());
+                expContext.setLocation(new TmfExperimentLocation(locationArray));
 
                 processEvent(event);
             }
@@ -384,9 +406,25 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
         return event;
     }
 
-    /* (non-Javadoc)
-     * @see java.lang.Object#toString()
+    /**
+     * @since 2.0
      */
+    @Override
+    public ITmfTimestamp getInitialRangeOffset() {
+        if ((fTraces == null) || (fTraces.length == 0)) {
+            return super.getInitialRangeOffset();
+        }
+
+        ITmfTimestamp initTs = TmfTimestamp.BIG_CRUNCH;
+        for (int i = 0; i < fTraces.length; i++) {
+            ITmfTimestamp ts = fTraces[i].getInitialRangeOffset();
+            if (ts.compareTo(initTs) < 0) {
+                initTs = ts;
+            }
+        }
+        return initTs;
+    }
+
     @Override
     @SuppressWarnings("nls")
     public synchronized String toString() {
@@ -467,9 +505,6 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
         thread.start();
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
-     */
     @Override
     public long getStreamingInterval() {
         long interval = 0;
@@ -483,9 +518,6 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     // Signal handlers
     // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#traceOpened(org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal)
-     */
     @Override
     @TmfSignalHandler
     public void traceOpened(TmfTraceOpenedSignal signal) {
This page took 0.02811 seconds and 5 git commands to generate.