use of org.eclipse.tracecompass.tmf.core.filter.ITmfFilter in project tracecompass by tracecompass.
the class TmfFilterHelperTest method testInputRegexNoAspect.
/**
* Test a regex whose parameter is not an aspect name
*/
@Test
public void testInputRegexNoAspect() {
ITmfEventAspect<@NonNull Object> contentAspect = TmfBaseAspects.getContentsAspect().forField(FIELD1_NAME);
String regex = FIELD1_NAME + " == \"" + FIELD1_VALUE1 + "\"";
ITmfFilter filter = getFilter(regex);
assertTrue(filter instanceof TmfFilterRootNode);
TmfFilterRootNode node = (TmfFilterRootNode) filter;
assertEquals(1, node.getChildrenCount());
ITmfFilterTreeNode child = node.getChild(0);
assertTrue(child instanceof TmfFilterEqualsNode);
TmfFilterEqualsNode equalsNode = (TmfFilterEqualsNode) child;
assertEquals(contentAspect, equalsNode.getEventAspect());
assertEquals(FIELD1_VALUE1, equalsNode.getValue());
// Test expected behavior on events
assertTrue(filter.matches(fEvent1));
assertFalse(filter.matches(fEvent2));
assertFalse(filter.matches(fEvent3));
}
use of org.eclipse.tracecompass.tmf.core.filter.ITmfFilter in project tracecompass by tracecompass.
the class TmfFilterHelperTest method testInputRegexPresent.
/**
* Test a regex with present
*/
@Test
public void testInputRegexPresent() {
ITmfEventAspect<@NonNull Object> aspect = TmfBaseAspects.getContentsAspect().forField(FIELD1_NAME);
String regex = FIELD1_NAME + " present";
ITmfFilter filter = getFilter(regex);
// verify the main root node
assertTrue(filter instanceof TmfFilterRootNode);
TmfFilterRootNode node = (TmfFilterRootNode) filter;
assertEquals(1, node.getChildrenCount());
ITmfFilterTreeNode child = node.getChild(0);
assertTrue(child instanceof TmfFilterMatchesNode);
// verify the equals node
TmfFilterMatchesNode equalsNode = (TmfFilterMatchesNode) child;
assertEquals(aspect, equalsNode.getEventAspect());
assertEquals(".*", equalsNode.getRegex());
// Test expected behavior on events
assertTrue(filter.matches(fEvent1));
assertFalse(filter.matches(fEvent2));
assertTrue(filter.matches(fEvent3));
}
use of org.eclipse.tracecompass.tmf.core.filter.ITmfFilter in project tracecompass by tracecompass.
the class TmfEventTableDataProvider method fetchIndex.
/**
* Find the index in the table of an event using the rank in the trace or
* the timestamp value. It will take any filter into consideration.
*
* @param fetchParameters
* Map of parameters that contain the filter applied to the
* table, if any. Everything else is ignored.
* @param traceRank
* Rank of the event in the trace
* @param timeBegin
* Timestamp of the event
* @param monitor
* Progress monitor
* @return Index in the table
*/
public TmfModelResponse<List<Long>> fetchIndex(Map<String, Object> fetchParameters, long traceRank, long timeBegin, @Nullable IProgressMonitor monitor) {
@Nullable ITmfFilter filter = extractFilter(fetchParameters);
long rank;
if (traceRank == -1) {
ITmfContext context = getTrace().seekEvent(TmfTimestamp.fromNanos(timeBegin));
rank = context.getRank();
} else {
rank = traceRank;
}
if (filter == null) {
return new TmfModelResponse<>(Collections.singletonList(rank), ITmfResponse.Status.COMPLETED, CommonStatusMessage.COMPLETED);
}
applyFilter(filter);
Entry<Long, Long> nearestEntry = fRankToIndexMap.floorEntry(rank);
long startingIndex = nearestEntry != null ? nearestEntry.getValue() : 0L;
long startingRank = nearestEntry != null ? nearestEntry.getKey() : 0L;
List<Long> foundIndex = new ArrayList<>();
TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, TmfTimeRange.ETERNITY, startingRank, ITmfEventRequest.ALL_DATA, ExecutionType.FOREGROUND) {
private long currentIndex = startingIndex;
private long fRank = startingRank;
@Override
public void handleData(ITmfEvent event) {
super.handleData(event);
if (monitor != null && monitor.isCanceled()) {
cancel();
return;
}
if (fRank >= rank) {
foundIndex.add(currentIndex);
done();
return;
}
if (filter.matches(event)) {
currentIndex++;
}
fRank++;
}
};
getTrace().sendRequest(request);
try {
request.waitForCompletion();
} catch (InterruptedException e) {
return new TmfModelResponse<>(null, ITmfResponse.Status.FAILED, NonNullUtils.nullToEmptyString(e.getMessage()));
}
return new TmfModelResponse<>(foundIndex, ITmfResponse.Status.COMPLETED, CommonStatusMessage.COMPLETED);
}
use of org.eclipse.tracecompass.tmf.core.filter.ITmfFilter in project tracecompass by tracecompass.
the class TmfEventsCache method getFilteredEventIndex.
/**
* Get the cache index of an event from his rank in the trace. This will
* take in consideration any filter that might be applied.
*
* @param rank
* The rank of the event in the trace
* @return The position (index) this event should use once cached
*/
public int getFilteredEventIndex(final long rank) {
int current;
int startRank;
TmfEventRequest request;
final ITmfFilter filter = fFilter;
synchronized (this) {
int start = 0;
int end = fFilterIndex.size();
if ((fCacheEndIndex - fCacheStartIndex) > 1) {
if (rank < fCache[0].rank) {
end = (fCacheStartIndex / fCacheSize) + 1;
} else if (rank > fCache[fCacheEndIndex - fCacheStartIndex - 1].rank) {
start = fCacheEndIndex / fCacheSize;
} else {
for (int i = 0; i < (fCacheEndIndex - fCacheStartIndex); i++) {
if (fCache[i].rank >= rank) {
return fCacheStartIndex + i;
}
}
return fCacheEndIndex;
}
}
current = (start + end) / 2;
while (current != start) {
if (rank < fFilterIndex.get(current)) {
end = current;
current = (start + end) / 2;
} else {
start = current;
current = (start + end) / 2;
}
}
startRank = fFilterIndex.isEmpty() ? 0 : fFilterIndex.get(current);
}
final int index = current * fCacheSize;
class DataRequest extends TmfEventRequest {
ITmfFilter requestFilter;
TmfCollapseFilter requestCollapsedFilter;
int requestRank;
int requestIndex;
DataRequest(Class<? extends ITmfEvent> dataType, ITmfFilter reqFilter, int start, int nbRequested) {
super(dataType, TmfTimeRange.ETERNITY, start, nbRequested, TmfEventRequest.ExecutionType.FOREGROUND);
requestFilter = reqFilter;
requestRank = start;
requestIndex = index;
requestCollapsedFilter = fCollapseFilterEnabled ? new TmfCollapseFilter() : null;
}
@Override
public void handleData(ITmfEvent event) {
super.handleData(event);
if (isCancelled()) {
return;
}
if (requestRank >= rank) {
cancel();
return;
}
requestRank++;
if (requestFilter.matches(event)) {
if (requestCollapsedFilter == null || requestCollapsedFilter.matches(event)) {
requestIndex++;
}
}
}
public int getFilteredIndex() {
return requestIndex;
}
}
request = new DataRequest(ITmfEvent.class, filter, startRank, ITmfEventRequest.ALL_DATA);
((ITmfEventProvider) fTrace).sendRequest(request);
try {
request.waitForCompletion();
return ((DataRequest) request).getFilteredIndex();
} catch (InterruptedException e) {
// $NON-NLS-1$
Activator.getDefault().logError("Filter request interrupted!", e);
Thread.currentThread().interrupt();
}
return 0;
}
use of org.eclipse.tracecompass.tmf.core.filter.ITmfFilter in project tracecompass by tracecompass.
the class TmfEventsTable method createPopupMenu.
// ------------------------------------------------------------------------
// Operations
// ------------------------------------------------------------------------
/**
* Create a pop-up menu.
*/
private void createPopupMenu() {
final IAction copyAction = new Action(Messages.TmfEventsTable_CopyToClipboardActionText) {
@Override
public void run() {
ITmfTrace trace = fTrace;
if (trace == null || (fSelectedRank == -1 && fSelectedBeginRank == -1)) {
return;
}
List<TmfEventTableColumn> columns = new ArrayList<>();
for (int i : fTable.getColumnOrder()) {
TableColumn column = fTable.getColumns()[i];
// Omit the margin column and hidden columns
if (isVisibleEventColumn(column)) {
columns.add(fColumns.get(i));
}
}
long start = Math.min(fSelectedBeginRank, fSelectedRank);
long end = Math.max(fSelectedBeginRank, fSelectedRank);
final ITmfFilter filter = (ITmfFilter) fTable.getData(Key.FILTER_OBJ);
IRunnableWithProgress operation = new CopyToClipboardOperation(trace, filter, columns, start, end);
try {
PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
} catch (InvocationTargetException e) {
// $NON-NLS-1$
Activator.getDefault().logError("Invocation target exception copying to clipboard ", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
final IAction showTableAction = new Action(Messages.TmfEventsTable_ShowTableActionText) {
@Override
public void run() {
fTableComposite.setVisible(true);
fSashForm.layout();
}
};
final IAction hideTableAction = new Action(Messages.TmfEventsTable_HideTableActionText) {
@Override
public void run() {
fTableComposite.setVisible(false);
fSashForm.layout();
}
};
final IAction showRawAction = new Action(Messages.TmfEventsTable_ShowRawActionText) {
@Override
public void run() {
fRawViewer.setVisible(true);
fSashForm.layout();
final int index = fTable.getSelectionIndex();
if (index >= 1) {
fRawViewer.selectAndReveal(index - 1);
}
}
};
final IAction hideRawAction = new Action(Messages.TmfEventsTable_HideRawActionText) {
@Override
public void run() {
fRawViewer.setVisible(false);
fSashForm.layout();
}
};
final IAction openModelAction = new Action(Messages.TmfEventsTable_OpenModelActionText) {
@Override
public void run() {
final TableItem[] items = fTable.getSelection();
if (items.length != 1) {
return;
}
final TableItem item = items[0];
final Object eventData = item.getData();
if (eventData instanceof ITmfModelLookup) {
String modelURI = ((ITmfModelLookup) eventData).getModelUri();
if (modelURI != null) {
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IFile file = null;
final URI uri = URI.createURI(modelURI);
if (uri.isPlatformResource()) {
IPath path = new Path(uri.toPlatformString(true));
file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
} else if (uri.isFile() && !uri.isRelative()) {
file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(new Path(uri.toFileString()));
}
if (file != null) {
try {
/*
* create a temporary validation marker on the
* model file, remove it afterwards thus,
* navigation works with all model editors
* supporting the navigation to a marker
*/
IMarker marker = file.createMarker(EValidator.MARKER);
marker.setAttribute(EValidator.URI_ATTRIBUTE, modelURI);
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
IDE.openEditor(activePage, marker, OpenStrategy.activateOnOpen());
marker.delete();
} catch (CoreException e) {
TraceUtils.displayErrorMsg(e);
}
} else {
final Exception e = new FileNotFoundException('\'' + modelURI + '\'' + '\n' + Messages.TmfEventsTable_OpenModelUnsupportedURI);
TraceUtils.displayErrorMsg(e);
}
}
}
}
};
final IAction exportToTextAction = new Action(Messages.TmfEventsTable_Export_to_text) {
@Override
public void run() {
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
Object handlerServiceObject = activePage.getActiveEditor().getSite().getService(IHandlerService.class);
IHandlerService handlerService = (IHandlerService) handlerServiceObject;
Object cmdServiceObject = activePage.getActiveEditor().getSite().getService(ICommandService.class);
ICommandService cmdService = (ICommandService) cmdServiceObject;
try {
HashMap<String, Object> parameters = new HashMap<>();
Command command = cmdService.getCommand(ExportToTextCommandHandler.COMMAND_ID);
ParameterizedCommand cmd = ParameterizedCommand.generateCommand(command, parameters);
IEvaluationContext context = handlerService.getCurrentState();
List<TmfEventTableColumn> exportColumns = new ArrayList<>();
for (int i : fTable.getColumnOrder()) {
TableColumn column = fTable.getColumns()[i];
// Omit the margin column and hidden columns
if (isVisibleEventColumn(column)) {
exportColumns.add(fColumns.get(i));
}
}
context.addVariable(ExportToTextCommandHandler.TMF_EVENT_TABLE_COLUMNS_ID, exportColumns);
handlerService.executeCommandInContext(cmd, null, context);
} catch (ExecutionException | NotDefinedException | NotEnabledException | NotHandledException e) {
TraceUtils.displayErrorMsg(e);
}
}
};
final IAction addAsFilterAction = new Action(Messages.TmfEventsTable_AddAsFilterText) {
@Override
public void run() {
applySearchAsFilter();
}
};
final IAction clearFiltersAction = new Action(Messages.TmfEventsTable_ClearFiltersActionText) {
@Override
public void run() {
clearFilters();
}
};
final IAction collapseAction = new Action(Messages.TmfEventsTable_CollapseFilterMenuName) {
@Override
public void run() {
applyFilter(new TmfCollapseFilter());
}
};
final IAction synchronizeAction = new Action(Messages.TmfEventsTable_SynchronizeActionText, IAction.AS_CHECK_BOX) {
@Override
public void run() {
TmfTraceManager.getInstance().updateTraceContext(NonNullUtils.checkNotNull(fTrace), builder -> builder.setSynchronized(isChecked()));
}
};
class ToggleBookmarkAction extends Action {
Long fRank;
public ToggleBookmarkAction(final String text, final Long rank) {
super(text);
fRank = rank;
}
@Override
public void run() {
toggleBookmark(fRank);
}
}
fHeaderPopupMenuManager = new MenuManager();
fHeaderPopupMenuManager.setRemoveAllWhenShown(true);
fHeaderPopupMenuManager.addMenuListener(manager -> {
final Point point = fTable.toControl(fLastMenuCursorLocation);
TableColumn selectedColumn = fTable.getColumn(point);
if (selectedColumn != null && selectedColumn.getResizable()) {
fHeaderPopupMenuManager.add(createAutoFitAction(selectedColumn));
fHeaderPopupMenuManager.add(new Separator());
}
for (int index : fTable.getColumnOrder()) {
TableColumn column = fTable.getColumns()[index];
if (column.getData(Key.WIDTH) != null) {
fHeaderPopupMenuManager.add(createShowColumnAction(column));
}
}
fHeaderPopupMenuManager.add(new Separator());
fHeaderPopupMenuManager.add(createShowAllAction());
fHeaderPopupMenuManager.add(createResetAllAction());
});
fTablePopupMenuManager = new MenuManager();
fTablePopupMenuManager.setRemoveAllWhenShown(true);
fTablePopupMenuManager.addMenuListener(manager -> {
if (fTable.getSelectionIndices().length == 1 && fTable.getSelectionIndices()[0] == 0) {
// Right-click on header row
if (fHeaderState == HeaderState.SEARCH) {
fTablePopupMenuManager.add(addAsFilterAction);
}
return;
}
final Point point = fTable.toControl(fLastMenuCursorLocation);
final TableItem item = fTable.getSelection().length > 0 ? fTable.getSelection()[0] : null;
if (item != null) {
final Rectangle imageBounds = item.getImageBounds(0);
imageBounds.width = BOOKMARK_IMAGE.getBounds().width;
if (point.x <= (imageBounds.x + imageBounds.width)) {
// Right-click on left margin
final Long rank = (Long) item.getData(Key.RANK);
if ((rank != null) && (fBookmarksFile != null)) {
if (fBookmarksMap.containsKey(rank)) {
fTablePopupMenuManager.add(new ToggleBookmarkAction(Messages.TmfEventsTable_RemoveBookmarkActionText, rank));
} else {
fTablePopupMenuManager.add(new ToggleBookmarkAction(Messages.TmfEventsTable_AddBookmarkActionText, rank));
}
}
return;
}
}
// Right-click on table
if (fSelectedRank != -1 && fSelectedBeginRank != -1) {
fTablePopupMenuManager.add(copyAction);
fTablePopupMenuManager.add(new Separator());
}
if (fTable.isVisible() && fRawViewer.isVisible()) {
fTablePopupMenuManager.add(hideTableAction);
fTablePopupMenuManager.add(hideRawAction);
} else if (!fTable.isVisible()) {
fTablePopupMenuManager.add(showTableAction);
} else if (!fRawViewer.isVisible()) {
fTablePopupMenuManager.add(showRawAction);
}
fTablePopupMenuManager.add(exportToTextAction);
fTablePopupMenuManager.add(new Separator());
if (item != null) {
final Object data = item.getData();
Separator separator = null;
if (data instanceof ITmfSourceLookup) {
IContributionItem action = OpenSourceCodeAction.create(Messages.TmfSourceLookup_OpenSourceCodeActionText, (ITmfSourceLookup) data, fTable.getShell());
if (action != null) {
fTablePopupMenuManager.add(action);
separator = new Separator();
}
}
if (data instanceof ITmfModelLookup) {
ITmfModelLookup event2 = (ITmfModelLookup) data;
if (event2.getModelUri() != null) {
fTablePopupMenuManager.add(openModelAction);
separator = new Separator();
}
if (separator != null) {
fTablePopupMenuManager.add(separator);
}
}
}
/*
* Only show collapse filter if at least one trace can be
* collapsed.
*/
boolean isCollapsible = false;
if (fTrace != null) {
for (ITmfTrace trace1 : TmfTraceManager.getTraceSet(fTrace)) {
Class<? extends ITmfEvent> eventClass = trace1.getEventType();
isCollapsible = ITmfCollapsibleEvent.class.isAssignableFrom(eventClass);
if (isCollapsible) {
break;
}
}
}
if (isCollapsible && !fCollapseFilterEnabled) {
fTablePopupMenuManager.add(collapseAction);
fTablePopupMenuManager.add(new Separator());
}
fTablePopupMenuManager.add(clearFiltersAction);
final ITmfFilterTreeNode[] savedFilters = FilterManager.getSavedFilters();
if (savedFilters.length > 0) {
final MenuManager subMenu = new MenuManager(Messages.TmfEventsTable_ApplyPresetFilterMenuName);
for (final ITmfFilterTreeNode node : savedFilters) {
if (node instanceof TmfFilterNode) {
final TmfFilterNode filter = (TmfFilterNode) node;
subMenu.add(new Action(filter.getFilterName()) {
@Override
public void run() {
applyFilter(filter);
}
});
}
}
fTablePopupMenuManager.add(subMenu);
}
fTablePopupMenuManager.add(new Separator());
ITmfTrace trace2 = fTrace;
if (trace2 != null) {
synchronizeAction.setChecked(TmfTraceManager.getInstance().getTraceContext(trace2).isSynchronized());
fTablePopupMenuManager.add(synchronizeAction);
}
appendToTablePopupMenu(fTablePopupMenuManager, item);
});
fRawViewerPopupMenuManager = new MenuManager();
fRawViewerPopupMenuManager.setRemoveAllWhenShown(true);
fRawViewerPopupMenuManager.addMenuListener(manager -> {
if (fTable.isVisible() && fRawViewer.isVisible()) {
fRawViewerPopupMenuManager.add(hideTableAction);
fRawViewerPopupMenuManager.add(hideRawAction);
} else if (!fTable.isVisible()) {
fRawViewerPopupMenuManager.add(showTableAction);
} else if (!fRawViewer.isVisible()) {
fRawViewerPopupMenuManager.add(showRawAction);
}
appendToRawPopupMenu(fRawViewerPopupMenuManager);
});
fHeaderMenu = fHeaderPopupMenuManager.createContextMenu(fTable);
fTablePopup = fTablePopupMenuManager.createContextMenu(fTable);
fTable.setMenu(fTablePopup);
fRawTablePopup = fRawViewerPopupMenuManager.createContextMenu(fRawViewer);
fRawViewer.setMenu(fRawTablePopup);
}
Aggregations