-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.js
221 lines (180 loc) · 6.08 KB
/
graph.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
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
'use strict';
import * as cypher from './cypher.js'
const uniq = (value, index, self) => self.indexOf(value) === index
export class Graph {
constructor(nodes=[], rels=[]) {
this.nodes = nodes;
this.rels = rels;
}
addNode(type, props={}){
const obj = {type, in:[], out:[], props};
this.nodes.push(obj);
return this.nodes.length-1;
}
addRel(type, from, to, props={}) {
const obj = {type, from, to, props};
this.rels.push(obj);
const rid = this.rels.length-1;
this.nodes[from].out.push(rid)
this.nodes[to].in.push(rid);
return rid;
}
tally(){
const tally = list => list.reduce((s,e)=>{s[e.type] = s[e.type] ? s[e.type]+1 : 1; return s}, {});
return { nodes:tally(this.nodes), rels:tally(this.rels)};
}
size(){
return this.nodes.length + this.rels.length;
}
static load(obj) {
// let obj = await fetch(url).then(res => res.json())
return new Graph(obj.nodes, obj.rels)
}
static async fetch(url) {
const obj = await fetch(url).then(res => res.json())
return Graph.load(obj)
}
static async read(path) {
const json = await Deno.readTextFile(path);
const obj = JSON.parse(json);
return Graph.load(obj)
}
// static async import(path) {
// let module = await import(path, {assert: {type: "json"}})
// return Graph.load(module.default)
// }
n(type=null, props={}) {
let nids = Object.keys(this.nodes).map(key => +key)
if (type) nids = nids.filter(nid => this.nodes[nid].type == type)
for (const key in props) nids = nids.filter(nid => this.nodes[nid].props[key] == props[key])
return new Nodes(this, nids)
}
/**
* Converts a graph to a JavaScript Object Notation (JSON) string using JSON.stringify.
@param - replacer A function that transforms the results.
@param - space Adds indentation, white space, and line break characters to the return-
* @returns {string} JSON string containing serialized graph
*/
stringify(...args) {
const obj = { nodes: this.nodes, rels: this.rels }
return JSON.stringify(obj, ...args)
}
search (query, opt={}) {
const tree = cypher.parse(query)
// console.dir(tree, {depth:15})
const code = cypher.gen(0,tree[0][0],{})
// console.log(code)
cypher.check(this.tally(),code,opt.errors)
return cypher.apply(this, code)
}
}
// Fluent Interface (deprecated?)
export class Nodes {
constructor (graph, nids) {
// console.log('Nodes',{graph:graph.size(),type,nids})
this.graph = graph
this.nids = nids
}
// n(type=null, props={}) {
// // console.log('Nodes.n',{type,props})
// let nids = this.nids
// if (type) nids = nids.filter(nid => this.nodes[nid].type == type)
// for (let key in props) nids = nids.filter(nid => this.nodes[nid].props[key] == props[key])
// return new Nodes(this.graph, type, nids)
// }
i(type=null, props={}) {
// console.log('Nodes.i',{type,props})
let rids = this.nids.map(nid => this.graph.nodes[nid].in).flat().filter(uniq)
if (type) rids = rids.filter(rid => this.graph.rels[rid].type == type)
for (const key in props) rids = rids.filter(rid => this.graph.rels[rid].props[key] == props[key])
return new Rels(this.graph, rids)
}
o(type=null, props={}) {
// console.log('Nodes.o',{type,props})
let rids = this.nids.map(nid => this.graph.nodes[nid].out).flat().filter(uniq)
if (type) rids = rids.filter(rid => this.graph.rels[rid].type == type)
for (const key in props) rids = rids.filter(rid => this.graph.rels[rid].props[key] == props[key])
return new Rels(this.graph, rids)
}
props(key='name') {
// console.log('Nodes.p',{key})
return this.nids.map(nid => this.graph.nodes[nid].props[key]).filter(uniq).sort()
}
types() {
return this.nids.map(nid => this.graph.nodes[nid].type).filter(uniq).sort()
}
tally(){
const tally = list => list.reduce((s,e)=>{s[e.type] = s[e.type] ? s[e.type]+1 : 1; return s}, {});
return { nodes:tally(this.nids.map(nid => this.graph.nodes[nid]))};
}
size(){
return this.nids.length
}
filter(f) {
const nodes = this.graph.nodes
const nids = this.nids.filter(nid => {
const node = nodes[nid]
return f(node.type,node.props)
})
return new Nodes(this.graph,nids)
}
map(f) {
const nodes = this.graph.nodes
const result = this.nids.map(nid => {
const node = nodes[nid]
return f(node)
})
return result
}
}
export class Rels {
constructor (graph, rids) {
// console.log('Rels',{graph:graph.size(),type,rids})
this.graph = graph
this.rids = rids
}
f(type=null, props={}) {
// console.log('Rels.f',{type,props})
let nids = this.rids.map(rid => this.graph.rels[rid].from).filter(uniq)
if (type) nids = nids.filter(nid => this.graph.nodes[nid].type == type)
for (const key in props) nids = nids.filter(nid => this.graph.nodes[nid].props[key] == props[key])
return new Nodes(this.graph, nids)
}
t(type=null, props={}) {
// console.log('Rels.t',{type,props})
let nids = this.rids.map(rid => this.graph.rels[rid].to).filter(uniq)
if (type) nids = nids.filter(nid => this.graph.nodes[nid].type == type)
for (const key in props) nids = nids.filter(nid => this.graph.nodes[nid].props[key] == props[key])
return new Nodes(this.graph, nids)
}
props(key='name') {
// console.log('Rels.p',{key})
return this.rids.map(rid => this.graph.rels[rid].props[key]).filter(uniq).sort()
}
types() {
return this.rids.map(rid => this.graph.rels[rid].type).filter(uniq).sort()
}
tally(){
const tally = list => list.reduce((s,e)=>{s[e.type] = s[e.type] ? s[e.type]+1 : 1; return s}, {});
return { rels:tally(this.rids.map(nid => this.graph.rels[nid]))};
}
size(){
return this.rids.length
}
filter(f) {
const rels = this.graph.rels
const rids = this.rids.filter(rid => {
const rel = rels[rid]
return f(rel.type,rel.props)
})
return new Rels(this.graph,rids)
}
map(f) {
const rels = this.graph.rels
const result = this.rids.map(rid => {
const rel = rels[rid]
return f(rel)
})
return result
}
}