1 /*******************************************************************************
2 * Copyright (c) 2011-2014 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Collections
;
17 import java
.util
.List
;
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
;
35 * Representation of a particular instance of an event.
37 public final class EventDefinition
implements IDefinitionScope
, IEventDefinition
{
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
44 * The corresponding event declaration.
46 private final IEventDeclaration fDeclaration
;
49 * The timestamp of the current event.
51 private final long fTimestamp
;
53 private final ICompositeDefinition fEventHeaderDefinition
;
56 * The event context structure definition.
58 private final ICompositeDefinition fEventContext
;
60 private final ICompositeDefinition fStreamContext
;
62 private final ICompositeDefinition fPacketContext
;
65 * The event fields structure definition.
67 private final ICompositeDefinition fFields
;
70 * The current cpu, could be @link {@link IPacketHeader#UNKNOWN_CPU}
72 private final int fCpu
;
74 private final @NonNull Map
<String
, Object
> fPacketAttributes
;
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
81 * Constructs an event definition.
84 * The corresponding event declaration
86 * The cpu source of the event. You can use UNKNOWN_CPU if it is
90 * @param eventHeaderDefinition
91 * The event header definition, can be null if there is no header
95 * @param packetContext
96 * the packet context (the one with content size, not magic
98 * @param streamContext
102 * @param packetDescriptor
103 * descriptor of the packet containing this event
106 public EventDefinition(IEventDeclaration declaration
,
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
;
118 fTimestamp
= timestamp
;
120 fEventContext
= eventContext
;
121 fPacketContext
= packetContext
;
122 fStreamContext
= streamContext
;
123 fPacketAttributes
= packetDescriptor
!= null ? packetDescriptor
.getAttributes() : Collections
.EMPTY_MAP
;
126 // ------------------------------------------------------------------------
127 // Getters/Setters/Predicates
128 // ------------------------------------------------------------------------
131 public ILexicalScope
getScopePath() {
132 String eventName
= fDeclaration
.getName();
133 if (eventName
== null) {
136 ILexicalScope myScope
= ILexicalScope
.EVENT
.getChild(eventName
);
137 if (myScope
== null) {
138 myScope
= new LexicalScope(ILexicalScope
.EVENT
, eventName
);
144 public IEventDeclaration
getDeclaration() {
149 public ICompositeDefinition
getEventHeader() {
150 return fEventHeaderDefinition
;
154 public ICompositeDefinition
getFields() {
159 public ICompositeDefinition
getEventContext() {
160 return fEventContext
;
164 public ICompositeDefinition
getContext() {
166 /* Most common case so far */
167 if (fStreamContext
== null) {
168 return fEventContext
;
171 /* streamContext is not null, but the context of the event is null */
172 if (fEventContext
== null) {
173 return fStreamContext
;
176 // TODO: cache if this is a performance issue
178 /* The stream context and event context are assigned. */
179 StructDeclaration mergedDeclaration
= new StructDeclaration(1);
181 List
<Definition
> fieldValues
= new ArrayList
<>();
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
);
192 * Add fields from the event context, overwrite the stream ones if
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
);
201 fieldValues
.add(definition
);
204 return new StructDefinition(mergedDeclaration
, this, "context", //$NON-NLS-1$
205 fieldValues
.toArray(new Definition
[fieldValues
.size()]));
209 public ICompositeDefinition
getPacketContext() {
210 return fPacketContext
;
214 public int getCPU() {
219 public long getTimestamp() {
224 public Map
<String
, Object
> getPacketAttributes() {
225 return fPacketAttributes
;
228 // ------------------------------------------------------------------------
230 // ------------------------------------------------------------------------
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$
244 public String
toString() {
245 Iterable
<String
> list
;
246 StringBuilder retString
= new StringBuilder();
247 final String cr
= System
.getProperty("line.separator");//$NON-NLS-1$
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$
252 if (fEventContext
!= null) {
253 list
= fEventContext
.getFieldNames();
255 for (String field
: list
) {
256 retString
.append(field
).append(" : ").append(fEventContext
.getDefinition(field
).toString()).append(cr
); //$NON-NLS-1$
260 if (fFields
!= null) {
261 list
= fFields
.getFieldNames();
263 for (String field
: list
) {
264 retString
.append(field
).append(" : ").append(fFields
.getDefinition(field
).toString()).append(cr
); //$NON-NLS-1$
268 return retString
.toString();