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 "simulator_resource_model.h"
22 #include "simulator_resource_model_schema.h"
23 #include "simulator_exceptions.h"
26 #include <boost/lexical_cast.hpp>
29 #define TAG "SIM_RESOURCE_MODEL"
32 struct TypeConverter {};
35 struct TypeConverter<int>
37 constexpr static AttributeValueType type =
38 AttributeValueType::INTEGER;
42 struct TypeConverter<double>
44 constexpr static AttributeValueType type =
45 AttributeValueType::DOUBLE;
49 struct TypeConverter<bool>
51 constexpr static AttributeValueType type =
52 AttributeValueType::BOOLEAN;
56 struct TypeConverter<std::string>
58 constexpr static AttributeValueType type =
59 AttributeValueType::STRING;
63 struct TypeConverter<SimulatorResourceModel>
65 constexpr static AttributeValueType type =
66 AttributeValueType::RESOURCE_MODEL;
72 constexpr static AttributeValueType type =
73 TypeConverter<T>::type;
74 constexpr static AttributeValueType baseType =
75 TypeConverter<T>::type;
76 constexpr static int depth = 0;
80 struct TypeDetails<std::vector<T>>
82 constexpr static AttributeValueType type =
83 AttributeValueType::VECTOR;
84 constexpr static AttributeValueType baseType =
85 TypeDetails<T>::baseType;
86 constexpr static int depth = 1 + TypeDetails<T>::depth;
89 class AttributeTypeVisitor : public boost::static_visitor<>
92 AttributeTypeVisitor() : m_type(AttributeValueType::UNKNOWN),
93 m_baseType(AttributeValueType::UNKNOWN), m_depth(0) {}
96 void operator ()(const T &)
98 m_type = TypeDetails<T>::type;
99 m_baseType = TypeDetails<T>::baseType;
100 m_depth = TypeDetails<T>::depth;
103 AttributeValueType m_type;
104 AttributeValueType m_baseType;
108 class OCRepresentationBuilder
111 class ValueConverter : public boost::static_visitor<>
114 ValueConverter(OC::OCRepresentation &rep, const std::string &name)
115 : m_rep(rep), m_name(name) {}
117 template <typename T>
118 void operator ()(const T &value)
120 m_rep.setValue(m_name, value);
123 void operator ()(const SimulatorResourceModel &value)
125 OC::OCRepresentation ocRep;
126 for (auto &element : value.getAttributeValues())
128 ValueConverter visitor(ocRep, element.first);
129 boost::apply_visitor(visitor, element.second);
132 m_rep.setValue(m_name, ocRep);
135 template <typename T>
136 void operator ()(const std::vector<T> &values)
138 m_rep.setValue(m_name, values);
141 void operator ()(const std::vector<SimulatorResourceModel> &values)
143 std::vector<OC::OCRepresentation> ocRepArray(values.size());
144 for (size_t i = 0; i < values.size(); i++)
146 for (auto &element : values[i].getAttributeValues())
148 ValueConverter visitor(ocRepArray[i], element.first);
149 boost::apply_visitor(visitor, element.second);
153 m_rep.setValue(m_name, ocRepArray);
156 template <typename T>
157 void operator ()(const std::vector<std::vector<T>> &values)
159 m_rep.setValue(m_name, values);
162 void operator ()(const std::vector<std::vector<SimulatorResourceModel>> &values)
164 std::vector<std::vector<OC::OCRepresentation>> ocRepArray;
165 for (size_t i = 0; i < values.size(); i++)
167 for (size_t j = 0; j < values[i].size(); j++)
169 for (auto &element : values[i][j].getAttributeValues())
171 ValueConverter visitor(ocRepArray[i][j], element.first);
172 boost::apply_visitor(visitor, element.second);
177 m_rep.setValue(m_name, ocRepArray);
180 template <typename T>
181 void operator ()(const std::vector<std::vector<std::vector<T>>> &values)
183 m_rep.setValue(m_name, values);
186 void operator ()(const std::vector<std::vector<std::vector<SimulatorResourceModel>>> &values)
188 std::vector<std::vector<std::vector<OC::OCRepresentation>>> ocRepArray;
189 for (size_t i = 0; i < values.size(); i++)
191 for (size_t j = 0; j < values[i].size(); j++)
193 for (size_t k = 0; j < values[i][j].size(); k++)
195 for (auto &element : values[i][j][k].getAttributeValues())
197 ValueConverter visitor(ocRepArray[i][j][k], element.first);
198 boost::apply_visitor(visitor, element.second);
204 m_rep.setValue(m_name, ocRepArray);
208 OC::OCRepresentation &m_rep;
212 OC::OCRepresentation build(const SimulatorResourceModel &model)
214 OC::OCRepresentation ocRep;
215 for (auto &element : model.getAttributeValues())
217 ValueConverter visitor(ocRep, element.first);
218 boost::apply_visitor(visitor, element.second);
221 return std::move(ocRep);
225 // TODO: Class is very heavy, revisit again to clean
226 class SimulatorResourceModelBuilder
229 SimulatorResourceModel build(const OC::OCRepresentation &ocRep)
231 SimulatorResourceModel resModel;
232 handleRepresentationType(resModel, ocRep);
233 return std::move(resModel);
237 void handleRepresentationType(SimulatorResourceModel &resModel,
238 const OC::OCRepresentation &ocRep)
240 for (auto &ocAttribute : ocRep)
242 if (OC::AttributeType::Integer == ocAttribute.type())
244 resModel.add<int>(ocAttribute.attrname(), ocAttribute.getValue<int>());
246 else if (OC::AttributeType::Double == ocAttribute.type())
248 resModel.add<double>(ocAttribute.attrname(), ocAttribute.getValue<double>());
250 else if (OC::AttributeType::Boolean == ocAttribute.type())
252 resModel.add<bool>(ocAttribute.attrname(), ocAttribute.getValue<bool>());
254 else if (OC::AttributeType::String == ocAttribute.type())
256 resModel.add<std::string>(ocAttribute.attrname(), ocAttribute.getValue<std::string>());
258 else if (OC::AttributeType::OCRepresentation == ocAttribute.type())
260 SimulatorResourceModel subResModel;
261 OC::OCRepresentation ocSubRep = ocAttribute.getValue<OC::OCRepresentation>();
262 handleRepresentationType(subResModel, ocSubRep);
263 resModel.add<SimulatorResourceModel>(ocAttribute.attrname(), subResModel);
265 else if (OC::AttributeType::Vector == ocAttribute.type())
267 handleVectorType(resModel, ocAttribute);
272 void handleVectorType(SimulatorResourceModel &resModel,
273 const OC::OCRepresentation::AttributeItem &ocAttribute)
275 if (1 == ocAttribute.depth())
277 handleVectorTypeDepth1(resModel, ocAttribute);
279 else if (2 == ocAttribute.depth())
281 handleVectorTypeDepth2(resModel, ocAttribute);
283 else if (3 == ocAttribute.depth())
285 handleVectorTypeDepth3(resModel, ocAttribute);
289 void handleVectorTypeDepth1(SimulatorResourceModel &resModel,
290 const OC::OCRepresentation::AttributeItem &ocAttribute)
292 if (OC::AttributeType::Integer == ocAttribute.base_type())
294 resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<int>>());
296 else if (OC::AttributeType::Double == ocAttribute.base_type())
298 resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<double>>());
300 else if (OC::AttributeType::Boolean == ocAttribute.base_type())
302 resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<bool>>());
304 else if (OC::AttributeType::String == ocAttribute.base_type())
306 resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<std::string>>());
308 else if (OC::AttributeType::OCRepresentation == ocAttribute.base_type())
310 std::vector<OC::OCRepresentation> ocSubRepArray =
311 ocAttribute.getValue<std::vector<OC::OCRepresentation>>();
313 std::vector<SimulatorResourceModel> subResModelArray(ocSubRepArray.size());
314 for (size_t i = 0; i < ocSubRepArray.size(); i++)
316 handleRepresentationType(subResModelArray[i], ocSubRepArray[i]);
319 resModel.add<std::vector<SimulatorResourceModel>>(ocAttribute.attrname(), subResModelArray);
323 void handleVectorTypeDepth2(SimulatorResourceModel &resModel,
324 const OC::OCRepresentation::AttributeItem &ocAttribute)
326 if (OC::AttributeType::Integer == ocAttribute.base_type())
328 resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<std::vector<int>>>());
330 else if (OC::AttributeType::Double == ocAttribute.base_type())
332 resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<std::vector<double>>>());
334 else if (OC::AttributeType::Boolean == ocAttribute.base_type())
336 resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<std::vector<bool>>>());
338 else if (OC::AttributeType::String == ocAttribute.base_type())
340 resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<std::vector<std::string>>>());
342 else if (OC::AttributeType::OCRepresentation == ocAttribute.base_type())
344 std::vector<std::vector<OC::OCRepresentation>> ocSubRepArray =
345 ocAttribute.getValue<std::vector<std::vector<OC::OCRepresentation>>>();
347 std::vector<std::vector<SimulatorResourceModel>> subResModelArray(ocSubRepArray.size());
348 for (size_t i = 0; i < ocSubRepArray.size(); i++)
350 std::vector<SimulatorResourceModel> innerArray1(ocSubRepArray[i].size());
351 for (size_t j = 0; j < ocSubRepArray[i].size(); j++)
352 handleRepresentationType(innerArray1[j], ocSubRepArray[i][j]);
353 subResModelArray[i] = innerArray1;
356 resModel.add<std::vector<std::vector<SimulatorResourceModel>>>(
357 ocAttribute.attrname(), subResModelArray);
361 void handleVectorTypeDepth3(SimulatorResourceModel &resModel,
362 const OC::OCRepresentation::AttributeItem &ocAttribute)
364 if (OC::AttributeType::Integer == ocAttribute.base_type())
366 resModel.add(ocAttribute.attrname(),
367 ocAttribute.getValue<std::vector<std::vector<std::vector<int>>>>());
369 else if (OC::AttributeType::Double == ocAttribute.base_type())
371 resModel.add(ocAttribute.attrname(),
372 ocAttribute.getValue<std::vector<std::vector<std::vector<double>>>>());
374 else if (OC::AttributeType::Boolean == ocAttribute.base_type())
376 resModel.add(ocAttribute.attrname(),
377 ocAttribute.getValue<std::vector<std::vector<std::vector<bool>>>>());
379 else if (OC::AttributeType::String == ocAttribute.base_type())
381 resModel.add(ocAttribute.attrname(),
382 ocAttribute.getValue<std::vector<std::vector<std::vector<std::string>>>>());
384 else if (OC::AttributeType::OCRepresentation == ocAttribute.base_type())
386 std::vector<std::vector<std::vector<OC::OCRepresentation>>> ocSubRepArray =
387 ocAttribute.getValue<std::vector<std::vector<std::vector<OC::OCRepresentation>>>>();
389 std::vector<std::vector<std::vector<SimulatorResourceModel>>> subResModelArray(
390 ocSubRepArray.size());
391 for (size_t i = 0; i < ocSubRepArray.size(); i++)
393 std::vector<std::vector<SimulatorResourceModel>> innerArray1(ocSubRepArray[i].size());
394 for (size_t j = 0; j < ocSubRepArray[i].size(); j++)
396 std::vector<SimulatorResourceModel> innerArray2(ocSubRepArray[i][j].size());
397 for (size_t k = 0; k < ocSubRepArray[i][j].size(); k++)
399 handleRepresentationType(innerArray2[k], ocSubRepArray[i][j][k]);
401 innerArray1[j] = innerArray2;
403 subResModelArray[i] = innerArray1;
406 resModel.add<std::vector<std::vector<std::vector<SimulatorResourceModel>>>>(
407 ocAttribute.attrname(), subResModelArray);
412 class ToStringConverter
415 class ValueVisitor : public boost::static_visitor<std::string>
419 template <typename T>
420 std::string operator ()(const T &value)
424 return boost::lexical_cast<std::string>(value);
426 catch (const boost::bad_lexical_cast &e)
428 return "CONVERSION_FAILED!";
432 std::string operator ()(const SimulatorResourceModel &value)
434 std::ostringstream out;
436 for (auto &element : value.getAttributeValues())
438 out << "\"" << element.first << "\" : ";
440 ValueVisitor visitor;
441 out << boost::apply_visitor(visitor, element.second);
449 template <typename T>
450 std::string operator ()(const std::vector<T> &values)
452 std::ostringstream out;
455 for (size_t i = 0; i < values.size(); i++)
457 out << operator ()(values[i]);
465 template <typename T>
466 std::string operator ()(const std::vector<std::vector<T>> &values)
468 std::ostringstream out;
471 for (size_t i = 0; i < values.size(); i++)
473 out << operator ()(values[i]);
481 template <typename T>
482 std::string operator ()(const std::vector<std::vector<std::vector<T>>> &values)
484 std::ostringstream out;
487 for (size_t i = 0; i < values.size(); i++)
489 out << operator ()(values[i]);
498 std::string getStringRepresentation(const SimulatorResourceModel &resModel)
500 ValueVisitor visitor;
501 AttributeValueVariant value = resModel;
502 return boost::apply_visitor(visitor, value);
505 std::string getStringRepresentation(const AttributeValueVariant &value)
507 ValueVisitor visitor;
508 return boost::apply_visitor(visitor, value);
512 SimulatorResourceModel::TypeInfo::TypeInfo(
513 AttributeValueType type, AttributeValueType baseType, int depth)
514 : m_type (type), m_baseType(baseType), m_depth(depth) {}
516 AttributeValueType SimulatorResourceModel::TypeInfo::type() const
521 AttributeValueType SimulatorResourceModel::TypeInfo::baseType() const
526 int SimulatorResourceModel::TypeInfo::depth() const
531 bool SimulatorResourceModel::TypeInfo::operator==(
532 const SimulatorResourceModel::TypeInfo &rhs) const
534 if (m_type == rhs.m_type && m_baseType == rhs.m_baseType
535 && m_depth == rhs.m_depth)
540 SimulatorResourceAttribute::SimulatorResourceAttribute(const std::string &name)
543 SimulatorResourceAttribute::SimulatorResourceAttribute(const std::string &name,
544 const AttributeValueVariant &value) : m_name(name)
546 m_value = std::make_shared<AttributeValueVariant>(value);
549 void SimulatorResourceAttribute::setName(const std::string &name)
554 std::string SimulatorResourceAttribute::getName() const
559 const SimulatorResourceModel::TypeInfo SimulatorResourceAttribute::getType() const
563 AttributeTypeVisitor typeVisitor;
564 boost::apply_visitor(typeVisitor, *(m_value.get()));
565 return SimulatorResourceModel::TypeInfo(typeVisitor.m_type, typeVisitor.m_baseType,
566 typeVisitor.m_depth);
569 return SimulatorResourceModel::TypeInfo();
572 void SimulatorResourceAttribute::setProperty(const std::shared_ptr<AttributeProperty> &property)
574 m_property = property;
577 std::shared_ptr<AttributeProperty> SimulatorResourceAttribute::getProperty() const
582 AttributeValueVariant SimulatorResourceAttribute::getValue() const
587 std::string SimulatorResourceAttribute::asString() const
589 return ToStringConverter().getStringRepresentation(*m_value);
592 bool SimulatorResourceModel::remove(const std::string &name)
594 if (m_attributes.end() == m_attributes.find(name))
599 m_attributes.erase(m_attributes.find(name));
603 bool SimulatorResourceModel::contains(const std::string &name) const
605 if (m_attributes.end() != m_attributes.find(name))
613 size_t SimulatorResourceModel::size() const
615 return m_attributes.size();
618 SimulatorResourceModel::TypeInfo SimulatorResourceModel::getType(const std::string &name) const
620 if (m_attributes.end() != m_attributes.find(name))
622 return getTypeInfo(m_attributes.find(name)->second);
625 return SimulatorResourceModel::TypeInfo();
628 std::map<std::string, AttributeValueVariant> SimulatorResourceModel::getAttributeValues() const
633 AttributeValueVariant SimulatorResourceModel::getAttributeValue(const std::string &name) const
635 auto ite = m_attributes.find(name);
636 if (m_attributes.end() != ite)
638 return (*ite).second;
641 return AttributeValueVariant();
644 std::set<std::string> SimulatorResourceModel::getAttributeNameSet() const
646 std::set<std::string> nameSet;
647 for (auto &attributeEntry : m_attributes)
649 nameSet.insert(attributeEntry.first);
655 SimulatorResourceModel::TypeInfo SimulatorResourceModel::getTypeInfo(const AttributeValueVariant
658 AttributeTypeVisitor typeVisitor;
659 boost::apply_visitor(typeVisitor, value);
660 SimulatorResourceModel::TypeInfo typeInfo(typeVisitor.m_type, typeVisitor.m_baseType,
661 typeVisitor.m_depth);
665 bool SimulatorResourceModel::updateValue(const std::string &name,
666 const AttributeValueVariant &value)
673 if (m_attributes.end() == m_attributes.find(name))
678 if (!(getTypeInfo(m_attributes[name]) == getTypeInfo(value)))
683 m_attributes[name] = value;
687 OC::OCRepresentation SimulatorResourceModel::asOCRepresentation() const
689 return OCRepresentationBuilder().build(*this);
692 std::string SimulatorResourceModel::asString() const
694 return ToStringConverter().getStringRepresentation(*this);
697 SimulatorResourceModel SimulatorResourceModel::build(const OC::OCRepresentation &ocRep)
699 return SimulatorResourceModelBuilder().build(ocRep);