tmf: Rename time range signals
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / uml2sd / loader / TmfUml2SDSyncLoader.java
1 /**********************************************************************
2 * Copyright (c) 2011, 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made 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:
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ui.views.uml2sd.loader;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.concurrent.CopyOnWriteArrayList;
20 import java.util.concurrent.locks.ReentrantLock;
21
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.core.runtime.jobs.Job;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.StructuredSelection;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
30 import org.eclipse.tracecompass.tmf.core.component.TmfComponent;
31 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
32 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
33 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
34 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest.ExecutionType;
35 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
36 import org.eclipse.tracecompass.tmf.core.signal.TmfWindowRangeUpdatedSignal;
37 import org.eclipse.tracecompass.tmf.core.signal.TmfSignal;
38 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
39 import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
40 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
41 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
42 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
43 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
44 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
45 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
46 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
47 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
48 import org.eclipse.tracecompass.tmf.core.uml2sd.ITmfSyncSequenceDiagramEvent;
49 import org.eclipse.tracecompass.tmf.core.uml2sd.TmfSyncSequenceDiagramEvent;
50 import org.eclipse.tracecompass.tmf.ui.editors.ITmfTraceEditor;
51 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.SDView;
52 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.core.Frame;
53 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.core.GraphNode;
54 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.core.Lifeline;
55 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.dialogs.Criteria;
56 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.dialogs.FilterCriteria;
57 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.dialogs.FilterListDialog;
58 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.handlers.provider.ISDAdvancedPagingProvider;
59 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.handlers.provider.ISDFilterProvider;
60 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.handlers.provider.ISDFindProvider;
61 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.handlers.provider.ISDGraphNodeSupporter;
62 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.load.IUml2SDLoader;
63 import org.eclipse.ui.IEditorPart;
64 import org.eclipse.ui.ISelectionListener;
65 import org.eclipse.ui.IWorkbenchPart;
66 import org.eclipse.ui.IWorkbenchWindow;
67 import org.eclipse.ui.PlatformUI;
68 import org.eclipse.ui.progress.IProgressConstants;
69
70 /**
71 * <p>
72 * This class is a reference implementation of the
73 * <code>org.eclipse.tracecompass.tmf.ui.Uml2SDLoader</code> extension point. It
74 * provides a Sequence Diagram loader for a user space trace with specific trace
75 * content for sending and receiving signals between components. I also includes
76 * a default implementation for the <code>ITmfEvent</code> parsing.
77 * </p>
78 *
79 * The class <code>TmfUml2SDSyncLoader</code> analyzes events from type
80 * <code>ITmfEvent</code> and creates events type
81 * <code>ITmfSyncSequenceDiagramEvent</code> if the <code>ITmfEvent</code>
82 * contains all relevant information. The analysis checks that the event type
83 * strings contains either string SEND or RECEIVE. If event type matches these
84 * key words, the analyzer will look for strings sender, receiver and signal in
85 * the event fields of type <code>ITmfEventField</code>. If all the data is
86 * found a sequence diagram event from can be created. Note that Sync Messages
87 * are assumed, which means start and end time are the same. <br>
88 * <br>
89 * The parsing of the <code>ITmfEvent</code> is done in the method
90 * <code>getSequnceDiagramEvent()</code> of class
91 * <code>TmfUml2SDSyncLoader</code>. By extending the class
92 * <code>TmfUml2SDSyncLoader</code> and overwriting
93 * <code>getSequnceDiagramEvent()</code> a customized parsing algorithm can be
94 * implemented.<br>
95 * <br>
96 * Note that combined traces of multiple components, that contain the trace
97 * information about the same interactions are not supported in the class
98 * <code>TmfUml2SDSyncLoader</code>.
99 *
100 * @version 1.0
101 * @author Bernd Hufmann
102 */
103 public class TmfUml2SDSyncLoader extends TmfComponent implements IUml2SDLoader, ISDFindProvider, ISDFilterProvider, ISDAdvancedPagingProvider, ISelectionListener {
104
105 // ------------------------------------------------------------------------
106 // Constants
107 // ------------------------------------------------------------------------
108
109 /**
110 * Default title name.
111 */
112 protected static final String TITLE = Messages.TmfUml2SDSyncLoader_ViewName;
113
114 /**
115 * Maximum number of messages per page.
116 */
117 protected static final int MAX_NUM_OF_MSG = 10000;
118
119 private static final int INDEXING_THREAD_SLEEP_VALUE = 100;
120
121 // ------------------------------------------------------------------------
122 // Attributes
123 // ------------------------------------------------------------------------
124
125 // Experiment attributes
126 /**
127 * The TMF trace reference.
128 */
129 protected ITmfTrace fTrace = null;
130 /**
131 * The current indexing event request.
132 */
133 protected ITmfEventRequest fIndexRequest = null;
134 /**
135 * The current request to fill a page.
136 */
137 protected ITmfEventRequest fPageRequest = null;
138 /**
139 * Flag whether the time range signal was sent by this loader class or not
140 */
141 protected volatile boolean fIsSignalSent = false;
142
143 // The view and event attributes
144 /**
145 * The sequence diagram view reference.
146 */
147 protected SDView fView = null;
148 /**
149 * The current sequence diagram frame reference.
150 */
151 protected Frame fFrame = null;
152 /**
153 * The list of sequence diagram events of current page.
154 */
155 protected List<ITmfSyncSequenceDiagramEvent> fEvents = new ArrayList<>();
156
157 // Checkpoint and page attributes
158 /**
159 * The checkpoints of the whole sequence diagram trace (i.e. start time stamp of each page)
160 */
161 protected List<TmfTimeRange> fCheckPoints = new ArrayList<>(MAX_NUM_OF_MSG);
162 /**
163 * The current page displayed.
164 */
165 protected volatile int fCurrentPage = 0;
166 /**
167 * The current time selected.
168 */
169 protected ITmfTimestamp fCurrentTime = null;
170 /**
171 * Flag to specify that selection of message is done by selection or by signal.
172 */
173 protected volatile boolean fIsSelect = false;
174
175 // Search attributes
176 /**
177 * The job for searching across pages.
178 */
179 protected SearchJob fFindJob = null;
180 /**
181 * List of found nodes within a page.
182 */
183 protected List<GraphNode> fFindResults = new ArrayList<>();
184 /**
185 * The current find criteria reference
186 */
187 protected Criteria fFindCriteria = null;
188 /**
189 * The current find index within the list of found nodes (<code>fFindeResults</code> within a page.
190 */
191 protected volatile int fCurrentFindIndex = 0;
192
193 // Filter attributes
194 /**
195 * The list of active filters.
196 */
197 protected List<FilterCriteria> fFilterCriteria = null;
198
199 // Thread synchronization
200 /**
201 * The synchronization lock.
202 */
203 protected ReentrantLock fLock = new ReentrantLock();
204
205 // ------------------------------------------------------------------------
206 // Constructors
207 // ------------------------------------------------------------------------
208 /**
209 * Default constructor
210 */
211 public TmfUml2SDSyncLoader() {
212 super(TITLE);
213 }
214
215 /**
216 * Constructor
217 *
218 * @param name Name of loader
219 */
220 public TmfUml2SDSyncLoader(String name) {
221 super(name);
222 }
223
224 // ------------------------------------------------------------------------
225 // Operations
226 // ------------------------------------------------------------------------
227
228 /**
229 * Returns the current time if available else null.
230 *
231 * @return the current time if available else null
232 */
233 public ITmfTimestamp getCurrentTime() {
234 fLock.lock();
235 try {
236 if (fCurrentTime != null) {
237 return fCurrentTime;
238 }
239 return null;
240 } finally {
241 fLock.unlock();
242 }
243 }
244
245 /**
246 * Waits for the page request to be completed
247 */
248 public void waitForCompletion() {
249 fLock.lock();
250 ITmfEventRequest request = fPageRequest;
251 fLock.unlock();
252 if (request != null) {
253 try {
254 request.waitForCompletion();
255 } catch (InterruptedException e) {
256 // ignore
257 }
258 }
259 }
260
261 /**
262 * Handler for the trace opened signal.
263 * @param signal The trace opened signal
264 */
265 @TmfSignalHandler
266 public void traceOpened(TmfTraceOpenedSignal signal) {
267 fTrace = signal.getTrace();
268 loadTrace();
269 }
270
271
272 /**
273 * Signal handler for the trace selected signal.
274 *
275 * Spawns a request to index the trace (checkpoints creation) as well as it fills
276 * the first page.
277 *
278 * @param signal The trace selected signal
279 */
280 @TmfSignalHandler
281 public void traceSelected(TmfTraceSelectedSignal signal) {
282 // Update the trace reference
283 ITmfTrace trace = signal.getTrace();
284 if (!trace.equals(fTrace)) {
285 fTrace = trace;
286 }
287 loadTrace();
288 }
289
290 /**
291 * Method for loading the current selected trace into the view.
292 * Sub-class need to override this method to add the view specific implementation.
293 */
294 protected void loadTrace() {
295 ITmfEventRequest indexRequest = null;
296 fLock.lock();
297
298 try {
299 final Job job = new IndexingJob("Indexing " + getName() + "..."); //$NON-NLS-1$ //$NON-NLS-2$
300 job.setUser(false);
301 job.schedule();
302
303 indexRequest = fIndexRequest;
304
305 cancelOngoingRequests();
306
307 TmfTimeRange window = TmfTimeRange.ETERNITY;
308
309 fIndexRequest = new TmfEventRequest(ITmfEvent.class, window, 0,
310 ITmfEventRequest.ALL_DATA, ITmfEventRequest.ExecutionType.BACKGROUND) {
311
312 private ITmfTimestamp fFirstTime = null;
313 private ITmfTimestamp fLastTime = null;
314 private int fNbSeqEvents = 0;
315 private final List<ITmfSyncSequenceDiagramEvent> fSdEvents = new ArrayList<>(MAX_NUM_OF_MSG);
316
317 @Override
318 public void handleData(ITmfEvent event) {
319 super.handleData(event);
320
321 ITmfSyncSequenceDiagramEvent sdEvent = getSequenceDiagramEvent(event);
322 ITmfTimestamp firstTime = fFirstTime;
323 ITmfTimestamp lastTime = fLastTime;
324
325 if (sdEvent != null) {
326 ++fNbSeqEvents;
327
328 if (firstTime == null) {
329 firstTime = event.getTimestamp();
330 fFirstTime = firstTime;
331 }
332
333 lastTime = event.getTimestamp();
334 fLastTime = lastTime;
335
336 if ((fNbSeqEvents % MAX_NUM_OF_MSG) == 0) {
337 fLock.lock();
338 try {
339 fCheckPoints.add(new TmfTimeRange(firstTime, lastTime));
340 if (fView != null) {
341 fView.updateCoolBar();
342 }
343 } finally {
344 fLock.unlock();
345 }
346 fFirstTime = null;
347
348 }
349
350 if (fNbSeqEvents > MAX_NUM_OF_MSG) {
351 // page is full
352 return;
353 }
354
355 fSdEvents.add(sdEvent);
356
357 if (fNbSeqEvents == MAX_NUM_OF_MSG) {
358 fillCurrentPage(fSdEvents);
359 }
360 }
361 }
362
363 @Override
364 public void handleSuccess() {
365 final ITmfTimestamp firstTime = fFirstTime;
366 final ITmfTimestamp lastTime = fLastTime;
367 if ((firstTime != null) && (lastTime != null)) {
368
369 fLock.lock();
370 try {
371 fCheckPoints.add(new TmfTimeRange(firstTime, lastTime));
372 if (fView != null) {
373 fView.updateCoolBar();
374 }
375 } finally {
376 fLock.unlock();
377 }
378 }
379
380 if (fNbSeqEvents <= MAX_NUM_OF_MSG) {
381 fillCurrentPage(fSdEvents);
382 }
383
384 super.handleSuccess();
385 }
386
387 @Override
388 public void handleCompleted() {
389 if (fEvents.isEmpty()) {
390 fFrame = new Frame();
391 // make sure that view is not null when setting frame
392 SDView sdView;
393 fLock.lock();
394 try {
395 sdView = fView;
396 } finally {
397 fLock.unlock();
398 }
399 if (sdView != null) {
400 sdView.setFrameSync(fFrame);
401 }
402 }
403 super.handleCompleted();
404 job.cancel();
405 }
406 };
407
408 } finally {
409 fLock.unlock();
410 }
411 if (indexRequest != null && !indexRequest.isCompleted()) {
412 indexRequest.cancel();
413 }
414 resetLoader();
415 fTrace.sendRequest(fIndexRequest);
416 }
417
418 /**
419 * Signal handler for the trace closed signal.
420 *
421 * @param signal The trace closed signal
422 */
423 @TmfSignalHandler
424 public void traceClosed(TmfTraceClosedSignal signal) {
425 if (signal.getTrace() != fTrace) {
426 return;
427 }
428 ITmfEventRequest indexRequest = null;
429 fLock.lock();
430 try {
431 indexRequest = fIndexRequest;
432 fIndexRequest = null;
433
434 cancelOngoingRequests();
435
436 if (fFilterCriteria != null) {
437 fFilterCriteria.clear();
438 }
439
440 FilterListDialog.deactivateSavedGlobalFilters();
441 } finally {
442 fTrace = null;
443 fLock.unlock();
444 }
445 if (indexRequest != null && !indexRequest.isCompleted()) {
446 indexRequest.cancel();
447 }
448
449 resetLoader();
450 }
451
452 /**
453 * Moves to the page that contains the time provided by the signal. The
454 * messages will be selected if the provided time is the time of a message.
455 *
456 * @param signal
457 * The selection range signal
458 * @since 1.0
459 */
460 @TmfSignalHandler
461 public void selectionRangeUpdated(TmfSelectionRangeUpdatedSignal signal) {
462 fLock.lock();
463 try {
464 if ((signal.getSource() != this) && (fFrame != null) && (fCheckPoints.size() > 0)) {
465 fCurrentTime = signal.getBeginTime();
466 fIsSelect = true;
467 moveToMessage();
468 }
469 } finally {
470 fLock.unlock();
471 }
472 }
473
474 /**
475 * Moves to the page that contains the current time provided by signal. No
476 * message will be selected however the focus will be set to the message if
477 * the provided time is the time of a message.
478 *
479 * @param signal
480 * The window range signal
481 * @since 1.0
482 */
483 @TmfSignalHandler
484 public void windowRangeUpdated(TmfWindowRangeUpdatedSignal signal) {
485 fLock.lock();
486 try {
487 if ((signal.getSource() != this) && (fFrame != null) && !fIsSignalSent && (fCheckPoints.size() > 0)) {
488 TmfTimeRange newTimeRange = signal.getCurrentRange();
489
490 fIsSelect = false;
491 fCurrentTime = newTimeRange.getStartTime();
492
493 moveToMessage();
494 }
495 } finally {
496 fLock.unlock();
497 }
498
499 }
500
501 @Override
502 public void setViewer(SDView viewer) {
503
504 fLock.lock();
505 try {
506 fView = viewer;
507 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().addPostSelectionListener(this);
508 fView.setSDFindProvider(this);
509 fView.setSDPagingProvider(this);
510 fView.setSDFilterProvider(this);
511
512 resetLoader();
513 IEditorPart editor = fView.getSite().getPage().getActiveEditor();
514 if (editor instanceof ITmfTraceEditor) {
515 ITmfTrace trace = ((ITmfTraceEditor) editor).getTrace();
516 if (trace != null) {
517 traceSelected(new TmfTraceSelectedSignal(this, trace));
518 }
519 }
520 } finally {
521 fLock.unlock();
522 }
523 }
524
525 @Override
526 public String getTitleString() {
527 return getName();
528 }
529
530 @Override
531 public void dispose() {
532 super.dispose();
533 ITmfEventRequest indexRequest = null;
534 fLock.lock();
535 try {
536 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
537 // During Eclipse shutdown the active workbench window is null
538 if (window != null) {
539 window.getSelectionService().removePostSelectionListener(this);
540 }
541
542 indexRequest = fIndexRequest;
543 fIndexRequest = null;
544 cancelOngoingRequests();
545
546 fView.setSDFindProvider(null);
547 fView.setSDPagingProvider(null);
548 fView.setSDFilterProvider(null);
549 fView = null;
550 } finally {
551 fLock.unlock();
552 }
553 if (indexRequest != null && !indexRequest.isCompleted()) {
554 indexRequest.cancel();
555 }
556 }
557
558 @Override
559 public boolean isNodeSupported(int nodeType) {
560 switch (nodeType) {
561 case ISDGraphNodeSupporter.LIFELINE:
562 case ISDGraphNodeSupporter.SYNCMESSAGE:
563 return true;
564
565 default:
566 break;
567 }
568 return false;
569 }
570
571 @Override
572 public String getNodeName(int nodeType, String loaderClassName) {
573 switch (nodeType) {
574 case ISDGraphNodeSupporter.LIFELINE:
575 return Messages.TmfUml2SDSyncLoader_CategoryLifeline;
576 case ISDGraphNodeSupporter.SYNCMESSAGE:
577 return Messages.TmfUml2SDSyncLoader_CategoryMessage;
578 default:
579 break;
580 }
581 return ""; //$NON-NLS-1$
582 }
583
584 @Override
585 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
586 ISelection sel = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
587 if ((sel != null) && (sel instanceof StructuredSelection)) {
588 StructuredSelection stSel = (StructuredSelection) sel;
589 if (stSel.getFirstElement() instanceof TmfSyncMessage) {
590 TmfSyncMessage syncMsg = ((TmfSyncMessage) stSel.getFirstElement());
591 ITmfTimestamp startTime = syncMsg.getStartTime();
592 if (startTime == null) {
593 startTime = TmfTimestamp.BIG_BANG;
594 }
595 broadcast(new TmfSelectionRangeUpdatedSignal(this, startTime));
596 }
597 }
598 }
599
600 @Override
601 public boolean find(Criteria toSearch) {
602 fLock.lock();
603 try {
604 if (fFrame == null) {
605 return false;
606 }
607
608 if ((fFindResults == null) || (fFindCriteria == null) || !fFindCriteria.compareTo(toSearch)) {
609 fFindResults = new CopyOnWriteArrayList<>();
610 fFindCriteria = toSearch;
611 if (fFindCriteria.isLifeLineSelected()) {
612 for (int i = 0; i < fFrame.lifeLinesCount(); i++) {
613 if (fFindCriteria.matches(fFrame.getLifeline(i).getName())) {
614 fFindResults.add(fFrame.getLifeline(i));
615 }
616 }
617 }
618
619 ArrayList<GraphNode> msgs = new ArrayList<>();
620 if (fFindCriteria.isSyncMessageSelected()) {
621 for (int i = 0; i < fFrame.syncMessageCount(); i++) {
622 if (fFindCriteria.matches(fFrame.getSyncMessage(i).getName())) {
623 msgs.add(fFrame.getSyncMessage(i));
624 }
625 }
626 }
627
628 if (!msgs.isEmpty()) {
629 fFindResults.addAll(msgs);
630 }
631
632 List<GraphNode> selection = fView.getSDWidget().getSelection();
633 if ((selection != null) && (selection.size() == 1)) {
634 fCurrentFindIndex = fFindResults.indexOf(selection.get(0)) + 1;
635 } else {
636 fCurrentFindIndex = 0;
637 }
638 } else {
639 fCurrentFindIndex++;
640 }
641
642 if (fFindResults.size() > fCurrentFindIndex) {
643 GraphNode current = fFindResults.get(fCurrentFindIndex);
644 fView.getSDWidget().moveTo(current);
645 return true;
646 }
647 fFindResults = null;
648 fCurrentFindIndex =0;
649 return findInNextPages(fFindCriteria); // search in other page
650 } finally {
651 fLock.unlock();
652 }
653 }
654
655 @Override
656 public void cancel() {
657 cancelOngoingRequests();
658 }
659
660 @Override
661 public boolean filter(List<FilterCriteria> filters) {
662 fLock.lock();
663 try {
664 cancelOngoingRequests();
665
666 if (filters == null) {
667 fFilterCriteria = new ArrayList<>();
668 } else {
669 List<FilterCriteria> list = filters;
670 fFilterCriteria = new ArrayList<>(list);
671 }
672
673 fillCurrentPage(fEvents);
674
675 } finally {
676 fLock.unlock();
677 }
678 return true;
679 }
680
681 @Override
682 public boolean hasNextPage() {
683 fLock.lock();
684 try {
685 int size = fCheckPoints.size();
686 if (size > 0) {
687 return fCurrentPage < (size - 1);
688 }
689 } finally {
690 fLock.unlock();
691 }
692 return false;
693 }
694
695 @Override
696 public boolean hasPrevPage() {
697 fLock.lock();
698 try {
699 return fCurrentPage > 0;
700 } finally {
701 fLock.unlock();
702 }
703 }
704
705 @Override
706 public void nextPage() {
707 fLock.lock();
708 try {
709 // Safety check
710 if (fCurrentPage >= (fCheckPoints.size() - 1)) {
711 return;
712 }
713
714 cancelOngoingRequests();
715 fCurrentTime = null;
716 fCurrentPage++;
717 moveToPage();
718 } finally {
719 fLock.unlock();
720 }
721 }
722
723 @Override
724 public void prevPage() {
725 fLock.lock();
726 try {
727 // Safety check
728 if (fCurrentPage <= 0) {
729 return;
730 }
731
732 cancelOngoingRequests();
733 fCurrentTime = null;
734 fCurrentPage--;
735 moveToPage();
736 } finally {
737 fLock.unlock();
738 }
739 }
740
741 @Override
742 public void firstPage() {
743 fLock.lock();
744 try {
745
746 cancelOngoingRequests();
747 fCurrentTime = null;
748 fCurrentPage = 0;
749 moveToPage();
750 } finally {
751 fLock.unlock();
752 }
753 }
754
755 @Override
756 public void lastPage() {
757 fLock.lock();
758 try {
759 cancelOngoingRequests();
760 fCurrentTime = null;
761 fCurrentPage = fCheckPoints.size() - 1;
762 moveToPage();
763 } finally {
764 fLock.unlock();
765 }
766 }
767
768 @Override
769 public int currentPage() {
770 fLock.lock();
771 try {
772 return fCurrentPage;
773 } finally {
774 fLock.unlock();
775 }
776 }
777
778 @Override
779 public int pagesCount() {
780 fLock.lock();
781 try {
782 return fCheckPoints.size();
783 } finally {
784 fLock.unlock();
785 }
786 }
787
788 @Override
789 public void pageNumberChanged(int pagenNumber) {
790 int localPageNumber = pagenNumber;
791
792 fLock.lock();
793 try {
794 cancelOngoingRequests();
795
796 if (localPageNumber < 0) {
797 localPageNumber = 0;
798 }
799 int size = fCheckPoints.size();
800 if (localPageNumber > (size - 1)) {
801 localPageNumber = size - 1;
802 }
803 fCurrentPage = localPageNumber;
804 moveToPage();
805 } finally {
806 fLock.unlock();
807 }
808 }
809
810 @Override
811 public void broadcast(TmfSignal signal) {
812 fIsSignalSent = true;
813 super.broadcast(signal);
814 fIsSignalSent = false;
815 }
816
817 /**
818 * Cancels any ongoing find operation
819 */
820 protected void cancelOngoingRequests() {
821 fLock.lock();
822 ITmfEventRequest pageRequest = null;
823 try {
824 // Cancel the search thread
825 if (fFindJob != null) {
826 fFindJob.cancel();
827 }
828
829 fFindResults = null;
830 fFindCriteria = null;
831 fCurrentFindIndex = 0;
832
833 pageRequest = fPageRequest;
834 fPageRequest = null;
835 } finally {
836 fLock.unlock();
837 }
838 if (pageRequest != null && !pageRequest.isCompleted()) {
839 pageRequest.cancel();
840 }
841 }
842
843 /**
844 * Resets loader attributes
845 */
846 protected void resetLoader() {
847 fLock.lock();
848 try {
849 fCurrentTime = null;
850 fEvents.clear();
851 fCheckPoints.clear();
852 fCurrentPage = 0;
853 fCurrentFindIndex = 0;
854 fFindCriteria = null;
855 fFindResults = null;
856 fView.setFrameSync(new Frame());
857 fFrame = null;
858 }
859 finally {
860 fLock.unlock();
861 }
862
863 }
864
865 /**
866 * Fills current page with sequence diagram content.
867 *
868 * @param events sequence diagram events
869 */
870 protected void fillCurrentPage(List<ITmfSyncSequenceDiagramEvent> events) {
871
872 fLock.lock();
873 try {
874 fEvents = new ArrayList<>(events);
875 if (fView != null && !events.isEmpty()) {
876 fView.toggleWaitCursorAsync(true);
877 }
878 } finally {
879 fLock.unlock();
880 }
881
882 final Frame frame = new Frame();
883
884 if (!events.isEmpty()) {
885 Map<String, Lifeline> nodeToLifelineMap = new HashMap<>();
886
887 frame.setName(Messages.TmfUml2SDSyncLoader_FrameName);
888
889 for (int i = 0; i < events.size(); i++) {
890
891 ITmfSyncSequenceDiagramEvent sdEvent = events.get(i);
892
893 if ((nodeToLifelineMap.get(sdEvent.getSender()) == null) && (!filterLifeLine(sdEvent.getSender()))) {
894 Lifeline lifeline = new Lifeline();
895 lifeline.setName(sdEvent.getSender());
896 nodeToLifelineMap.put(sdEvent.getSender(), lifeline);
897 frame.addLifeLine(lifeline);
898 }
899
900 if ((nodeToLifelineMap.get(sdEvent.getReceiver()) == null) && (!filterLifeLine(sdEvent.getReceiver()))) {
901 Lifeline lifeline = new Lifeline();
902 lifeline.setName(sdEvent.getReceiver());
903 nodeToLifelineMap.put(sdEvent.getReceiver(), lifeline);
904 frame.addLifeLine(lifeline);
905 }
906 }
907
908 int eventOccurence = 1;
909
910 for (int i = 0; i < events.size(); i++) {
911 ITmfSyncSequenceDiagramEvent sdEvent = events.get(i);
912
913 // Check message filter
914 if (filterMessage(sdEvent)) {
915 continue;
916 }
917
918 // Set the message sender and receiver
919 Lifeline startLifeline = nodeToLifelineMap.get(sdEvent.getSender());
920 Lifeline endLifeline = nodeToLifelineMap.get(sdEvent.getReceiver());
921
922 // Check if any of the lifelines were filtered
923 if ((startLifeline == null) || (endLifeline == null)) {
924 continue;
925 }
926
927 int tmp = Math.max(startLifeline.getEventOccurrence(), endLifeline.getEventOccurrence());
928 eventOccurence = Math.max(eventOccurence, tmp);
929
930 startLifeline.setCurrentEventOccurrence(eventOccurence);
931 endLifeline.setCurrentEventOccurrence(eventOccurence);
932
933 TmfSyncMessage message = new TmfSyncMessage(sdEvent, eventOccurence++);
934
935 message.setStartLifeline(startLifeline);
936 message.setEndLifeline(endLifeline);
937
938 message.setTime(sdEvent.getStartTime());
939
940 // add the message to the frame
941 frame.addMessage(message);
942
943 }
944 fLock.lock();
945 try {
946 if (!fView.getSDWidget().isDisposed()) {
947 fView.getSDWidget().getDisplay().asyncExec(new Runnable() {
948
949 @Override
950 public void run() {
951
952 fLock.lock();
953 try {
954 // check if view was disposed in the meanwhile
955 if ((fView != null) && (!fView.getSDWidget().isDisposed())) {
956 fFrame = frame;
957 fView.setFrame(fFrame);
958
959 if (fCurrentTime != null) {
960 moveToMessageInPage();
961 }
962
963 if (fFindCriteria != null) {
964 find(fFindCriteria);
965 }
966
967 fView.toggleWaitCursorAsync(false);
968 }
969 } finally {
970 fLock.unlock();
971 }
972
973 }
974 });
975 }
976 }
977 finally {
978 fLock.unlock();
979 }
980 }
981 }
982
983 /**
984 * Moves to a certain message defined by timestamp (across pages)
985 */
986 protected void moveToMessage() {
987 int page = 0;
988
989 fLock.lock();
990 try {
991 page = getPage(fCurrentTime);
992
993 if (page == fCurrentPage) {
994 moveToMessageInPage();
995 return;
996 }
997 fCurrentPage = page;
998 moveToPage(false);
999 } finally {
1000 fLock.unlock();
1001 }
1002 }
1003
1004 /**
1005 * Moves to a certain message defined by timestamp in current page
1006 */
1007 protected void moveToMessageInPage() {
1008 fLock.lock();
1009 try {
1010 if (!fView.getSDWidget().isDisposed()) {
1011 // Check for GUI thread
1012 if(Display.getCurrent() != null) {
1013 // Already in GUI thread - execute directly
1014 TmfSyncMessage prevMessage = null;
1015 TmfSyncMessage syncMessage = null;
1016 boolean isExactTime = false;
1017 for (int i = 0; i < fFrame.syncMessageCount(); i++) {
1018 if (fFrame.getSyncMessage(i) instanceof TmfSyncMessage) {
1019 syncMessage = (TmfSyncMessage) fFrame.getSyncMessage(i);
1020 if (syncMessage.getStartTime().compareTo(fCurrentTime) == 0) {
1021 isExactTime = true;
1022 break;
1023 } else if ((syncMessage.getStartTime().compareTo(fCurrentTime) > 0) && (prevMessage != null)) {
1024 syncMessage = prevMessage;
1025 break;
1026 }
1027 prevMessage = syncMessage;
1028 }
1029 }
1030 if (fIsSelect && isExactTime) {
1031 fView.getSDWidget().moveTo(syncMessage);
1032 }
1033 else {
1034 fView.getSDWidget().ensureVisible(syncMessage);
1035 fView.getSDWidget().clearSelection();
1036 fView.getSDWidget().redraw();
1037 }
1038 }
1039 else {
1040 // Not in GUI thread - queue action in GUI thread.
1041 fView.getSDWidget().getDisplay().asyncExec(new Runnable() {
1042 @Override
1043 public void run() {
1044 moveToMessageInPage();
1045 }
1046 });
1047 }
1048 }
1049 }
1050 finally {
1051 fLock.unlock();
1052 }
1053 }
1054
1055 /**
1056 * Moves to a certain message defined by timestamp (across pages)
1057 */
1058 protected void moveToPage() {
1059 moveToPage(true);
1060 }
1061
1062 /**
1063 * Moves to a certain page.
1064 *
1065 * @param notifyAll true to broadcast time range signal to other signal handlers else false
1066 */
1067 protected void moveToPage(boolean notifyAll) {
1068
1069 TmfTimeRange window = null;
1070
1071 fLock.lock();
1072 try {
1073 // Safety check
1074 if (fCurrentPage > fCheckPoints.size()) {
1075 return;
1076 }
1077 window = fCheckPoints.get(fCurrentPage);
1078 } finally {
1079 fLock.unlock();
1080 }
1081
1082 if (window == null) {
1083 window = TmfTimeRange.ETERNITY;
1084 }
1085
1086 fPageRequest = new TmfEventRequest(ITmfEvent.class, window, 0,
1087 ITmfEventRequest.ALL_DATA, ITmfEventRequest.ExecutionType.FOREGROUND) {
1088 private final List<ITmfSyncSequenceDiagramEvent> fSdEvent = new ArrayList<>();
1089
1090 @Override
1091 public void handleData(ITmfEvent event) {
1092 super.handleData(event);
1093
1094 ITmfSyncSequenceDiagramEvent sdEvent = getSequenceDiagramEvent(event);
1095
1096 if (sdEvent != null) {
1097 fSdEvent.add(sdEvent);
1098 }
1099 }
1100
1101 @Override
1102 public void handleSuccess() {
1103 fillCurrentPage(fSdEvent);
1104 super.handleSuccess();
1105 }
1106
1107 };
1108
1109 fTrace.sendRequest(fPageRequest);
1110
1111 if (notifyAll) {
1112 TmfTimeRange timeRange = getSignalTimeRange(window.getStartTime());
1113 broadcast(new TmfWindowRangeUpdatedSignal(this, timeRange));
1114 }
1115 }
1116
1117 /**
1118 * Gets page that contains timestamp
1119 *
1120 * @param time The timestamp
1121 * @return page that contains the time
1122 */
1123 protected int getPage(ITmfTimestamp time) {
1124 int page;
1125 int size;
1126 fLock.lock();
1127 try {
1128 size = fCheckPoints.size();
1129 for (page = 0; page < size; page++) {
1130 TmfTimeRange timeRange = fCheckPoints.get(page);
1131 if (timeRange.getEndTime().compareTo(time) >= 0) {
1132 break;
1133 }
1134 }
1135 if (page >= size) {
1136 page = size - 1;
1137 }
1138 return page;
1139 } finally {
1140 fLock.unlock();
1141 }
1142 }
1143
1144 /**
1145 * Background search in trace for expression in criteria.
1146 *
1147 * @param findCriteria The find criteria
1148 * @return true if background request was started else false
1149 */
1150 protected boolean findInNextPages(Criteria findCriteria) {
1151 fLock.lock();
1152 try {
1153 if (fFindJob != null) {
1154 return true;
1155 }
1156
1157 int nextPage = fCurrentPage + 1;
1158
1159 if ((nextPage) >= fCheckPoints.size()) {
1160 // we are at the end
1161 return false;
1162 }
1163
1164 TmfTimeRange window = new TmfTimeRange(fCheckPoints.get(nextPage).getStartTime(), fCheckPoints.get(fCheckPoints.size()-1).getEndTime());
1165 fFindJob = new SearchJob(findCriteria, window);
1166 fFindJob.schedule();
1167 fView.toggleWaitCursorAsync(true);
1168 } finally {
1169 fLock.unlock();
1170 }
1171 return true;
1172 }
1173
1174 /**
1175 * Gets time range for time range signal.
1176 *
1177 * @param startTime The start time of time range.
1178 * @return the time range
1179 */
1180 protected TmfTimeRange getSignalTimeRange(ITmfTimestamp startTime) {
1181 fLock.lock();
1182 try {
1183 TmfTimeRange currentRange = TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange();
1184 long offset = fTrace == null ? 0 : currentRange.getEndTime().getDelta(currentRange.getStartTime()).normalize(0, startTime.getScale()).getValue();
1185 TmfTimestamp initialEndOfWindow = new TmfTimestamp(startTime.getValue() + offset, startTime.getScale());
1186 return new TmfTimeRange(startTime, initialEndOfWindow);
1187 }
1188 finally {
1189 fLock.unlock();
1190 }
1191 }
1192
1193 /**
1194 * Checks if filter criteria matches the message name in given SD event.
1195 *
1196 * @param sdEvent The SD event to check
1197 * @return true if match else false.
1198 */
1199 protected boolean filterMessage(ITmfSyncSequenceDiagramEvent sdEvent) {
1200 fLock.lock();
1201 try {
1202 if (fFilterCriteria != null) {
1203 for(FilterCriteria criteria : fFilterCriteria) {
1204 if (criteria.isActive() && criteria.getCriteria().isSyncMessageSelected() && criteria.getCriteria().matches(sdEvent.getName())) {
1205 return true;
1206 }
1207 }
1208 }
1209 } finally {
1210 fLock.unlock();
1211 }
1212 return false;
1213 }
1214
1215 /**
1216 * Checks if filter criteria matches a lifeline name (sender or receiver) in given SD event.
1217 *
1218 * @param lifeline the message receiver
1219 * @return true if match else false.
1220 */
1221 protected boolean filterLifeLine(String lifeline) {
1222 fLock.lock();
1223 try {
1224 if (fFilterCriteria != null) {
1225 for(FilterCriteria criteria : fFilterCriteria) {
1226 if (criteria.isActive() && criteria.getCriteria().isLifeLineSelected() && criteria.getCriteria().matches(lifeline)) {
1227 return true;
1228 }
1229 }
1230 }
1231 } finally {
1232 fLock.unlock();
1233 }
1234 return false;
1235 }
1236
1237 /**
1238 * Job to search in trace for given time range.
1239 */
1240 protected class SearchJob extends Job {
1241
1242 /**
1243 * The search event request.
1244 */
1245 protected final SearchEventRequest fSearchRequest;
1246
1247 /**
1248 * Constructor
1249 *
1250 * @param findCriteria The search criteria
1251 * @param window Time range to search in
1252 */
1253 public SearchJob(Criteria findCriteria, TmfTimeRange window) {
1254 super(Messages.TmfUml2SDSyncLoader_SearchJobDescrition);
1255 fSearchRequest = new SearchEventRequest(window, ITmfEventRequest.ALL_DATA,
1256 ITmfEventRequest.ExecutionType.FOREGROUND, findCriteria);
1257 }
1258
1259 @Override
1260 protected IStatus run(IProgressMonitor monitor) {
1261 fSearchRequest.setMonitor(monitor);
1262
1263 fTrace.sendRequest(fSearchRequest);
1264
1265 try {
1266 fSearchRequest.waitForCompletion();
1267 } catch (InterruptedException e) {
1268 Activator.getDefault().logError("Search request interrupted!", e); //$NON-NLS-1$
1269 }
1270
1271 IStatus status = Status.OK_STATUS;
1272 if (fSearchRequest.isFound() && (fSearchRequest.getFoundTime() != null)) {
1273 fCurrentTime = fSearchRequest.getFoundTime();
1274
1275 // Avoid double-selection. Selection will be done when calling find(criteria)
1276 // after moving to relevant page
1277 fIsSelect = false;
1278 if (!fView.getSDWidget().isDisposed()) {
1279 fView.getSDWidget().getDisplay().asyncExec(new Runnable() {
1280
1281 @Override
1282 public void run() {
1283 moveToMessage();
1284 }
1285 });
1286 }
1287 }
1288 else {
1289 if (monitor.isCanceled()) {
1290 status = Status.CANCEL_STATUS;
1291 }
1292 else {
1293 // String was not found
1294 status = new Status(IStatus.WARNING, Activator.PLUGIN_ID, Messages.TmfUml2SDSyncLoader_SearchNotFound);
1295 }
1296 setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
1297 }
1298 monitor.done();
1299
1300 fLock.lock();
1301 try {
1302 fView.toggleWaitCursorAsync(false);
1303 fFindJob = null;
1304 } finally {
1305 fLock.unlock();
1306 }
1307
1308 return status;
1309 }
1310
1311 @Override
1312 protected void canceling() {
1313 fSearchRequest.cancel();
1314 fLock.lock();
1315 try {
1316 fFindJob = null;
1317 } finally {
1318 fLock.unlock();
1319 }
1320 }
1321 }
1322
1323 /**
1324 * TMF event request for searching within trace.
1325 */
1326 protected class SearchEventRequest extends TmfEventRequest {
1327
1328 /**
1329 * The find criteria.
1330 */
1331 private final Criteria fCriteria;
1332 /**
1333 * A progress monitor
1334 */
1335 private IProgressMonitor fMonitor;
1336 /**
1337 * Flag to indicate that node was found according the criteria .
1338 */
1339 private boolean fIsFound = false;
1340 /**
1341 * Time stamp of found item.
1342 */
1343 private ITmfTimestamp fFoundTime = null;
1344
1345 /**
1346 * Constructor
1347 * @param range see {@link TmfEventRequest#TmfEventRequest(Class, TmfTimeRange, long, int, org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest.ExecutionType) TmfEventRequest}
1348 * @param nbRequested see {@link TmfEventRequest#TmfEventRequest(Class, TmfTimeRange, long, int, org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest.ExecutionType) TmfEventRequest}
1349 * @param priority {@link ExecutionType#FOREGROUND} or {@link ExecutionType#BACKGROUND}
1350 * @param criteria The search criteria
1351 */
1352 public SearchEventRequest(TmfTimeRange range, int nbRequested, ExecutionType priority, Criteria criteria) {
1353 this(range, nbRequested, priority, criteria, null);
1354 }
1355
1356 /**
1357 * Constructor
1358 * @param range see {@link TmfEventRequest#TmfEventRequest(Class, TmfTimeRange, long, int, org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest.ExecutionType) TmfEventRequest}
1359 * @param nbRequested see {@link TmfEventRequest#TmfEventRequest(Class, TmfTimeRange, long, int, org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest.ExecutionType) TmfEventRequest}
1360 * @param priority {@link ExecutionType#FOREGROUND} or {@link ExecutionType#BACKGROUND}
1361 * @param criteria The search criteria
1362 * @param monitor progress monitor
1363 */
1364 public SearchEventRequest(TmfTimeRange range, int nbRequested, ExecutionType priority, Criteria criteria, IProgressMonitor monitor) {
1365 super(ITmfEvent.class, range, 0, nbRequested, priority);
1366 fCriteria = new Criteria(criteria);
1367 fMonitor = monitor;
1368 }
1369
1370 @Override
1371 public void handleData(ITmfEvent event) {
1372 super.handleData(event);
1373
1374 if ((fMonitor!= null) && fMonitor.isCanceled()) {
1375 super.cancel();
1376 return;
1377 }
1378
1379 ITmfSyncSequenceDiagramEvent sdEvent = getSequenceDiagramEvent(event);
1380
1381 if (sdEvent != null) {
1382
1383 if (fCriteria.isLifeLineSelected()) {
1384 if (fCriteria.matches(sdEvent.getSender())) {
1385 fFoundTime = event.getTimestamp();
1386 fIsFound = true;
1387 super.cancel();
1388 }
1389
1390 if (fCriteria.matches(sdEvent.getReceiver())) {
1391 fFoundTime = event.getTimestamp();
1392 fIsFound = true;
1393 super.cancel();
1394 }
1395 }
1396
1397 if (fCriteria.isSyncMessageSelected() && fCriteria.matches(sdEvent.getName())) {
1398 fFoundTime = event.getTimestamp();
1399 fIsFound = true;
1400 super.cancel();
1401 }
1402 }
1403 }
1404
1405 /**
1406 * Set progress monitor.
1407 *
1408 * @param monitor The monitor to assign
1409 */
1410 public void setMonitor(IProgressMonitor monitor) {
1411 fMonitor = monitor;
1412 }
1413
1414 /**
1415 * Check if find criteria was met.
1416 *
1417 * @return true if find criteria was met.
1418 */
1419 public boolean isFound() {
1420 return fIsFound;
1421 }
1422
1423 /**
1424 * Returns timestamp of found time.
1425 *
1426 * @return timestamp of found time.
1427 */
1428 public ITmfTimestamp getFoundTime() {
1429 return fFoundTime;
1430 }
1431 }
1432
1433 /**
1434 * Job class to provide progress monitor feedback.
1435 *
1436 * @author Bernd Hufmann
1437 */
1438 protected static class IndexingJob extends Job {
1439
1440 /**
1441 * @param name The job name
1442 */
1443 public IndexingJob(String name) {
1444 super(name);
1445 }
1446
1447 @Override
1448 protected IStatus run(IProgressMonitor monitor) {
1449 while (!monitor.isCanceled()) {
1450 try {
1451 Thread.sleep(INDEXING_THREAD_SLEEP_VALUE);
1452 } catch (InterruptedException e) {
1453 return Status.OK_STATUS;
1454 }
1455 }
1456 monitor.done();
1457 return Status.OK_STATUS;
1458 }
1459 }
1460
1461
1462 /**
1463 * Returns sequence diagram event if details in given event are available else null.
1464 *
1465 * @param tmfEvent Event to parse for sequence diagram event details
1466 * @return sequence diagram event if details are available else null
1467 */
1468 protected ITmfSyncSequenceDiagramEvent getSequenceDiagramEvent(ITmfEvent tmfEvent){
1469 //type = .*RECEIVE.* or .*SEND.*
1470 //content = sender:<sender name>:receiver:<receiver name>,signal:<signal name>
1471 String eventType = tmfEvent.getType().toString();
1472 if (eventType.contains(Messages.TmfUml2SDSyncLoader_EventTypeSend) || eventType.contains(Messages.TmfUml2SDSyncLoader_EventTypeReceive)) {
1473 Object sender = tmfEvent.getContent().getField(Messages.TmfUml2SDSyncLoader_FieldSender);
1474 Object receiver = tmfEvent.getContent().getField(Messages.TmfUml2SDSyncLoader_FieldReceiver);
1475 Object name = tmfEvent.getContent().getField(Messages.TmfUml2SDSyncLoader_FieldSignal);
1476 if ((sender instanceof ITmfEventField) && (receiver instanceof ITmfEventField) && (name instanceof ITmfEventField)) {
1477 ITmfSyncSequenceDiagramEvent sdEvent = new TmfSyncSequenceDiagramEvent(tmfEvent,
1478 ((ITmfEventField) sender).getValue().toString(),
1479 ((ITmfEventField) receiver).getValue().toString(),
1480 ((ITmfEventField) name).getValue().toString());
1481
1482 return sdEvent;
1483 }
1484 }
1485 return null;
1486 }
1487 }
This page took 0.068494 seconds and 6 git commands to generate.