1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "attribute_generator.h"
23 AttributeGenerator::AttributeGenerator(const SimulatorResourceModel::Attribute &attribute)
24 : m_name(attribute.getName()),
28 m_allowedValueIndex(0),
30 m_hasAllowedValue(false)
32 if (attribute.getValueType() ==
33 SimulatorResourceModel::Attribute::ValueType::INTEGER)
35 attribute.getRange(m_min, m_max);
36 if (INT_MIN != m_min && INT_MAX != m_max)
44 m_allowedValues = attribute.getAllowedValues();
45 if (0 != m_allowedValues.size())
47 m_hasAllowedValue = true;
52 bool AttributeGenerator::hasNext()
54 if (m_hasRange && m_rangeIndex <= m_max)
59 if (m_hasAllowedValue && m_allowedValueIndex < m_allowedValues.size())
67 bool AttributeGenerator::next(SimulatorResourceModel::Attribute &attribute)
69 attribute.setName(m_name);
73 attribute.setValue(m_rangeIndex++);
76 else if (m_hasAllowedValue)
78 attribute.setValue(m_allowedValues[m_allowedValueIndex++]);
85 SimulatorResourceModel::Attribute AttributeGenerator::current()
87 SimulatorResourceModel::Attribute attribute;
89 attribute.setName(m_name);
92 attribute.setValue(m_rangeIndex);
94 else if (m_hasAllowedValue)
96 attribute.setValue(m_allowedValues[m_allowedValueIndex]);
102 void AttributeGenerator::reset()
106 m_rangeIndex = m_min;
108 else if (m_hasAllowedValue)
110 m_allowedValueIndex = 0;
114 AttributeCombinationGen::AttributeCombinationGen(
115 const std::vector<SimulatorResourceModel::Attribute> &attributes)
117 for (auto &attr : attributes)
119 AttributeGenerator attrGen(attr);
120 m_attrGenList.push_back(attr);
126 bool AttributeCombinationGen::next(SimulatorResourceModel &resModel)
128 if (!m_attrGenList.size())
133 std::lock_guard<std::mutex> lock(m_lock);
135 // This block will execute for only first time
138 for (int index = 0; index < m_attrGenList.size(); index++)
140 // Add the attribute on resource model
141 addAttributeToModel(index);
144 m_index = m_attrGenList.size() - 1;
145 resModel = m_resModel;
149 // Get the next attribute from statck top element
150 if (m_attrGenList[m_index].hasNext())
152 addAttributeToModel(m_index);
153 resModel = m_resModel;
158 for (int index = m_index; index >= 0; index--)
160 if (!m_attrGenList[index].hasNext())
165 m_attrGenList[index].reset();
166 addAttributeToModel(index);
170 addAttributeToModel(index);
175 resModel = m_resModel;
182 void AttributeCombinationGen::addAttributeToModel(int index)
184 SimulatorResourceModel::Attribute attribute;
185 m_attrGenList[index].next(attribute);
186 m_resModel.addAttribute(attribute, true);