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