nnc: Implement plugin example (#278)
authorVitaliy Cherepanov/AI Tools Lab/Engineer/삼성전자 <v.cherepanov@samsung.com>
Tue, 5 Jun 2018 08:30:42 +0000 (11:30 +0300)
committerSergey Vostokov/AI Tools Lab/Staff Engineer/삼성전자 <s.vostokov@samsung.com>
Tue, 5 Jun 2018 08:30:42 +0000 (11:30 +0300)
nnc: Implement plugin example

This commit show how to create plugin for nnc

Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
contrib/nnc/CMakeLists.txt
contrib/nnc/examples/CMakeLists.txt [new file with mode: 0644]
contrib/nnc/examples/plugin_example/CMakeLists.txt [new file with mode: 0644]
contrib/nnc/examples/plugin_example/main.cpp [new file with mode: 0644]

index 167726e..b1e1dcc 100644 (file)
@@ -10,3 +10,4 @@ target_link_libraries(nnc PRIVATE dl)
 target_include_directories(nnc PUBLIC include)
 
 add_subdirectory(libs)
+add_subdirectory(examples)
diff --git a/contrib/nnc/examples/CMakeLists.txt b/contrib/nnc/examples/CMakeLists.txt
new file mode 100644 (file)
index 0000000..8a4d01e
--- /dev/null
@@ -0,0 +1,6 @@
+file(GLOB CONTRIB_EXAMPLES_CMAKE_FILES "*/CMakeLists.txt")
+
+foreach(CONTRIB_EXAMPLES_CMAKE_FILE ${CONTRIB_EXAMPLES_CMAKE_FILES})
+  get_filename_component(CONTRIB_BASE ${CONTRIB_EXAMPLES_CMAKE_FILE} DIRECTORY)
+  add_subdirectory(${CONTRIB_BASE})
+endforeach(CONTRIB_EXAMPLES_CMAKE_FILE ${CONTRIB_EXAMPLES_CMAKE_FILES})
diff --git a/contrib/nnc/examples/plugin_example/CMakeLists.txt b/contrib/nnc/examples/plugin_example/CMakeLists.txt
new file mode 100644 (file)
index 0000000..dc0219a
--- /dev/null
@@ -0,0 +1,13 @@
+file(GLOB_RECURSE PL_EXAMPLE_PARSE_SRC *.cpp)
+file(GLOB_RECURSE PL_EXAMPLE_PARSE_H)
+
+add_library(some_parser SHARED ${PL_EXAMPLE_PARSE_SRC} ${PL_EXAMPLE_PARSE_H})
+add_library(some_parser_second SHARED ${PL_EXAMPLE_PARSE_SRC} ${PL_EXAMPLE_PARSE_H})
+
+target_link_libraries(some_parser PRIVATE nnc_plugin_core)
+target_link_libraries(some_parser PRIVATE nncc_foundation)
+target_link_libraries(some_parser PRIVATE nnc_core)
+
+target_link_libraries(some_parser_second PRIVATE nnc_plugin_core)
+target_link_libraries(some_parser_second PRIVATE nncc_foundation)
+target_link_libraries(some_parser_second PRIVATE nnc_core)
diff --git a/contrib/nnc/examples/plugin_example/main.cpp b/contrib/nnc/examples/plugin_example/main.cpp
new file mode 100644 (file)
index 0000000..cbca17f
--- /dev/null
@@ -0,0 +1,93 @@
+#include <map>
+#include <vector>
+#include <iostream>
+#include "PluginInstance.h"
+#include "PluginException.h"
+#include "ConfigException.h"
+#include "PluginType.h"
+
+static const std::string pluginName = "Your plugin name here";
+static const std::string pluginVersion = "Your plugin version here";
+static const std::string pluginDesc = "Your plugin description here";
+static const auto pluginType = nncc::contrib::plugin::typeFrontEnd;
+
+using namespace nncc::contrib::config;
+using namespace nncc::contrib::plugin;
+
+class SamplePluginInstance : public AbstractPluginInstance
+{
+public:
+  static AbstractPluginInstance &getInstance();
+  void fillSession() override;
+  void checkConfig() override;
+  void *execute(void *data) override;
+
+  void setParam(const std::string &name) override;
+  void setParam(const std::string &name, const std::string &value) override;
+
+private:
+  std::string _filename;
+};
+
+AbstractPluginInstance &SamplePluginInstance::getInstance()
+{
+  static SamplePluginInstance instance;
+  std::cout << std::endl << "!!! plugin (" << pluginName << ") " << __func__ << std::endl;
+
+  return instance;
+}
+
+void SamplePluginInstance::fillSession()
+{
+  static std::map<std::string, std::string> info = {{"module description", pluginDesc}};
+
+  static std::vector<PluginParam> moduleParams = {{"filename", "[model file name]", false},
+                                                  {"someoption", "[Your <someoption> description here]", true}};
+
+  AbstractPluginInstance::fillSessionBase(pluginType, pluginVersion, pluginName);
+
+  for (auto &i : info)
+    getSession()->addInfo(i.first, i.second);
+
+  for (auto &p : moduleParams)
+    getSession()->registerParam(p);
+}
+
+void SamplePluginInstance::checkConfig()
+{
+  std::cout << std::endl << "!!! plugin (" << pluginName << ") " << __func__ << std::endl;
+
+  std::string info = "!!! plugin (" + pluginName + ") " + __func__ + " [Bad configuration]";
+
+  throw nncc::contrib::ConfigException(info);
+}
+
+void *SamplePluginInstance::execute(void *data)
+{
+  std::cout << std::endl << "!!! plugin (" << pluginName << ") " << __func__ << std::endl;
+  return data;
+}
+
+void SamplePluginInstance::setParam(const std::string &name)
+{
+  throw nncc::contrib::ConfigException("bad parameter <" + name + ">");
+}
+
+void SamplePluginInstance::setParam(const std::string &name, const std::string &value)
+{
+  if (name == "filename")
+  {
+    _filename = value;
+  }
+  else
+  {
+    throw nncc::contrib::ConfigException("unsupported parameter <" + name + ">");
+  }
+  std::cout << __func__ << " : " << name << " = " << value << std::endl;
+}
+
+extern "C" AbstractPluginInstance *get_instance()
+{
+  std::cout << std::endl << "!!! plugin (" << pluginName << ") " << __func__ << std::endl;
+  return &SamplePluginInstance::getInstance();
+}