forked from graphmalizer/graphmalizer-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.yaml
286 lines (250 loc) · 8.98 KB
/
queries.yaml
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
equivalence:
add:
cypher: |
// find s, t nodes
MERGE (s:_ {id: {source}})
ON CREATE SET s :_:_VACANT
MERGE (t:_ {id: {target}})
ON CREATE SET t :_:_VACANT
WITH s,t LIMIT 1
// find current equivalence class representatives (ECR)
OPTIONAL MATCH (s) <-[:`=`]- (se:`=`)
OPTIONAL MATCH (t) <-[:`=`]- (te:`=`)
OPTIONAL MATCH (se) -[a]- ()
OPTIONAL MATCH (te) -[b]- ()
WITH s, t, se, te, collect(DISTINCT a) AS as, collect(DISTINCT b) AS bs
// wipe the arcs
FOREACH(a IN as | DELETE a)
FOREACH(b IN bs | DELETE b)
// delete the old ECRs
DELETE se, te
// ensure that at this point we have only one row
WITH s, t LIMIT 1
// create equivalence class node
CREATE (eq:`=`)
// create relation node
CREATE UNIQUE (s) -[:`«type»`]-> (r:_:_Rel:`«type»` {id: {id}}) -[:`«type»`]-> (t)
SET r.dataset = {dataset}
// find all nodes reachable under the equivalence relation
WITH s, t, r, eq LIMIT 1
OPTIONAL MATCH (s) -[:`«type»` * 0 .. 4]- (n:_)
WHERE NOT n:_Rel // skip the relation nodes
WITH s, t, r, eq, collect(DISTINCT n) AS ns LIMIT 1
// link to new equivalence class representative
FOREACH (n IN ns |
CREATE UNIQUE n <-[:`=`]- eq
CREATE UNIQUE n -[:`=i`]-> eq
)
// at this point we collect all members
RETURN s, t, r, eq, count(ns) AS size LIMIT 1
remove:
cypher: |
// find relation node
MATCH (r:_:_Rel {id: {id}})
// find source, target nodes
MATCH (s) -[x:`«type»`]-> (r) -[y:`«type»`]-> (t)
// remove the relation
DELETE x, y, r
// ok, wipe the old ECR
WITH s, t LIMIT 1
// find current equivalence class representatives (ECR)
OPTIONAL MATCH (s) <-[:`=`]- (se:`=`)
OPTIONAL MATCH (t) <-[:`=`]- (te:`=`)
OPTIONAL MATCH (se) -[a]- ()
OPTIONAL MATCH (te) -[b]- ()
WITH s, t, se, te, collect(DISTINCT a) AS as, collect(DISTINCT b) AS bs
// wipe the arcs
FOREACH(a IN as | DELETE a)
FOREACH(b IN bs | DELETE b)
// delete the old ECRs
DELETE se, te
// ensure that at this point we have only one row
WITH s, t LIMIT 1
// create equivalence class nodes for s and t
MERGE (s) <-[:`=`]- (se:`=`)
MERGE (t) <-[:`=`]- (te:`=`)
WITH s, t, se, te LIMIT 1
// s-size
OPTIONAL MATCH (s) -[:`«type»` * 0 .. 4]- (n:_)
WHERE NOT n:_Rel // skip the relation nodes
WITH s, t, se, te, collect(DISTINCT n) AS ns LIMIT 1
// link to new equivalence class representative
FOREACH (n IN ns |
CREATE UNIQUE n <-[:`=`]- se
CREATE UNIQUE n -[:`=i`]-> se
)
WITH s, t, se, te, count(ns) AS s_size LIMIT 1
OPTIONAL MATCH (t) -[:`«type»` * 0 .. 4]- (n:_)
WHERE NOT n:_Rel // skip the relation nodes
WITH s, t, se, te, s_size, collect(DISTINCT n) AS ns LIMIT 1
// link to new equivalence class representative
FOREACH (n IN ns |
CREATE UNIQUE n <-[:`=`]- te
CREATE UNIQUE n -[:`=i`]-> te
)
// at this point we collect all members
RETURN s, t, se, te, s_size, count(ns) AS t_size LIMIT 1
node:
add:
ruleset:
- ' x => (*) '
- ' ( ) => (*) '
- ' (*) => 500 '
cypher: |
MERGE (n:_ {id: {id}})
ON CREATE
SET n = {data},
n.created = timestamp(),
n.id = {id},
n.dataset = {dataset},
n:`«type»`
ON MATCH
SET n = {data},
n.accessTime = timestamp(),
n.counter = coalesce(n.counter, 0) + 1,
n.id = {id},
n.dataset = {dataset},
n:`«type»`
REMOVE n:_VACANT
RETURN n
# update:
# ruleset:
# - ' x => 404 '
# - ' ( ) => (*) '
# - ' (*) => (*) '
# cypher: |
# MATCH (n:_ {id: {id}})
# WITH n, coalesce(n.counter, 0) + 1 AS c
# SET n = {data},
# n.accessTime = timestamp(),
# n.counter = c,
# n.id = {id},
# n.dataset = {dataset}
# RETURN n
remove:
ruleset:
- ' x => 404 '
- ' --( ) => 404 '
- ' (*) => x '
- ' --(*) => --( ) '
cypher: |
MATCH (n:_ {id: {id}})
WHERE NOT n:_VACANT
SET n:_VACANT
SET n={}
SET n.id = {id}
SET n.dataset = {dataset}
UNION ALL
OPTIONAL MATCH (n:_:_VACANT {id: {id}})
WHERE length( (n)--() ) = 0
DELETE n
RETURN n
arc:
add:
ruleset:
- ' x x => 404 '
- ' ( ) x => ( )-->( ) '
- ' (*) x => (*)-->( ) '
- ' x ( ) => ( )-->( ) '
- ' x (*) => ( )-->(*) '
- ' ( ) ( ) => ( )-->( ) '
- ' (*) ( ) => (*)-->( ) '
- ' ( ) (*) => ( )-->(*) '
- ' (*) (*) => (*)-->(*) '
cypher: |
MERGE (s:_ {id: {source}})
ON CREATE SET s :_:_VACANT
MERGE (t:_ {id: {target}})
ON CREATE SET t :_:_VACANT
//MERGE (s) <-[:`=`]- (eq1:`=`)
//MERGE (t) <-[:`=`]- (eq2:`=`)
WITH s,t LIMIT 1
CREATE UNIQUE s -[:`«type»`]-> (r:_:_Rel:`«type»` {id: {id}}) -[:`«type»`]-> t
SET r = {data},
r.created = timestamp(),
r.id = {id},
r.dataset = {dataset}
//MERGE eq1 -[:`=R` {label: "`«type»`"}]-> eq2
RETURN s, t, r LIMIT 1
update:
ruleset:
- ' x x => 404 '
- ' ( ) x => 404 '
- ' (*) x => 404 '
- ' x ( ) => 404 '
- ' x (*) => 404 '
- ' ( )-->( ) => ( )-->( ) '
- ' (*)-->( ) => (*)-->( ) '
- ' ( )-->(*) => ( )-->(*) '
- ' (*)-->(*) => (*)-->(*) '
cypher: |
MATCH (s)-[:`«type»`]-(r:_:_Rel:`«type»` {id: {id}})-[:`«type»`]->(t)
WITH r, coalesce(r.counter, 0) + 1 AS c
SET r = {data},
r.accessTime = timestamp(),
r.counter = c,
r.id = {id},
r.dataset = {dataset}
RETURN r
remove:
ruleset:
- ' x x => 404 '
- ' ( ) x => 404 '
- ' (*) x => 404 '
- ' x ( ) => 404 '
- ' x (*) => 404 '
- ' ( )-->( ) => x x '
- ' (*)-->( ) => (*) x '
- ' ( )-->(*) => x (*) '
- ' (*)-->(*) => (*) (*) '
- ' --( )-->( )-- => --( ) ( )-- '
- ' --(*)-->( )-- => --(*) ( )-- '
- ' --( )-->(*)-- => --( ) (*)-- '
- ' --(*)-->(*)-- => --(*) (*)-- '
- ' ( )-->( )-- => x ( )-- '
- ' (*)-->( )-- => (*) ( )-- '
- ' ( )-->(*)-- => x (*)-- '
- ' (*)-->(*)-- => (*) (*)-- '
- ' --( )-->( ) => --( ) x '
- ' --(*)-->( ) => --(*) x '
- ' --( )-->(*) => --( ) (*) '
- ' --(*)-->(*) => --(*) (*) '
cypher: |
MATCH (s)-[p:`«type»`]-(r:_:_Rel:`«type»` {id: {id}})-[q:`«type»`]->(t)
DELETE p,q,r
WITH s.id AS source, t.id AS target
OPTIONAL MATCH (n:_:_VACANT)
WHERE n.id IN [target, source] AND length( (n)-[]-() ) = 0
DELETE n
RETURN true
query:
test:
cypher: "WITH true AS b RETURN b"
schema:
description: Setup Neo4J schema
cypher: |
CREATE CONSTRAINT ON (n:_)
ASSERT n.id IS UNIQUE
clear:
description: Remove all managed nodes and edges
cypher: |
OPTIONAL MATCH ()-[e]-() DELETE e
RETURN DISTINCT true
UNION
OPTIONAL MATCH (n) DELETE n
RETURN DISTINCT true
graph:
description: Node and edge list
cypher: |
// match nodes and return {labels: .., id: ..}
MATCH (n)
WITH collect(DISTINCT {
labels: labels(n),
id: coalesce(n.id, "neo:" + id(n))
}) AS nodes LIMIT 1
// match edges and return {s: .., t: ..}
OPTIONAL MATCH s -[e]-> t
RETURN nodes, collect(DISTINCT {
source: coalesce(s.id, "neo:" + id(s)),
target: coalesce(t.id, "neo:" + id(t))
}) AS edges LIMIT 1