tmf: remove deprecated methods from tmf
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / project / model / TmfTraceType.java
CommitLineData
4bf17f4a 1/*******************************************************************************
281def42 2 * Copyright (c) 2011, 2016 Ericsson, École Polytechnique de Montréal
cfd22ad0 3 *
4bf17f4a 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
cfd22ad0 8 *
4bf17f4a 9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
d04ec5a7 11 * Matthew Khouzam - Added import functionalities
1ec2ca01 12 * Geneviève Bastien - Added support for experiment types
c9b31f60 13 * Bernd Hufmann - Updated custom trace type ID handling
4bf17f4a 14 *******************************************************************************/
15
2bdf0193 16package org.eclipse.tracecompass.tmf.core.project.model;
4bf17f4a 17
281def42 18import java.io.File;
d04ec5a7 19import java.util.ArrayList;
252c602c
BH
20import java.util.Collections;
21import java.util.Comparator;
d04ec5a7 22import java.util.HashMap;
d04ec5a7 23import java.util.LinkedHashMap;
4bf17f4a 24import java.util.List;
d04ec5a7 25import java.util.Map;
88424bb7 26import java.util.Map.Entry;
281def42 27import java.util.TreeSet;
4bf17f4a 28
4b3b667b 29import org.eclipse.core.resources.IResource;
05627bda 30import org.eclipse.core.runtime.CoreException;
4bf17f4a 31import org.eclipse.core.runtime.IConfigurationElement;
d16bb0dd 32import org.eclipse.core.runtime.IStatus;
4bf17f4a 33import org.eclipse.core.runtime.Platform;
281def42
BH
34import org.eclipse.jdt.annotation.NonNull;
35import org.eclipse.osgi.util.NLS;
d77f31da 36import org.eclipse.tracecompass.internal.tmf.core.Activator;
281def42 37import org.eclipse.tracecompass.internal.tmf.core.project.model.Messages;
2bdf0193
AM
38import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
39import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTrace;
40import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition;
41import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace;
42import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition;
43import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
44import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
281def42 45import org.eclipse.tracecompass.tmf.core.util.Pair;
4bf17f4a 46
b544077e 47/**
d04ec5a7
MK
48 * Utility class for accessing TMF trace type extensions from the platform's
49 * extensions registry.
cfd22ad0 50 *
b544077e 51 * @author Patrick Tasse
d04ec5a7 52 * @author Matthew Khouzam
b544077e 53 */
d04ec5a7 54public final class TmfTraceType {
4bf17f4a 55
a4a116c3
PT
56 // ------------------------------------------------------------------
57 // Constants
58 // ------------------------------------------------------------------
59
47aafe74 60 /** Extension point ID */
a926c25c 61 public static final String TMF_TRACE_TYPE_ID = "org.eclipse.linuxtools.tmf.core.tracetype"; //$NON-NLS-1$
4bf17f4a 62
47aafe74 63 /** Extension point element 'Category' */
4bf17f4a 64 public static final String CATEGORY_ELEM = "category"; //$NON-NLS-1$
47aafe74
AM
65
66 /** Extension point element 'Type' */
4bf17f4a 67 public static final String TYPE_ELEM = "type"; //$NON-NLS-1$
47aafe74 68
1ec2ca01
GB
69 /** Extension point element 'Experiment' */
70 public static final String EXPERIMENT_ELEM = "experiment"; //$NON-NLS-1$
71
47aafe74 72 /** Extension point attribute 'ID' */
4bf17f4a 73 public static final String ID_ATTR = "id"; //$NON-NLS-1$
47aafe74
AM
74
75 /** Extension point attribute 'name' */
4bf17f4a 76 public static final String NAME_ATTR = "name"; //$NON-NLS-1$
47aafe74
AM
77
78 /** Extension point attribute 'category' */
4bf17f4a 79 public static final String CATEGORY_ATTR = "category"; //$NON-NLS-1$
47aafe74
AM
80
81 /** Extension point attribute 'trace_type' */
4bf17f4a 82 public static final String TRACE_TYPE_ATTR = "trace_type"; //$NON-NLS-1$
47aafe74
AM
83
84 /** Extension point attribute 'event_type' */
4bf17f4a 85 public static final String EVENT_TYPE_ATTR = "event_type"; //$NON-NLS-1$
47aafe74 86
1ec2ca01
GB
87 /** Extension point attribute 'experiment_type' */
88 public static final String EXPERIMENT_TYPE_ATTR = "experiment_type"; //$NON-NLS-1$
89
d8aba2e2
BH
90 /** Extension point attribute 'isDirectory' */
91 public static final String IS_DIR_ATTR = "isDirectory"; //$NON-NLS-1$
92
1ec2ca01
GB
93 /** Default experiment type */
94 public static final String DEFAULT_EXPERIMENT_TYPE = "org.eclipse.linuxtools.tmf.core.experiment.generic"; //$NON-NLS-1$
95
d04ec5a7
MK
96 // The mapping of available trace type IDs to their corresponding
97 // configuration element
a4a116c3
PT
98 private static final Map<String, IConfigurationElement> TRACE_TYPE_ATTRIBUTES = new HashMap<>();
99 private static final Map<String, IConfigurationElement> TRACE_CATEGORIES = new HashMap<>();
3adbcf71 100 private static final Map<String, @NonNull TraceTypeHelper> TRACE_TYPES = new LinkedHashMap<>();
d04ec5a7 101
a4a116c3
PT
102 static {
103 populateCategoriesAndTraceTypes();
104 populateCustomTraceTypes();
3adbcf71 105 enableTraceTypes();
a4a116c3 106 }
d04ec5a7 107
376fdfbd
GB
108 /**
109 * Enum to say whether a type applies to a trace or experiment
110 *
111 * @author Geneviève Bastien
112 */
113 public enum TraceElementType {
114 /** Trace type applies to trace */
115 TRACE,
116 /** Trace type applies to experiment */
117 EXPERIMENT,
118 }
119
a4a116c3
PT
120 // ------------------------------------------------------------------
121 // Constructor
122 // ------------------------------------------------------------------
123
124 private TmfTraceType() {
125 }
126
127 // ------------------------------------------------------------------
128 // Operations
129 // ------------------------------------------------------------------
130
252c602c
BH
131 /**
132 * Get an iterable view of the existing trace type helpers.
133 *
134 * @return The currently registered trace type helpers
135 */
3adbcf71 136 public static Iterable<@NonNull TraceTypeHelper> getTraceTypeHelpers() {
a4a116c3 137 return TRACE_TYPES.values();
252c602c
BH
138 }
139
d04ec5a7 140 /**
2b0005f0 141 * Returns a list of trace type labels "category : name", ...
d04ec5a7 142 *
376fdfbd
GB
143 * Returns only trace types, not experiment types
144 *
2b0005f0 145 * @return a list of trace type labels
d04ec5a7 146 */
a4a116c3 147 public static String[] getAvailableTraceTypes() {
252c602c
BH
148 return getAvailableTraceTypes(null);
149 }
150
151 /**
88424bb7
MK
152 * Returns a list of trace type labels "category : name", ... sorted by
153 * given comparator.
252c602c 154 *
376fdfbd
GB
155 * Returns only trace types, not experiment types
156 *
252c602c
BH
157 * @param comparator
158 * Comparator class (type String) or null for alphabetical order.
88424bb7
MK
159 * @return a list of trace type labels sorted according to the given
160 * comparator
252c602c 161 */
a4a116c3 162 public static String[] getAvailableTraceTypes(Comparator<String> comparator) {
d04ec5a7
MK
163
164 // Generate the list of Category:TraceType to populate the ComboBox
507b1336 165 List<String> traceTypes = new ArrayList<>();
d04ec5a7 166
3adbcf71
JCK
167 for (TraceTypeHelper tt : getTraceTypeHelpers()) {
168 if (tt.isEnabled() && !tt.isExperimentType()) {
2b0005f0 169 traceTypes.add(tt.getLabel());
376fdfbd 170 }
d04ec5a7 171 }
d04ec5a7 172
252c602c
BH
173 if (comparator == null) {
174 Collections.sort(traceTypes);
175 } else {
176 Collections.sort(traceTypes, comparator);
177 }
178
d04ec5a7
MK
179 // Format result
180 return traceTypes.toArray(new String[traceTypes.size()]);
181 }
182
a4a116c3 183 private static void populateCustomTraceTypes() {
d04ec5a7
MK
184 // add the custom trace types
185 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
2b0005f0
PT
186 CustomTxtTrace trace = new CustomTxtTrace(def);
187 String traceTypeId = trace.getTraceTypeId();
4b3b667b 188 TraceTypeHelper tt = new TraceTypeHelper(traceTypeId, def.categoryName, def.definitionName, trace, false, TraceElementType.TRACE);
a4a116c3 189 TRACE_TYPES.put(traceTypeId, tt);
88424bb7
MK
190 // Deregister trace as signal handler because it is only used for
191 // validation
72807127 192 TmfSignalManager.deregister(trace);
d04ec5a7
MK
193 }
194 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
2b0005f0
PT
195 CustomXmlTrace trace = new CustomXmlTrace(def);
196 String traceTypeId = trace.getTraceTypeId();
4b3b667b 197 TraceTypeHelper tt = new TraceTypeHelper(traceTypeId, def.categoryName, def.definitionName, trace, false, TraceElementType.TRACE);
a4a116c3 198 TRACE_TYPES.put(traceTypeId, tt);
88424bb7
MK
199 // Deregister trace as signal handler because it is only used for
200 // validation
72807127 201 TmfSignalManager.deregister(trace);
d04ec5a7 202 }
52885aeb
PT
203 }
204
205 /**
206 * Add or replace a custom trace type
207 *
4b3b667b
PT
208 * @param traceClass
209 * The custom trace class, either {@link CustomTxtTrace} or
210 * {@link CustomXmlTrace}
211 * @param category
212 * The custom parser category
213 * @param definitionName
214 * The custom parser definition name to add or replace
215 */
216 public static void addCustomTraceType(Class<? extends ITmfTrace> traceClass, String category, String definitionName) {
52885aeb 217 String traceTypeId = null;
c9b31f60 218 ITmfTrace trace = null;
52885aeb 219
4b3b667b 220 if (traceClass.equals(CustomTxtTrace.class)) {
332527a4 221 CustomTxtTraceDefinition def = CustomTxtTraceDefinition.load(category, definitionName);
52885aeb
PT
222 if (def != null) {
223 trace = new CustomTxtTrace(def);
2b0005f0 224 traceTypeId = trace.getTraceTypeId();
52885aeb 225 }
4b3b667b 226 } else if (traceClass.equals(CustomXmlTrace.class)) {
332527a4 227 CustomXmlTraceDefinition def = CustomXmlTraceDefinition.load(category, definitionName);
52885aeb
PT
228 if (def != null) {
229 trace = new CustomXmlTrace(def);
2b0005f0 230 traceTypeId = trace.getTraceTypeId();
52885aeb
PT
231 }
232 }
233
234 if (traceTypeId != null && trace != null) {
a4a116c3 235 TraceTypeHelper helper = TRACE_TYPES.get(traceTypeId);
52885aeb
PT
236 if (helper != null) {
237 helper.getTrace().dispose();
238 }
376fdfbd 239 TraceTypeHelper tt = new TraceTypeHelper(traceTypeId, category, definitionName, trace, false, TraceElementType.TRACE);
a4a116c3 240 TRACE_TYPES.put(traceTypeId, tt);
88424bb7
MK
241 // Deregister trace as signal handler because it is only used for
242 // validation
52885aeb
PT
243 TmfSignalManager.deregister(trace);
244 }
245 }
246
4b3b667b
PT
247 /**
248 * Remove a custom trace type
249 *
250 * @param traceClass
251 * The custom trace class, either {@link CustomTxtTrace} or
252 * {@link CustomXmlTrace}
253 * @param category
254 * The custom parser category
255 * @param definitionName
256 * The custom parser definition name to add or replace
257 */
258 public static void removeCustomTraceType(Class<? extends ITmfTrace> traceClass, String category, String definitionName) {
c9b31f60
BH
259 String traceTypeId = null;
260 if (traceClass.equals(CustomTxtTrace.class)) {
261 traceTypeId = CustomTxtTrace.buildTraceTypeId(category, definitionName);
262 } else if (traceClass.equals(CustomXmlTrace.class)) {
263 traceTypeId = CustomXmlTrace.buildTraceTypeId(category, definitionName);
264 }
265 if (traceTypeId != null) {
266 TraceTypeHelper helper = TRACE_TYPES.remove(traceTypeId);
267 if (helper != null) {
268 helper.getTrace().dispose();
269 }
52885aeb 270 }
d04ec5a7
MK
271 }
272
273 /**
274 * Gets a trace type for a given canonical id
275 *
276 * @param id
277 * the ID of the trace
278 * @return the return type
d04ec5a7 279 */
a4a116c3
PT
280 public static TraceTypeHelper getTraceType(String id) {
281 return TRACE_TYPES.get(id);
d04ec5a7
MK
282 }
283
3adbcf71
JCK
284 private static void enableTraceTypes() {
285 List<String> preferences = TraceTypePreferences.getPreferenceValue();
286 TRACE_TYPES.values().forEach(helper -> {
287 if (!helper.isExperimentType()) {
288 helper.setEnabled(!preferences.contains(helper.getTraceTypeId()));
289 }
290 });
291 }
292
a4a116c3
PT
293 private static void populateCategoriesAndTraceTypes() {
294 if (TRACE_TYPES.isEmpty()) {
d04ec5a7
MK
295 // Populate the Categories and Trace Types
296 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
297 for (IConfigurationElement ce : config) {
298 String elementName = ce.getName();
299 if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
300 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
a4a116c3 301 TRACE_TYPE_ATTRIBUTES.put(traceTypeId, ce);
d04ec5a7
MK
302 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
303 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
a4a116c3 304 TRACE_CATEGORIES.put(categoryId, ce);
376fdfbd
GB
305 } else if (elementName.equals(TmfTraceType.EXPERIMENT_ELEM)) {
306 String experimentTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
a4a116c3 307 TRACE_TYPE_ATTRIBUTES.put(experimentTypeId, ce);
d04ec5a7
MK
308 }
309 }
310 // create the trace types
88424bb7
MK
311 for (Entry<String, IConfigurationElement> entry : TRACE_TYPE_ATTRIBUTES.entrySet()) {
312 IConfigurationElement ce = entry.getValue();
d04ec5a7
MK
313 final String category = getCategory(ce);
314 final String attribute = ce.getAttribute(TmfTraceType.NAME_ATTR);
315 ITmfTrace trace = null;
376fdfbd 316 TraceElementType elementType = TraceElementType.TRACE;
d04ec5a7 317 try {
376fdfbd
GB
318 if (ce.getName().equals(TmfTraceType.TYPE_ELEM)) {
319 trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
320 } else if (ce.getName().equals(TmfTraceType.EXPERIMENT_ELEM)) {
321 trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.EXPERIMENT_TYPE_ATTR);
322 elementType = TraceElementType.EXPERIMENT;
323 }
a8ddd783
GB
324 if (trace == null) {
325 break;
376fdfbd 326 }
a8ddd783
GB
327 // Deregister trace as signal handler because it is only
328 // used for validation
329 TmfSignalManager.deregister(trace);
330
331 final String dirString = ce.getAttribute(TmfTraceType.IS_DIR_ATTR);
332 boolean isDir = Boolean.parseBoolean(dirString);
333
88424bb7 334 final String typeId = entry.getKey();
a8ddd783 335 TraceTypeHelper tt = new TraceTypeHelper(typeId, category, attribute, trace, isDir, elementType);
a4a116c3 336 TRACE_TYPES.put(typeId, tt);
d04ec5a7 337 } catch (CoreException e) {
d77f31da 338 Activator.logError("Unexpected error during populating trace types", e); //$NON-NLS-1$
d04ec5a7 339 }
d8aba2e2 340
d04ec5a7
MK
341 }
342 }
343 }
344
a4a116c3 345 private static String getCategory(IConfigurationElement ce) {
d04ec5a7
MK
346 final String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
347 if (categoryId != null) {
a4a116c3 348 IConfigurationElement category = TRACE_CATEGORIES.get(categoryId);
d04ec5a7
MK
349 if (category != null && !category.getName().equals("")) { //$NON-NLS-1$
350 return category.getAttribute(TmfTraceType.NAME_ATTR);
351 }
352 }
2b0005f0 353 return ""; //$NON-NLS-1$
d04ec5a7
MK
354 }
355
d04ec5a7
MK
356 /**
357 * Get a configuration element for a given name
358 *
359 * @param traceType
360 * the name canonical
361 * @return the configuration element, can be null
d04ec5a7 362 */
a4a116c3
PT
363 public static IConfigurationElement getTraceAttributes(String traceType) {
364 return TRACE_TYPE_ATTRIBUTES.get(traceType);
d04ec5a7
MK
365 }
366
367 /**
2b0005f0 368 * Find the id of a trace type by its label "category : name"
d04ec5a7 369 *
2b0005f0
PT
370 * @param label
371 * the trace type label
372 * @return the trace type id
d04ec5a7 373 */
2b0005f0 374 public static String getTraceTypeId(String label) {
88424bb7
MK
375 for (Entry<String, TraceTypeHelper> entry : TRACE_TYPES.entrySet()) {
376 if (entry.getValue().getLabel().equals(label)) {
377 return entry.getKey();
d04ec5a7
MK
378 }
379 }
380 return null;
381 }
76fccfb0 382
252c602c 383 /**
d8aba2e2 384 * Checks if a trace is a valid directory trace
88424bb7 385 *
d8aba2e2 386 * @param path
252c602c 387 * the file name (and path)
88424bb7
MK
388 * @return <code>true</code> if the trace is a valid directory trace else
389 * <code>false</code>
252c602c 390 */
a4a116c3 391 public static boolean isDirectoryTrace(String path) {
d8aba2e2
BH
392 final Iterable<TraceTypeHelper> traceTypeHelpers = getTraceTypeHelpers();
393 for (TraceTypeHelper traceTypeHelper : traceTypeHelpers) {
394 if (traceTypeHelper.isDirectoryTraceType() &&
d16bb0dd 395 (traceTypeHelper.validate(path).getSeverity() != IStatus.ERROR)) {
d8aba2e2
BH
396 return true;
397 }
252c602c
BH
398 }
399 return false;
400 }
401
402 /**
403 * @param traceType
88424bb7 404 * the trace type
3adbcf71
JCK
405 * @return <code>true</code> if it is a directory trace type,
406 * <code>false</code> otherwise
252c602c 407 */
a4a116c3 408 public static boolean isDirectoryTraceType(String traceType) {
d8aba2e2
BH
409 if (traceType != null) {
410 TraceTypeHelper traceTypeHelper = getTraceType(traceType);
411 if (traceTypeHelper != null) {
412 return traceTypeHelper.isDirectoryTraceType();
413 }
1dec1830 414 return false;
252c602c 415 }
d8aba2e2 416 throw new IllegalArgumentException("Invalid trace type string: " + traceType); //$NON-NLS-1$
252c602c 417 }
d8aba2e2 418
4b3b667b
PT
419 /**
420 * Get the trace type id for a resource
421 *
422 * @param resource
423 * the resource
424 * @return the trace type id or null if it is not set
425 * @throws CoreException
426 * if the trace type id cannot be accessed
4b3b667b
PT
427 */
428 public static String getTraceTypeId(IResource resource) throws CoreException {
429 String traceTypeId = resource.getPersistentProperties().get(TmfCommonConstants.TRACETYPE);
e86f7ac4
BH
430 return buildCompatibilityTraceTypeId(traceTypeId);
431 }
432
433 /**
434 * This methods builds a trace type ID from a given ID taking into
435 * consideration any format changes that were done for the IDs of custom
436 * text or XML traces. For example, such format change took place when
437 * moving to Trace Compass. Trace type IDs that are part of the plug-in
438 * extension for trace types won't be changed.
439 *
440 * This method is useful for IDs that were persisted in the workspace before
441 * the format changes (e.g. in the persistent properties of a trace
442 * resource).
443 *
444 * It ensures backwards compatibility of the workspace for custom text and
445 * XML traces.
446 *
447 * @param traceTypeId
448 * the legacy trace type ID
449 * @return the trace type ID in Trace Compass format
450 */
451 public static String buildCompatibilityTraceTypeId(String traceTypeId) {
88424bb7
MK
452 // Fix custom trace type id with old class name or without category name
453 // for backward compatibility
4b3b667b 454 if (traceTypeId != null) {
c9b31f60
BH
455 String newTraceType = CustomTxtTrace.buildCompatibilityTraceTypeId(traceTypeId);
456 if (newTraceType.equals(traceTypeId)) {
457 newTraceType = CustomXmlTrace.buildCompatibilityTraceTypeId(traceTypeId);
4b3b667b 458 }
c9b31f60 459 return newTraceType;
4b3b667b
PT
460 }
461 return traceTypeId;
462 }
c9b31f60 463
281def42
BH
464 /**
465 * This method figures out the trace type of a given trace.
466 *
467 * @param path
468 * The path of trace to import (file or directory for directory traces)
469 * @param traceTypeHint
470 * the ID of a trace (like "o.e.l.specifictrace" )
471 * @return a list of {@link TraceTypeHelper} sorted by confidence (highest first)
472 *
473 * @throws TmfTraceImportException
474 * if there are errors in the trace file or no trace type found
475 * for a directory trace
476 * @since 2.0
477 */
478 public static @NonNull List<TraceTypeHelper> selectTraceType(String path, String traceTypeHint) throws TmfTraceImportException {
479
480 Comparator<Pair<Integer, TraceTypeHelper>> comparator = new Comparator<Pair<Integer, TraceTypeHelper>>() {
481 @Override
482 public int compare(Pair<Integer, TraceTypeHelper> o1, Pair<Integer, TraceTypeHelper> o2) {
483 int res = -o1.getFirst().compareTo(o2.getFirst()); // invert so that highest confidence is first
484 if (res == 0) {
485 res = o1.getSecond().getName().compareTo(o2.getSecond().getName());
486 }
487 return res;
488 }
489 };
490
491 TreeSet<Pair<Integer, TraceTypeHelper>> validCandidates = new TreeSet<>(comparator);
492 final Iterable<TraceTypeHelper> traceTypeHelpers = TmfTraceType.getTraceTypeHelpers();
493 for (TraceTypeHelper traceTypeHelper : traceTypeHelpers) {
3adbcf71 494 if (!traceTypeHelper.isEnabled() || traceTypeHelper.isExperimentType()) {
281def42
BH
495 continue;
496 }
497 int confidence = traceTypeHelper.validateWithConfidence(path);
498 if (confidence >= 0) {
499 // insert in the tree map, ordered by confidence (highest confidence first) then name
500 Pair<Integer, TraceTypeHelper> element = new Pair<>(confidence, traceTypeHelper);
501 validCandidates.add(element);
502 }
503 }
504
505 List<TraceTypeHelper> returned = new ArrayList<>();
506 if (validCandidates.isEmpty()) {
507 File traceFile = new File(path);
508 if (traceFile.isFile()) {
509 return returned;
510 }
511 final String errorMsg = NLS.bind(Messages.TmfOpenTraceHelper_NoTraceTypeMatch, path);
512 throw new TmfTraceImportException(errorMsg);
513 }
514
515 if (validCandidates.size() != 1) {
516 List<Pair<Integer, TraceTypeHelper>> candidates = new ArrayList<>(validCandidates);
517 List<Pair<Integer, TraceTypeHelper>> reducedCandidates = reduce(candidates);
518 for (Pair<Integer, TraceTypeHelper> candidatePair : reducedCandidates) {
519 TraceTypeHelper candidate = candidatePair.getSecond();
520 if (candidate.getTraceTypeId().equals(traceTypeHint)) {
521 returned.add(candidate);
522 break;
523 }
524 }
525 if (returned.size() == 0) {
526 if (reducedCandidates.size() == 0) {
527 throw new TmfTraceImportException("Error reducing trace type candidates"); //$NON-NLS-1$
528 } else if (reducedCandidates.size() == 1) {
529 // Don't select the trace type if it has the lowest confidence
530 if (reducedCandidates.get(0).getFirst() > 0) {
531 returned.add(reducedCandidates.get(0).getSecond());
532 }
533 } else {
534 for (Pair<Integer, TraceTypeHelper> candidatePair : reducedCandidates) {
68621ce0
BH
535 // Don't select the trace type if it has the lowest confidence
536 if (candidatePair.getFirst() > 0) {
537 returned.add(candidatePair.getSecond());
538 }
281def42
BH
539 }
540 }
541 }
542 } else {
543 // Don't select the trace type if it has the lowest confidence
544 if (validCandidates.first().getFirst() > 0) {
545 returned.add(validCandidates.first().getSecond());
546 }
547 }
548 return returned;
549 }
550
551 private static List<Pair<Integer, TraceTypeHelper>> reduce(List<Pair<Integer, TraceTypeHelper>> candidates) {
552 List<Pair<Integer, TraceTypeHelper>> retVal = new ArrayList<>();
553
554 // get all the tracetypes that are unique in that stage
555 for (Pair<Integer, TraceTypeHelper> candidatePair : candidates) {
556 TraceTypeHelper candidate = candidatePair.getSecond();
557 if (isUnique(candidate, candidates)) {
558 retVal.add(candidatePair);
559 }
560 }
561 return retVal;
562 }
563
564 /*
565 * Only return the leaves of the trace types. Ignore custom trace types.
566 */
567 private static boolean isUnique(TraceTypeHelper trace, List<Pair<Integer, TraceTypeHelper>> set) {
568 if (trace.getTraceClass().equals(CustomTxtTrace.class) ||
569 trace.getTraceClass().equals(CustomXmlTrace.class)) {
570 return true;
571 }
572 // check if the trace type is the leaf. we make an instance of the trace
573 // type and if it is only an instance of itself, it is a leaf
574 final ITmfTrace tmfTrace = trace.getTrace();
575 int count = -1;
576 for (Pair<Integer, TraceTypeHelper> child : set) {
577 final ITmfTrace traceCandidate = child.getSecond().getTrace();
578 if (tmfTrace.getClass().isInstance(traceCandidate)) {
579 count++;
580 }
581 }
582 return count == 0;
583 }
584
4bf17f4a 585}
This page took 0.136938 seconds and 5 git commands to generate.