Fix test case. It really is supposed to return true.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTrace.java
CommitLineData
b1baa808
MK
1/*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
a3fc8213
AM
12package org.eclipse.linuxtools.tmf.core.ctfadaptor;
13
a3fc8213
AM
14import org.eclipse.core.resources.IProject;
15import org.eclipse.core.resources.IResource;
139d5c1a 16import org.eclipse.core.runtime.CoreException;
a1a24d68 17import org.eclipse.core.runtime.IPath;
a3fc8213
AM
18import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
19import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
20import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
21import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
22import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
23import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
b4f71e4a 24import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
a3fc8213
AM
25import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
26import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
27import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
28import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
18ab1d18 29import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier;
a3fc8213
AM
30import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
31import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
32import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
a3fc8213 33
b1baa808
MK
34/**
35 */
25e48683 36public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTrace<CtfTmfEvent> {
a3fc8213
AM
37
38 // ------------------------------------------------------------------------
39 // Constants
40 // ------------------------------------------------------------------------
41
a3fc8213
AM
42 // ------------------------------------------------------------------------
43 // Attributes
44 // ------------------------------------------------------------------------
45
46 // the Ctf Trace
47 private CTFTrace fTrace;
48
a3fc8213
AM
49 // The number of events collected
50 protected long fNbEvents = 0;
51
52 // The time span of the event stream
53 private ITmfTimestamp fStartTime = TmfTimestamp.BIG_CRUNCH;
54 private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG;
55
56 // The trace resource
57 private IResource fResource;
58
59 /*
60 * Since in TMF, "traces" can read events, this trace here will have its own
61 * iterator. The user can instantiate extra iterator if they want to seek at
62 * many places at the same time.
63 */
64 protected CtfIterator iterator;
65
11d6f468 66 /* Reference to the state system assigned to this trace */
d26f90fd 67 protected IStateSystemQuerier ss = null;
11d6f468 68
a3fc8213
AM
69 // ------------------------------------------------------------------------
70 // Constructors
71 // ------------------------------------------------------------------------
72
73 public CtfTmfTrace() {
74 super();
75 }
76
b1baa808
MK
77 /**
78 * Method initTrace.
79 * @param resource IResource
80 * @param path String
81 * @param eventType Class<CtfTmfEvent>
82 * @throws TmfTraceException
83 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(IResource, String, Class<CtfTmfEvent>)
84 */
a1a24d68 85 @SuppressWarnings("unused")
a3fc8213 86 @Override
25e48683 87 public void initTrace(final IResource resource, final String path, final Class<CtfTmfEvent> eventType)
b4f71e4a 88 throws TmfTraceException {
25e48683 89 this.fResource = resource;
a3fc8213
AM
90 try {
91 this.fTrace = new CTFTrace(path);
25e48683 92 } catch (final CTFReaderException e) {
a3fc8213
AM
93 /*
94 * If it failed at the init(), we can assume it's because the file
95 * was not found or was not recognized as a CTF trace. Throw into
96 * the new type of exception expected by the rest of TMF.
97 */
b4f71e4a 98 throw new TmfTraceException(e.getMessage());
a3fc8213
AM
99 }
100 this.iterator = new CtfIterator(this, 0, 0);
57c073c5
MK
101 setStartTime(TmfTimestamp.BIG_BANG);
102 if( !this.iterator.getLocation().equals(CtfIterator.nullLocation)) {
103 setStartTime(iterator.getCurrentEvent().getTimestamp());
104 }
a3fc8213 105 TmfSignalManager.register(this);
b1baa808 106 // FIXME this should become a request
11d6f468 107 buildStateSystem();
139d5c1a
AM
108
109 /* Refresh the project, so it can pick up new files that got created. */
110 if ( resource != null) {
111 try {
112 resource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
113 } catch (CoreException e) {
114 throw new TmfTraceException(e.getMessage());
115 }
116 }
a3fc8213
AM
117 }
118
b1baa808
MK
119 /**
120 * Method dispose.
121 * @see org.eclipse.linuxtools.tmf.core.component.ITmfComponent#dispose()
122 */
a3fc8213
AM
123 @Override
124 public void dispose() {
125 TmfSignalManager.deregister(this);
126 }
127
b1baa808
MK
128 /**
129 * Method broadcast.
130 * @param signal TmfSignal
131 * @see org.eclipse.linuxtools.tmf.core.component.ITmfComponent#broadcast(TmfSignal)
132 */
a3fc8213 133 @Override
25e48683 134 public void broadcast(final TmfSignal signal) {
a3fc8213
AM
135 TmfSignalManager.dispatchSignal(signal);
136 }
137
b1baa808
MK
138 /**
139 * Method validate.
140 * @param project IProject
141 * @param path String
142 * @return boolean
143 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
144 */
a1a24d68 145 @SuppressWarnings("unused")
a3fc8213 146 @Override
25e48683 147 public boolean validate(final IProject project, final String path) {
a3fc8213
AM
148 try {
149 final CTFTrace temp = new CTFTrace(path);
150 return temp.majortIsSet(); // random test
25e48683 151 } catch (final CTFReaderException e) {
90235d6b
AM
152 /* Nope, not a CTF trace we can read */
153 return false;
a3fc8213 154 }
a3fc8213
AM
155 }
156
a3fc8213
AM
157 // ------------------------------------------------------------------------
158 // Accessors
159 // ------------------------------------------------------------------------
160
25e48683 161 /**
b1baa808 162 * Method getEventType.
25e48683 163 * @return the trace path
b1baa808 164 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
25e48683
FC
165 */
166 @Override
13cb5f43 167 public Class<CtfTmfEvent> getEventType() {
25e48683
FC
168 return fType;
169 }
170
b1baa808
MK
171 /**
172 * Method getNbEnvVars.
173 * @return int
174 */
ce2388e0
FC
175 public int getNbEnvVars() {
176 return this.fTrace.getEnvironment().size();
177 }
178
179
b1baa808
MK
180 /**
181 * Method getEnvNames.
182 * @return String[]
183 */
ce2388e0 184 public String[] getEnvNames() {
25e48683 185 final String[] s = new String[getNbEnvVars()];
ce2388e0
FC
186 return this.fTrace.getEnvironment().keySet().toArray(s);
187 }
188
b1baa808
MK
189 /**
190 * Method getEnvValue.
191 * @param key String
192 * @return String
193 */
25e48683 194 public String getEnvValue(final String key) {
ce2388e0
FC
195 return this.fTrace.getEnvironment().get(key);
196 }
197
198
a3fc8213 199 /**
b1baa808
MK
200
201 * @return the trace path * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
a3fc8213
AM
202 */
203 @Override
204 public String getPath() {
205 return this.fTrace.getPath();
206 }
207
b1baa808
MK
208 /**
209 * Method getName.
210 * @return String
211 * @see org.eclipse.linuxtools.tmf.core.component.ITmfComponent#getName()
212 */
a3fc8213
AM
213 @Override
214 public String getName() {
a1a24d68
MK
215 String traceName = (fResource != null) ? fResource.getName() : null;
216 // If no resource was provided, extract the display name the trace path
217 if (traceName == null) {
218 final String path = this.fTrace.getPath();
219 final int sep = path.lastIndexOf(IPath.SEPARATOR);
220 traceName = (sep >= 0) ? path.substring(sep + 1) : path;
11d6f468 221 }
a1a24d68 222 return traceName;
a3fc8213
AM
223 }
224
b1baa808
MK
225 /**
226 * Method getCacheSize.
227 * @return int
228 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCacheSize()
229 */
a3fc8213 230 @Override
20658947 231 public int getCacheSize() {
ce2388e0 232 return 50000; // not true, but it works
a3fc8213
AM
233 }
234
b1baa808
MK
235 /**
236 * Method getNbEvents.
237 * @return long
238 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
239 */
a3fc8213
AM
240 @Override
241 public long getNbEvents() {
242 return this.fNbEvents;
243 }
244
b1baa808
MK
245 /**
246 * Method getTimeRange.
247 * @return TmfTimeRange
248 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
249 */
a3fc8213
AM
250 @Override
251 public TmfTimeRange getTimeRange() {
252 return new TmfTimeRange(this.fStartTime, this.fEndTime);
253 }
254
b1baa808
MK
255 /**
256 * Method getStartTime.
257 * @return ITmfTimestamp
258 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
259 */
a3fc8213
AM
260 @Override
261 public ITmfTimestamp getStartTime() {
262 return this.fStartTime;
263 }
264
b1baa808
MK
265 /**
266 * Method getEndTime.
267 * @return ITmfTimestamp
268 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
269 */
a3fc8213
AM
270 @Override
271 public ITmfTimestamp getEndTime() {
272 return this.fEndTime;
273 }
274
b1baa808
MK
275 /**
276 * Method getCurrentLocation.
277 * @return ITmfLocation<?>
278 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
279 */
a3fc8213
AM
280 @Override
281 public ITmfLocation<?> getCurrentLocation() {
282 return iterator.getLocation();
283 }
284
a3fc8213
AM
285 // ------------------------------------------------------------------------
286 // Operators
287 // ------------------------------------------------------------------------
288
b1baa808
MK
289 /**
290 * Method setTimeRange.
291 * @param range TmfTimeRange
292 */
25e48683 293 protected void setTimeRange(final TmfTimeRange range) {
a3fc8213
AM
294 this.fStartTime = range.getStartTime();
295 this.fEndTime = range.getEndTime();
296 }
297
b1baa808
MK
298 /**
299 * Method setStartTime.
300 * @param startTime ITmfTimestamp
301 */
25e48683 302 protected void setStartTime(final ITmfTimestamp startTime) {
a3fc8213
AM
303 this.fStartTime = startTime;
304 }
305
b1baa808
MK
306 /**
307 * Method setEndTime.
308 * @param endTime ITmfTimestamp
309 */
25e48683 310 protected void setEndTime(final ITmfTimestamp endTime) {
a3fc8213
AM
311 this.fEndTime = endTime;
312 }
313
314 // ------------------------------------------------------------------------
315 // TmfProvider
316 // ------------------------------------------------------------------------
317
b1baa808
MK
318 /**
319 * Method armRequest.
320 * @param request ITmfDataRequest<CtfTmfEvent>
321 * @return ITmfContext
322 */
a3fc8213 323 @Override
25e48683 324 public ITmfContext armRequest(final ITmfDataRequest<CtfTmfEvent> request) {
a3fc8213 325 if ((request instanceof ITmfEventRequest<?>)
ce2388e0 326 && !TmfTimestamp.BIG_BANG
25e48683
FC
327 .equals(((ITmfEventRequest<CtfTmfEvent>) request)
328 .getRange().getStartTime())
329 && (request.getIndex() == 0)) {
330 final ITmfContext context = seekEvent(((ITmfEventRequest<CtfTmfEvent>) request)
ce2388e0
FC
331 .getRange().getStartTime());
332 ((ITmfEventRequest<CtfTmfEvent>) request)
25e48683 333 .setStartIndex((int) context.getRank());
a3fc8213
AM
334 return context;
335 }
336 return seekEvent(request.getIndex());
337 }
338
339 /**
340 * The trace reader keeps its own iterator: the "context" parameter here
341 * will be ignored.
ce2388e0 342 *
a3fc8213
AM
343 * If you wish to specify a new context, instantiate a new CtfIterator and
344 * seek() it to where you want, and use that to read events.
ce2388e0 345 *
a3fc8213
AM
346 * FIXME merge with getNextEvent below once they both use the same parameter
347 * type.
b1baa808
MK
348 * @param context ITmfContext
349 * @return CtfTmfEvent
a3fc8213 350 */
a1a24d68 351 @SuppressWarnings("unused")
a3fc8213 352 @Override
25e48683 353 public CtfTmfEvent getNext(final ITmfContext context) {
a3fc8213
AM
354 iterator.advance();
355 return iterator.getCurrentEvent();
356 }
357
358 // ------------------------------------------------------------------------
359 // ITmfTrace
360 // ------------------------------------------------------------------------
361
b1baa808
MK
362 /**
363 * Method seekEvent.
364 * @param location ITmfLocation<?>
365 * @return ITmfContext
366 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfLocation<?>)
367 */
a3fc8213 368 @Override
7e6347b0 369 public ITmfContext seekEvent(final ITmfLocation<?> location) {
ce2388e0 370 CtfLocation currentLocation = (CtfLocation) location;
11d6f468 371 if (currentLocation == null) {
ce2388e0 372 currentLocation = new CtfLocation(0L);
11d6f468 373 }
57c073c5
MK
374 if( !iterator.getLocation().equals(CtfIterator.nullLocation)) {
375 iterator.setLocation(currentLocation);
376 }
a3fc8213
AM
377 return iterator;
378 }
379
b1baa808
MK
380 /**
381 * Method getLocationRatio.
382 * @param location ITmfLocation<?>
383 * @return double
384 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getLocationRatio(ITmfLocation<?>)
385 */
a3fc8213 386 @Override
25e48683
FC
387 public double getLocationRatio(final ITmfLocation<?> location) {
388 final CtfLocation curLocation = (CtfLocation) location;
ce2388e0
FC
389 iterator.seek(curLocation.getLocation());
390 return ((double) iterator.getCurrentEvent().getTimestampValue() - iterator
391 .getStartTime())
392 / (iterator.getEndTime() - iterator.getStartTime());
a3fc8213
AM
393 }
394
b1baa808
MK
395 /**
396 * Method getStreamingInterval.
397 * @return long
398 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
399 */
a3fc8213
AM
400 @Override
401 public long getStreamingInterval() {
402 return 0;
403 }
404
b1baa808
MK
405 /**
406 * Method seekEvent.
407 * @param timestamp ITmfTimestamp
408 * @return ITmfContext
409 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfTimestamp)
410 */
a3fc8213 411 @Override
25e48683 412 public ITmfContext seekEvent(final ITmfTimestamp timestamp) {
a3fc8213
AM
413 iterator.seek(timestamp.getValue());
414 return iterator;
415 }
416
417 /**
418 * Seek by rank
b1baa808
MK
419 * @param rank long
420 * @return ITmfContext
421 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
a3fc8213
AM
422 */
423 @Override
25e48683 424 public ITmfContext seekEvent(final long rank) {
a3fc8213
AM
425 iterator.setRank(rank);
426 return iterator;
427 }
428
429 /**
430 * Seek rank ratio
b1baa808
MK
431 * @param ratio double
432 * @return ITmfContext
433 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(double)
a3fc8213
AM
434 */
435 @Override
7e6347b0 436 public ITmfContext seekEvent(final double ratio) {
a3fc8213
AM
437 iterator.seek((long) (this.fNbEvents * ratio));
438 return iterator;
439 }
440
b1baa808
MK
441 /**
442 * Method readNextEvent.
443 * @param context ITmfContext
444 * @return CtfTmfEvent
445 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(ITmfContext)
446 */
a1a24d68 447 @SuppressWarnings("unused")
a3fc8213 448 @Override
b4f71e4a 449 public CtfTmfEvent readNextEvent(final ITmfContext context) {
a3fc8213
AM
450 iterator.advance();
451 return iterator.getCurrentEvent();
452 }
453
b1baa808
MK
454 /**
455 * Method getResource.
456 * @return IResource
457 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
458 */
a3fc8213
AM
459 @Override
460 public IResource getResource() {
461 return this.fResource;
462 }
463
b1baa808
MK
464 /**
465 * Method getStateSystem.
466 * @return IStateSystemQuerier
467 */
d26f90fd 468 public IStateSystemQuerier getStateSystem() {
11d6f468
AM
469 return this.ss;
470 }
471
b1baa808
MK
472 /**
473 * Method getCTFTrace.
474 * @return CTFTrace
475 */
90235d6b 476 CTFTrace getCTFTrace() {
a3fc8213
AM
477 return fTrace;
478 }
a1a24d68 479
8636b448 480
d26f90fd
AM
481 /**
482 * Suppressing the warning, because the 'throws' will usually happen in
483 * sub-classes.
b1baa808 484 * @throws TmfTraceException
d26f90fd 485 */
a1a24d68 486 @SuppressWarnings({ "unused", "static-method" })
11d6f468
AM
487 protected void buildStateSystem() throws TmfTraceException {
488 /*
489 * Nothing is done in the basic implementation, please specify
490 * how/if to build a state system in derived classes.
491 */
492 return;
493 }
ce2388e0 494
a3fc8213 495}
This page took 0.05274 seconds and 5 git commands to generate.