From: Mu-Woong Lee Date: Thu, 3 Aug 2017 04:02:41 +0000 (+0900) Subject: trigger: add the skeleton of the class CustomTemplate, which is used to validate... X-Git-Tag: submit/tizen/20170926.083539^2~15 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1942318373c31f94357c19a402e12012db0e0d93;p=platform%2Fcore%2Fapi%2Fcontext.git trigger: add the skeleton of the class CustomTemplate, which is used to validate custom facts Change-Id: Ib62eb2c409defbb6e4ea2ace342663c0e6e934e6 Signed-off-by: Mu-Woong Lee --- diff --git a/src/trigger/CustomTemplate.cpp b/src/trigger/CustomTemplate.cpp new file mode 100644 index 0000000..68b88b1 --- /dev/null +++ b/src/trigger/CustomTemplate.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "CustomTemplate.h" + +static std::list __instances; + +CustomTemplate::CustomTemplate(const std::string& name, const Json::Value& templateJson) : + __name(name), + __templateJson(templateJson) +{ +} + +const std::string& CustomTemplate::getName() const +{ + return __name; +} + +bool CustomTemplate::match(const std::string& fact) +{ + //TODO: true if the given fact is valid w.r.t. the template + return true; +} + +bool CustomTemplate::add(const std::string& name, const std::string& attrTmpl) +{ + IF_FAIL_RETURN(!get(name), false); + + //TODO: validity check of the name (regex) + + Json::Reader reader; + Json::Value tmplJson; + + try { + reader.parse(attrTmpl, tmplJson); + } catch (const Json::Exception& e) { + _E("Exception: %s", e.what()); + return false; + } + + //TODO: further validity check of the template + + __instances.emplace_back(name, tmplJson); + + return true; +} + +void CustomTemplate::remove(const std::string& name) +{ + __instances.remove_if( + [&name](const CustomTemplate& tmpl)->bool { + return tmpl.getName() == name; + }); +} + +CustomTemplate* CustomTemplate::get(const std::string& name) +{ + for (auto& tmpl : __instances) { + if (tmpl.getName() == name) + return &tmpl; + } + + return NULL; +} diff --git a/src/trigger/CustomTemplate.h b/src/trigger/CustomTemplate.h new file mode 100644 index 0000000..751202d --- /dev/null +++ b/src/trigger/CustomTemplate.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_TRIGGER_CUSTOM_TEMPLATE_H__ +#define __CONTEXT_TRIGGER_CUSTOM_TEMPLATE_H__ + +#include +#include +#include +#include + +class CustomTemplate { +public: + CustomTemplate(const std::string& name, const Json::Value& templateJson); + + const std::string& getName() const; + bool match(const std::string& fact); + + static bool add(const std::string& name, const std::string& attrTmpl); + static void remove(const std::string& name); + static CustomTemplate* get(const std::string& name); + +private: + std::string __name; + Json::Value __templateJson; +}; + +#endif