1 /*
2  * Copyright (c) 2011-2015, 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 "Utility.h"
33 #include <log/Logger.h>
34 
35 #define base CElement
36 
37 using namespace core;
38 
CSelectionCriterion(const std::string & strName,const CSelectionCriterionType * pType,core::log::Logger & logger)39 CSelectionCriterion::CSelectionCriterion(const std::string &strName,
40                                          const CSelectionCriterionType *pType,
41                                          core::log::Logger &logger)
42     : base(strName), _pType(pType), _logger(logger)
43 {
44 }
45 
getKind() const46 std::string CSelectionCriterion::getKind() const
47 {
48     return "SelectionCriterion";
49 }
50 
hasBeenModified() const51 bool CSelectionCriterion::hasBeenModified() const
52 {
53     return _uiNbModifications != 0;
54 }
55 
resetModifiedStatus()56 void CSelectionCriterion::resetModifiedStatus()
57 {
58     _uiNbModifications = 0;
59 }
60 
61 /// From ISelectionCriterionInterface
62 // State
setCriterionState(int iState)63 void CSelectionCriterion::setCriterionState(int iState)
64 {
65     // Check for a change
66     if (_iState != iState) {
67 
68         _iState = iState;
69 
70         _logger.info() << "Selection criterion changed event: "
71                        << getFormattedDescription(false, false);
72 
73         // Check if the previous criterion value has been taken into account (i.e. at least one
74         // Configuration was applied
75         // since the last criterion change)
76         if (_uiNbModifications != 0) {
77 
78             _logger.warning() << "Selection criterion '" << getName() << "' has been modified "
79                               << _uiNbModifications
80                               << " time(s) without any configuration application";
81         }
82 
83         // Track the number of modifications for this criterion
84         _uiNbModifications++;
85     }
86 }
87 
getCriterionState() const88 int CSelectionCriterion::getCriterionState() const
89 {
90     return _iState;
91 }
92 
93 // Name
getCriterionName() const94 std::string CSelectionCriterion::getCriterionName() const
95 {
96     return getName();
97 }
98 
99 // Type
getCriterionType() const100 const ISelectionCriterionTypeInterface *CSelectionCriterion::getCriterionType() const
101 {
102     return _pType;
103 }
104 
105 /// Match methods
is(int iState) const106 bool CSelectionCriterion::is(int iState) const
107 {
108     return _iState == iState;
109 }
110 
isNot(int iState) const111 bool CSelectionCriterion::isNot(int iState) const
112 {
113     return _iState != iState;
114 }
115 
includes(int iState) const116 bool CSelectionCriterion::includes(int iState) const
117 {
118     // For inclusive criterion, Includes checks if ALL the bit sets in iState are set in the
119     // current _iState.
120     return (_iState & iState) == iState;
121 }
122 
excludes(int iState) const123 bool CSelectionCriterion::excludes(int iState) const
124 {
125     return (_iState & iState) == 0;
126 }
127 
128 /// User request
getFormattedDescription(bool bWithTypeInfo,bool bHumanReadable) const129 std::string CSelectionCriterion::getFormattedDescription(bool bWithTypeInfo,
130                                                          bool bHumanReadable) const
131 {
132     std::string strFormattedDescription;
133 
134     if (bHumanReadable) {
135 
136         if (bWithTypeInfo) {
137 
138             // Display type info
139             utility::appendTitle(strFormattedDescription, getName() + ":");
140 
141             // States
142             strFormattedDescription += "Possible states ";
143 
144             // Type Kind
145             strFormattedDescription += "(";
146             strFormattedDescription += _pType->isTypeInclusive() ? "Inclusive" : "Exclusive";
147             strFormattedDescription += "): ";
148 
149             // States
150             strFormattedDescription += _pType->listPossibleValues() + "\n";
151 
152             // Current State
153             strFormattedDescription += "Current state";
154         } else {
155             // Name only
156             strFormattedDescription = getName();
157         }
158 
159         // Current State
160         strFormattedDescription += " = " + _pType->getFormattedState(_iState);
161     } else {
162         // Name
163         strFormattedDescription = "Criterion name: " + getName();
164 
165         if (bWithTypeInfo) {
166             // Type Kind
167             strFormattedDescription += ", type kind: ";
168             strFormattedDescription += _pType->isTypeInclusive() ? "inclusive" : "exclusive";
169         }
170 
171         // Current State
172         strFormattedDescription += ", current state: " + _pType->getFormattedState(_iState);
173 
174         if (bWithTypeInfo) {
175             // States
176             strFormattedDescription += ", states: " + _pType->listPossibleValues();
177         }
178     }
179     return strFormattedDescription;
180 }
181 
182 // XML export
toXml(CXmlElement & xmlElement,CXmlSerializingContext & serializingContext) const183 void CSelectionCriterion::toXml(CXmlElement &xmlElement,
184                                 CXmlSerializingContext &serializingContext) const
185 {
186     // Current Value
187     xmlElement.setAttribute("Value", _pType->getFormattedState(_iState));
188 
189     // Serialize Type node
190     _pType->toXml(xmlElement, serializingContext);
191 
192     base::toXml(xmlElement, serializingContext);
193 }
194