blob: 6f350c3e20294a3f8447c7a3a23bb2c711b374ae [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
Sebastien Gonzalved9526492014-02-20 22:28:03 +010040using std::string;
41
Patrick Benavoli592ae562011-09-05 16:53:58 +020042const CSelectionCriterionRule::SMatchingRuleDescription CSelectionCriterionRule::_astMatchesWhen[CSelectionCriterionRule::ENbMatchesWhen] = {
Patrick Benavoli082dd472011-11-07 19:46:00 +010043 { "Is", true },
44 { "IsNot", true },
45 { "Includes", false },
46 { "Excludes", false }
Patrick Benavoli68a91282011-08-31 11:23:23 +020047};
48
49CSelectionCriterionRule::CSelectionCriterionRule() : _pSelectionCriterion(NULL), _eMatchesWhen(CSelectionCriterionRule::EIs), _iMatchValue(0)
50{
51}
52
53// Class kind
54string CSelectionCriterionRule::getKind() const
55{
56 return "SelectionCriterionRule";
57}
58
Patrick Benavoli0bd50542011-11-29 11:10:27 +010059// Content dumping
60void CSelectionCriterionRule::logValue(string& strValue, CErrorContext& errorContext) const
61{
62 (void)errorContext;
63
64 // Dump rule
65 dump(strValue);
66}
67
68// Parse
69bool CSelectionCriterionRule::parse(CRuleParser& ruleParser, string& strError)
70{
71 // Criterion
72 _pSelectionCriterion = ruleParser.getSelectionCriteriaDefinition()->getSelectionCriterion(ruleParser.getType());
73
74 // Check existence
75 if (!_pSelectionCriterion) {
76
77 strError = "Couldn't find selection criterion " + ruleParser.getType();
78
79 return false;
80 }
81
82 // Verb
83 string strMatchesWhen;
84
85 if (!ruleParser.next(strMatchesWhen, strError)) {
86
87 return false;
88 }
89 // Value
90 string strValue;
91
92 if (!ruleParser.next(strValue, strError)) {
93
94 return false;
95 }
96
97 // Matches when
98 if (!setMatchesWhen(strMatchesWhen, strError)) {
99
100 strError = "Verb error: " + strError;
101
102 return false;
103 }
104
105 // Value
106 if (!_pSelectionCriterion->getCriterionType()->getNumericalValue(strValue, _iMatchValue)) {
107
108 strError = "Value error: " + strError;
109
110 return false;
111 }
112
113 return true;
114}
115
116// Dump
117void CSelectionCriterionRule::dump(string& strResult) const
118{
119 // Criterion
120 strResult += _pSelectionCriterion->getName();
121 strResult += " ";
122 // Verb
123 strResult += _astMatchesWhen[_eMatchesWhen].pcMatchesWhen;
124 strResult += " ";
125 // Value
126 string strValue;
127 _pSelectionCriterion->getCriterionType()->getLiteralValue(_iMatchValue, strValue);
128 strResult += strValue;
129}
130
Patrick Benavoli68a91282011-08-31 11:23:23 +0200131// Rule check
132bool CSelectionCriterionRule::matches() const
133{
134 assert(_pSelectionCriterion);
135
136 switch(_eMatchesWhen) {
137 case EIs:
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200138 return _pSelectionCriterion->is(_iMatchValue);
139 case EIsNot:
140 return _pSelectionCriterion->isNot(_iMatchValue);
141 case EIncludes:
142 return _pSelectionCriterion->includes(_iMatchValue);
143 case EExcludes:
144 return _pSelectionCriterion->excludes(_iMatchValue);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200145 default:
146 assert(0);
147 return false;
148 }
149}
150
151// From IXmlSink
152bool CSelectionCriterionRule::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
153{
154 // Retrieve actual context
155 CXmlDomainSerializingContext& xmlDomainSerializingContext = static_cast<CXmlDomainSerializingContext&>(serializingContext);
156
157 // Get selection criterion
158 string strSelectionCriterion = xmlElement.getAttributeString("SelectionCriterion");
159
160 _pSelectionCriterion = xmlDomainSerializingContext.getSelectionCriteriaDefinition()->getSelectionCriterion(strSelectionCriterion);
161
162 // Check existence
163 if (!_pSelectionCriterion) {
164
165 xmlDomainSerializingContext.setError("Couldn't find selection criterion " + strSelectionCriterion + " in " + getKind() + " " + xmlElement.getPath());
166
167 return false;
168 }
169
170 // Get MatchesWhen
171 string strMatchesWhen = xmlElement.getAttributeString("MatchesWhen");
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200172 string strError;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200173
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200174 if (!setMatchesWhen(strMatchesWhen, strError)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200175
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200176 xmlDomainSerializingContext.setError("Wrong MatchesWhen attribute " + strMatchesWhen + " in " + getKind() + " " + xmlElement.getPath() + ": " + strError);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200177
178 return false;
179 }
180
181 // Get Value
182 string strValue = xmlElement.getAttributeString("Value");
183
184 if (!_pSelectionCriterion->getCriterionType()->getNumericalValue(strValue, _iMatchValue)) {
185
186 xmlDomainSerializingContext.setError("Wrong Value attribute value " + strValue + " in " + getKind() + " " + xmlElement.getPath());
187
188 return false;
189 }
190
191 // Done
192 return true;
193}
194
195// From IXmlSource
196void CSelectionCriterionRule::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
197{
198 (void)serializingContext;
199
200 assert(_pSelectionCriterion);
201
202 // Set selection criterion
203 xmlElement.setAttributeString("SelectionCriterion", _pSelectionCriterion->getName());
204
205 // Set MatchesWhen
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200206 xmlElement.setAttributeString("MatchesWhen", _astMatchesWhen[_eMatchesWhen].pcMatchesWhen);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200207
208 // Set Value
209 string strValue;
210
211 _pSelectionCriterion->getCriterionType()->getLiteralValue(_iMatchValue, strValue);
212
213 xmlElement.setAttributeString("Value", strValue);
214}
215
216// XML MatchesWhen attribute parsing
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200217bool CSelectionCriterionRule::setMatchesWhen(const string& strMatchesWhen, string& strError)
Patrick Benavoli68a91282011-08-31 11:23:23 +0200218{
219 uint32_t uiMatchesWhen;
220
221 for (uiMatchesWhen = 0; uiMatchesWhen < ENbMatchesWhen; uiMatchesWhen++) {
222
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200223 const SMatchingRuleDescription* pstMatchingRuleDescription = &_astMatchesWhen[uiMatchesWhen];
224
225 if (strMatchesWhen == pstMatchingRuleDescription->pcMatchesWhen) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200226
227 // Found it!
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200228
229 // Get Type
230 const ISelectionCriterionTypeInterface* pSelectionCriterionType = _pSelectionCriterion->getCriterionType();
231
232 // Check compatibility if relevant
Patrick Benavoli082dd472011-11-07 19:46:00 +0100233 if (!pSelectionCriterionType->isTypeInclusive() && !pstMatchingRuleDescription->bExclusiveTypeCompatible) {
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200234
Patrick Benavoli082dd472011-11-07 19:46:00 +0100235 strError = "Value incompatible with exclusive kind of type";
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200236
237 return false;
238 }
239
240 // Store
Patrick Benavoli68a91282011-08-31 11:23:23 +0200241 _eMatchesWhen = (MatchesWhen)uiMatchesWhen;
242
243 return true;
244 }
245 }
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200246
247 strError = "Value not found";
248
Patrick Benavoli68a91282011-08-31 11:23:23 +0200249 return false;
250}