blob: 7f9e9123178e07c8bd398609f3d19d9098794cb9 [file] [log] [blame]
David Wagnerb76c9d62014-02-05 18:30:24 +01001/*
2 * Copyright (c) 2011-2014, Intel Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Patrick Benavoli68a91282011-08-31 11:23:23 +020029 */
30#include "SelectionCriterionRule.h"
31#include "SelectionCriterion.h"
32#include "XmlDomainSerializingContext.h"
33#include "SelectionCriteriaDefinition.h"
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020034#include "SelectionCriterionTypeInterface.h"
Patrick Benavoli0bd50542011-11-29 11:10:27 +010035#include "RuleParser.h"
Patrick Benavoli68a91282011-08-31 11:23:23 +020036#include <assert.h>
37
38#define base CRule
39
Patrick Benavoli592ae562011-09-05 16:53:58 +020040const CSelectionCriterionRule::SMatchingRuleDescription CSelectionCriterionRule::_astMatchesWhen[CSelectionCriterionRule::ENbMatchesWhen] = {
Patrick Benavoli082dd472011-11-07 19:46:00 +010041 { "Is", true },
42 { "IsNot", true },
43 { "Includes", false },
44 { "Excludes", false }
Patrick Benavoli68a91282011-08-31 11:23:23 +020045};
46
47CSelectionCriterionRule::CSelectionCriterionRule() : _pSelectionCriterion(NULL), _eMatchesWhen(CSelectionCriterionRule::EIs), _iMatchValue(0)
48{
49}
50
51// Class kind
52string CSelectionCriterionRule::getKind() const
53{
54 return "SelectionCriterionRule";
55}
56
Patrick Benavoli0bd50542011-11-29 11:10:27 +010057// Content dumping
58void CSelectionCriterionRule::logValue(string& strValue, CErrorContext& errorContext) const
59{
60 (void)errorContext;
61
62 // Dump rule
63 dump(strValue);
64}
65
66// Parse
67bool CSelectionCriterionRule::parse(CRuleParser& ruleParser, string& strError)
68{
69 // Criterion
70 _pSelectionCriterion = ruleParser.getSelectionCriteriaDefinition()->getSelectionCriterion(ruleParser.getType());
71
72 // Check existence
73 if (!_pSelectionCriterion) {
74
75 strError = "Couldn't find selection criterion " + ruleParser.getType();
76
77 return false;
78 }
79
80 // Verb
81 string strMatchesWhen;
82
83 if (!ruleParser.next(strMatchesWhen, strError)) {
84
85 return false;
86 }
87 // Value
88 string strValue;
89
90 if (!ruleParser.next(strValue, strError)) {
91
92 return false;
93 }
94
95 // Matches when
96 if (!setMatchesWhen(strMatchesWhen, strError)) {
97
98 strError = "Verb error: " + strError;
99
100 return false;
101 }
102
103 // Value
104 if (!_pSelectionCriterion->getCriterionType()->getNumericalValue(strValue, _iMatchValue)) {
105
106 strError = "Value error: " + strError;
107
108 return false;
109 }
110
111 return true;
112}
113
114// Dump
115void CSelectionCriterionRule::dump(string& strResult) const
116{
117 // Criterion
118 strResult += _pSelectionCriterion->getName();
119 strResult += " ";
120 // Verb
121 strResult += _astMatchesWhen[_eMatchesWhen].pcMatchesWhen;
122 strResult += " ";
123 // Value
124 string strValue;
125 _pSelectionCriterion->getCriterionType()->getLiteralValue(_iMatchValue, strValue);
126 strResult += strValue;
127}
128
Patrick Benavoli68a91282011-08-31 11:23:23 +0200129// Rule check
130bool CSelectionCriterionRule::matches() const
131{
132 assert(_pSelectionCriterion);
133
134 switch(_eMatchesWhen) {
135 case EIs:
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200136 return _pSelectionCriterion->is(_iMatchValue);
137 case EIsNot:
138 return _pSelectionCriterion->isNot(_iMatchValue);
139 case EIncludes:
140 return _pSelectionCriterion->includes(_iMatchValue);
141 case EExcludes:
142 return _pSelectionCriterion->excludes(_iMatchValue);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200143 default:
144 assert(0);
145 return false;
146 }
147}
148
149// From IXmlSink
150bool CSelectionCriterionRule::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
151{
152 // Retrieve actual context
153 CXmlDomainSerializingContext& xmlDomainSerializingContext = static_cast<CXmlDomainSerializingContext&>(serializingContext);
154
155 // Get selection criterion
156 string strSelectionCriterion = xmlElement.getAttributeString("SelectionCriterion");
157
158 _pSelectionCriterion = xmlDomainSerializingContext.getSelectionCriteriaDefinition()->getSelectionCriterion(strSelectionCriterion);
159
160 // Check existence
161 if (!_pSelectionCriterion) {
162
163 xmlDomainSerializingContext.setError("Couldn't find selection criterion " + strSelectionCriterion + " in " + getKind() + " " + xmlElement.getPath());
164
165 return false;
166 }
167
168 // Get MatchesWhen
169 string strMatchesWhen = xmlElement.getAttributeString("MatchesWhen");
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200170 string strError;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200171
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200172 if (!setMatchesWhen(strMatchesWhen, strError)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200173
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200174 xmlDomainSerializingContext.setError("Wrong MatchesWhen attribute " + strMatchesWhen + " in " + getKind() + " " + xmlElement.getPath() + ": " + strError);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200175
176 return false;
177 }
178
179 // Get Value
180 string strValue = xmlElement.getAttributeString("Value");
181
182 if (!_pSelectionCriterion->getCriterionType()->getNumericalValue(strValue, _iMatchValue)) {
183
184 xmlDomainSerializingContext.setError("Wrong Value attribute value " + strValue + " in " + getKind() + " " + xmlElement.getPath());
185
186 return false;
187 }
188
189 // Done
190 return true;
191}
192
193// From IXmlSource
194void CSelectionCriterionRule::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
195{
196 (void)serializingContext;
197
198 assert(_pSelectionCriterion);
199
200 // Set selection criterion
201 xmlElement.setAttributeString("SelectionCriterion", _pSelectionCriterion->getName());
202
203 // Set MatchesWhen
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200204 xmlElement.setAttributeString("MatchesWhen", _astMatchesWhen[_eMatchesWhen].pcMatchesWhen);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200205
206 // Set Value
207 string strValue;
208
209 _pSelectionCriterion->getCriterionType()->getLiteralValue(_iMatchValue, strValue);
210
211 xmlElement.setAttributeString("Value", strValue);
212}
213
214// XML MatchesWhen attribute parsing
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200215bool CSelectionCriterionRule::setMatchesWhen(const string& strMatchesWhen, string& strError)
Patrick Benavoli68a91282011-08-31 11:23:23 +0200216{
217 uint32_t uiMatchesWhen;
218
219 for (uiMatchesWhen = 0; uiMatchesWhen < ENbMatchesWhen; uiMatchesWhen++) {
220
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200221 const SMatchingRuleDescription* pstMatchingRuleDescription = &_astMatchesWhen[uiMatchesWhen];
222
223 if (strMatchesWhen == pstMatchingRuleDescription->pcMatchesWhen) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200224
225 // Found it!
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200226
227 // Get Type
228 const ISelectionCriterionTypeInterface* pSelectionCriterionType = _pSelectionCriterion->getCriterionType();
229
230 // Check compatibility if relevant
Patrick Benavoli082dd472011-11-07 19:46:00 +0100231 if (!pSelectionCriterionType->isTypeInclusive() && !pstMatchingRuleDescription->bExclusiveTypeCompatible) {
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200232
Patrick Benavoli082dd472011-11-07 19:46:00 +0100233 strError = "Value incompatible with exclusive kind of type";
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200234
235 return false;
236 }
237
238 // Store
Patrick Benavoli68a91282011-08-31 11:23:23 +0200239 _eMatchesWhen = (MatchesWhen)uiMatchesWhen;
240
241 return true;
242 }
243 }
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200244
245 strError = "Value not found";
246
Patrick Benavoli68a91282011-08-31 11:23:23 +0200247 return false;
248}