[neurun][dotdumper] Introduce NodeAttr class (#3244)
author김수진/동작제어Lab(SR)/Engineer/삼성전자 <sjsujin.kim@samsung.com>
Fri, 19 Oct 2018 00:53:15 +0000 (09:53 +0900)
committer오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Fri, 19 Oct 2018 00:53:15 +0000 (09:53 +0900)
This commit introduces `NodeAttr` class that can be more easier to add attributes of node.

- ASIS
```
_dot << dotinfo.index_str() << " [shape=" << dotinfo.dot_shape() << " "
     << "label=\"" << dotinfo.label() << "\" "
     << "style=\"filled\" "
     << "fillcolor=\"" << dotinfo.bg_color() << "\"];\n";

- TOBE
```
NodeAttr attr;

attr.addAttr("shape", dotinfo.dot_shape())
    .addAttr("label", dotinfo.label())
    .addAttr("style", "filled")
    .addAttr("fillcolor", dotinfo.bg_color());
attr.finish();

_dot << dotinfo.index_str() << attr.attr_stream();
```

Signed-off-by: sjsujinkim <sjsujin.kim@samsung.com>
runtimes/neurun/src/dumper/dot/DotBuilder.cc
runtimes/neurun/src/dumper/dot/DotBuilder.h

index 728a9b9..0ee203a 100644 (file)
@@ -23,6 +23,26 @@ namespace dumper
 namespace dot
 {
 
+// NodeAttr
+NodeAttr &NodeAttr::addAttr(const std::string &name, const std::string &attr)
+{
+  _attrs.emplace_back(name, attr);
+
+  return *this;
+}
+
+void NodeAttr::finish()
+{
+  _attr_stream << "[";
+  for (auto attr : _attrs)
+  {
+    _attr_stream << attr.first << "="
+                 << "\"" << attr.second << "\" ";
+  }
+  _attr_stream << "];\n";
+}
+
+// DotDumper
 DotBuilder::DotBuilder() {}
 
 void DotBuilder::update(const IDotInfo &node_info)
@@ -43,10 +63,15 @@ void DotBuilder::writeDot(std::ostream &os)
 
 void DotBuilder::addNode(const IDotInfo &dotinfo)
 {
-  _dot << dotinfo.index_str() << " [shape=" << dotinfo.dot_shape() << " "
-       << "label=\"" << dotinfo.label() << "\" "
-       << "style=\"filled\" "
-       << "fillcolor=\"" << dotinfo.bg_color() << "\"];\n";
+  NodeAttr attr;
+  attr.addAttr("shape", dotinfo.dot_shape())
+      .addAttr("label", dotinfo.label())
+      .addAttr("style", "filled")
+      .addAttr("fillcolor", dotinfo.bg_color());
+
+  attr.finish();
+
+  _dot << dotinfo.index_str() << attr.attr_stream();
 }
 
 void DotBuilder::addEdge(const IDotInfo &dotinfo1, const IDotInfo &dotinfo2)
index ae3ef83..5d9c42d 100644 (file)
@@ -38,6 +38,23 @@ namespace dumper
 namespace dot
 {
 
+class NodeAttr
+{
+public:
+  NodeAttr() = default;
+
+public:
+  void finish();
+  NodeAttr &addAttr(const std::string &name, const std::string &attr);
+
+public:
+  std::stringbuf *attr_stream() { return _attr_stream.rdbuf(); }
+
+private:
+  std::vector<std::pair<std::string, std::string>> _attrs;
+  std::stringstream _attr_stream;
+};
+
 class DotBuilder
 {
 public: