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.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; | |
19 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; | |
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 | |
27 | * @since 2.0 | |
28 | */ | |
29 | public class TmfXmlBasicTransition { | |
30 | ||
31 | private static final Pattern WILDCARD_PATTERN = Pattern.compile("\\*"); //$NON-NLS-1$ | |
32 | ||
33 | private final List<String> fCond; | |
34 | private final List<Pattern> fAcceptedEvents; | |
35 | ||
36 | ||
37 | /** | |
38 | * Constructor | |
39 | * | |
40 | * @param element | |
41 | * the XML basic transition element | |
42 | */ | |
43 | public TmfXmlBasicTransition(Element element) { | |
44 | final @NonNull String events = element.getAttribute(TmfXmlStrings.EVENT); | |
45 | fAcceptedEvents = new ArrayList<>(); | |
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 | } | |
50 | final @NonNull String conditions = element.getAttribute(TmfXmlStrings.COND); | |
51 | fCond = conditions.isEmpty() ? new ArrayList<>() : Arrays.asList(conditions.split(TmfXmlStrings.AND_SEPARATOR)); | |
52 | } | |
53 | ||
54 | /** | |
55 | * Validate the transition with the current event | |
56 | * | |
57 | * @param event | |
58 | * The active event | |
59 | * @param scenarioInfo | |
60 | * The active scenario details. | |
61 | * @param tests | |
62 | * The map of test in the XML file | |
63 | * @return true if the transition is validate false if not | |
64 | */ | |
65 | public boolean test(ITmfEvent event, @Nullable TmfXmlScenarioInfo scenarioInfo, Map<String, TmfXmlTransitionValidator> tests) { | |
66 | if (!validateEvent(event)) { | |
67 | return false; | |
68 | } | |
69 | ||
70 | for (String cond : fCond) { | |
71 | TmfXmlTransitionValidator test = tests.get(cond); | |
72 | if (test == null) { | |
73 | throw new IllegalStateException("Failed to find cond " + cond); //$NON-NLS-1$ | |
74 | } | |
75 | if (!test.test(event, scenarioInfo)) { | |
76 | return false; | |
77 | } | |
78 | } | |
79 | return true; | |
80 | } | |
81 | ||
82 | private boolean validateEvent(ITmfEvent event) { | |
83 | String eventName = event.getName(); | |
84 | ||
85 | /* | |
86 | * This validates the event name with the accepted regular expressions | |
87 | */ | |
88 | for (Pattern nameRegex : fAcceptedEvents) { | |
89 | if (nameRegex.matcher(eventName).matches()) { | |
90 | return true; | |
91 | } | |
92 | } | |
93 | return false; | |
94 | } | |
95 | } |