ctf: Introduce IEventDefinition
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / 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.internal.ctf.core.event;
14
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
23 import org.eclipse.tracecompass.ctf.core.event.IEventDefinition;
24 import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
25 import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
26 import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
27 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
28 import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
29 import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
30 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
31 import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
32 import org.eclipse.tracecompass.ctf.core.trace.ICTFPacketDescriptor;
33
34 /**
35 * Representation of a particular instance of an event.
36 */
37 public final class EventDefinition implements IDefinitionScope, IEventDefinition {
38
39 // ------------------------------------------------------------------------
40 // Attributes
41 // ------------------------------------------------------------------------
42
43 /**
44 * The corresponding event declaration.
45 */
46 private final IEventDeclaration fDeclaration;
47
48 /**
49 * The timestamp of the current event.
50 */
51 private final long fTimestamp;
52
53 private final ICompositeDefinition fEventHeaderDefinition;
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 current cpu, could be @link {@link IPacketHeader#UNKNOWN_CPU}
71 */
72 private final int fCpu;
73
74 private final @NonNull Map<String, Object> fPacketAttributes;
75
76 // ------------------------------------------------------------------------
77 // Constructors
78 // ------------------------------------------------------------------------
79
80 /**
81 * Constructs an event definition.
82 *
83 * @param declaration
84 * The corresponding event declaration
85 * @param cpu
86 * The cpu source of the event. You can use UNKNOWN_CPU if it is
87 * not known.
88 * @param timestamp
89 * event timestamp
90 * @param eventHeaderDefinition
91 * The event header definition, can be null if there is no header
92 * definition
93 * @param eventContext
94 * The event context
95 * @param packetContext
96 * the packet context (the one with content size, not magic
97 * number)
98 * @param streamContext
99 * the stream context
100 * @param fields
101 * The event fields
102 * @param packetDescriptor
103 * descriptor of the packet containing this event
104 * @since 2.0
105 */
106 public EventDefinition(IEventDeclaration declaration,
107 int cpu,
108 long timestamp,
109 ICompositeDefinition eventHeaderDefinition,
110 ICompositeDefinition streamContext,
111 ICompositeDefinition eventContext,
112 ICompositeDefinition packetContext,
113 ICompositeDefinition fields,
114 @Nullable ICTFPacketDescriptor packetDescriptor) {
115 fDeclaration = declaration;
116 fEventHeaderDefinition = eventHeaderDefinition;
117 fCpu = cpu;
118 fTimestamp = timestamp;
119 fFields = fields;
120 fEventContext = eventContext;
121 fPacketContext = packetContext;
122 fStreamContext = streamContext;
123 fPacketAttributes = packetDescriptor != null ? packetDescriptor.getAttributes() : Collections.EMPTY_MAP;
124 }
125
126 // ------------------------------------------------------------------------
127 // Getters/Setters/Predicates
128 // ------------------------------------------------------------------------
129
130 @Override
131 public ILexicalScope getScopePath() {
132 String eventName = fDeclaration.getName();
133 if (eventName == null) {
134 return null;
135 }
136 ILexicalScope myScope = ILexicalScope.EVENT.getChild(eventName);
137 if (myScope == null) {
138 myScope = new LexicalScope(ILexicalScope.EVENT, eventName);
139 }
140 return myScope;
141 }
142
143 @Override
144 public IEventDeclaration getDeclaration() {
145 return fDeclaration;
146 }
147
148 @Override
149 public ICompositeDefinition getEventHeader() {
150 return fEventHeaderDefinition;
151 }
152
153 @Override
154 public ICompositeDefinition getFields() {
155 return fFields;
156 }
157
158 @Override
159 public ICompositeDefinition getEventContext() {
160 return fEventContext;
161 }
162
163 @Override
164 public ICompositeDefinition getContext() {
165
166 /* Most common case so far */
167 if (fStreamContext == null) {
168 return fEventContext;
169 }
170
171 /* streamContext is not null, but the context of the event is null */
172 if (fEventContext == null) {
173 return fStreamContext;
174 }
175
176 // TODO: cache if this is a performance issue
177
178 /* The stream context and event context are assigned. */
179 StructDeclaration mergedDeclaration = new StructDeclaration(1);
180
181 List<Definition> fieldValues = new ArrayList<>();
182
183 /* Add fields from the stream */
184 List<@NonNull String> fieldNames = fStreamContext.getFieldNames();
185 for (String fieldName : fieldNames) {
186 Definition definition = fStreamContext.getDefinition(fieldName);
187 mergedDeclaration.addField(fieldName, definition.getDeclaration());
188 fieldValues.add(definition);
189 }
190
191 /*
192 * Add fields from the event context, overwrite the stream ones if
193 * needed.
194 */
195 for (String fieldName : fEventContext.getFieldNames()) {
196 Definition definition = fEventContext.getDefinition(fieldName);
197 mergedDeclaration.addField(fieldName, definition.getDeclaration());
198 if (fieldNames.contains(fieldName)) {
199 fieldValues.set((fieldNames.indexOf(fieldName)), definition);
200 } else {
201 fieldValues.add(definition);
202 }
203 }
204 return new StructDefinition(mergedDeclaration, this, "context", //$NON-NLS-1$
205 fieldValues.toArray(new Definition[fieldValues.size()]));
206 }
207
208 @Override
209 public ICompositeDefinition getPacketContext() {
210 return fPacketContext;
211 }
212
213 @Override
214 public int getCPU() {
215 return fCpu;
216 }
217
218 @Override
219 public long getTimestamp() {
220 return fTimestamp;
221 }
222
223 @Override
224 public Map<String, Object> getPacketAttributes() {
225 return fPacketAttributes;
226 }
227
228 // ------------------------------------------------------------------------
229 // Operations
230 // ------------------------------------------------------------------------
231
232 @Override
233 public IDefinition lookupDefinition(String lookupPath) {
234 if (lookupPath.equals("context")) { //$NON-NLS-1$
235 return fEventContext;
236 } else if (lookupPath.equals("fields")) { //$NON-NLS-1$
237 return fFields;
238 } else {
239 return null;
240 }
241 }
242
243 @Override
244 public String toString() {
245 Iterable<String> list;
246 StringBuilder retString = new StringBuilder();
247 final String cr = System.getProperty("line.separator");//$NON-NLS-1$
248
249 retString.append("Event type: ").append(fDeclaration.getName()).append(cr); //$NON-NLS-1$
250 retString.append("Timestamp: ").append(Long.toString(fTimestamp)).append(cr); //$NON-NLS-1$
251
252 if (fEventContext != null) {
253 list = fEventContext.getFieldNames();
254
255 for (String field : list) {
256 retString.append(field).append(" : ").append(fEventContext.getDefinition(field).toString()).append(cr); //$NON-NLS-1$
257 }
258 }
259
260 if (fFields != null) {
261 list = fFields.getFieldNames();
262
263 for (String field : list) {
264 retString.append(field).append(" : ").append(fFields.getDefinition(field).toString()).append(cr); //$NON-NLS-1$
265 }
266 }
267
268 return retString.toString();
269 }
270
271 }
This page took 0.059153 seconds and 5 git commands to generate.