-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathottograf.jq
117 lines (117 loc) · 3.79 KB
/
ottograf.jq
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
. as $all
##
#
# Find @relation hints in the @context
#
#
| .["@context"] as $context
| $context
| to_entries
| map(select(.value | .["@relation"]? == true))
| [ .[].key ] as $links
##
#
# Build root object by removing @relation and @context keys
#
#
| $all
| del(.["@context"])
| to_entries
| map(select( [.key] as $keys
| $links
| contains($keys)
| not ))
| from_entries
| . as $root
# | reduce ( $opt_transform | fromjson )[] as $transform ( $root
# ; .
# | with_entries(.key |= ( if . == ( $transform | .input | tostring )
# then ( $transform | .output | tostring )
# else . end )) ) | . as $root
# | with_entries(.key |= ( if . == "@id" then "id" else . end )) | . as $root
##
#
# Recursively traverse the input and recognise unique nodes.
#
#
| $all
| del(.["@context"])
| [ ..
| objects
| to_entries
| map(select( [.key] as $keys
| $links
| contains($keys)))
| reduce .[] as $item ( []
; . + ( [ $item.value[]
| {"type": $item.key } + .
] ) )
] | reduce .[] as $item ( []
; . + $item )
| unique_by(.["@id"]) | . as $nodes
| $nodes
| [ .[]
| to_entries
| map(select( [.key] as $keys
| $links
| contains($keys)
| not ))
| from_entries
# | with_entries(.key |= ( if . == "@id" then "id" else . end ))
] as $nodes
# Add root object to the nodes array.
| ( [ $root ] + $nodes ) as $nodes
##
#
# Build the edges array.
#
#
| [ $all
| del(.["@context"])
| ..
| .["@id"]? as $id
| $links[]
| . as $link
| $all
| ..
| objects
| select(.["@id"]? == $id)
| to_entries
| map(select( [.key] as $keys
| [ $link ]
| contains($keys)))
| [ .[].value[]
| { ($opt_source) : ( if ($opt_index == "numeric") then ( [ $nodes[] | .["@id"] ]| index($id) ) else $id end )
, type : $link
, ($opt_target) : ( if ($opt_index == "numeric") then ( .["@id"] as $target | [ $nodes[] | .["@id"] ]| index($target) ) else (.["@id"]?) end)
} ]
] | reduce .[] as $item ( []
; . + $item )
| [ .[] | select( (.["\($opt_target)"]? | length) !=0 ) ]
| . as $edges
##
#
# Transform input keys into specified output keys.
#
#
| $nodes
| [ .[] as $node
| reduce ( $opt_transform | fromjson )[] as $transform_nodes ( $node
; with_entries(.key |= ( if . == ( $transform_nodes | .input | tostring )
then ( $transform_nodes | .output | tostring )
else . end )) ) | . ] | . as $nodes
| $edges
| [ .[] as $edge
| reduce ( $opt_transform | fromjson )[] as $transform_nodes ( $edge
; with_entries(.key |= ( if . == ( $transform_nodes | .input | tostring )
then ( $transform_nodes | .output | tostring )
else . end )) ) | . ] | . as $edges
##
#
# Output the graph (merging prefix if needed).
#
#
| ($opt_graph_prefix | fromjson)
* { ($opt_nodes): $nodes
, ($opt_edges): $edges
}