-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdge.js
152 lines (147 loc) · 5.13 KB
/
Edge.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
"use strict";
/* code ported from https://github.com/DrTom/digraph-demo/blob/master/app/models/arc.rb */
var Promise = require('bluebird');
var sql = require('bookshelfw');
var createError = require('create-error');
var _ = require('underscore');
var Vertex = require('./Vertex');
var NonDAGError = createError('NonDAGError');
var Edge = module.exports = sql.Model.extend({
tableName: 'edges',
initialize: function () {
this.on('saving', this.preventBackLoop, this);
this.on('saving', this.preventCycle, this);
this.on('saving', this.preventMultiEdge, this);
this.on('saving', this.preventSelfLoop, this);
},
source: function () {
return this.belongsTo(Vertex, 'sourceId');
},
target: function () {
return this.belongsTo(Vertex, 'targetId');
},
step: function (to, from, edgeIds) {
// return edges that are a step away in the graph.
//
// in other words, return all edges where :to
// is in edgeIds (which defaults to this.get(:from)).
edgeIds = edgeIds || [this.get(from)];
var Edges = require('./Edges');
return new Edges().query('whereIn', to, edgeIds);
},
previous: function (edgeIds) {
// return edges that are a step before this edge in the graph.
//
// in other words, return all edges where 'targetId'
// is in this.get('sourceId') (or edgeIds if specified).
return this.step('targetId', 'sourceId', edgeIds);
},
next: function (edgeIds) {
// return edges that are a step after this edge in the graph.
//
// in other words, return all edges where 'sourceId'
// is in this.get('targetId') (or edgeIds if specified).
return this.step('sourceId', 'targetId', edgeIds);
},
search: function (to, from, startIds) {
// return edges that are connected either
// before or after this edge in the graph.
//
// in other words,
// returns all edges where :to is in startIds
// (defaults to this.get(:from)), recursively.
var searchStep = function (soFarIds, prevIds) {
prevIds = prevIds || soFarIds;
return this.step(to, from, prevIds).fetch().then(function (nextEdges) {
// get from ids from next edges
var nextIds = _.pluck(nextEdges.toJSON(), from)
// remove any we have already seen
nextIds = _.difference(nextIds, soFarIds);
// join all the ids we have seen together
soFarIds = _.union(soFarIds, prevIds, nextIds);
// if we didn't see anything new
if (nextIds.length === 0) {
// return all edges found
return this.step(to, from, soFarIds).fetch();
} else {
// query for more edges
return searchStep(soFarIds, nextIds);
}
}.bind(this));
}.bind(this);
return searchStep(startIds || [this.get(from)]);
},
predecessors: function (startIds) {
// return edges that are before this edge in the graph.
//
// in other words, return all edges where
// :targetId is in this.get(:sourceId), recursively.
return this.search('targetId', 'sourceId', startIds);
},
successors: function (startIds) {
// return edges that are after this edge in the graph.
//
// in other words, return all edges where
// :sourceId is in this.get(:targetId), recursively.
return this.search('sourceId', 'targetId', startIds);
},
preventBackLoop: function () {
var Edges = require('./Edges');
return new Edges()
.query('where', 'targetId', this.get('sourceId'))
.query('andWhere', 'sourceId', this.get('targetId'))
.fetch()
.then(function (result) {
if (result.length !== 0) {
return Promise.reject(new Edge.Err.BackLoopError);
}
});
},
preventCycle: function () {
var Vertex = require('./Vertex');
var sourceId = this.get('sourceId');
// get all descendants of target vertex
return new Vertex({ id: this.get('targetId') }).descendants()
.then(function (descendants) {
var descendantIds = _.pluck(descendants.models, 'id');
// if descendants include source vertex
if (_.indexOf(descendantIds, sourceId) !== -1) {
// reject with error
return Promise.reject(new Edge.Err.CycleError);
}
});
},
preventMultiEdge: function () {
var Edges = require('./Edges');
return new Edges()
.query('where', 'targetId', this.get('targetId'))
.query('andWhere', 'sourceId', this.get('sourceId'))
.fetch()
.then(function (result) {
if (result.length !== 0) {
return Promise.reject(new Edge.Err.MultiEdgeError);
}
});
},
preventSelfLoop: function () {
if (this.get('targetId') === this.get('sourceId')) {
return Promise.reject(new Edge.Err.SelfLoopError);
}
},
}, {
Err: {
NonDAGError: NonDAGError,
BackLoopError: createError(NonDAGError, "BackLoopError", {
message: "back loops are not allowed.",
}),
CycleError: createError(NonDAGError, "CycleError", {
message: "cycles are not allowed.",
}),
MultiEdgeError: createError(NonDAGError, "MultiEdgeError", {
message: "multi-edges are not allowed.",
}),
SelfLoopError: createError(NonDAGError, "SelfLoopError", {
message: "self loops are not allowed.",
}),
},
});