surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #include "DotSerializer.hpp" |
| 7 | |
| 8 | #include <boost/assert.hpp> |
| 9 | #include <sstream> |
| 10 | #include <cstring> |
| 11 | |
| 12 | namespace armnn |
| 13 | { |
| 14 | |
| 15 | namespace |
| 16 | { |
| 17 | std::string Indent(int numSpaces) |
| 18 | { |
| 19 | std::stringstream ss; |
| 20 | for (int i = 0; i < numSpaces; i++) |
| 21 | { |
| 22 | ss << " "; |
| 23 | } |
| 24 | return ss.str(); |
| 25 | } |
| 26 | } //namespace |
| 27 | |
| 28 | |
| 29 | HtmlFont::HtmlFont(std::ostream& stream, int fontSize, const char *color, const char *face) |
| 30 | : DotBase(stream) |
| 31 | { |
| 32 | GetStream() << "<FONT"; |
| 33 | |
| 34 | if (fontSize > -1) |
| 35 | { |
| 36 | GetStream() << " POINT-SIZE=" << "\"" << fontSize << "\""; |
| 37 | } |
| 38 | |
| 39 | if (color && std::strlen(color) != 0) |
| 40 | { |
| 41 | GetStream() << " COLOR=\"" << color << "\" "; |
| 42 | } |
| 43 | |
| 44 | if (face && std::strlen(face) != 0) |
| 45 | { |
| 46 | GetStream() << " FACE=\"" << face << "\" "; |
| 47 | } |
| 48 | |
| 49 | GetStream() << ">"; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | HtmlFont::HtmlFont(std::ostream& stream) |
| 54 | : HtmlFont(stream, -1, nullptr, nullptr) |
| 55 | {} |
| 56 | |
| 57 | HtmlFont::~HtmlFont() |
| 58 | { |
| 59 | GetStream() << "</FONT>"; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | DotAttributeSet::DotAttributeSet(std::ostream& stream) |
| 64 | : DotBase(stream) |
| 65 | { |
| 66 | GetStream() << "["; |
| 67 | } |
| 68 | |
| 69 | DotAttributeSet::~DotAttributeSet() |
| 70 | { |
| 71 | bool doSpace=false; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 72 | for (auto&& attrib : m_Attributes) |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 73 | { |
| 74 | if (doSpace) |
| 75 | { |
| 76 | GetStream() << " "; |
| 77 | } |
| 78 | |
| 79 | GetStream() << attrib; |
| 80 | doSpace=true; |
| 81 | } |
| 82 | |
| 83 | GetStream() << "]"; |
| 84 | } |
| 85 | |
| 86 | DotAttributeSet & DotAttributeSet::AddAttribute(const std::string& name, const std::stringstream& value) |
| 87 | { |
| 88 | std::stringstream ss; |
| 89 | ss << name <<"=" << value.str(); |
| 90 | m_Attributes.push_back(ss.str()); |
| 91 | return *this; |
| 92 | } |
| 93 | |
| 94 | DotAttributeSet & DotAttributeSet::AddAttribute(const std::string& name, int value) |
| 95 | { |
| 96 | std::stringstream ss; |
| 97 | ss << name <<"=" << value; |
| 98 | m_Attributes.push_back(ss.str()); |
| 99 | return *this; |
| 100 | } |
| 101 | |
| 102 | DotAttributeSet & DotAttributeSet::AddAttribute(const std::string& name, const std::string& value) |
| 103 | { |
| 104 | std::stringstream ss; |
| 105 | ss << name <<"=\"" << value << "\""; |
| 106 | m_Attributes.push_back(ss.str()); |
| 107 | return *this; |
| 108 | } |
| 109 | |
| 110 | DotEdge::DotEdge(std::ostream& stream, unsigned int fromNodeId, unsigned int toNodeId) |
| 111 | : DotBase(stream) |
| 112 | { |
| 113 | std::stringstream ss; |
| 114 | ss << Indent(4) << fromNodeId << " -> " << toNodeId << " "; |
| 115 | GetStream() << ss.str(); |
| 116 | |
| 117 | m_Attributes = std::make_unique<DotAttributeSet>(stream); |
| 118 | } |
| 119 | |
| 120 | DotEdge::~DotEdge() |
| 121 | { |
| 122 | m_Attributes.reset(nullptr); |
| 123 | GetStream() << ";" << std::endl; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | NodeContent::NodeContent(std::ostream& stream) |
| 128 | : DotBase(stream) |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | NodeContent & NodeContent::SetName(const std::string & name) |
| 133 | { |
| 134 | m_Name = name; |
| 135 | return *this; |
| 136 | } |
| 137 | |
| 138 | NodeContent & NodeContent::AddContent(const std::string & content) |
| 139 | { |
| 140 | m_Contents.push_back(content); |
| 141 | return *this; |
| 142 | } |
| 143 | |
| 144 | NodeContent::~NodeContent() |
| 145 | { |
| 146 | std::stringstream ss; |
| 147 | ss << "label=\"{" << m_Name; |
| 148 | if (!m_Contents.empty()) |
| 149 | { |
| 150 | ss << "|"; |
| 151 | } |
| 152 | for (auto & content : m_Contents) |
| 153 | { |
| 154 | ss << content; |
| 155 | ss << "\\l"; |
| 156 | } |
| 157 | ss << "}\""; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 158 | |
| 159 | std::string s; |
| 160 | try |
| 161 | { |
| 162 | // Coverity fix: std::stringstream::str() may throw an exception of type std::length_error. |
| 163 | s = ss.str(); |
| 164 | } |
| 165 | catch (const std::exception&) { } // Swallow any exception. |
| 166 | |
| 167 | GetStream() << s; |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | DotNode::DotNode(std::ostream& stream, unsigned int nodeId, const char* label) |
| 171 | : DotBase(stream) |
| 172 | { |
| 173 | std::stringstream ss; |
| 174 | ss << Indent(4) << nodeId; |
| 175 | |
| 176 | GetStream() << ss.str() << " "; |
| 177 | |
| 178 | m_Contents = std::make_unique<NodeContent>(stream); |
| 179 | m_Attributes = std::make_unique<DotAttributeSet>(stream); |
| 180 | |
| 181 | if (std::strlen(label) != 0) |
| 182 | { |
| 183 | m_Contents->SetName(label); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | m_Contents->SetName("<noname>"); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | DotNode::~DotNode() |
| 192 | { |
| 193 | m_Contents.reset(nullptr); |
| 194 | m_Attributes.reset(nullptr); |
| 195 | GetStream() << ";" << std::endl; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | DotDefaults::DotDefaults(std::ostream& stream, const char* type) |
| 200 | : DotBase(stream) |
| 201 | { |
| 202 | std::stringstream ss; |
| 203 | ss << Indent(4) << type; |
| 204 | |
| 205 | GetStream() << ss.str() << " "; |
| 206 | m_Attributes = std::make_unique<DotAttributeSet>(stream); |
| 207 | } |
| 208 | |
| 209 | DotDefaults::~DotDefaults() |
| 210 | { |
| 211 | m_Attributes.reset(nullptr); |
| 212 | GetStream() << ";" << std::endl; |
| 213 | } |
| 214 | |
| 215 | DotGraph::DotGraph(std::ostream& stream, const char* name) |
| 216 | : DotBase(stream) |
| 217 | { |
| 218 | GetStream() << "digraph " << name << " {" << std::endl; |
| 219 | } |
| 220 | |
| 221 | DotGraph::~DotGraph() |
| 222 | { |
| 223 | GetStream() << "}" << std::endl; |
| 224 | } |
| 225 | |
| 226 | } //namespace armnn |
| 227 | |
| 228 | |