Commit | Line | Data |
---|---|---|
8a6ff07f GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 École Polytechnique de Montréal | |
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 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.statesystem; | |
14 | ||
15 | import java.io.File; | |
16 | import java.util.HashMap; | |
17 | import java.util.Map; | |
18 | ||
19 | import org.eclipse.core.runtime.IProgressMonitor; | |
20 | import org.eclipse.jdt.annotation.NonNull; | |
21 | import org.eclipse.linuxtools.tmf.core.analysis.TmfAbstractAnalysisModule; | |
22 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; | |
23 | import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager; | |
24 | ||
25 | /** | |
26 | * Abstract analysis module to generate a state system. It is a base class that | |
27 | * can be used as a shortcut by analysis who just need to build a single state | |
28 | * system with a state provider. | |
29 | * | |
30 | * Analysis implementing this class should only need to provide a state system | |
31 | * and optionally a backend (default to NULL) and, if required, a filename | |
32 | * (defaults to the analysis'ID) | |
33 | * | |
34 | * @author Geneviève Bastien | |
35 | * @since 3.0 | |
36 | */ | |
37 | public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisModule | |
38 | implements ITmfStateSystemAnalysisModule { | |
39 | ||
40 | private ITmfStateSystem fStateSystem = null; | |
41 | private static final String EXTENSION = ".ht"; //$NON-NLS-1$ | |
42 | ||
43 | /** | |
44 | * State system backend types | |
45 | * | |
46 | * @author Geneviève Bastien | |
47 | */ | |
48 | protected enum StateSystemBackendType { | |
49 | /** Full history in file */ | |
50 | FULL, | |
51 | /** In memory state system */ | |
52 | INMEM, | |
53 | /** Null history */ | |
54 | NULL, | |
55 | /** State system backed with partial history */ | |
56 | PARTIAL | |
57 | } | |
58 | ||
59 | /** | |
60 | * Get the state provider for this analysis module | |
61 | * | |
62 | * @return the state provider | |
63 | */ | |
64 | @NonNull | |
65 | protected abstract ITmfStateProvider createStateProvider(); | |
66 | ||
67 | /** | |
68 | * Get the state system backend type used by this module | |
69 | * | |
70 | * @return The {@link StateSystemBackendType} | |
71 | */ | |
72 | protected abstract StateSystemBackendType getBackendType(); | |
73 | ||
74 | /** | |
75 | * Get the supplementary file name where to save this state system. The | |
76 | * default is the ID of the analysis followed by the extension. | |
77 | * | |
78 | * @return The supplementary file name | |
79 | */ | |
80 | protected String getSsFileName() { | |
81 | return getId() + EXTENSION; | |
82 | } | |
83 | ||
84 | /** | |
85 | * Get the state system generated by this analysis | |
86 | * | |
87 | * @return The state system | |
88 | */ | |
89 | public ITmfStateSystem getStateSystem() { | |
90 | return fStateSystem; | |
91 | } | |
92 | ||
93 | @Override | |
94 | protected boolean executeAnalysis(final IProgressMonitor monitor) { | |
95 | ||
96 | final ITmfStateProvider htInput = createStateProvider(); | |
97 | ||
98 | /* FIXME: State systems should make use of the monitor, to be cancelled */ | |
99 | try { | |
100 | /* Get the state system according to backend */ | |
101 | StateSystemBackendType backend = getBackendType(); | |
102 | String directory; | |
103 | switch (backend) { | |
104 | case FULL: | |
105 | directory = TmfTraceManager.getSupplementaryFileDir(getTrace()); | |
106 | final File htFile = new File(directory + getSsFileName()); | |
107 | fStateSystem = TmfStateSystemFactory.newFullHistory(htFile, htInput, true); | |
108 | break; | |
109 | case PARTIAL: | |
110 | directory = TmfTraceManager.getSupplementaryFileDir(getTrace()); | |
111 | final File htPartialFile = new File(directory + getSsFileName()); | |
112 | fStateSystem = TmfStateSystemFactory.newPartialHistory(htPartialFile, htInput, true); | |
113 | break; | |
114 | case INMEM: | |
115 | fStateSystem = TmfStateSystemFactory.newInMemHistory(htInput, true); | |
116 | break; | |
117 | case NULL: | |
118 | fStateSystem = TmfStateSystemFactory.newNullHistory(htInput); | |
119 | break; | |
120 | default: | |
121 | break; | |
122 | } | |
123 | } catch (TmfTraceException e) { | |
124 | return false; | |
125 | } | |
126 | return true; | |
127 | } | |
128 | ||
129 | @Override | |
130 | protected void canceling() { | |
131 | /* | |
132 | * FIXME: I guess that will do to cancel the state system building, but | |
133 | * it may be preferable to just tell the state system and he will handle | |
134 | * himself how to cancel its work | |
135 | */ | |
136 | fStateSystem.dispose(); | |
137 | } | |
138 | ||
139 | @Override | |
140 | public Map<String, ITmfStateSystem> getStateSystems() { | |
141 | Map<String, ITmfStateSystem> map = new HashMap<String, ITmfStateSystem>(); | |
142 | map.put(getId(), fStateSystem); | |
143 | return map; | |
144 | } | |
145 | } |