2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // File: Condition.cpp
20 // Created on June 3, 2009, 9:00 AM
24 #include <dpl/log/log.h>
25 #include <dpl/foreach.h>
26 #include <ace/Condition.h>
29 * Check if attribute in condition matches the values obtained from PIP
30 * attrSet - attributes from PIP
33 Attribute::MatchResult Condition::evaluateCondition(
34 const AttributeSet * attrSet) const
36 //Condition may include either matches of attributes or other conditions
37 //in this method all attributes are matched at first and if possible the
38 //condition is evaluated. If evaluation is not possible based solely on
39 //attributes then we start recursion into child conditions.
41 Attribute::MatchResult match;
42 bool undeterminedMatchFound = false;
43 bool isFinalMatch = false;
45 LogDebug("Attributes to be matched");
46 printAttributes(*attrSet);
47 LogDebug("Condition attributes values");
48 printAttributes(attributes);
50 if (this->isEmpty()) {
51 LogDebug("Condition is empty, returning true");
52 //Condition is empty, it means it evaluates to TRUE
53 return Attribute::MatchResult::MRTrue;
56 match = evaluateAttributes(attrSet, isFinalMatch, undeterminedMatchFound);
58 LogDebug("Evaluate attributes returning verdict" ) ; //<< match);
62 match = evaluateChildConditions(attrSet,
64 undeterminedMatchFound);
66 LogDebug("Evaluate child conditions returning verdict" ); // << match);
70 if (undeterminedMatchFound) {
71 //If any child condition/attribute-match was undetermined and
72 //so far we couldn't make a decision then we must return undetermined
73 LogDebug("Evaluate condition returning MRUndetermined");
74 return Attribute::MatchResult::MRUndetermined;
77 if (this->isAndCondition()) {
78 match = Attribute::MatchResult::MRTrue;
79 } else if (this->isOrCondition()) {
80 match = Attribute::MatchResult::MRFalse;
82 Assert(false && "Condition has to be either AND or OR");
87 // KW Attribute::MatchResult Condition::performORalgorithm(const std::set<Attribute>* attrSet) const{
89 // KW Attribute::MatchResult match;
90 // KW bool undeterminedMatchFound = false;
91 // KW bool isFinalMatch = false;
93 // KW LogDebug("Performing OR algorithm");
95 // KW match = evaluateAttributes(attrSet, isFinalMatch, undeterminedMatchFound);
96 // KW if(isFinalMatch){
97 // KW LogDebug("OR algorithm evaluate attributes returning verdict" << match);
101 // KW match = evaluateChildConditions(attrSet, isFinalMatch, undeterminedMatchFound);
102 // KW if(isFinalMatch){
106 // KW if(undeterminedMatchFound){
107 // KW //If any child condition/attribute-match was undetermined and
108 // KW //so far we couldn't make a decision then we must return undetermined
109 // KW LogDebug("OR algorithm returning MRUndetermined");
110 // KW return Attribute::MRUndetermined;
113 // KW LogDebug("OR algorithm returning MRFalse");
114 // KW return Attribute::MRFalse;
117 // KW Attribute::MatchResult Condition::performANDalgorithm(const std::set<Attribute>* attrSet) const{
120 // KW Attribute::MatchResult match;
121 // KW bool undeterminedMatchFound = false;
122 // KW bool isFinalMatch = false;
124 // KW LogDebug("Performing AND algorithm");
125 // KW match = evaluateAttributes(attrSet, isFinalMatch, undeterminedMatchFound);
126 // KW if(isFinalMatch){
127 // KW LogDebug("AND algorithm evaluate attributes returning verdict" << match);
130 // KW match = evaluateChildConditions(attrSet, isFinalMatch, undeterminedMatchFound);
131 // KW if(isFinalMatch){
132 // KW LogDebug("AND algorithm evaluate child returning verdict " << match);
135 // KW if(undeterminedMatchFound){
136 // KW //If any child condition/attribute-match was undetermined and
137 // KW //so far we couldn't make a decision then we must return undetermined
138 // KW LogDebug("AND algorithm returning Undetermined");
139 // KW return Attribute::MRUndetermined;
142 // KW LogDebug("AND algorithm returning MRTrue");
143 // KW return Attribute::MRTrue;
147 Attribute::MatchResult Condition::evaluateAttributes(
148 const AttributeSet * attrSet,
150 bool & undeterminedMatchFound) const
152 Attribute::MatchResult match = Attribute::MatchResult::MRUndetermined;
154 std::list<Attribute>::const_iterator condIt = this->attributes.begin();
155 while (condIt != this->attributes.end()) {
156 //Find the value of needed attribute, based on attribute name
157 AttributeSet::const_iterator attr =
158 std::find_if(attrSet->begin(),
160 AceDB::BaseAttribute::UnaryPredicate(&(*condIt)));
161 if (attr == attrSet->end()) {
162 LogError("Couldn't find required attribute. This should not happen");
165 "Couldn't find attribute required in condition. This should not happen"
166 "This means that some attributes has not been obtained from PIP");
167 //Return undetermined here because it seems one of the attributes is unknown/undetermined
169 match = Attribute::MatchResult::MRUndetermined;
173 match = condIt->matchAttributes(&(*(*attr)));
174 if ((match == Attribute::MatchResult::MRFalse) && isAndCondition()) {
175 //FALSE match found in AND condition
178 } else if ((match == Attribute::MatchResult::MRTrue) && isOrCondition()) {
179 //TRUE match found in OR condition
182 } else if (match == Attribute::MatchResult::MRUndetermined) {
183 //Just mark that there was undetermined value found
184 undeterminedMatchFound = true;
192 Attribute::MatchResult Condition::evaluateChildConditions(
193 const AttributeSet * attrSet,
195 bool & undefinedMatchFound) const
197 Attribute::MatchResult match = Attribute::MatchResult::MRUndetermined;
199 std::list<Condition>::const_iterator it = conditions.begin();
200 while (it != conditions.end()) {
201 match = it->evaluateCondition(attrSet);
203 if ((match == Attribute::MatchResult::MRFalse) && isAndCondition()) {
204 //FALSE match found in AND condition
205 LogDebug("Child conditions results MRFalse)");
208 } else if ((match == Attribute::MatchResult::MRTrue) && isOrCondition()) {
209 //TRUE match found in OR condition
210 LogDebug("Child conditions result MRTrue");
213 } else if (match == Attribute::MatchResult::MRUndetermined) {
214 undefinedMatchFound = true;
222 void Condition::getAttributes(AttributeSet * attrSet)
224 //Get attributes from current condition
225 FOREACH (it, attributes)
227 AceDB::BaseAttributePtr attr(new Attribute(it->getName(), it->getMatchFunction(), it->getType()));
228 attrSet->insert(attr);
230 //Get attributes from any child conditions
231 FOREACH (it, conditions)
233 it->getAttributes(attrSet);