Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use crate::error::OpossumError;
use crate::optic_ports::OpticPorts;
type Result<T> = std::result::Result<T, OpossumError>;
/// This trait deals with the translation of the OpticScenery-graph structure to the dot-file format which is needed to visualize the graphs
pub trait Dottable {
/// Return component type specific code in 'dot' format for `graphviz` visualization.
fn to_dot(
&self,
node_index: &str,
name: &str,
inverted: bool,
ports: &OpticPorts,
mut parent_identifier: String,
rankdir: &str
) -> Result<String> {
let inv_string = if inverted { " (inv)" } else { "" };
let node_name = format!("{}{}", name, inv_string);
parent_identifier = if parent_identifier.is_empty() {
format!("i{}", node_index)
} else {
format!("{}_i{}", &parent_identifier, node_index)
};
let mut dot_str = format!("\t{} [\n\t\tshape=plaintext\n", &parent_identifier);
let mut indent_level = 2;
dot_str.push_str(&self.add_html_like_labels(
&node_name,
&mut indent_level,
ports,
inverted,
rankdir
));
Ok(dot_str)
}
/// creates a table-cell wrapper around an "inner" string
fn add_table_cell_container(
&self,
inner_str: &str,
border_flag: bool,
indent_level: &mut i32,
) -> String {
if inner_str.is_empty() {
format!(
"{}<TD BORDER=\"{}\">{}</TD>\n",
"\t".repeat(*indent_level as usize),
border_flag,
inner_str
)
} else {
format!(
"{}<TD BORDER=\"{}\">{}{}{}</TD>\n",
"\t".repeat(*indent_level as usize),
border_flag,
inner_str,
"\t".repeat((*indent_level + 1) as usize),
"\t".repeat(*indent_level as usize)
)
}
}
/// create the dot-string of each port
fn create_port_cell_str(
&self,
port_name: &str,
input_flag: bool,
port_index: usize,
) -> String {
// inputs marked as green, outputs as blue
let color_str = if input_flag {
"\"lightgreen\""
} else {
"\"lightblue\""
};
// part of the tooltip that describes if the port is an input or output
let in_out_str = if input_flag {
"Input port"
} else {
"Output port"
};
format!(
"<TD HEIGHT=\"16\" WIDTH=\"16\" PORT=\"{}\" BORDER=\"1\" BGCOLOR={} HREF=\"\" TOOLTIP=\"{} {}: {}\">{}</TD>\n",
port_name,
color_str,
in_out_str,
port_index,
port_name,
port_index
)
}
/// create the dot-string that describes the ports, including their row/table/cell wrappers
fn create_port_cells_str(
&self,
input_flag: bool,
indent_level: &mut i32,
indent_incr: i32,
ports: &OpticPorts,
) -> String {
let mut ports = if input_flag {
ports.inputs()
} else {
ports.outputs()
};
ports.sort();
let mut dot_str = self.create_html_like_container("row", indent_level, true, 1);
dot_str.push_str(&self.create_html_like_container("cell", indent_level, true, 1));
dot_str.push_str(&self.create_html_like_container("table", indent_level, true, 1));
dot_str.push_str(&self.create_html_like_container("row", indent_level, true, 1));
dot_str.push_str(&self.add_table_cell_container("", false, indent_level));
let mut port_index = 1;
for port in ports {
dot_str.push_str(&self.create_port_cell_str(
&port,
input_flag,
port_index,
));
dot_str.push_str(&self.add_table_cell_container("", false, indent_level));
port_index += 1;
}
*indent_level -= 1;
dot_str.push_str(&self.create_html_like_container("row", indent_level, false, -1));
dot_str.push_str(&self.create_html_like_container("table", indent_level, false, -1));
dot_str.push_str(&self.create_html_like_container("cell", indent_level, false, -1));
dot_str.push_str(&self.create_html_like_container("row", indent_level, false, indent_incr));
dot_str
}
/// Returns the color of the node.
fn node_color(&self) -> &str {
"lightgray"
}
fn create_main_node_row_str(&self, node_name: &str, indent_level: &mut i32) -> String {
let mut dot_str = self.create_html_like_container("row", indent_level, true, 1);
dot_str.push_str(&format!("{}<TD BORDER=\"1\" BGCOLOR=\"{}\" ALIGN=\"CENTER\" WIDTH=\"80\" CELLPADDING=\"10\" HEIGHT=\"80\" STYLE=\"ROUNDED\">{}</TD>\n", "\t".repeat(*indent_level as usize), self.node_color(), node_name));
*indent_level -= 1;
dot_str.push_str(&self.create_html_like_container("row", indent_level, false, 0));
dot_str
}
/// starts or ends an html-like container
fn create_html_like_container(
&self,
container_str: &str,
indent_level: &mut i32,
start_flag: bool,
indent_incr: i32,
) -> String {
let container = match container_str {
"row" => {
if start_flag {
"<TR BORDER=\"0\">"
} else {
"</TR>"
}
}
"cell" => {
if start_flag {
"<TD BORDER=\"0\">"
} else {
"</TD>"
}
}
"table" => {
if start_flag {
"<TABLE BORDER=\"0\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\" ALIGN=\"CENTER\">"
} else {
"</TABLE>"
}
}
_ => "Invalid container string!",
};
let new_str = "\t".repeat(*indent_level as usize) + container + "\n";
*indent_level += indent_incr;
new_str
}
fn create_node_table_cells(
&self,
ports: (&Vec<String>,&Vec<String>),
// outputs: &Vec<String>,
ports_count: (&mut usize, &mut usize),
ax_nums: (usize,usize),
node_name: &str
) -> String{
let mut dot_str = "".to_owned();
let max_port_num = if ports.0.len() <= ports.1.len(){
ports.1.len()
} else{
ports.0.len()
};
let port_0_count = ports_count.0;
let port_1_count = ports_count.1;
let (node_cell_size,
num_cells,
row_col_span,
port_0_start,
port_1_start
) = if max_port_num > 1{
(16*(max_port_num*2+1),
(max_port_num+1)*2+1,
max_port_num*2+1,
max_port_num - ports.0.len()+2,
max_port_num - ports.1.len()+2
)
}
else {
(80, 7, 5, 3,3)
};
if port_0_count < &mut ports.0.len() && ax_nums.0 >= port_0_start && (ax_nums.0-port_0_start) % 2 == 0 && ax_nums.1 == 0 {
dot_str.push_str(&self.create_port_cell_str(
&ports.0[*port_0_count],
true,
*port_0_count+1,
));
*port_0_count+=1;
}
else if port_1_count < &mut ports.1.len() && ax_nums.0 >= port_1_start && (ax_nums.0-port_1_start) % 2 == 0 && ax_nums.1 == num_cells-1{
dot_str.push_str(&self.create_port_cell_str(
&ports.1[*port_1_count],
false,
*port_1_count+1,
));
*port_1_count+=1;
}
else if ax_nums.0 == 1 && ax_nums.1 == 1{
dot_str.push_str(&format!( "<TD ROWSPAN=\"{}\" COLSPAN=\"{}\" BGCOLOR=\"{}\" WIDTH=\"{}\" HEIGHT=\"{}\" BORDER=\"1\" ALIGN=\"CENTER\" CELLPADDING=\"10\" STYLE=\"ROUNDED\">{}</TD>\n",
row_col_span,
row_col_span,
self.node_color(),
node_cell_size,
node_cell_size,
node_name));
}
else{
dot_str.push_str("<TD ALIGN=\"CENTER\" HEIGHT=\"16\" WIDTH=\"16\"> </TD>\n")
};
dot_str
}
/// creates the node label defined by html-like strings
fn add_html_like_labels(
&self,
node_name: &str,
indent_level: &mut i32,
ports: &OpticPorts,
inverted: bool,
rankdir:&str,
) -> String {
let mut dot_str = "\t\tlabel=<\n".to_owned();
let mut inputs = ports.inputs();
let mut outputs = ports.outputs();
let mut in_port_count = 0;
let mut out_port_count = 0;
inputs.sort();
outputs.sort();
let num_cells = if inputs.len() <=outputs.len() && outputs.len() > 1{
(outputs.len()+1)*2+1
} else if inputs.len() > outputs.len() && inputs.len() > 1{
(inputs.len()+1)*2+1
}
else{
7
};
let port_list = if inverted{(&outputs, &inputs)}else{(&inputs, &outputs)};
// Start Table environment
dot_str.push_str(&self.create_html_like_container("table", indent_level, true, 1));
//create each cell of the table
for row_num in 0..num_cells{
dot_str.push_str(&self.create_html_like_container("row", indent_level, true, 1));
for col_num in 0..num_cells{
if rankdir == "LR"
&& !(col_num > 1 && col_num <num_cells-1 && row_num>= 1 && row_num <num_cells-1)
&& !(col_num >= 1 && col_num <num_cells-1 && row_num> 1 && row_num <num_cells-1){
dot_str.push_str(&"\t".repeat(*indent_level as usize));
dot_str.push_str(
&self.create_node_table_cells(
port_list,
(&mut in_port_count, &mut out_port_count),
(row_num,col_num),
node_name
)
)
}
else if rankdir != "LR"
&& !(row_num > 1 && row_num <num_cells-1 && col_num>= 1 && col_num <num_cells-1)
&& !(row_num >= 1 && row_num <num_cells-1 && col_num> 1 && col_num <num_cells-1){
dot_str.push_str(&"\t".repeat(*indent_level as usize));
dot_str.push_str(
&self.create_node_table_cells(
port_list,
(&mut in_port_count, &mut out_port_count),
(col_num, row_num),
node_name)
)
};
}
*indent_level -= 1;
dot_str.push_str(&self.create_html_like_container("row", indent_level, false, 0));
}
//end table environment
dot_str.push_str(&self.create_html_like_container("table", indent_level, false, -1));
//end node-shape description
dot_str.push_str(&format!("{}>];\n", "\t".repeat(*indent_level as usize)));
dot_str
}
}