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