1 /*
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.
29  */
30 
31 #include "SelectionCriterion.h"
32 #include "AutoLog.h"
33 #include "Utility.h"
34 
35 #define base CElement
36 
CSelectionCriterion(const std::string & strName,const CSelectionCriterionType * pType)37 CSelectionCriterion::CSelectionCriterion(const std::string& strName, const CSelectionCriterionType* pType) : base(strName), _iState(0), _pType(pType), _uiNbModifications(0)
38 {
39 }
40 
getKind() const41 std::string CSelectionCriterion::getKind() const
42 {
43     return "SelectionCriterion";
44 }
45 
hasBeenModified() const46 bool CSelectionCriterion::hasBeenModified() const
47 {
48     return _uiNbModifications != 0;
49 }
50 
resetModifiedStatus()51 void CSelectionCriterion::resetModifiedStatus()
52 {
53     _uiNbModifications = 0;
54 }
55 
56 /// From ISelectionCriterionInterface
57 // State
setCriterionState(int iState)58 void CSelectionCriterion::setCriterionState(int iState)
59 {
60     // Check for a change
61     if (_iState != iState) {
62 
63         _iState = iState;
64 
65         log_info("Selection criterion changed event: %s", getFormattedDescription(false, false).c_str());
66 
67         // Check if the previous criterion value has been taken into account (i.e. at least one Configuration was applied
68         // since the last criterion change)
69         if (_uiNbModifications != 0) {
70 
71             log_warning("Selection criterion \"%s\" has been modified %d time(s) without any configuration application", getName().c_str(), _uiNbModifications);
72         }
73 
74         // Track the number of modifications for this criterion
75         _uiNbModifications++;
76     }
77 }
78 
getCriterionState() const79 int CSelectionCriterion::getCriterionState() const
80 {
81     return _iState;
82 }
83 
84 // Name
getCriterionName() const85 std::string CSelectionCriterion::getCriterionName() const
86 {
87     return getName();
88 }
89 
90 // Type
getCriterionType() const91 const ISelectionCriterionTypeInterface* CSelectionCriterion::getCriterionType() const
92 {
93     return _pType;
94 }
95 
96 /// Match methods
is(int iState) const97 bool CSelectionCriterion::is(int iState) const
98 {
99     return _iState == iState;
100 }
101 
isNot(int iState) const102 bool CSelectionCriterion::isNot(int iState) const
103 {
104     return _iState != iState;
105 }
106 
includes(int iState) const107 bool CSelectionCriterion::includes(int iState) const
108 {
109     // For inclusive criterion, Includes checks if ALL the bit sets in iState are set in the
110     // current _iState.
111     return (_iState & iState) == iState;
112 }
113 
excludes(int iState) const114 bool CSelectionCriterion::excludes(int iState) const
115 {
116     return (_iState & iState) == 0;
117 }
118 
119 /// User request
getFormattedDescription(bool bWithTypeInfo,bool bHumanReadable) const120 std::string CSelectionCriterion::getFormattedDescription(bool bWithTypeInfo, bool bHumanReadable) const
121 {
122     std::string strFormattedDescription;
123 
124     if (bHumanReadable) {
125 
126         if (bWithTypeInfo) {
127 
128             // Display type info
129             CUtility::appendTitle(strFormattedDescription, getName() + ":");
130 
131             // States
132             strFormattedDescription += "Possible states ";
133 
134             // Type Kind
135             strFormattedDescription += "(";
136             strFormattedDescription += _pType->isTypeInclusive() ? "Inclusive" : "Exclusive";
137             strFormattedDescription += "): ";
138 
139             // States
140             strFormattedDescription += _pType->listPossibleValues() + "\n";
141 
142             // Current State
143             strFormattedDescription += "Current state";
144         } else {
145             // Name only
146             strFormattedDescription = getName();
147         }
148 
149         // Current State
150         strFormattedDescription += " = " + _pType->getFormattedState(_iState);
151     } else {
152         // Name
153         strFormattedDescription = "Criterion name: " + getName();
154 
155         if (bWithTypeInfo) {
156             // Type Kind
157             strFormattedDescription += ", type kind: ";
158             strFormattedDescription +=  _pType->isTypeInclusive() ? "inclusive" : "exclusive";
159         }
160 
161         // Current State
162         strFormattedDescription += ", current state: " +
163                                    _pType->getFormattedState(_iState);
164 
165          if (bWithTypeInfo) {
166             // States
167             strFormattedDescription += ", states: " +
168                                        _pType->listPossibleValues();
169         }
170     }
171     return strFormattedDescription;
172 }
173 
174 // XML export
toXml(CXmlElement & xmlElement,CXmlSerializingContext & serializingContext) const175 void CSelectionCriterion::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
176 {
177     // Current Value
178     xmlElement.setAttributeString("Value", _pType->getFormattedState(_iState));
179 
180     // Serialize Type node
181     _pType->toXml(xmlElement, serializingContext);
182 
183     base::toXml(xmlElement, serializingContext);
184 }
185