ctf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / EventDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2013 Ericsson, Ecole Polytechnique de Montreal and others
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.ctf.core.event;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
20 import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
21 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
22 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
23 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
24 import org.eclipse.linuxtools.ctf.core.trace.CTFStreamInputReader;
25 import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration;
26
27 import com.google.common.collect.ImmutableList;
28 import com.google.common.collect.ImmutableList.Builder;
29
30 /**
31 * Representation of a particular instance of an event.
32 */
33 public final class EventDefinition implements IDefinitionScope {
34
35 // ------------------------------------------------------------------------
36 // Attributes
37 // ------------------------------------------------------------------------
38
39 /**
40 * A null event, can be used for testing or poison pilling
41 *
42 * @since 3.0
43 */
44 @NonNull
45 public static final EventDefinition NULL_EVENT = new EventDefinition(new EventDeclaration(), null, -1L, null, null, null, null);
46
47 /**
48 * The corresponding event declaration.
49 */
50 private final IEventDeclaration fDeclaration;
51
52 /**
53 * The timestamp of the current event.
54 */
55 private final long fTimestamp;
56
57 /**
58 * The event context structure definition.
59 */
60 private final StructDefinition fEventContext;
61
62 private final StructDefinition fStreamContext;
63
64 private final StructDefinition fPacketContext;
65
66 /**
67 * The event fields structure definition.
68 */
69 private final StructDefinition fFields;
70
71 /**
72 * The StreamInputReader that reads this event definition.
73 */
74 private final CTFStreamInputReader fStreamInputReader;
75
76 // ------------------------------------------------------------------------
77 // Constructors
78 // ------------------------------------------------------------------------
79
80 /**
81 * Constructs an event definition.
82 *
83 * @param declaration
84 * The corresponding event declaration
85 * @param streamInputReader
86 * The SIR from where this EventDef was read
87 * @param timestamp
88 * event timestamp
89 * @param eventContext
90 * The event context
91 * @param packetContext
92 * the packet context
93 * @param streamContext
94 * the stream context
95 * @param fields
96 * The event fields
97 * @since 3.0
98 */
99 public EventDefinition(IEventDeclaration declaration,
100 CTFStreamInputReader streamInputReader,
101 long timestamp,
102 StructDefinition streamContext,
103 StructDefinition eventContext,
104 StructDefinition packetContext,
105 StructDefinition fields) {
106 fDeclaration = declaration;
107 fStreamInputReader = streamInputReader;
108 fTimestamp = timestamp;
109 fFields = fields;
110 fEventContext = eventContext;
111 fPacketContext = packetContext;
112 fStreamContext = streamContext;
113 }
114
115 // ------------------------------------------------------------------------
116 // Getters/Setters/Predicates
117 // ------------------------------------------------------------------------
118
119 /**
120 * @since 3.0
121 */
122 @Override
123 public LexicalScope getScopePath() {
124 String eventName = fDeclaration.getName();
125 if (eventName == null) {
126 return null;
127 }
128 LexicalScope myScope = LexicalScope.EVENT.getChild(eventName);
129 if (myScope == null) {
130 myScope = new LexicalScope(LexicalScope.EVENT, eventName);
131 }
132 return myScope;
133 }
134
135 /**
136 * Gets the declaration (the form) of the data
137 *
138 * @return the event declaration
139 * @since 2.0
140 */
141 public IEventDeclaration getDeclaration() {
142 return fDeclaration;
143 }
144
145 /**
146 * Gets the fields of a definition
147 *
148 * @return the fields of a definition in struct form. Can be null.
149 */
150 public StructDefinition getFields() {
151 return fFields;
152 }
153
154 /**
155 * Gets the context of this event without the context of the stream
156 *
157 * @return the context in struct form
158 * @since 1.2
159 */
160 public StructDefinition getEventContext() {
161 return fEventContext;
162 }
163
164 /**
165 * Gets the context of this event within a stream
166 *
167 * @return the context in struct form
168 */
169 public StructDefinition getContext() {
170
171 /* Most common case so far */
172 if (fStreamContext == null) {
173 return fEventContext;
174 }
175
176 /* streamContext is not null, but the context of the event is null */
177 if (fEventContext == null) {
178 return fStreamContext;
179 }
180
181 // TODO: cache if this is a performance issue
182
183 /* The stream context and event context are assigned. */
184 StructDeclaration mergedDeclaration = new StructDeclaration(1);
185
186 Builder<String> builder = ImmutableList.<String> builder();
187 List<Definition> fieldValues = new ArrayList<>();
188
189 /* Add fields from the stream */
190 for (String fieldName : fStreamContext.getFieldNames()) {
191 Definition definition = fStreamContext.getDefinition(fieldName);
192 mergedDeclaration.addField(fieldName, definition.getDeclaration());
193 builder.add(fieldName);
194 fieldValues.add(definition);
195 }
196
197 ImmutableList<String> fieldNames = builder.build();
198 /*
199 * Add fields from the event context, overwrite the stream ones if
200 * needed.
201 */
202 for (String fieldName : fEventContext.getFieldNames()) {
203 Definition definition = fEventContext.getDefinition(fieldName);
204 mergedDeclaration.addField(fieldName, definition.getDeclaration());
205 if (fieldNames.contains(fieldName)) {
206 fieldValues.set((fieldNames.indexOf(fieldName)), definition);
207 } else {
208 builder.add(fieldName);
209 fieldValues.add(definition);
210 }
211 }
212 fieldNames = builder.build();
213 StructDefinition mergedContext = new StructDefinition(mergedDeclaration, this, "context", //$NON-NLS-1$
214 fieldNames,
215 fieldValues.toArray(new Definition[fieldValues.size()]));
216 return mergedContext;
217 }
218
219 /**
220 * Gets the stream input reader that this event was made by
221 *
222 * @return the parent
223 * @since 3.0
224 */
225 public CTFStreamInputReader getStreamInputReader() {
226 return fStreamInputReader;
227 }
228
229 /**
230 * Gets the context of packet the event is in.
231 *
232 * @return the packet context
233 */
234 public StructDefinition getPacketContext() {
235 return fPacketContext;
236 }
237
238 /**
239 * gets the CPU the event was generated by. Slightly LTTng specific
240 *
241 * @return The CPU the event was generated by
242 */
243 public int getCPU() {
244 return fStreamInputReader.getCPU();
245 }
246
247 /**
248 * @return the timestamp
249 */
250 public long getTimestamp() {
251 return fTimestamp;
252 }
253
254 // ------------------------------------------------------------------------
255 // Operations
256 // ------------------------------------------------------------------------
257
258 @Override
259 public Definition lookupDefinition(String lookupPath) {
260 if (lookupPath.equals("context")) { //$NON-NLS-1$
261 return fEventContext;
262 } else if (lookupPath.equals("fields")) { //$NON-NLS-1$
263 return fFields;
264 } else {
265 return null;
266 }
267 }
268
269 @Override
270 public String toString() {
271 Iterable<String> list;
272 StringBuilder retString = new StringBuilder();
273 final String cr = System.getProperty("line.separator");//$NON-NLS-1$
274
275 retString.append("Event type: " + fDeclaration.getName() + cr); //$NON-NLS-1$
276 retString.append("Timestamp: " + Long.toString(fTimestamp) + cr); //$NON-NLS-1$
277
278 if (fEventContext != null) {
279 list = fEventContext.getDeclaration().getFieldsList();
280
281 for (String field : list) {
282 retString.append(field
283 + " : " + fEventContext.getDefinition(field).toString() + cr); //$NON-NLS-1$
284 }
285 }
286
287 if (fFields != null) {
288 list = fFields.getDeclaration().getFieldsList();
289
290 for (String field : list) {
291 retString.append(field
292 + " : " + fFields.getDefinition(field).toString() + cr); //$NON-NLS-1$
293 }
294 }
295
296 return retString.toString();
297 }
298
299 }
This page took 0.050823 seconds and 5 git commands to generate.