tizen 2.3.1 release
[framework/web/wearable/wrt-security.git] / ace / engine / Condition.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 //
17 // File: Condition.cpp
18 // Author: notroot
19 //
20 // Created on June 3, 2009, 9:00 AM
21 //
22
23 #include <iostream>
24 #include <dpl/log/log.h>
25 #include <dpl/foreach.h>
26 #include <ace/Condition.h>
27
28 /**
29  * Check if attribute in condition matches the values obtained from PIP
30  * attrSet - attributes from PIP
31  */
32
33 Attribute::MatchResult Condition::evaluateCondition(
34         const AttributeSet * attrSet) const
35 {
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.
40
41     Attribute::MatchResult match;
42     bool undeterminedMatchFound = false;
43     bool isFinalMatch = false;
44
45 #ifdef ALL_LOGS
46     LogDebug("Attributes to be matched");
47     printAttributes(*attrSet);
48     LogDebug("Condition attributes values");
49     printAttributes(attributes);
50 #endif
51
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;
56     }
57
58     match = evaluateAttributes(attrSet, isFinalMatch, undeterminedMatchFound);
59     if (isFinalMatch) {
60         LogDebug("Evaluate attributes returning verdict" ) ; //<< match);
61         return match;
62     }
63
64     match = evaluateChildConditions(attrSet,
65                                     isFinalMatch,
66                                     undeterminedMatchFound);
67     if (isFinalMatch) {
68         LogDebug("Evaluate child conditions returning verdict" ); // << match);
69         return match;
70     }
71
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;
77     }
78
79     if (this->isAndCondition()) {
80         match = Attribute::MatchResult::MRTrue;
81     } else if (this->isOrCondition()) {
82         match = Attribute::MatchResult::MRFalse;
83     } else {
84         Assert(false && "Condition has to be either AND or OR");
85     }
86     return match;
87 }
88
89 // KW Attribute::MatchResult Condition::performORalgorithm(const std::set<Attribute>* attrSet) const{
90 // KW
91 // KW     Attribute::MatchResult match;
92 // KW     bool undeterminedMatchFound = false;
93 // KW     bool isFinalMatch = false;
94 // KW
95 // KW     LogDebug("Performing OR algorithm");
96 // KW
97 // KW     match = evaluateAttributes(attrSet, isFinalMatch, undeterminedMatchFound);
98 // KW     if(isFinalMatch){
99 // KW         LogDebug("OR algorithm evaluate attributes returning verdict" << match);
100 // KW         return match;
101 // KW     }
102 // KW
103 // KW     match = evaluateChildConditions(attrSet, isFinalMatch, undeterminedMatchFound);
104 // KW     if(isFinalMatch){
105 // KW         return match;
106 // KW     }
107 // KW
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;
113 // KW     }
114 // KW
115 // KW     LogDebug("OR algorithm returning MRFalse");
116 // KW     return Attribute::MRFalse;
117 // KW }
118
119 // KW Attribute::MatchResult Condition::performANDalgorithm(const std::set<Attribute>* attrSet) const{
120 // KW
121 // KW
122 // KW     Attribute::MatchResult match;
123 // KW     bool undeterminedMatchFound = false;
124 // KW     bool isFinalMatch = false;
125 // KW
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);
130 // KW         return match;
131 // KW     }
132 // KW     match = evaluateChildConditions(attrSet, isFinalMatch, undeterminedMatchFound);
133 // KW     if(isFinalMatch){
134 // KW         LogDebug("AND algorithm evaluate child returning verdict " << match);
135 // KW         return match;
136 // KW     }
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;
142 // KW     }
143 // KW
144 // KW     LogDebug("AND algorithm returning MRTrue");
145 // KW     return Attribute::MRTrue;
146 // KW
147 // KW }
148
149 Attribute::MatchResult Condition::evaluateAttributes(
150         const AttributeSet * attrSet,
151         bool& isFinalMatch,
152         bool & undeterminedMatchFound) const
153 {
154     Attribute::MatchResult match = Attribute::MatchResult::MRUndetermined;
155
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(),
161                              attrSet->end(),
162                              AceDB::BaseAttribute::UnaryPredicate(&(*condIt)));
163         if (attr == attrSet->end()) {
164             LogError("Couldn't find required attribute. This should not happen");
165             Assert(
166                 false &&
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
170             isFinalMatch = true;
171             match = Attribute::MatchResult::MRUndetermined;
172             break;
173         }
174
175         match = condIt->matchAttributes(&(*(*attr)));
176         if ((match == Attribute::MatchResult::MRFalse) && isAndCondition()) {
177             //FALSE match found in AND condition
178             isFinalMatch = true;
179             break;
180         } else if ((match == Attribute::MatchResult::MRTrue) && isOrCondition()) {
181             //TRUE match found in OR condition
182             isFinalMatch = true;
183             break;
184         } else if (match == Attribute::MatchResult::MRUndetermined) {
185             //Just mark that there was undetermined value found
186             undeterminedMatchFound = true;
187         }
188         ++condIt;
189     }
190
191     return match;
192 }
193
194 Attribute::MatchResult Condition::evaluateChildConditions(
195         const AttributeSet * attrSet,
196         bool& isFinalMatch,
197         bool & undefinedMatchFound) const
198 {
199     Attribute::MatchResult match = Attribute::MatchResult::MRUndetermined;
200
201     std::list<Condition>::const_iterator it = conditions.begin();
202     while (it != conditions.end()) {
203         match = it->evaluateCondition(attrSet);
204
205         if ((match == Attribute::MatchResult::MRFalse) && isAndCondition()) {
206             //FALSE match found in AND condition
207             LogDebug("Child conditions results MRFalse)");
208             isFinalMatch = true;
209             break;
210         } else if ((match == Attribute::MatchResult::MRTrue) && isOrCondition()) {
211             //TRUE match found in OR condition
212             LogDebug("Child conditions result MRTrue");
213             isFinalMatch = true;
214             break;
215         } else if (match == Attribute::MatchResult::MRUndetermined) {
216             undefinedMatchFound = true;
217         }
218         ++it;
219     }
220
221     return match;
222 }
223
224 void Condition::getAttributes(AttributeSet * attrSet)
225 {
226     //Get attributes from current condition
227     FOREACH (it, attributes)
228     {
229         AceDB::BaseAttributePtr attr(new Attribute(it->getName(), it->getMatchFunction(), it->getType()));
230         attrSet->insert(attr);
231     }
232     //Get attributes from any child conditions
233     FOREACH (it, conditions)
234     {
235         it->getAttributes(attrSet);
236     }
237 }
238