trigger: add the skeleton of the class CustomTemplate, which is used to validate... 92/142192/2
authorMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 3 Aug 2017 04:02:41 +0000 (13:02 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 3 Aug 2017 07:31:00 +0000 (16:31 +0900)
Change-Id: Ib62eb2c409defbb6e4ea2ace342663c0e6e934e6
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/trigger/CustomTemplate.cpp [new file with mode: 0644]
src/trigger/CustomTemplate.h [new file with mode: 0644]

diff --git a/src/trigger/CustomTemplate.cpp b/src/trigger/CustomTemplate.cpp
new file mode 100644 (file)
index 0000000..68b88b1
--- /dev/null
@@ -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<CustomTemplate> __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 (file)
index 0000000..751202d
--- /dev/null
@@ -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 <string>
+#include <list>
+#include <json/json.h>
+#include <ContextTypes.h>
+
+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