-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphToObject.js
163 lines (136 loc) · 3.93 KB
/
graphToObject.js
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
var graphlib = require("graphlib")
const parse = require('csv-parse')
const fs = require('fs')
const delimiter = "\t"
var graphName
var graph = new graphlib.Graph({ directed: true });
class Label {
constructor(label, value) {
this.value = value
this.label = label
this.object = {}
}
}
function graphToObject(graph){
if(!graphlib.alg.isAcyclic(graph)){
var cycles = graphlib.alg.findCycles(graph)
//console.log(cycles)
var message = ""
for(cycle in cycles){
message = message + cycles[cycle] + "\n"
}
throw("Explanation graph has one or more cycles:\n"+message)
}
var nodes = graphlib.alg.postorder(graph, graphName)
//console.log(nodes)
for (n in nodes){
//console.log(graph.node(nodes[n]))
//console.log(graph.successors(nodes[n]))
//console.log(graph.successors(nodes[n]).length)
if(graph.successors(nodes[n]).length != 0){
//console.log(graph.node(nodes[n]))
//this is object
var children = graph.successors(nodes[n])
var obj = {}
for (child in children){
//console.log(graph.node(children[child]))
let label = graph.node(children[child]).label
let object = graph.node(children[child]).object
if(!obj.hasOwnProperty(label)){
obj[label] = object
}else if(!Array.isArray(obj[label])){
let tmp = obj[label]
obj[label] = [tmp, object]
}else{
obj[label].push(object)
}
//graph.node(children[child]).object = obj
graph.node(nodes[n]).object = obj
//console.log(graph.node(nodes[n]))
}
//console.log(graph)
}else{
graph.node(nodes[n]).object = graph.node(nodes[n]).value
}
}
//console.log(graph.node("explanation"))
var explanation = graph.node(graphName).object
//console.log(explanation)
return explanation
}
function main(csvPath){
const data = []
return new Promise((resolve, reject) => {
fs.createReadStream(csvPath)
.pipe(parse({ delimiter: delimiter }))
.on('data', (r) => {
//console.log(r);
data.push(r);
})
.on('end', () => {
//console.log(data);
count = 0;
graphName = data[0][0]
var a = data[0]
for (d of data){
d[0] = d[0].toLowerCase()
//d[1] = d[1].toLowerCase()
d[2] = d[2].toLowerCase()
//from
if(graph.node(d[0])==null){
graph.setNode(d[0], new Label(d[0], d[0]))
}
//avoid identical numerical nodes and cycles
if(isNaN(d[2]) && d[0]!=d[2]){
graph.setNode(d[2], new Label(d[1], d[2]))
graph.setEdge(d[0], d[2])
}else{
graph.setNode(count, new Label(d[1], d[2]))
graph.setEdge(d[0], count)
count++
}
}
var e = graphToObject(graph)
//console.log(e)
resolve(e)
})
})
}
function test(csvPath){
var data = []
fs.createReadStream(csvPath)
.pipe(parse({ delimiter: delimiter }))
.on('data', (r) => {
//console.log(r);
data.push(r);
})
.on('end', () => {
//console.log(data);
count = 0;
graphName = data[0][0]
var a = data[0]
for (d of data){
d[0] = d[0].toLowerCase()
//d[1] = d[1].toLowerCase()
d[2] = d[2].toLowerCase()
//from
if(graph.node(d[0])==null){
graph.setNode(d[0], new Label(d[0], d[0]))
}
//to
//avoid identical numerical nodes and cycles
if(isNaN(d[2]) && d[0]!=d[2]){
graph.setNode(d[2], new Label(d[1], d[2]))
graph.setEdge(d[0], d[2])
}else{
graph.setNode(count, new Label(d[1], d[2]))
graph.setEdge(d[0], count)
count++
}
}
var e = graphToObject(graph)
console.log(e)
})
}
module.exports = {main}
//test("graphs/exp_graph_1_it.csv")