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