Commit | Line | Data |
---|---|---|
3a5f73a1 JCK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 Ecole Polytechnique de Montreal, Ericsson | |
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 | package org.eclipse.tracecompass.tmf.analysis.xml.core.model; | |
10 | ||
11 | import java.util.ArrayList; | |
12 | import java.util.List; | |
13 | ||
14 | import org.eclipse.jdt.annotation.NonNull; | |
15 | import org.eclipse.jdt.annotation.Nullable; | |
16 | import org.eclipse.tracecompass.common.core.NonNullUtils; | |
17 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator; | |
18 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.pattern.stateprovider.XmlPatternStateProvider; | |
19 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; | |
20 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; | |
21 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer; | |
22 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils; | |
23 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; | |
24 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
25 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; | |
dd21f749 | 26 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; |
3a5f73a1 JCK |
27 | import org.w3c.dom.Element; |
28 | ||
29 | /** | |
30 | * This Class implements an action tree in the XML-defined state system. | |
31 | * An action is a collection of {@link ITmfXmlAction} that are executed when necessary. | |
32 | * | |
33 | * @author Jean-Christian Kouame | |
34 | * @since 2.0 | |
35 | */ | |
36 | public class TmfXmlAction implements ITmfXmlAction { | |
37 | ||
38 | private final IXmlStateSystemContainer fParent; | |
39 | private final String fId; | |
40 | private final List<ITmfXmlAction> fActionList = new ArrayList<>(); | |
41 | ||
42 | /** | |
43 | * Constructor | |
44 | * | |
45 | * @param modelFactory | |
46 | * The factory used to create XML model elements | |
47 | * @param node | |
48 | * The XML root of this action | |
49 | * @param container | |
50 | * The state system container this action belongs to | |
51 | */ | |
52 | public TmfXmlAction(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer container) { | |
53 | fParent = container; | |
54 | fId = NonNullUtils.checkNotNull(node.getAttribute(TmfXmlStrings.ID)); | |
55 | List<@Nullable Element> childElements = XmlUtils.getChildElements(node); | |
56 | for (Element child : childElements) { | |
57 | final @NonNull Element nonNullChild = NonNullUtils.checkNotNull(child); | |
58 | switch (nonNullChild.getNodeName()) { | |
59 | case TmfXmlStrings.STATE_CHANGE: | |
60 | fActionList.add(new StateChange(modelFactory, nonNullChild, fParent)); | |
61 | break; | |
62 | case TmfXmlStrings.FSM_SCHEDULE_ACTION: | |
63 | fActionList.add(new ScheduleNewScenario(modelFactory, nonNullChild, fParent)); | |
64 | break; | |
65 | case TmfXmlStrings.SEGMENT: | |
66 | fActionList.add(new GeneratePatternSegment(modelFactory, nonNullChild, fParent)); | |
67 | break; | |
68 | case TmfXmlStrings.ACTION: | |
69 | fActionList.add(new TmfXmlAction(modelFactory, nonNullChild, fParent)); | |
70 | break; | |
71 | default: | |
72 | Activator.logError("Invalid action type : " + nonNullChild.getNodeName()); //$NON-NLS-1$ | |
73 | } | |
74 | } | |
75 | ||
76 | } | |
77 | ||
78 | /** | |
79 | * Get the ID of this action | |
80 | * | |
81 | * @return The id of this action | |
82 | */ | |
83 | public String getId() { | |
84 | return fId; | |
85 | } | |
86 | ||
87 | @Override | |
88 | public void execute(@NonNull ITmfEvent event, TmfXmlScenarioInfo scenarioInfo) { | |
89 | // the order of the actions is important, do not parallelize. | |
90 | for (ITmfXmlAction action : fActionList) { | |
91 | action.execute(event, scenarioInfo); | |
92 | } | |
93 | } | |
94 | ||
95 | /** | |
96 | * Private class for an action that will create a state change in the state | |
97 | * system | |
98 | */ | |
99 | private class StateChange implements ITmfXmlAction { | |
100 | ||
101 | private final TmfXmlStateChange fStateChange; | |
102 | ||
103 | public StateChange(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer parent) { | |
104 | fStateChange = modelFactory.createStateChange(node, parent); | |
105 | } | |
106 | ||
107 | @Override | |
108 | public void execute(@NonNull ITmfEvent event, TmfXmlScenarioInfo scenarioInfo) { | |
109 | try { | |
110 | fStateChange.handleEvent(event, scenarioInfo); | |
111 | } catch (StateValueTypeException | AttributeNotFoundException e) { | |
112 | Activator.logError("Exception when executing action state change", e); //$NON-NLS-1$ | |
113 | } | |
114 | } | |
115 | } | |
116 | ||
117 | /** | |
118 | * Private class for an action that will instantiate and schedule a new instance of | |
119 | * an fsm | |
120 | */ | |
121 | private static class ScheduleNewScenario implements ITmfXmlAction { | |
122 | ||
123 | /** | |
124 | * Constructor | |
125 | * | |
126 | * @param modelFactory | |
127 | * The factory used to create XML model elements | |
128 | * @param node | |
129 | * The XML root of this action | |
130 | * @param container | |
131 | * The state system container this action belongs to | |
132 | */ | |
133 | public ScheduleNewScenario(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer container) { | |
134 | } | |
135 | ||
136 | @Override | |
137 | public void execute(ITmfEvent event, TmfXmlScenarioInfo scenarioInfo) { | |
138 | // TODO This action needs to be implemented | |
139 | throw new UnsupportedOperationException("Schedule an FSM is not yet supported"); //$NON-NLS-1$ | |
140 | } | |
141 | } | |
142 | ||
143 | /** | |
144 | * Private class for an action that will generate pattern segment | |
145 | */ | |
146 | private static class GeneratePatternSegment implements ITmfXmlAction { | |
147 | ||
148 | private final TmfXmlPatternSegmentBuilder fSegmentBuilder; | |
149 | private final XmlPatternStateProvider fProvider; | |
150 | ||
151 | public GeneratePatternSegment(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer parent) { | |
152 | fProvider = ((XmlPatternStateProvider) parent); | |
153 | fSegmentBuilder = modelFactory.createPatternSegmentBuilder(node, parent); | |
154 | } | |
155 | ||
156 | @Override | |
157 | public void execute(ITmfEvent event, TmfXmlScenarioInfo scenarioInfo) { | |
158 | long ts = fProvider.getHistoryBuilder().getStartTime(fProvider, scenarioInfo, event); | |
159 | // FIXME Should the scale always be nanoseconds? | |
dd21f749 | 160 | ITmfTimestamp start = TmfTimestamp.fromNanos(ts); |
3a5f73a1 JCK |
161 | ITmfTimestamp end = event.getTimestamp(); |
162 | fSegmentBuilder.generatePatternSegment(event, start, end, scenarioInfo); | |
163 | } | |
164 | } | |
165 | } |