ctf: Add support for scoped event contexts
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / EventDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2012 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.HashMap;
16 import java.util.List;
17 import java.util.Map.Entry;
18
19 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
20 import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
21 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
22 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
23 import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
24
25 /**
26 * Representation of a particular instance of an event.
27 */
28 public class EventDefinition implements IDefinitionScope {
29
30 // ------------------------------------------------------------------------
31 // Attributes
32 // ------------------------------------------------------------------------
33
34 /**
35 * The corresponding event declaration.
36 */
37 private final EventDeclaration declaration;
38
39 /**
40 * The timestamp of the current event.
41 */
42 private long timestamp;
43
44 /**
45 * The event context structure definition.
46 */
47 private StructDefinition context;
48
49 /**
50 * The event fields structure definition.
51 */
52 private StructDefinition fields;
53
54 /**
55 * The StreamInputReader that reads this event definition.
56 */
57 private final StreamInputReader streamInputReader;
58
59 // ------------------------------------------------------------------------
60 // Constructors
61 // ------------------------------------------------------------------------
62
63 /**
64 * Constructs an event definition.
65 *
66 * @param declaration
67 * The corresponding event declaration
68 * @param streamInputReader
69 * The SIR from where this EventDef was read
70 */
71 public EventDefinition(EventDeclaration declaration,
72 StreamInputReader streamInputReader) {
73 this.declaration = declaration;
74 this.streamInputReader = streamInputReader;
75 }
76
77 // ------------------------------------------------------------------------
78 // Getters/Setters/Predicates
79 // ------------------------------------------------------------------------
80
81 @Override
82 public String getPath() {
83 return "event"; //$NON-NLS-1$
84 }
85
86 /**
87 * Gets the declaration (the form) of the data
88 *
89 * @return the event declaration
90 */
91 public EventDeclaration getDeclaration() {
92 return declaration;
93 }
94
95 /**
96 * Gets the fields of a definition
97 *
98 * @return the fields of a definition in struct form. Can be null.
99 */
100 public StructDefinition getFields() {
101 return fields;
102 }
103
104 /**
105 * Gets the context of this event without the context of the stream
106 *
107 * @return the context in struct form
108 * @since 1.2
109 */
110 public StructDefinition getEventContext() {
111 return context;
112 }
113
114 /**
115 * Gets the context of this event within a stream
116 *
117 * @return the context in struct form
118 */
119 public StructDefinition getContext() {
120 final StructDefinition streamContext =
121 streamInputReader.getPacketReader().getStreamEventContextDef();
122
123 /* Most common case so far */
124 if (streamContext == null) {
125 return context;
126 }
127
128 /* streamContext is not null, but the context of the event is null */
129 if (context == null) {
130 return streamContext;
131 }
132
133 /* The stream context and event context are assigned. */
134 StructDeclaration mergedDeclaration = new StructDeclaration(1);
135
136 /* Add fields from the stream */
137 HashMap<String, Definition> defs = streamContext.getDefinitions();
138 for (Entry<String, Definition> entry : defs.entrySet()) {
139 mergedDeclaration.addField(entry.getKey(), entry.getValue().getDeclaration());
140 }
141
142 /* Add fields from the event context, overwrite the stream ones if needed. */
143 for (Entry<String, Definition> entry : context.getDefinitions().entrySet()) {
144 mergedDeclaration.addField(entry.getKey(), entry.getValue().getDeclaration());
145 }
146
147 StructDefinition mergedContext = mergedDeclaration.createDefinition(null, "context"); //$NON-NLS-1$
148 for (String key : mergedContext.getDefinitions().keySet()) {
149 final Definition lookupDefinition = context.lookupDefinition(key);
150 /*
151 * If the key is in the event context, add it from there, if it is
152 * not, then it's in the stream. There is a priority with scoping so
153 * if there is a field like "context" in both stream and context,
154 * you display the context.
155 */
156 if (lookupDefinition != null) {
157 mergedContext.getDefinitions().put(key, lookupDefinition);
158 } else {
159 mergedContext.getDefinitions().put(key, streamContext.lookupDefinition(key));
160 }
161 }
162 return mergedContext;
163 }
164
165 /**
166 * Gets the stream input reader that this event was made by
167 *
168 * @return the parent
169 */
170 public StreamInputReader getStreamInputReader() {
171 return streamInputReader;
172 }
173
174 /**
175 * Gets the context of packet the event is in.
176 *
177 * @return the packet context
178 */
179 public StructDefinition getPacketContext() {
180 return streamInputReader.getCurrentPacketContext();
181 }
182
183 /**
184 * gets the CPU the event was generated by. Slightly LTTng specific
185 *
186 * @return The CPU the event was generated by
187 */
188 public int getCPU() {
189 return streamInputReader.getCPU();
190 }
191
192 /**
193 * @return the timestamp
194 */
195 public long getTimestamp() {
196 return timestamp;
197 }
198
199 /**
200 * @param timestamp
201 * the timestamp to set
202 */
203 public void setTimestamp(long timestamp) {
204 this.timestamp = timestamp;
205 }
206
207 /**
208 * @param context
209 * the context to set
210 */
211 public void setContext(StructDefinition context) {
212 this.context = context;
213 }
214
215 /**
216 * @param fields
217 * the fields to set
218 */
219 public void setFields(StructDefinition fields) {
220 this.fields = fields;
221 }
222
223 // ------------------------------------------------------------------------
224 // Operations
225 // ------------------------------------------------------------------------
226
227 @Override
228 public Definition lookupDefinition(String lookupPath) {
229 if (lookupPath.equals("context")) { //$NON-NLS-1$
230 return context;
231 } else if (lookupPath.equals("fields")) { //$NON-NLS-1$
232 return fields;
233 } else {
234 return null;
235 }
236 }
237
238 @Override
239 public String toString() {
240 HashMap<String, Definition> definitions;
241 List<String> list;
242 StringBuilder retString = new StringBuilder();
243 final String cr = System.getProperty("line.separator");//$NON-NLS-1$
244
245 retString.append("Event type: " + declaration.getName() + cr); //$NON-NLS-1$
246 retString.append("Timestamp: " + Long.toString(timestamp) + cr); //$NON-NLS-1$
247
248 if (context != null) {
249 definitions = context.getDefinitions();
250 list = context.getDeclaration().getFieldsList();
251
252 for (String field : list) {
253 retString.append(field
254 + " : " + definitions.get(field).toString() + cr); //$NON-NLS-1$
255 }
256 }
257
258 if (fields != null) {
259 definitions = fields.getDefinitions();
260 list = fields.getDeclaration().getFieldsList();
261
262 for (String field : list) {
263 retString.append(field
264 + " : " + definitions.get(field).toString() + cr); //$NON-NLS-1$
265 }
266 }
267
268 return retString.toString();
269 }
270
271 }
This page took 0.038602 seconds and 6 git commands to generate.