Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / simulator / src / client-controller / attribute_generator.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include "attribute_generator.h"
22
23 AttributeGenerator::AttributeGenerator(const SimulatorResourceModel::Attribute &attribute)
24     :   m_name(attribute.getName()),
25         m_min(INT_MIN),
26         m_max(INT_MAX),
27         m_rangeIndex(-1),
28         m_allowedValueIndex(0),
29         m_hasRange(false),
30         m_hasAllowedValue(false)
31 {
32     if (attribute.getValueType() ==
33         SimulatorResourceModel::Attribute::ValueType::INTEGER)
34     {
35         attribute.getRange(m_min, m_max);
36         if (INT_MIN != m_min && INT_MAX != m_max)
37         {
38             m_hasRange = true;
39             m_rangeIndex = m_min;
40         }
41     }
42     else
43     {
44         m_allowedValues = attribute.getAllowedValues();
45         if (0 != m_allowedValues.size())
46         {
47             m_hasAllowedValue = true;
48         }
49     }
50 }
51
52 bool AttributeGenerator::hasNext()
53 {
54     if (m_hasRange && m_rangeIndex <= m_max)
55     {
56         return true;
57     }
58
59     if (m_hasAllowedValue && m_allowedValueIndex < m_allowedValues.size())
60     {
61         return true;
62     }
63
64     return false;
65 }
66
67 bool AttributeGenerator::next(SimulatorResourceModel::Attribute &attribute)
68 {
69     attribute.setName(m_name);
70
71     if (m_hasRange)
72     {
73         attribute.setValue(m_rangeIndex++);
74         return true;
75     }
76     else if (m_hasAllowedValue)
77     {
78         attribute.setValue(m_allowedValues[m_allowedValueIndex++]);
79         return true;
80     }
81
82     return false;
83 }
84
85 SimulatorResourceModel::Attribute AttributeGenerator::current()
86 {
87     SimulatorResourceModel::Attribute attribute;
88
89     attribute.setName(m_name);
90     if (m_hasRange)
91     {
92         attribute.setValue(m_rangeIndex);
93     }
94     else if (m_hasAllowedValue)
95     {
96         attribute.setValue(m_allowedValues[m_allowedValueIndex]);
97     }
98
99     return attribute;
100 }
101
102 void AttributeGenerator::reset()
103 {
104     if (m_hasRange)
105     {
106         m_rangeIndex = m_min;
107     }
108     else if (m_hasAllowedValue)
109     {
110         m_allowedValueIndex = 0;
111     }
112 }
113
114 AttributeCombinationGen::AttributeCombinationGen(
115         const std::vector<SimulatorResourceModel::Attribute> &attributes)
116 {
117     for (auto &attr : attributes)
118     {
119         AttributeGenerator attrGen(attr);
120         m_attrGenList.push_back(attr);
121     }
122
123     m_index = -1;
124 }
125
126 bool AttributeCombinationGen::next(SimulatorResourceModel &resModel)
127 {
128     if (!m_attrGenList.size())
129     {
130         return false;
131     }
132
133     std::lock_guard<std::mutex> lock(m_lock);
134
135     // This block will execute for only first time
136     if (-1 == m_index)
137     {
138         for (int index = 0; index < m_attrGenList.size(); index++)
139         {
140             // Add the attribute on resource model
141             addAttributeToModel(index);
142         }
143
144         m_index = m_attrGenList.size() - 1;
145         resModel = m_resModel;
146         return true;
147     }
148
149     // Get the next attribute from statck top element
150     if (m_attrGenList[m_index].hasNext())
151     {
152         addAttributeToModel(m_index);
153         resModel = m_resModel;
154         return true;
155     }
156     else
157     {
158         for (int index = m_index; index >= 0; index--)
159         {
160             if (!m_attrGenList[index].hasNext())
161             {
162                 if (!index)
163                     return false;
164
165                 m_attrGenList[index].reset();
166                 addAttributeToModel(index);
167             }
168             else
169             {
170                 addAttributeToModel(index);
171                 break;
172             }
173         }
174
175         resModel = m_resModel;
176         return true;
177     }
178
179     return false;
180 }
181
182 void AttributeCombinationGen::addAttributeToModel(int index)
183 {
184     SimulatorResourceModel::Attribute attribute;
185     m_attrGenList[index].next(attribute);
186     m_resModel.addAttribute(attribute, true);
187 }