tmf : Add latency density view for the pattern analysis
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.ui / src / org / eclipse / tracecompass / tmf / analysis / xml / ui / module / TmfXmlAnalysisOutputSource.java
1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal and others
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 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.analysis.xml.ui.module;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.List;
18 import java.util.Map;
19
20 import javax.xml.parsers.DocumentBuilder;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.parsers.ParserConfigurationException;
23
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.pattern.stateprovider.XmlPatternAnalysis;
26 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.Activator;
27 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.TmfXmlUiStrings;
28 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.module.Messages;
29 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency.PatternDensityView;
30 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency.PatternLatencyTableView;
31 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency.PatternScatterGraphView;
32 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.xychart.XmlXYView;
33 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
34 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
35 import org.eclipse.tracecompass.tmf.analysis.xml.ui.views.timegraph.XmlTimeGraphView;
36 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
37 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisOutput;
38 import org.eclipse.tracecompass.tmf.core.analysis.ITmfNewAnalysisModuleListener;
39 import org.eclipse.tracecompass.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.NodeList;
43 import org.xml.sax.SAXException;
44
45 /**
46 * This class searches all XML files to find outputs applicable to the newly
47 * created analysis
48 *
49 * @author Geneviève Bastien
50 */
51 public class TmfXmlAnalysisOutputSource implements ITmfNewAnalysisModuleListener {
52
53 /** String separating data elements for the output properties */
54 public static final @NonNull String DATA_SEPARATOR = ";;;"; //$NON-NLS-1$
55
56 /**
57 * Enum to match the name of a view's XML element to its view ID.
58 */
59 public static enum ViewType {
60 /**
61 * Time graph view element
62 */
63 TIME_GRAPH_VIEW(TmfXmlUiStrings.TIME_GRAPH_VIEW, XmlTimeGraphView.ID),
64 /**
65 * XY chart view element
66 */
67 XY_VIEW(TmfXmlUiStrings.XY_VIEW, XmlXYView.ID);
68
69 private final @NonNull String fXmlElem;
70 private final String fViewId;
71
72 private ViewType(@NonNull String xmlElem, String viewId) {
73 fXmlElem = xmlElem;
74 fViewId = viewId;
75 }
76
77 /**
78 * Get the XML element corresponding to this view type
79 *
80 * @return The XML element corresponding to this type
81 */
82 public @NonNull String getXmlElem() {
83 return fXmlElem;
84 }
85
86 private String getViewId() {
87 return fViewId;
88 }
89 }
90
91 /**
92 * Enum for latency view type.
93 *
94 * @author Jean-Christian Kouame
95 * @since 2.0
96 *
97 */
98 public static enum LatencyViewType {
99
100 /**
101 * Latency Table View type
102 */
103 LATENCY_TABLE(PatternLatencyTableView.ID, Messages.TmfXmlAnalysisOutputSource_LatencyTable),
104
105 /**
106 * Latency Scatter View type
107 */
108 SCATTER_GRAPH(PatternScatterGraphView.ID, Messages.TmfXmlAnalysisOutputSource_ScatterGraphTitle),
109
110 /**
111 * Latency Density View type
112 */
113 DENSITY_VIEW(PatternDensityView.ID, Messages.TmfXmlAnalysisOutputSource_DensityChartTitle);
114
115 private @NonNull String fLatencyViewId;
116 private String fLatencyViewLabel;
117
118 private LatencyViewType(@NonNull String viewId, String label) {
119 fLatencyViewId = viewId;
120 fLatencyViewLabel = label;
121 }
122
123 /**
124 * Get the ID of the latency view
125 *
126 * @return The ID
127 */
128 public String getViewId() {
129 return fLatencyViewId;
130 }
131
132 /**
133 * Get the label of the view
134 *
135 * @return The label
136 */
137 public String getLabel() {
138 return fLatencyViewLabel;
139 }
140 }
141
142 @Override
143 public void moduleCreated(IAnalysisModule module) {
144 Map<String, File> files = XmlUtils.listFiles();
145 for (File xmlFile : files.values()) {
146 if (!XmlUtils.xmlValidate(xmlFile).isOK()) {
147 continue;
148 }
149
150 try {
151 /* Load the XML File */
152 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
153 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
154 Document doc = dBuilder.parse(xmlFile);
155 doc.getDocumentElement().normalize();
156
157 /* get state provider views if the analysis has state systems */
158 if (module instanceof ITmfAnalysisModuleWithStateSystems) {
159 for (ViewType viewType : ViewType.values()) {
160 NodeList ssViewNodes = doc.getElementsByTagName(viewType.getXmlElem());
161 for (int i = 0; i < ssViewNodes.getLength(); i++) {
162 Element node = (Element) ssViewNodes.item(i);
163
164 /* Check if analysis is the right one */
165 List<Element> headNodes = XmlUtils.getChildElements(node, TmfXmlStrings.HEAD);
166 if (headNodes.size() != 1) {
167 continue;
168 }
169
170 List<Element> analysisNodes = XmlUtils.getChildElements(headNodes.get(0), TmfXmlStrings.ANALYSIS);
171 for (Element analysis : analysisNodes) {
172 String analysisId = analysis.getAttribute(TmfXmlStrings.ID);
173 if (analysisId.equals(module.getId())) {
174 String viewId = viewType.getViewId();
175 IAnalysisOutput output = new TmfXmlViewOutput(viewId, viewType);
176 output.setOutputProperty(TmfXmlUiStrings.XML_OUTPUT_DATA, node.getAttribute(TmfXmlStrings.ID) + DATA_SEPARATOR + xmlFile.getAbsolutePath(), false);
177 module.registerOutput(output);
178 }
179 }
180 }
181 }
182 }
183
184 // Add the latency views for pattern analysis
185 if (module instanceof XmlPatternAnalysis) {
186 for (LatencyViewType viewType : LatencyViewType.values()) {
187 IAnalysisOutput output = new TmfXmlLatencyViewOutput(viewType.getViewId(), viewType.getLabel());
188 output.setOutputProperty(TmfXmlUiStrings.XML_LATENCY_OUTPUT_DATA, module.getId() + DATA_SEPARATOR + viewType.getLabel(), false);
189 module.registerOutput(output);
190 }
191 }
192
193 } catch (ParserConfigurationException | SAXException | IOException e) {
194 Activator.logError("Error opening XML file", e); //$NON-NLS-1$
195 }
196 }
197 }
198
199 }
This page took 0.035764 seconds and 5 git commands to generate.