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;
46 LogDebug("Attributes to be matched");
47 printAttributes(*attrSet);
48 LogDebug("Condition attributes values");
49 printAttributes(attributes);
52 if (this->isEmpty()) {
53 LogDebug("Condition is empty, returning true");
54 //Condition is empty, it means it evaluates to TRUE
55 return Attribute::MatchResult::MRTrue;
58 match = evaluateAttributes(attrSet, isFinalMatch, undeterminedMatchFound);
60 LogDebug("Evaluate attributes returning verdict" ) ; //<< match);
64 match = evaluateChildConditions(attrSet,
66 undeterminedMatchFound);
68 LogDebug("Evaluate child conditions returning verdict" ); // << match);
72 if (undeterminedMatchFound) {
73 //If any child condition/attribute-match was undetermined and
74 //so far we couldn't make a decision then we must return undetermined
75 LogDebug("Evaluate condition returning MRUndetermined");
76 return Attribute::MatchResult::MRUndetermined;
79 if (this->isAndCondition()) {
80 match = Attribute::MatchResult::MRTrue;
81 } else if (this->isOrCondition()) {
82 match = Attribute::MatchResult::MRFalse;
84 Assert(false && "Condition has to be either AND or OR");
89 // KW Attribute::MatchResult Condition::performORalgorithm(const std::set<Attribute>* attrSet) const{
91 // KW Attribute::MatchResult match;
92 // KW bool undeterminedMatchFound = false;
93 // KW bool isFinalMatch = false;
95 // KW LogDebug("Performing OR algorithm");
97 // KW match = evaluateAttributes(attrSet, isFinalMatch, undeterminedMatchFound);
98 // KW if(isFinalMatch){
99 // KW LogDebug("OR algorithm evaluate attributes returning verdict" << match);
103 // KW match = evaluateChildConditions(attrSet, isFinalMatch, undeterminedMatchFound);
104 // KW if(isFinalMatch){
108 // KW if(undeterminedMatchFound){
109 // KW //If any child condition/attribute-match was undetermined and
110 // KW //so far we couldn't make a decision then we must return undetermined
111 // KW LogDebug("OR algorithm returning MRUndetermined");
112 // KW return Attribute::MRUndetermined;
115 // KW LogDebug("OR algorithm returning MRFalse");
116 // KW return Attribute::MRFalse;
119 // KW Attribute::MatchResult Condition::performANDalgorithm(const std::set<Attribute>* attrSet) const{
122 // KW Attribute::MatchResult match;
123 // KW bool undeterminedMatchFound = false;
124 // KW bool isFinalMatch = false;
126 // KW LogDebug("Performing AND algorithm");
127 // KW match = evaluateAttributes(attrSet, isFinalMatch, undeterminedMatchFound);
128 // KW if(isFinalMatch){
129 // KW LogDebug("AND algorithm evaluate attributes returning verdict" << match);
132 // KW match = evaluateChildConditions(attrSet, isFinalMatch, undeterminedMatchFound);
133 // KW if(isFinalMatch){
134 // KW LogDebug("AND algorithm evaluate child returning verdict " << match);
137 // KW if(undeterminedMatchFound){
138 // KW //If any child condition/attribute-match was undetermined and
139 // KW //so far we couldn't make a decision then we must return undetermined
140 // KW LogDebug("AND algorithm returning Undetermined");
141 // KW return Attribute::MRUndetermined;
144 // KW LogDebug("AND algorithm returning MRTrue");
145 // KW return Attribute::MRTrue;
149 Attribute::MatchResult Condition::evaluateAttributes(
150 const AttributeSet * attrSet,
152 bool & undeterminedMatchFound) const
154 Attribute::MatchResult match = Attribute::MatchResult::MRUndetermined;
156 std::list<Attribute>::const_iterator condIt = this->attributes.begin();
157 while (condIt != this->attributes.end()) {
158 //Find the value of needed attribute, based on attribute name
159 AttributeSet::const_iterator attr =
160 std::find_if(attrSet->begin(),
162 AceDB::BaseAttribute::UnaryPredicate(&(*condIt)));
163 if (attr == attrSet->end()) {
164 LogError("Couldn't find required attribute. This should not happen");
167 "Couldn't find attribute required in condition. This should not happen"
168 "This means that some attributes has not been obtained from PIP");
169 //Return undetermined here because it seems one of the attributes is unknown/undetermined
171 match = Attribute::MatchResult::MRUndetermined;
175 match = condIt->matchAttributes(&(*(*attr)));
176 if ((match == Attribute::MatchResult::MRFalse) && isAndCondition()) {
177 //FALSE match found in AND condition
180 } else if ((match == Attribute::MatchResult::MRTrue) && isOrCondition()) {
181 //TRUE match found in OR condition
184 } else if (match == Attribute::MatchResult::MRUndetermined) {
185 //Just mark that there was undetermined value found
186 undeterminedMatchFound = true;
194 Attribute::MatchResult Condition::evaluateChildConditions(
195 const AttributeSet * attrSet,
197 bool & undefinedMatchFound) const
199 Attribute::MatchResult match = Attribute::MatchResult::MRUndetermined;
201 std::list<Condition>::const_iterator it = conditions.begin();
202 while (it != conditions.end()) {
203 match = it->evaluateCondition(attrSet);
205 if ((match == Attribute::MatchResult::MRFalse) && isAndCondition()) {
206 //FALSE match found in AND condition
207 LogDebug("Child conditions results MRFalse)");
210 } else if ((match == Attribute::MatchResult::MRTrue) && isOrCondition()) {
211 //TRUE match found in OR condition
212 LogDebug("Child conditions result MRTrue");
215 } else if (match == Attribute::MatchResult::MRUndetermined) {
216 undefinedMatchFound = true;
224 void Condition::getAttributes(AttributeSet * attrSet)
226 //Get attributes from current condition
227 FOREACH (it, attributes)
229 AceDB::BaseAttributePtr attr(new Attribute(it->getName(), it->getMatchFunction(), it->getType()));
230 attrSet->insert(attr);
232 //Get attributes from any child conditions
233 FOREACH (it, conditions)
235 it->getAttributes(attrSet);