Tizen 2.1 base
[framework/web/wrt-commons.git] / modules / ace / include / dpl / ace / Condition.h
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.h
18 // Author: notroot
19 //
20 // Created on June 3, 2009, 9:00 AM
21 //
22 #ifndef _CONDITION_H
23 #define _CONDITION_H
24
25 #include <list>
26 #include <set>
27 #include <iostream>
28 #include <dpl/foreach.h>
29
30 #include "Attribute.h"
31 #include "Effect.h"
32 #include "TreeNode.h"
33
34 class Condition
35 {
36   public:
37     enum CombineType
38     {
39         AND, OR
40     };
41
42     void addCondition(const Condition & condition)
43     {
44         this->conditions.push_back(condition);
45     }
46
47     void addAttribute(const Attribute & attribute)
48     {
49         this->attributes.push_back(attribute);
50     }
51
52     void setCombineType(CombineType type)
53     {
54         this->combineType = type;
55     }
56
57     Condition() : combineType(AND),
58         parent(NULL)
59     {
60     }
61
62     Condition(CombineType type) : combineType(type),
63         parent(NULL)
64     {
65     }
66
67     virtual ~Condition()
68     {
69     }
70
71     Condition * getParent()
72     {
73         return this->parent;
74     }
75
76     void setParent(Condition * condition)
77     {
78         this->parent = condition;
79     }
80
81     Attribute::MatchResult evaluateCondition(
82             const AttributeSet * attrSet) const;
83
84     friend std::ostream & operator<<(std::ostream & out,
85             Condition & condition)
86     {
87         FOREACH (it, condition.attributes)
88         {
89             out << *it;
90         }
91         return out;
92     }
93     //[CR] change function name
94     void getAttributes(AttributeSet * attrSet);
95
96     //deserializer, TODO - first condition doesnt have Condtion parent
97     Condition(std::istream&,
98             Condition* parent = NULL);
99
100     bool serialize(std::ostream&);
101
102   private:
103     Attribute::MatchResult evaluateChildConditions(
104             const AttributeSet * attrSet,
105             bool &isFinalMatch,
106             bool & undefinedMatchFound) const;
107
108     Attribute::MatchResult evaluateAttributes(
109             const AttributeSet * attrSet,
110             bool& isFinalMatch,
111             bool & undefinedMatchFound) const;
112
113     // KW     Attribute::MatchResult performANDalgorithm(const std::set<Attribute> * attributes) const;
114
115     // KW     Attribute::MatchResult performORalgorithm(const std::set<Attribute> * attributes) const;
116
117     bool isEmpty() const
118     {
119         return attributes.empty() && conditions.empty();
120     }
121
122     bool isAndCondition() const
123     {
124         return combineType == AND;
125     }
126
127     bool isOrCondition() const
128     {
129         return combineType == OR;
130     }
131
132     std::list<Condition> conditions;
133     CombineType combineType;
134     std::list<Attribute> attributes;
135     Condition *parent;
136 };
137
138 #endif    /* _CONDITION_H */
139