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