blob: bb353bd013af421655787dac24e4d3afd8683647 [file] [log] [blame]
Patrick Benavoli68a91282011-08-31 11:23:23 +02001/* <auto_header>
2 * <FILENAME>
3 *
4 * INTEL CONFIDENTIAL
5 * Copyright © 2011 Intel
6 * Corporation All Rights Reserved.
7 *
8 * The source code contained or described herein and all documents related to
9 * the source code ("Material") are owned by Intel Corporation or its suppliers
10 * or licensors. Title to the Material remains with Intel Corporation or its
11 * suppliers and licensors. The Material contains trade secrets and proprietary
12 * and confidential information of Intel or its suppliers and licensors. The
13 * Material is protected by worldwide copyright and trade secret laws and
14 * treaty provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed, or
16 * disclosed in any way without Intel’s prior express written permission.
17 *
18 * No license under any patent, copyright, trade secret or other intellectual
19 * property right is granted to or conferred upon you by disclosure or delivery
20 * of the Materials, either expressly, by implication, inducement, estoppel or
21 * otherwise. Any license under such intellectual property rights must be
22 * express and approved by Intel in writing.
23 *
24 * AUTHOR: Patrick Benavoli (patrickx.benavoli@intel.com)
25 * CREATED: 2011-06-01
26 * UPDATED: 2011-07-27
27 *
28 *
29 * </auto_header>
30 */
31#include "SelectionCriterionRule.h"
32#include "SelectionCriterion.h"
33#include "XmlDomainSerializingContext.h"
34#include "SelectionCriteriaDefinition.h"
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020035#include "SelectionCriterionTypeInterface.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
57// Rule check
58bool CSelectionCriterionRule::matches() const
59{
60 assert(_pSelectionCriterion);
61
62 switch(_eMatchesWhen) {
63 case EIs:
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020064 return _pSelectionCriterion->is(_iMatchValue);
65 case EIsNot:
66 return _pSelectionCriterion->isNot(_iMatchValue);
67 case EIncludes:
68 return _pSelectionCriterion->includes(_iMatchValue);
69 case EExcludes:
70 return _pSelectionCriterion->excludes(_iMatchValue);
Patrick Benavoli68a91282011-08-31 11:23:23 +020071 default:
72 assert(0);
73 return false;
74 }
75}
76
77// From IXmlSink
78bool CSelectionCriterionRule::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
79{
80 // Retrieve actual context
81 CXmlDomainSerializingContext& xmlDomainSerializingContext = static_cast<CXmlDomainSerializingContext&>(serializingContext);
82
83 // Get selection criterion
84 string strSelectionCriterion = xmlElement.getAttributeString("SelectionCriterion");
85
86 _pSelectionCriterion = xmlDomainSerializingContext.getSelectionCriteriaDefinition()->getSelectionCriterion(strSelectionCriterion);
87
88 // Check existence
89 if (!_pSelectionCriterion) {
90
91 xmlDomainSerializingContext.setError("Couldn't find selection criterion " + strSelectionCriterion + " in " + getKind() + " " + xmlElement.getPath());
92
93 return false;
94 }
95
96 // Get MatchesWhen
97 string strMatchesWhen = xmlElement.getAttributeString("MatchesWhen");
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020098 string strError;
Patrick Benavoli68a91282011-08-31 11:23:23 +020099
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200100 if (!setMatchesWhen(strMatchesWhen, strError)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200101
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200102 xmlDomainSerializingContext.setError("Wrong MatchesWhen attribute " + strMatchesWhen + " in " + getKind() + " " + xmlElement.getPath() + ": " + strError);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200103
104 return false;
105 }
106
107 // Get Value
108 string strValue = xmlElement.getAttributeString("Value");
109
110 if (!_pSelectionCriterion->getCriterionType()->getNumericalValue(strValue, _iMatchValue)) {
111
112 xmlDomainSerializingContext.setError("Wrong Value attribute value " + strValue + " in " + getKind() + " " + xmlElement.getPath());
113
114 return false;
115 }
116
117 // Done
118 return true;
119}
120
121// From IXmlSource
122void CSelectionCriterionRule::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
123{
124 (void)serializingContext;
125
126 assert(_pSelectionCriterion);
127
128 // Set selection criterion
129 xmlElement.setAttributeString("SelectionCriterion", _pSelectionCriterion->getName());
130
131 // Set MatchesWhen
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200132 xmlElement.setAttributeString("MatchesWhen", _astMatchesWhen[_eMatchesWhen].pcMatchesWhen);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200133
134 // Set Value
135 string strValue;
136
137 _pSelectionCriterion->getCriterionType()->getLiteralValue(_iMatchValue, strValue);
138
139 xmlElement.setAttributeString("Value", strValue);
140}
141
142// XML MatchesWhen attribute parsing
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200143bool CSelectionCriterionRule::setMatchesWhen(const string& strMatchesWhen, string& strError)
Patrick Benavoli68a91282011-08-31 11:23:23 +0200144{
145 uint32_t uiMatchesWhen;
146
147 for (uiMatchesWhen = 0; uiMatchesWhen < ENbMatchesWhen; uiMatchesWhen++) {
148
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200149 const SMatchingRuleDescription* pstMatchingRuleDescription = &_astMatchesWhen[uiMatchesWhen];
150
151 if (strMatchesWhen == pstMatchingRuleDescription->pcMatchesWhen) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200152
153 // Found it!
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200154
155 // Get Type
156 const ISelectionCriterionTypeInterface* pSelectionCriterionType = _pSelectionCriterion->getCriterionType();
157
158 // Check compatibility if relevant
Patrick Benavoli082dd472011-11-07 19:46:00 +0100159 if (!pSelectionCriterionType->isTypeInclusive() && !pstMatchingRuleDescription->bExclusiveTypeCompatible) {
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200160
Patrick Benavoli082dd472011-11-07 19:46:00 +0100161 strError = "Value incompatible with exclusive kind of type";
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200162
163 return false;
164 }
165
166 // Store
Patrick Benavoli68a91282011-08-31 11:23:23 +0200167 _eMatchesWhen = (MatchesWhen)uiMatchesWhen;
168
169 return true;
170 }
171 }
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200172
173 strError = "Value not found";
174
Patrick Benavoli68a91282011-08-31 11:23:23 +0200175 return false;
176}