Cerata
A library to generate structural hardware designs
style.cc
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "cerata/dot/style.h"
16 
17 #include <string>
18 #include <sstream>
19 
20 #include "cerata/api.h"
21 
22 namespace cerata::dot {
23 
25  static Style ret;
26 
27  ret.config = Config::normal();
28 
29  ret.subgraph.base = "filled";
31 
32  ret.nodegroup.base = "filled";
34 
35  ret.edge.color.stream = Palette::normal().d[3];
36 
37  ret.edge.base = "penwidth=1";
38  ret.edge.port_to_sig = "dir=forward";
39  ret.edge.sig_to_port = "dir=forward";
40  ret.edge.port_to_port = "dir=forward";
41  ret.edge.param = "style=dotted, arrowhead=none, arrowtail=none";
42  ret.edge.stream = "penwidth=3";
43  ret.edge.lit = "style=dotted, arrowhead=none, arrowtail=none";
44  ret.edge.expr = "style=dotted, arrowhead=none, arrowtail=none";
45 
46  ret.node.color.stream = Palette::normal().b[3];
49 
50  ret.node.color.record = Palette::normal().b[4];
53 
54  ret.node.base = "style=filled, width=0, height=0, margin=0.025";
55 
56  ret.node.port = "shape=rect";
57  ret.node.signal = "shape=ellipse, margin=-0.2";
58  ret.node.parameter = "shape=note, fontsize = 8";
59  ret.node.literal = "shape=plaintext, fontsize = 8";
60  ret.node.expression = "shape=signature";
61 
62  ret.node.nested = "html";
63 
64  ret.node.type.bit = awq("fillcolor", Palette::normal().b[0]);
65  ret.node.type.boolean = awq("fillcolor", Palette::normal().b[1]);
66  ret.node.type.vector = awq("fillcolor", Palette::normal().b[2]);
67  ret.node.type.stream = awq("fillcolor", Palette::normal().b[3]);
68  ret.node.type.record = awq("fillcolor", Palette::normal().b[4]);
69  ret.node.type.integer = awq("fillcolor", Palette::normal().b[5]);
70  ret.node.type.string = awq("fillcolor", Palette::normal().b[6]);
71 
72  return ret;
73 }
74 
75 std::string StyleBuilder::ToString() {
76  std::stringstream str;
77  for (const auto &p : parts) {
78  if (!p.empty()) {
79  str << p;
80  if (p != parts.back()) {
81  str << ", ";
82  }
83  }
84  }
85  return str.str();
86 }
87 
88 StyleBuilder &StyleBuilder::operator<<(const std::string &part) {
89  parts.push_back(part);
90  return *this;
91 }
92 
93 bool Config::operator()(const Node &node) {
94  switch (node.node_id()) {
97  case Node::NodeID::SIGNAL: return nodes.signals;
98  case Node::NodeID::PORT: return nodes.ports;
100  }
101  return true;
102 }
103 
105  static Config ret;
106  ret.nodes.parameters = false;
107  ret.nodes.literals = false;
108  ret.nodes.signals = true;
109  ret.nodes.ports = true;
110  ret.nodes.expressions = false;
111  ret.nodes.types.bit = false;
112  ret.nodes.types.vector = false;
113  ret.nodes.types.record = false;
114  ret.nodes.types.stream = true;
115  return ret;
116 }
117 
119  static Config ret;
120  ret.nodes.parameters = false;
121  ret.nodes.literals = false;
122  ret.nodes.signals = true;
123  ret.nodes.ports = true;
124  ret.nodes.expressions = false;
125 
126  ret.nodes.expand.record = false;
127  ret.nodes.expand.stream = false;
128  ret.nodes.expand.expression = false;
129 
130  ret.nodes.types.bit = true;
131  ret.nodes.types.vector = true;
132  ret.nodes.types.record = true;
133  ret.nodes.types.stream = true;
134 
135  return ret;
136 }
137 
139  static Config ret;
140 
141  ret.nodes.parameters = true;
142  ret.nodes.literals = true;
143  ret.nodes.signals = true;
144  ret.nodes.ports = true;
145  ret.nodes.expressions = true;
146 
147  ret.nodes.expand.record = true;
148  ret.nodes.expand.stream = true;
149  ret.nodes.expand.expression = true;
150 
151  ret.nodes.types.bit = true;
152  ret.nodes.types.vector = true;
153  ret.nodes.types.record = true;
154  ret.nodes.types.stream = true;
155 
156  return ret;
157 }
158 
160  static Palette ret;
161  ret.black = "#000000";
162  ret.white = "#ffffff";
163  ret.gray = "#A0A0A0";
164  ret.dark = "#808080";
165  ret.darker = "#404040";
166  ret.light = "#D0D0D0";
167  ret.lighter = "#E0E0E0";
168  ret.num_colors = 7;
169  ret.b = {"#ff8181", "#ffe081", "#bfff81", "#81ffd1", "#81ceff", "#9381ff", "#f281ff"};
170  ret.m = {"#e85858", "#e8c558", "#9fe858", "#58e8b3", "#58b0e8", "#6c58e8", "#d958e8"};
171  ret.d = {"#c04040", "#c0a140", "#7fc040", "#40c091", "#408fc0", "#5340c0", "#b340c0"};
172  return ret;
173 }
174 
175 std::string Style::GetStyle(const Node &n) {
176  StyleBuilder sb;
177 
178  sb << node.base;
179 
180  // Add label
181  switch (n.type()->id()) {
182  case Type::RECORD:break;
183  case Type::VECTOR: sb << node.type.vector;
184  break;
185  case Type::BIT:sb << node.type.bit;
186  break;
187  case Type::INTEGER:sb << node.type.integer;
188  break;
189  case Type::STRING:sb << node.type.string;
190  break;
191  case Type::BOOLEAN:sb << node.type.boolean;
192  break;
193  default:break;
194  }
195 
196  sb << GetLabel(n);
197 
198  // Add other style
199  switch (n.node_id()) {
200  case Node::NodeID::PORT: sb << node.port;
201  break;
202  case Node::NodeID::SIGNAL:sb << node.signal;
203  break;
205  break;
207  break;
209  break;
210  }
211 
212  return sb.ToString();
213 }
214 
215 std::string Style::GetLabel(const Node &n) {
216  StyleBuilder sb;
217  bool expand = false;
218 
219  if (n.type()->Is(Type::RECORD)) {
220  expand |= config.nodes.expand.record;
221  if (config.nodes.expand.record) {
222  sb << awq("fillcolor", node.color.record_child);
223  sb << awq("color", node.color.record_border);
224  } else {
225  sb << node.type.record;
226  }
227  }
228 
229  std::stringstream str;
230  if (n.type()->IsNested() && expand) {
231  str << "label=<";
232  if (node.nested == "html") {
233  str << GenHTMLTableCell(*n.type(), n.name());
234  } else {
235  str << GenDotRecordCell(*n.type(), n.name());
236  }
237  str << ">";
238  } else if (n.IsParameter()) {
239  auto par = dynamic_cast<const Parameter &>(n);
240  str << "label=\"" + sanitize(par.name());
241  auto val = par.value();
242  str << ":" << sanitize(val->ToString());
243  str << "\"";
244  } else {
245  str << "label=\"" + sanitize(n.name()) + "\"";
246  }
247 
248  sb << str.str();
249 
250  return sb.ToString();
251 }
252 
253 } // namespace cerata::dot
cerata::Type::IsNested
virtual bool IsNested() const =0
Return true if the Type is a nested, false otherwise.
cerata::dot::Style::NodeStyle::type
struct cerata::dot::Style::NodeStyle::TypeStyle type
Styles for types.
cerata::dot::Config::NodeConfig::signals
bool signals
Show signals.
Definition: style.h:88
cerata::Type::RECORD
@ RECORD
? | ? | Yes
Definition: type.h:77
cerata::dot::Config::nodes
struct cerata::dot::Config::NodeConfig nodes
Node configuration.
cerata::dot::Style::subgraph
struct cerata::dot::Style::SubGraph subgraph
Style for sub graphs.
cerata::dot::Config::NodeConfig::ExpandConfig::stream
bool stream
Expand streams.
Definition: style.h:95
cerata::dot::Config::normal
static Config normal()
Return a configuration that will generate default constructs.
Definition: style.cc:118
cerata::Type::BOOLEAN
@ BOOLEAN
No | No | No.
Definition: type.h:75
cerata::dot::Style::NodeStyle::nested
str nested
Style for nested nodes.
Definition: style.h:171
cerata::dot::Style::NodeStyle::Colors::record
str record
Record node color.
Definition: style.h:161
cerata::dot::Style::EdgeStyle::param
str param
Style for parameter edges.
Definition: style.h:148
cerata::Node::NodeID::LITERAL
@ LITERAL
No-input AND multi-output node with storage type and storage value.
cerata::dot::Style::EdgeStyle::base
str base
Base style.
Definition: style.h:144
cerata::dot::Style::NodeStyle::Colors::stream_child
str stream_child
Stream child color.
Definition: style.h:160
cerata::dot::Style::normal
static Style normal()
Default style.
Definition: style.cc:24
cerata::dot::Config::NodeConfig::expressions
bool expressions
Show expressions.
Definition: style.h:90
cerata::dot::Palette::darker
std::string darker
Darker gray color.
Definition: style.h:60
cerata::dot::Style::NodeStyle::parameter
str parameter
Style for parameters.
Definition: style.h:168
cerata::dot::Style::NodeGroup::base
str base
Base style for groups.
Definition: style.h:133
cerata::dot::Style::EdgeStyle::port_to_port
str port_to_port
Style for port-to-port.
Definition: style.h:147
cerata::Type::BIT
@ BIT
Yes | No | No.
Definition: type.h:70
cerata::dot::Config::NodeConfig::ExpandConfig::record
bool record
Expand records.
Definition: style.h:94
cerata::dot::Style::EdgeStyle::expr
str expr
Style for expressions.
Definition: style.h:151
cerata::dot::Palette::light
std::string light
Light gray color.
Definition: style.h:62
cerata::dot::Style::EdgeStyle::color
struct cerata::dot::Style::EdgeStyle::Colors color
Colors for specific edges.
cerata::dot::Palette::white
std::string white
White color.
Definition: style.h:58
cerata::dot::Style::NodeStyle::Colors::stream
str stream
Stream node color.
Definition: style.h:158
cerata::dot::Palette::dark
std::string dark
Very dark gray color.
Definition: style.h:61
cerata::dot::Style::SubGraph::color
str color
Subgraph color.
Definition: style.h:128
cerata::dot::Style::str
std::string str
Short-hand for std::string.
Definition: style.h:123
cerata::Node
A node.
Definition: node.h:42
cerata::dot::Style::NodeGroup::color
str color
Color for groups.
Definition: style.h:134
cerata::dot::Style::NodeStyle::literal
str literal
Style for literals.
Definition: style.h:169
cerata::dot::Style::NodeStyle::TypeStyle::bit
str bit
Style for bits.
Definition: style.h:175
cerata::dot::Style::GetStyle
std::string GetStyle(const Node &n)
Get the style for a node.
Definition: style.cc:175
cerata::dot::StyleBuilder
Convenience structure to build up dot styles.
Definition: style.h:74
cerata::dot::awq
std::string awq(const std::string &attribute, const std::string &style)
Assign with quotes.
Definition: style.h:46
cerata::dot::Config::NodeConfig::parameters
bool parameters
Show parameters.
Definition: style.h:86
cerata::Type::Is
bool Is(ID type_id) const
Return true if the Type ID is type_id, false otherwise.
Definition: type.cc:28
cerata::dot::Config::all
static Config all()
Return a configuration that will generate every construct.
Definition: style.cc:138
cerata::Type::id
ID id() const
Return the Type ID.
Definition: type.h:88
cerata::dot::StyleBuilder::operator<<
StyleBuilder & operator<<(const std::string &part)
Append a part to the style.
Definition: style.cc:88
cerata::Node::NodeID::PARAMETER
@ PARAMETER
Single-input AND multi-output node with default value.
cerata::dot::Style::EdgeStyle::port_to_sig
str port_to_sig
Style for port-to-signal.
Definition: style.h:145
cerata::dot::Config
DOT output configuration. Determines what Cerata constructs will be used for generation.
Definition: style.h:83
cerata::dot::Config::streams
static Config streams()
Return a configuration that will generate onnly stream constructs.
Definition: style.cc:104
cerata::dot::Style::NodeStyle::TypeStyle::integer
str integer
Style for integers.
Definition: style.h:180
cerata::dot::Style::nodegroup
struct cerata::dot::Style::NodeGroup nodegroup
Style for group of nodes.
cerata::Node::node_id
NodeID node_id() const
Return the node type ID.
Definition: node.h:62
cerata::Named::name
std::string name() const
Return the name of the object.
Definition: utils.h:45
cerata::dot::Style::NodeStyle::Colors::record_border
str record_border
Record border color.
Definition: style.h:162
cerata::dot::StyleBuilder::parts
std::vector< std::string > parts
Definition: style.h:75
cerata::dot::Style::NodeStyle::port
str port
Style for ports.
Definition: style.h:166
cerata::Node::NodeID::EXPRESSION
@ EXPRESSION
No-input AND multi-output node that forms a binary tree with operations and nodes.
cerata::dot::Style::EdgeStyle::stream
str stream
Style for stream edges.
Definition: style.h:149
cerata::Type::STRING
@ STRING
No | No | No.
Definition: type.h:74
cerata::dot
Contains everything related to the DOT back-end.
Definition: dot.cc:27
cerata::dot::Config::NodeConfig::types
struct cerata::dot::Config::NodeConfig::TypeConfig types
Type configuration.
cerata::dot::Style::GetLabel
std::string GetLabel(const Node &n)
Get the label for a node.
Definition: style.cc:215
cerata::dot::Config::operator()
bool operator()(const Node &node)
Return whether a node should be generated on the DOT graph.
Definition: style.cc:93
cerata::dot::Config::NodeConfig::TypeConfig::record
bool record
Show record types.
Definition: style.h:103
cerata::dot::Style::node
struct cerata::dot::Style::NodeStyle node
Style for nodes.
cerata::dot::Config::NodeConfig::expand
struct cerata::dot::Config::NodeConfig::ExpandConfig expand
Configures what types of nodes to expand.
cerata::Node::NodeID::SIGNAL
@ SIGNAL
Single-input AND multi-output node.
cerata::dot::Palette::gray
std::string gray
Gray color.
Definition: style.h:59
cerata::dot::Palette::lighter
std::string lighter
Lighter gray color.
Definition: style.h:63
cerata::dot::Palette
A color Palette.
Definition: style.h:55
cerata::Parameter
A Parameter node.
Definition: parameter.h:29
cerata::dot::Style::NodeStyle::TypeStyle::record
str record
Style for records.
Definition: style.h:179
cerata::dot::Style::NodeStyle::TypeStyle::stream
str stream
Style for streams.
Definition: style.h:178
cerata::dot::Style::edge
struct cerata::dot::Style::EdgeStyle edge
Style for edges.
cerata::dot::Style
Dot style configuration.
Definition: style.h:121
cerata::dot::Config::NodeConfig::ExpandConfig::expression
bool expression
Expand expressions.
Definition: style.h:96
cerata::dot::Style::SubGraph::base
str base
Subgraph base style.
Definition: style.h:127
cerata::dot::Style::EdgeStyle::sig_to_port
str sig_to_port
Style for signal-to-port.
Definition: style.h:146
cerata::dot::Config::NodeConfig::TypeConfig::stream
bool stream
Show stream types.
Definition: style.h:104
cerata::dot::Palette::d
std::vector< std::string > d
Dark.
Definition: style.h:67
cerata::dot::StyleBuilder::ToString
std::string ToString()
Generate the style string.
Definition: style.cc:75
cerata::dot::Config::NodeConfig::TypeConfig::vector
bool vector
Show vector types.
Definition: style.h:102
cerata::dot::Style::config
Config config
Configuration of what types of constructs to show or hide for this style.
Definition: style.h:186
cerata::dot::Palette::b
std::vector< std::string > b
Bright.
Definition: style.h:65
cerata::dot::Style::GenDotRecordCell
static std::string GenDotRecordCell(const Type &t, const std::string &name, int level=0)
Generate a DOT record cell from a type.
cerata::Type::INTEGER
@ INTEGER
No | No | No.
Definition: type.h:73
cerata::dot::Config::NodeConfig::literals
bool literals
Show literals.
Definition: style.h:87
cerata::dot::Style::EdgeStyle::lit
str lit
Style for literal edges.
Definition: style.h:150
cerata::dot::Palette::black
std::string black
Black color.
Definition: style.h:57
cerata::dot::sanitize
std::string sanitize(std::string in)
Sanitize a string for usage in DOT.
Definition: style.h:38
cerata::dot::Style::NodeStyle::TypeStyle::boolean
str boolean
Style for booleans.
Definition: style.h:176
cerata::dot::Style::NodeStyle::Colors::stream_border
str stream_border
Stream border color.
Definition: style.h:159
cerata::dot::Style::NodeStyle::TypeStyle::vector
str vector
Style for vectors.
Definition: style.h:177
cerata::dot::Config::NodeConfig::ports
bool ports
Show ports.
Definition: style.h:89
cerata::dot::Style::NodeStyle::expression
str expression
Style for expressions.
Definition: style.h:170
cerata::dot::Config::NodeConfig::TypeConfig::bit
bool bit
Show bit types.
Definition: style.h:101
cerata::Node::NodeID::PORT
@ PORT
Single-input AND multi-output node with direction.
cerata::dot::Style::NodeStyle::signal
str signal
Style for signals.
Definition: style.h:167
cerata::dot::Palette::normal
static Palette normal()
Default palette.
Definition: style.cc:159
cerata::dot::Style::GenHTMLTableCell
std::string GenHTMLTableCell(const Type &t, const std::string &name, int level=0)
Generate a HTML table cell from a type.
Definition: dot.cc:140
cerata::Type::VECTOR
@ VECTOR
Yes | Yes | No.
Definition: type.h:71
cerata::dot::Style::NodeStyle::Colors::record_child
str record_child
Record child color.
Definition: style.h:163
cerata::dot::Style::NodeStyle::TypeStyle::string
str string
Style for strings.
Definition: style.h:181
cerata::dot::Palette::m
std::vector< std::string > m
Medium.
Definition: style.h:66
cerata::dot::Style::EdgeStyle::Colors::stream
str stream
Colors for stream edges.
Definition: style.h:141
cerata::dot::Style::NodeStyle::color
struct cerata::dot::Style::NodeStyle::Colors color
Colors for specific nodes.
cerata::dot::Style::NodeStyle::base
str base
Base node style.
Definition: style.h:165
cerata::Node::type
Type * type() const
Return the node Type.
Definition: node.h:57
cerata::dot::Palette::num_colors
int num_colors
Number of colors of this Palette.
Definition: style.h:56