forked from srblum/ruleMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruleMap.html
424 lines (374 loc) · 10.7 KB
/
ruleMap.html
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<!DOCTYPE html>
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
.minimap {
fill: white;
stroke: black;
}
.node {
cursor: pointer;
}
.node rect{
opacity:0.35;
stroke:black;
stroke-width:1.2px;
rx:2;
ry:2;
}
.mininode rect{
opacity:0.35;
stroke:lightgrey;
stroke-width:0.4px;
rx:1;
ry:1;
}
.link {
fill: none;
stroke: grey;
stroke-width:1px;
}
.mini-link{
fill: none;
stroke: lightgrey;
stroke-width:0.1px;
}
g.brush > .handle {
display: none;
}
.overlay {
pointer-events: none;
}
.caption{
position: absolute;
width: 20em;
color: white;
padding: 1em;
background-color: #0e242b;
border-color: #0e242b transparent;
border-radius: 5px;
pointer-events: none;
}
.caption:before{
position: absolute;
top: -15px;
content: '';
width: 0px;
height: 0px;
border-color: inherit;
border-width: 0 15px 15px;
border-style: solid;
}
.caption h3{
color: white;
font-size:18px;
margin:0px;
}
.caption p{
font-size: 14px;
margin:0px;
}
</style>
</head>
<body>
<script>
var input_file = "big.json"
var i = 0,
mr = 4,
nodeSuggestions = [],
root,gWidth,minZoom;
var width = 1000,
height = 600,
nodeWidth = 400,
minimapWidth = width/mr,
minimapHeight,brush,zoom;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
var caption = d3.select("body").append("div")
.attr("class", "caption")
.style("display", "none")
.style("opacity", 1e-6);
d3.json(input_file, function(error, mttree) {
if (error) {
console.log(error);
} else {
var logic = mttree.data.logic
var subRules = mttree.data.subRules
function remove_GOTOs_and_ARFs(d){
var nodes = [d];
while(node = nodes.pop()){
if(node.GOTO){
var children = subRules[node.GOTO]['children']
node.children = children
// nodes.push.apply(nodes,children)
}
if(node.children){
var newChildren = []
for(var i=0; i<node.children.length;i++){
var child = node.children[i]
if(child.entity.entityType.name==='CONDITION_ALWAYS'){
for(var j=0;j<child.children.length;j++){
var grandChild = child.children[j]
newChildren.push(grandChild)
nodes.push(grandChild)
}
}else{
newChildren.push(child)
nodes.push(child)
}
}
node.children = newChildren
}
}
}
remove_GOTOs_and_ARFs(logic)
root = d3.hierarchy(logic, function(d){return d.children || null})
leafs = root.leaves().length
if(leafs > 15){
treemap = d3.tree().size([height * leafs/15, width])
}else{
treemap = d3.tree().size([height, width])
}
treemap(root)
// Compute the new tree layout.
var nodes = root.descendants()
var links = nodes.slice(1)
// Update the nodes...
var node_elems = svg.selectAll('g.node')
.data(nodes, function(d) {return d.id || (d.id = ++i) })
.enter().append('g')
.attr('class', 'node')
.attr("transform", function(d) { return `translate(${d.y},${d.x})`})
.on("click",clicked)
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseout", mouseout)
// Add labels for the nodes
node_elems.append("text")
.attr("x", -10)
.attr("dy", ".35em")
.attr("text-anchor", "start")
.style('font-size','20px')
.style('letter-spacing','0px')
.each(function(d) {
var isTruncated = false,
name;
if(d.data.entity.entityType.name === "OUTCOME"){
name = d.data.entity.content
}else{
name = d.data.entity.name
}
var text = d3.select(this)
text.html(name)
while(this.getBBox().width > nodeWidth){
isTruncated = true
name = name.substring(0,name.length-1)
text.html(name)
}
if(isTruncated) name = name + "..."
text.html(name)
})
.attr('textLength',function(d){return this.getBBox().width}) //fixes safari zoom bug
.each(function(d){
var bbox = this.getBBox()
bbox.width = nodeWidth + 10
d.bbox = bbox
})
root.y = 100
var n = 1
// var maxWidth = root.bbox.width
var maxWidth = nodeWidth
gWidth = 0
while(true){
var level = []
root.each(function(d){
if(d.depth===n){
d.y = d.parent.y + maxWidth + 100
level.push(d)
gWidth = Math.max(gWidth,d.y + d.bbox.width)
}
})
maxWidth = d3.max(level, d => d.bbox.width)
if(level.length === 0){
break
}
n+=1
}
yMin = d3.min(root.leaves(),d=>+d.x)
yMax = d3.max(root.leaves(),d=>+d.x)
gHeight = yMax - yMin
minZoom = width/gWidth
maxZoom = minZoom * 4
gHeight=gHeight > height / minZoom ? gHeight : height / minZoom
node_elems.attr("transform", function(d) { return `translate(${d.y},${d.x})`})
//Add rectangle surrounding node
node_elems.insert('rect','text')
.each(function(d){
var rect = d3.select(this)
// var bbox = this.nextSibling.getBBox()
var bbox = d.bbox
var type = d.data.entity.entityType.name
if(type === 'TOPIC'){
color = "#6D256C"
}else if(type === 'ADVICE' || type === 'OUTCOME'){
color = "#FAAB18"
}else if(type === 'INPUT_REQUEST'){
color = "#3DB1F2"
}else if(type === 'CONDITION'){
color = "#CE1B6D"
}else if(d.data.GOTO){
color = "yellow"
}else{
color = "white"
}
rect.attr('x', bbox.x-5)
.attr('y',bbox.y-2)
.attr('width', bbox.width+10)
.attr('height',bbox.height+4)
.attr('fill',color)
.attr('rx',2)
.attr('ry',2)
});
var link = svg.selectAll('path.link')
.data(links, function(d) { return d.id; })
.enter().insert('path','g')
.attr("class", "link")
.attr('d', function(d){return connection(d.parent, d)})
minimapHeight = minimapWidth * gHeight/gWidth
mr = gWidth / minimapWidth
minimap = d3.select('svg')
.append('g')
.attr("transform", "translate(" + (width - minimapWidth) + "," + (height - minimapHeight) + ")");
minimap.raise()
minimap.append("rect")
.attr("class", "minimap")
.attr("width", minimapWidth)
.attr("height", minimapHeight)
var mini_elems = minimap.selectAll('g.node')
.data(nodes)
.enter().append('g')
.attr('class', 'mininode')
.attr("transform", function(d) { return `translate(${d.y/mr},${d.x/mr})`})
mini_elems.append('rect')
.each(function(d){
var rect = d3.select(this)
var type = d.data.entity.entityType.name
if(type === 'TOPIC'){
color = "#6D256C"
}else if(type === 'ADVICE' || type === 'OUTCOME'){
color = "#FAAB18"
}else if(type === 'INPUT_REQUEST'){
color = "#3DB1F2"
}else if(type === 'CONDITION'){
color = "#CE1B6D"
}else if(d.data.GOTO){
color = "yellow"
}else{
color = "white"
}
rect.attr('x', (d.bbox.x-5)/mr)
.attr('y',(d.bbox.y-2)/mr)
.attr('width', (d.bbox.width+10)/mr)
.attr('height',(d.bbox.height+4)/mr)
.attr('fill',color)
.attr('rx',2)
.attr('ry',2)
});
var link = minimap.selectAll('path.link')
.data(links)
.enter().insert('path','g')
.attr("class", "mini-link")
.attr('d', function(d){return mini_connection(d.parent, d)})
brush = d3.brush()
.extent([[0, 0], [minimapWidth, minimapHeight]])
.on("brush", brushed)
zoom = d3.zoom().on("zoom",zoomed)
.translateExtent([[0,0],[gWidth,gHeight]])
.scaleExtent([minZoom,maxZoom])
d3.select('svg').call(zoom)
.on("dblclick.zoom", null)
minimap.append("g")
.attr("class", "brush")
.call(brush)
.call(brush.move, [[0, 0], [minimapWidth/2, minimapHeight/2]])
}});
// Creates a right-angle path from parent to the child nodes
function connection(s, d) {
sY = s.y + s.bbox.width
path = `M ${sY - 5} ${s.x}
L ${d.y - 45},${s.x}
L ${d.y - 45},${d.x}
L ${d.y - 15},${d.x}`
return path
}
function mini_connection(s, d) {
sY = (s.y + s.bbox.width)/mr
sX = s.x/mr
dY = d.y/mr
dX = d.x/mr
path = `M ${sY - 5/mr} ${sX}
L ${dY - 45/mr},${sX}
L ${dY - 45/mr},${dX}
L ${dY - 15/mr},${dX}`
return path
}
function clicked(d){
console.log(d)
}
// Show the caption
function mouseover(d){
var name
if(d.data.entity.entityType.name === "OUTCOME"){ name = d.data.entity.content}
else{ name = d.data.entity.name}
caption
.html('<h3>' + d.data.entity.entityType.name + '</h3><p>' + name + '</p>')
// .style('background-color', d.data.color)
// .style('border-color', d.data.color + ' transparent')
.transition()
.duration(100)
.style("display", "block")
.style("opacity", 1);
}
// move caption pop-over as mouse moves
function mousemove(d){
caption
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY) + 20 + "px");
}
// hide caption
function mouseout(d){
caption.transition()
.duration(100)
.style("display", "none")
.style("opacity", 1e-6)
}
svgEl = document.querySelector('svg')
function zoomed(){
caption.style("display","none")
var t = d3.event.transform
var kr = t.k/minZoom
brush.move(d3.select('.brush'), [ [-t.x/mr/t.k,
-t.y/mr/t.k],
[-t.x/mr/t.k+minimapWidth/kr,
-t.y/mr/t.k+minimapHeight/kr]
]);
}
function brushed() {
var s = d3.event.selection
var c0 = s[0]
var c1 = s[1]
//minimap coordinates
var x0 = c0[0]
var y0 = c0[1]
var x1 = c1[0]
var y1 = c1[1]
var scale = minimapWidth/(x1-x0) * minZoom
svgEl.__zoom.x = -x0*scale*mr
svgEl.__zoom.y = -y0*scale*mr
svgEl.__zoom.k = scale
svg.attr("transform",`translate(${-x0*scale*mr},${-y0*scale*mr})scale(${scale})`)
}
</script>
</body>