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