Commit | Line | Data |
---|---|---|
0f7276b6 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 Ecole Polytechnique de Montreal | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made 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: | |
10 | * Florian Wininger - Initial API and implementation | |
11 | ******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.analysis.xml.core.model; |
0f7276b6 GB |
14 | |
15 | import java.util.ArrayList; | |
16 | import java.util.List; | |
17 | ||
18 | import org.eclipse.jdt.annotation.NonNull; | |
12685851 | 19 | import org.eclipse.jdt.annotation.Nullable; |
e894a508 AM |
20 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; |
21 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; | |
22 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; | |
2bdf0193 AM |
23 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer; |
24 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils; | |
25 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; | |
26 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
0f7276b6 GB |
27 | import org.w3c.dom.Element; |
28 | import org.w3c.dom.Node; | |
29 | ||
30 | /** | |
1d7e62f9 | 31 | * This Class implement a State Change in the XML-defined state system |
0f7276b6 GB |
32 | * |
33 | * <pre> | |
34 | * example 1: Simple state change | |
35 | * <stateChange> | |
36 | * <stateAttribute type="location" value="CurrentThread" /> | |
37 | * <stateAttribute type="constant" value="System_call" /> | |
38 | * <stateValue type="null" /> | |
39 | * </stateChange> | |
40 | * | |
41 | * example 2: Conditional state change | |
42 | * <stateChange> | |
43 | * <if> | |
44 | * <condition> | |
45 | * <stateAttribute type="location" value="CurrentThread" /> | |
46 | * <stateAttribute type="constant" value="System_call" /> | |
47 | * <stateValue type="null" /> | |
48 | * </condition> | |
49 | * </if> | |
50 | * <then> | |
51 | * <stateAttribute type="location" value="CurrentThread" /> | |
52 | * <stateAttribute type="constant" value="Status" /> | |
53 | * <stateValue int="$PROCESS_STATUS_RUN_USERMODE"/> | |
54 | * </then> | |
55 | * <else> | |
56 | * <stateAttribute type="location" value="CurrentThread" /> | |
57 | * <stateAttribute type="constant" value="Status" /> | |
58 | * <stateValue int="$PROCESS_STATUS_RUN_SYSCALL"/> | |
59 | * </else> | |
60 | * </stateChange> | |
61 | * </pre> | |
62 | * | |
63 | * @author Florian Wininger | |
64 | */ | |
65 | public class TmfXmlStateChange { | |
66 | ||
67 | private final IXmlStateChange fChange; | |
1d7e62f9 | 68 | private final IXmlStateSystemContainer fContainer; |
0f7276b6 GB |
69 | |
70 | /** | |
71 | * Constructor | |
72 | * | |
1d7e62f9 GB |
73 | * @param modelFactory |
74 | * The factory used to create XML model elements | |
0f7276b6 GB |
75 | * @param statechange |
76 | * XML node root of this state change | |
1d7e62f9 GB |
77 | * @param container |
78 | * The state system container this state change belongs to | |
0f7276b6 | 79 | */ |
1d7e62f9 GB |
80 | public TmfXmlStateChange(ITmfXmlModelFactory modelFactory, Element statechange, IXmlStateSystemContainer container) { |
81 | fContainer = container; | |
0f7276b6 GB |
82 | |
83 | /* | |
84 | * child nodes is either a list of TmfXmlStateAttributes and | |
85 | * TmfXmlStateValues, or an if-then-else series of nodes. | |
86 | */ | |
87 | Node ifNode = statechange.getElementsByTagName(TmfXmlStrings.IF).item(0); | |
88 | if (ifNode != null) { | |
89 | /* the state change has a condition */ | |
1d7e62f9 | 90 | fChange = new XmlConditionalChange(modelFactory, statechange); |
0f7276b6 GB |
91 | } else { |
92 | /* the state change does not have a condition */ | |
1d7e62f9 | 93 | fChange = new XmlStateValueChange(modelFactory, statechange); |
0f7276b6 GB |
94 | } |
95 | } | |
96 | ||
97 | /** | |
98 | * Execute the state change for an event. If necessary, it validates the | |
99 | * condition and executes the required change. | |
100 | * | |
101 | * @param event | |
102 | * The event to process | |
103 | * @throws AttributeNotFoundException | |
104 | * Pass through the exception it received | |
105 | * @throws TimeRangeException | |
106 | * Pass through the exception it received | |
107 | * @throws StateValueTypeException | |
108 | * Pass through the exception it received | |
109 | */ | |
12685851 | 110 | public void handleEvent(ITmfEvent event) throws AttributeNotFoundException, StateValueTypeException, TimeRangeException { |
0f7276b6 GB |
111 | fChange.handleEvent(event); |
112 | } | |
113 | ||
446598f9 GB |
114 | @Override |
115 | public String toString() { | |
116 | return "TmfXmlStateChange: " + fChange; //$NON-NLS-1$ | |
117 | } | |
118 | ||
0f7276b6 GB |
119 | /* Interface for both private classes to handle the event */ |
120 | private interface IXmlStateChange { | |
12685851 | 121 | void handleEvent(ITmfEvent event) throws AttributeNotFoundException, StateValueTypeException, TimeRangeException; |
0f7276b6 GB |
122 | } |
123 | ||
124 | /** | |
125 | * Conditional state change with a condition to verify | |
126 | */ | |
127 | private class XmlConditionalChange implements IXmlStateChange { | |
128 | private final TmfXmlCondition fCondition; | |
129 | private final TmfXmlStateChange fThenChange; | |
12685851 | 130 | private final @Nullable TmfXmlStateChange fElseChange; |
0f7276b6 | 131 | |
1d7e62f9 | 132 | public XmlConditionalChange(ITmfXmlModelFactory modelFactory, Element statechange) { |
0f7276b6 GB |
133 | /* |
134 | * The if node exists, it has been verified before calling this | |
135 | */ | |
136 | Node ifNode = statechange.getElementsByTagName(TmfXmlStrings.IF).item(0); | |
12685851 GB |
137 | if (ifNode == null) { |
138 | throw new IllegalArgumentException(); | |
139 | } | |
1d7e62f9 | 140 | fCondition = modelFactory.createCondition((Element) ifNode, fContainer); |
0f7276b6 GB |
141 | |
142 | Node thenNode = statechange.getElementsByTagName(TmfXmlStrings.THEN).item(0); | |
143 | if (thenNode == null) { | |
144 | throw new IllegalArgumentException("Conditional state change: there should be a then clause."); //$NON-NLS-1$ | |
145 | } | |
1d7e62f9 | 146 | fThenChange = modelFactory.createStateChange((Element) thenNode, fContainer); |
0f7276b6 GB |
147 | |
148 | Node elseNode = statechange.getElementsByTagName(TmfXmlStrings.ELSE).item(0); | |
149 | if (elseNode != null) { | |
1d7e62f9 | 150 | fElseChange = modelFactory.createStateChange((Element) elseNode, fContainer); |
0f7276b6 GB |
151 | } else { |
152 | fElseChange = null; | |
153 | } | |
154 | } | |
155 | ||
156 | @Override | |
1d7e62f9 | 157 | public void handleEvent(@NonNull ITmfEvent event) throws AttributeNotFoundException, StateValueTypeException, TimeRangeException { |
0f7276b6 GB |
158 | TmfXmlStateChange toExecute = fThenChange; |
159 | try { | |
160 | if (!fCondition.testForEvent(event)) { | |
161 | toExecute = fElseChange; | |
162 | } | |
163 | } catch (AttributeNotFoundException e) { | |
164 | /* | |
165 | * An attribute in the condition did not exist (yet), return | |
166 | * from the state change | |
167 | */ | |
168 | return; | |
169 | } | |
170 | ||
171 | if (toExecute == null) { | |
172 | return; | |
173 | } | |
174 | toExecute.handleEvent(event); | |
175 | } | |
446598f9 GB |
176 | |
177 | @Override | |
178 | public String toString() { | |
179 | return "Condition: " + fCondition; //$NON-NLS-1$ | |
180 | } | |
0f7276b6 GB |
181 | } |
182 | ||
183 | /** | |
184 | * State change with no condition | |
185 | */ | |
186 | private class XmlStateValueChange implements IXmlStateChange { | |
1d7e62f9 | 187 | private final ITmfXmlStateValue fValue; |
0f7276b6 | 188 | |
1d7e62f9 | 189 | public XmlStateValueChange(ITmfXmlModelFactory modelFactory, Element statechange) { |
4c4e2816 | 190 | List<@Nullable Element> childElements = XmlUtils.getChildElements(statechange); |
0f7276b6 GB |
191 | |
192 | /* | |
193 | * Last child element is the state value, the others are attributes | |
194 | * to reach to value to set | |
195 | */ | |
196 | Element stateValueElement = childElements.remove(childElements.size() - 1); | |
12685851 GB |
197 | if (stateValueElement == null) { |
198 | throw new IllegalStateException(); | |
199 | } | |
1d7e62f9 | 200 | List<ITmfXmlStateAttribute> attributes = new ArrayList<>(); |
0f7276b6 | 201 | for (Element element : childElements) { |
4c4e2816 | 202 | if (element == null || !element.getNodeName().equals(TmfXmlStrings.STATE_ATTRIBUTE)) { |
0f7276b6 GB |
203 | throw new IllegalArgumentException("TmfXmlStateChange: a state change must have only TmfXmlStateAttribute elements before the state value"); //$NON-NLS-1$ |
204 | } | |
1d7e62f9 | 205 | ITmfXmlStateAttribute attribute = modelFactory.createStateAttribute(element, fContainer); |
0f7276b6 GB |
206 | attributes.add(attribute); |
207 | } | |
208 | if (attributes.isEmpty()) { | |
209 | throw new IllegalArgumentException("TmfXmlStateChange: a state change must have at least one TmfXmlStateAttribute element before the state value"); //$NON-NLS-1$ | |
210 | } | |
1d7e62f9 | 211 | fValue = modelFactory.createStateValue(stateValueElement, fContainer, attributes); |
0f7276b6 GB |
212 | } |
213 | ||
214 | @Override | |
1d7e62f9 | 215 | public void handleEvent(@NonNull ITmfEvent event) throws AttributeNotFoundException, StateValueTypeException, TimeRangeException { |
0f7276b6 GB |
216 | fValue.handleEvent(event); |
217 | } | |
446598f9 GB |
218 | |
219 | @Override | |
220 | public String toString() { | |
221 | return "Value: " + fValue; //$NON-NLS-1$ | |
222 | } | |
0f7276b6 GB |
223 | } |
224 | ||
225 | } |