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 | ******************************************************************************/ | |
6eca054d | 9 | package org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model; |
3a5f73a1 JCK |
10 | |
11 | import java.util.ArrayList; | |
12 | import java.util.Arrays; | |
13 | import java.util.List; | |
14 | import java.util.Map; | |
15 | import java.util.regex.Pattern; | |
16 | ||
17 | import org.eclipse.jdt.annotation.NonNull; | |
18 | import org.eclipse.jdt.annotation.Nullable; | |
6eca054d | 19 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; |
3a5f73a1 JCK |
20 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
21 | import org.w3c.dom.Element; | |
22 | ||
23 | /** | |
24 | * Implementation of a basic transition in the XML file | |
25 | * | |
26 | * @author Jean-Christian Kouame | |
3a5f73a1 JCK |
27 | */ |
28 | public class TmfXmlBasicTransition { | |
29 | ||
30 | private static final Pattern WILDCARD_PATTERN = Pattern.compile("\\*"); //$NON-NLS-1$ | |
31 | ||
32 | private final List<String> fCond; | |
33 | private final List<Pattern> fAcceptedEvents; | |
34 | ||
35 | ||
36 | /** | |
37 | * Constructor | |
38 | * | |
39 | * @param element | |
40 | * the XML basic transition element | |
41 | */ | |
42 | public TmfXmlBasicTransition(Element element) { | |
43 | final @NonNull String events = element.getAttribute(TmfXmlStrings.EVENT); | |
44 | fAcceptedEvents = new ArrayList<>(); | |
72060c02 JCK |
45 | if (!events.isEmpty()) { |
46 | for (String eventName : Arrays.asList(events.split(TmfXmlStrings.OR_SEPARATOR))) { | |
47 | String name = WILDCARD_PATTERN.matcher(eventName).replaceAll(".*"); //$NON-NLS-1$ | |
48 | fAcceptedEvents.add(Pattern.compile(name)); | |
49 | } | |
3a5f73a1 JCK |
50 | } |
51 | final @NonNull String conditions = element.getAttribute(TmfXmlStrings.COND); | |
52 | fCond = conditions.isEmpty() ? new ArrayList<>() : Arrays.asList(conditions.split(TmfXmlStrings.AND_SEPARATOR)); | |
53 | } | |
54 | ||
55 | /** | |
56 | * Validate the transition with the current event | |
57 | * | |
58 | * @param event | |
59 | * The active event | |
60 | * @param scenarioInfo | |
61 | * The active scenario details. | |
62 | * @param tests | |
63 | * The map of test in the XML file | |
64 | * @return true if the transition is validate false if not | |
65 | */ | |
66 | public boolean test(ITmfEvent event, @Nullable TmfXmlScenarioInfo scenarioInfo, Map<String, TmfXmlTransitionValidator> tests) { | |
67 | if (!validateEvent(event)) { | |
68 | return false; | |
69 | } | |
70 | ||
71 | for (String cond : fCond) { | |
72 | TmfXmlTransitionValidator test = tests.get(cond); | |
73 | if (test == null) { | |
74 | throw new IllegalStateException("Failed to find cond " + cond); //$NON-NLS-1$ | |
75 | } | |
76 | if (!test.test(event, scenarioInfo)) { | |
77 | return false; | |
78 | } | |
79 | } | |
80 | return true; | |
81 | } | |
82 | ||
83 | private boolean validateEvent(ITmfEvent event) { | |
84 | String eventName = event.getName(); | |
85 | ||
72060c02 JCK |
86 | if (fAcceptedEvents.isEmpty()) { |
87 | return true; | |
88 | } | |
3a5f73a1 JCK |
89 | /* |
90 | * This validates the event name with the accepted regular expressions | |
91 | */ | |
92 | for (Pattern nameRegex : fAcceptedEvents) { | |
93 | if (nameRegex.matcher(eventName).matches()) { | |
94 | return true; | |
95 | } | |
96 | } | |
97 | return false; | |
98 | } | |
99 | } |