Commit 40142450 authored by Aayush's avatar Aayush

updated parser for pd_diagram->ascii_art

parent bf1c4cc3
Pipeline #1668 failed with stage
in 0 seconds
"use strict";
function makeArray(w, h, val) {
function makeArray(h, w, val) {
var arr = [];
for(let i = 0; i < h; i++) {
arr[i] = [];
arr.push([]);
for(let j = 0; j < w; j++) {
arr[i][j] = val;
arr[i].push(val);
}
}
return arr;
......@@ -16,7 +17,7 @@ function compare(a,b) {
return a.y - b.y;
}
function make_connection(matrix, connection, object1, object2, cell_x1, cell_y1, cell_x2, cell_y2) {
function make_connection(matrix, cell_x1, cell_y1, cell_x2, cell_y2) {
if ( cell_y1 == cell_y2 ) {
for ( let i = cell_x1+1 ; i < cell_x2 ; i++ ) {
matrix[i][cell_y1] = "|";
......@@ -45,9 +46,10 @@ function make_connection(matrix, connection, object1, object2, cell_x1, cell_y1,
}
function parse_pd_message(message) {
var i,j,l,lines,
var i,j,l,k,lines,
splitted_str,
temp_str,
final_str="",
obj_msg_info = [],
connection_info = [],
count = 0,
......@@ -58,14 +60,13 @@ function parse_pd_message(message) {
min_y=Infinity,
max_x=0,
max_y=0,
max_length=0,
matrix;
lines = message.split(";");
for ( i = 0 ; i<lines.length-1 ; i++ ) {
lines[i].replace(";","");
splitted_str = lines[i].split(" ");
splitted_str = lines[i].split(" ");
if ( splitted_str[1] == "connect") {
connection_info.push({start : splitted_str[2],
end : splitted_str[4],
......@@ -73,46 +74,52 @@ function parse_pd_message(message) {
point2 : splitted_str[5]});
} else {
temp_str = splitted_str.slice(4,splitted_str.length);
final_str = temp_str.join(" ");
obj_msg_info.push({type : splitted_str[1],
x : splitted_str[3],
y : splitted_str[2],
obj_no : count,
text : temp_str,
text : final_str,
cell_start_x : 0,
cell_start_y : 0,
cell_end_x : 0,
cell_end_y : 0});
count = count+1;
if (min_y > splitted_str[2]) {
min_y = splitted_str[2];
var b1 = parseInt(splitted_str[2]);
var b2 = parseInt(splitted_str[3]);
if (min_y > b1) {
min_y = b1;
}
if (min_x > splitted_str[3]) {
min_x = splitted_str[3];
if (min_x > b2) {
min_x = b2;
}
if (max_y < splitted_str[2]) {
max_y = splitted_str[2];
if (max_y < b1) {
max_y = b1;
}
if (max_x < splitted_str[3]) {
max_x = splitted_str[3];
if (max_length < final_str.length){
max_length = final_str.length;
}
if (max_x < b2) {
max_x = b2;
}
}
}
matrix = makeArray(max_x/20 + 5,max_y/20 + 5,'*');
matrix = makeArray(Math.floor((max_x-min_x)/10) + 1,Math.floor((max_y-min_y)/10) + max_length + 3,'*');
obj_msg_info.sort(compare);
i=0;
j=0;
counter=0;
while( counter < obj_msg_info.length ) {
obj_msg_info[counter].cell_start_x = Math.floor((obj_msg_info[counter].x - min_x)/20);
obj_msg_info[counter].cell_start_y = Math.floor((obj_msg_info[counter].y - min_y)/20);
obj_msg_info[counter].cell_start_x = Math.floor((obj_msg_info[counter].x - min_x)/10);
obj_msg_info[counter].cell_start_y = Math.floor((obj_msg_info[counter].y - min_y)/10);
i = obj_msg_info[counter].cell_start_x;
j = obj_msg_info[counter].cell_start_y;
matrix[i][j] = "[";
j++;
str_count=0;
for ( l = j ; l < j + obj_msg_info[counter].text[0].length ; l++ ) {
matrix[i][l] = obj_msg_info[counter].text[0][str_count];
for ( l = j ; l < j + obj_msg_info[counter].text.length ; l++ ) {
matrix[i][l] = obj_msg_info[counter].text[str_count];
str_count++;
}
j=l;
......@@ -135,26 +142,26 @@ function parse_pd_message(message) {
for ( i = 0 ; i < connection_info.length ; i++ ) {
if ( object_msg_index_info[connection_info[i].start].cell_start_x < object_msg_index_info[connection_info[i].end].cell_start_x ) {
if ( connection_info[i].point1 == 0 && connection_info[i].point2 == 0 ) {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start].cell_start_x, object_msg_index_info[connection_info[i].start].cell_start_y, object_msg_index_info[connection_info[i].end].cell_start_x, object_msg_index_info[connection_info[i].end].cell_start_y );
make_connection(matrix, object_msg_index_info[connection_info[i].start].cell_start_x, object_msg_index_info[connection_info[i].start].cell_start_y, object_msg_index_info[connection_info[i].end].cell_start_x, object_msg_index_info[connection_info[i].end].cell_start_y );
} else if ( connection_info[i].point1 == 0 && connection_info[i].point2 != 0 ) {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start].cell_start_x, object_msg_index_info[connection_info[i].start].cell_start_y, object_msg_index_info[connection_info[i].end].cell_end_x , object_msg_index_info[connection_info[i].end].cell_end_y );
make_connection(matrix, object_msg_index_info[connection_info[i].start].cell_start_x, object_msg_index_info[connection_info[i].start].cell_start_y, object_msg_index_info[connection_info[i].end].cell_end_x , object_msg_index_info[connection_info[i].end].cell_end_y );
} else if ( connection_info[i].point1 != 0 && connection_info[i].point2 == 0 ) {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start].cell_end_x, object_msg_index_info[connection_info[i].start].cell_end_y, object_msg_index_info[connection_info[i].end].cell_start_x, object_msg_index_info[connection_info[i].end].cell_start_y );
make_connection(matrix, object_msg_index_info[connection_info[i].start].cell_end_x, object_msg_index_info[connection_info[i].start].cell_end_y, object_msg_index_info[connection_info[i].end].cell_start_x, object_msg_index_info[connection_info[i].end].cell_start_y );
} else {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start].cell_end_x, object_msg_index_info[connection_info[i].start].cell_end_y, object_msg_index_info[connection_info[i].end].cell_end_x , object_msg_index_info[connection_info[i].end].cell_end_y );
make_connection(matrix, object_msg_index_info[connection_info[i].start].cell_end_x, object_msg_index_info[connection_info[i].start].cell_end_y, object_msg_index_info[connection_info[i].end].cell_end_x , object_msg_index_info[connection_info[i].end].cell_end_y );
}
} else {
if ( connection_info[i].point1 == 0 && connection_info[i].point2 == 0 ) {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end].cell_start_x, object_msg_index_info[connection_info[i].end].cell_start_y,
object_msg_index_info[connection_info[i].start].cell_start_x, object_msg_index_info[connection_info[i].start].cell_start_y);
make_connection(matrix, object_msg_index_info[connection_info[i].end].cell_start_x, object_msg_index_info[connection_info[i].end].cell_start_y,
object_msg_index_info[connection_info[i].start].cell_start_x, object_msg_index_info[connection_info[i].start].cell_start_y);
} else if ( connection_info[i].point1 == 0 && connection_info[i].point2 != 0 ) {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end].cell_end_x , object_msg_index_info[connection_info[i].end].cell_end_y,
make_connection(matrix, object_msg_index_info[connection_info[i].end].cell_end_x , object_msg_index_info[connection_info[i].end].cell_end_y,
object_msg_index_info[connection_info[i].start].cell_start_x, object_msg_index_info[connection_info[i].start].cell_start_y);
} else if ( connection_info[i].point1 != 0 && connection_info[i].point2 == 0 ) {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end].cell_start_x, object_msg_index_info[connection_info[i].end].cell_start_y,
make_connection(matrix, object_msg_index_info[connection_info[i].end].cell_start_x, object_msg_index_info[connection_info[i].end].cell_start_y,
object_msg_index_info[connection_info[i].start].cell_end_x, object_msg_index_info[connection_info[i].start].cell_end_y);
} else {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end].cell_end_x , object_msg_index_info[connection_info[i].end].cell_end_y, object_msg_index_info[connection_info[i].start].cell_end_x, object_msg_index_info[connection_info[i].start].cell_end_y );
make_connection(matrix, object_msg_index_info[connection_info[i].end].cell_end_x , object_msg_index_info[connection_info[i].end].cell_end_y, object_msg_index_info[connection_info[i].start].cell_end_x, object_msg_index_info[connection_info[i].start].cell_end_y );
}
}
}
......@@ -173,5 +180,4 @@ function parse_pd_message(message) {
}
return ascii_art;
}
exports.parse_pd_message = parse_pd_message;
\ No newline at end of file
exports.parse_pd_message = parse_pd_message;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment