rcp: Move plugins to their own sub-directory
[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.ILexicalScope;
21 import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
22 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
23 import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
24 import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
25 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
26 import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
27 import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInputReader;
28 import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration;
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 ICompositeDefinition fEventContext;
59
60 private final ICompositeDefinition fStreamContext;
61
62 private final ICompositeDefinition fPacketContext;
63
64 /**
65 * The event fields structure definition.
66 */
67 private final ICompositeDefinition 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 * @since 1.0
96 */
97 public EventDefinition(IEventDeclaration declaration,
98 CTFStreamInputReader streamInputReader,
99 long timestamp,
100 ICompositeDefinition streamContext,
101 ICompositeDefinition eventContext,
102 ICompositeDefinition packetContext,
103 ICompositeDefinition fields) {
104 fDeclaration = declaration;
105 fStreamInputReader = streamInputReader;
106 fTimestamp = timestamp;
107 fFields = fields;
108 fEventContext = eventContext;
109 fPacketContext = packetContext;
110 fStreamContext = streamContext;
111 }
112
113 // ------------------------------------------------------------------------
114 // Getters/Setters/Predicates
115 // ------------------------------------------------------------------------
116
117 /**
118 * @since 1.0
119 */
120 @Override
121 public ILexicalScope getScopePath() {
122 String eventName = fDeclaration.getName();
123 if (eventName == null) {
124 return null;
125 }
126 ILexicalScope myScope = ILexicalScope.EVENT.getChild(eventName);
127 if (myScope == null) {
128 myScope = new LexicalScope(ILexicalScope.EVENT, eventName);
129 }
130 return myScope;
131 }
132
133 /**
134 * Gets the declaration (the form) of the data
135 *
136 * @return the event declaration
137 */
138 public IEventDeclaration getDeclaration() {
139 return fDeclaration;
140 }
141
142 /**
143 * Gets the fields of a definition
144 *
145 * @return the fields of a definition in struct form. Can be null.
146 * @since 1.0
147 */
148 public ICompositeDefinition getFields() {
149 return fFields;
150 }
151
152 /**
153 * Gets the context of this event without the context of the stream
154 *
155 * @return the context in struct form
156 * @since 1.0
157 */
158 public ICompositeDefinition getEventContext() {
159 return fEventContext;
160 }
161
162 /**
163 * Gets the context of this event within a stream
164 *
165 * @return the context in struct form
166 * @since 1.0
167 */
168 public ICompositeDefinition getContext() {
169
170 /* Most common case so far */
171 if (fStreamContext == null) {
172 return fEventContext;
173 }
174
175 /* streamContext is not null, but the context of the event is null */
176 if (fEventContext == null) {
177 return fStreamContext;
178 }
179
180 // TODO: cache if this is a performance issue
181
182 /* The stream context and event context are assigned. */
183 StructDeclaration mergedDeclaration = new StructDeclaration(1);
184
185 List<Definition> fieldValues = new ArrayList<>();
186
187 /* Add fields from the stream */
188 List<String> fieldNames = fStreamContext.getFieldNames();
189 for (String fieldName : fieldNames) {
190 Definition definition = fStreamContext.getDefinition(fieldName);
191 mergedDeclaration.addField(fieldName, definition.getDeclaration());
192 fieldValues.add(definition);
193 }
194
195 /*
196 * Add fields from the event context, overwrite the stream ones if
197 * needed.
198 */
199 for (String fieldName : fEventContext.getFieldNames()) {
200 Definition definition = fEventContext.getDefinition(fieldName);
201 mergedDeclaration.addField(fieldName, definition.getDeclaration());
202 if (fieldNames.contains(fieldName)) {
203 fieldValues.set((fieldNames.indexOf(fieldName)), definition);
204 } else {
205 fieldValues.add(definition);
206 }
207 }
208 return new StructDefinition(mergedDeclaration, this, "context", //$NON-NLS-1$
209 fieldValues.toArray(new Definition[fieldValues.size()]));
210 }
211
212 /**
213 * Gets the stream input reader that this event was made by
214 *
215 * @return the parent
216 */
217 public CTFStreamInputReader getStreamInputReader() {
218 return fStreamInputReader;
219 }
220
221 /**
222 * Gets the context of packet the event is in.
223 *
224 * @return the packet context
225 * @since 1.0
226 */
227 public ICompositeDefinition getPacketContext() {
228 return fPacketContext;
229 }
230
231 /**
232 * gets the CPU the event was generated by. Slightly LTTng specific
233 *
234 * @return The CPU the event was generated by
235 */
236 public int getCPU() {
237 return fStreamInputReader.getCPU();
238 }
239
240 /**
241 * @return the timestamp
242 */
243 public long getTimestamp() {
244 return fTimestamp;
245 }
246
247 // ------------------------------------------------------------------------
248 // Operations
249 // ------------------------------------------------------------------------
250
251 /**
252 * @since 1.0
253 */
254 @Override
255 public IDefinition lookupDefinition(String lookupPath) {
256 if (lookupPath.equals("context")) { //$NON-NLS-1$
257 return fEventContext;
258 } else if (lookupPath.equals("fields")) { //$NON-NLS-1$
259 return fFields;
260 } else {
261 return null;
262 }
263 }
264
265 @Override
266 public String toString() {
267 Iterable<String> list;
268 StringBuilder retString = new StringBuilder();
269 final String cr = System.getProperty("line.separator");//$NON-NLS-1$
270
271 retString.append("Event type: ").append(fDeclaration.getName()).append(cr); //$NON-NLS-1$
272 retString.append("Timestamp: ").append(Long.toString(fTimestamp)).append(cr); //$NON-NLS-1$
273
274 if (fEventContext != null) {
275 list = fEventContext.getFieldNames();
276
277 for (String field : list) {
278 retString.append(field).append(" : ").append(fEventContext.getDefinition(field).toString()).append(cr); //$NON-NLS-1$
279 }
280 }
281
282 if (fFields != null) {
283 list = fFields.getFieldNames();
284
285 for (String field : list) {
286 retString.append(field).append(" : ").append(fFields.getDefinition(field).toString()).append(cr); //$NON-NLS-1$
287 }
288 }
289
290 return retString.toString();
291 }
292
293 }
This page took 0.049361 seconds and 5 git commands to generate.