Commit | Line | Data |
---|---|---|
866e5b51 FC |
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 | ||
18 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
19 | import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; | |
20 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
21 | import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; | |
22 | ||
23 | /** | |
24 | * <b><u>EventDefinition</u></b> | |
25 | * <p> | |
26 | * Represents an instance of an event. | |
27 | */ | |
28 | public class EventDefinition implements IDefinitionScope { | |
29 | ||
30 | // ------------------------------------------------------------------------ | |
31 | // Attributes | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
34 | /** | |
35 | * The corresponding event declaration. | |
36 | */ | |
aa572e22 | 37 | private final EventDeclaration declaration; |
866e5b51 FC |
38 | |
39 | /** | |
40 | * The timestamp of the current event. | |
41 | */ | |
aa572e22 | 42 | private long timestamp; |
866e5b51 FC |
43 | |
44 | /** | |
45 | * The event context structure definition. | |
46 | */ | |
aa572e22 | 47 | private StructDefinition context; |
866e5b51 FC |
48 | |
49 | /** | |
50 | * The event fields structure definition. | |
51 | */ | |
aa572e22 | 52 | private StructDefinition fields; |
866e5b51 FC |
53 | |
54 | /** | |
55 | * The StreamInputReader that reads this event definition. | |
56 | */ | |
57 | public 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 | */ | |
69 | public EventDefinition(EventDeclaration declaration, | |
70 | StreamInputReader streamInputReader) { | |
71 | this.declaration = declaration; | |
72 | this.streamInputReader = streamInputReader; | |
73 | } | |
74 | ||
75 | // ------------------------------------------------------------------------ | |
76 | // Getters/Setters/Predicates | |
77 | // ------------------------------------------------------------------------ | |
78 | ||
79 | @Override | |
80 | public String getPath() { | |
81 | return "event"; //$NON-NLS-1$ | |
82 | } | |
83 | ||
84 | public EventDeclaration getDeclaration() { | |
85 | return declaration; | |
86 | } | |
87 | ||
88 | public StructDefinition getFields() { | |
89 | return fields; | |
90 | } | |
91 | ||
92 | public StructDefinition getContext() { | |
93 | return context; | |
94 | } | |
95 | ||
96 | public StreamInputReader getStreamInputReader() { | |
97 | return streamInputReader; | |
98 | } | |
99 | ||
100 | public StructDefinition getPacketContext() { | |
101 | return streamInputReader.getCurrentPacketContext(); | |
102 | } | |
103 | ||
104 | public int getCPU() { | |
105 | return streamInputReader.getCPU(); | |
106 | } | |
107 | ||
aa572e22 MK |
108 | /** |
109 | * @return the timestamp | |
110 | */ | |
111 | public long getTimestamp() { | |
112 | return timestamp; | |
113 | } | |
114 | ||
115 | /** | |
116 | * @param timestamp the timestamp to set | |
117 | */ | |
118 | public void setTimestamp(long timestamp) { | |
119 | this.timestamp = timestamp; | |
120 | } | |
121 | ||
122 | /** | |
123 | * @param context the context to set | |
124 | */ | |
125 | public void setContext(StructDefinition context) { | |
126 | this.context = context; | |
127 | } | |
128 | ||
129 | /** | |
130 | * @param fields the fields to set | |
131 | */ | |
132 | public void setFields(StructDefinition fields) { | |
133 | this.fields = fields; | |
134 | } | |
135 | ||
866e5b51 FC |
136 | // ------------------------------------------------------------------------ |
137 | // Operations | |
138 | // ------------------------------------------------------------------------ | |
139 | ||
140 | @Override | |
141 | public Definition lookupDefinition(String lookupPath) { | |
142 | if (lookupPath.equals("context")) { //$NON-NLS-1$ | |
143 | return context; | |
144 | } else if (lookupPath.equals("fields")) { //$NON-NLS-1$ | |
145 | return fields; | |
146 | } else { | |
147 | return null; | |
148 | } | |
149 | } | |
150 | ||
151 | @Override | |
152 | public String toString() { | |
153 | HashMap<String, Definition> f; | |
154 | List<String> list; | |
155 | StringBuilder b = new StringBuilder(); | |
156 | ||
157 | b.append("Event type: " + declaration.getName() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ | |
158 | b.append("Timestamp: " + Long.toString(timestamp) + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ | |
159 | ||
160 | if (context != null) { | |
161 | f = context.getDefinitions(); | |
162 | list = context.getDeclaration().getFieldsList(); | |
163 | ||
164 | for (String field : list) { | |
165 | b.append(field + " : " + f.get(field).toString() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ | |
166 | } | |
167 | } | |
168 | ||
169 | if (fields != null) { | |
170 | f = fields.getDefinitions(); | |
171 | list = fields.getDeclaration().getFieldsList(); | |
172 | ||
173 | for (String field : list) { | |
174 | b.append(field + " : " + f.get(field).toString() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ | |
175 | } | |
176 | } | |
177 | ||
178 | return b.toString(); | |
179 | } | |
180 | ||
181 | } |