This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrules.js
219 lines (196 loc) · 6.25 KB
/
rules.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
const { entityAttributesProperty, entityPrimaryKeyProperty } = require('@codaco/shared-consts');
const { operators } = require('./predicate');
const predicate = require('./predicate').default;
const singleEdgeRule = ({ type, attribute, operator, value: other }) =>
(node, edges) => {
const nodeEdges = edges.filter(edge =>
edge.from === node[entityPrimaryKeyProperty]
|| edge.to === node[entityPrimaryKeyProperty]);
const nodeHasEdgeOfType = nodeEdges.some(edge => edge.type === type);
if (!attribute) {
switch (operator) {
case 'EXISTS':
return nodeHasEdgeOfType;
default:
return !nodeHasEdgeOfType;
}
}
const nodeHasEdgeWithAttribute = nodeHasEdgeOfType
&& nodeEdges.some(edge =>
edge.type === type && predicate(operator)({
value: edge[entityAttributesProperty][attribute],
other,
}));
return nodeHasEdgeWithAttribute;
};
const singleNodeRule = ({ type, attribute, operator, value: other }) =>
(node) => {
if (!attribute) {
switch (operator) {
case operators.EXISTS:
return node.type === type;
default:
return node.type !== type;
}
}
return node.type === type && predicate(operator)({
value: node[entityAttributesProperty][attribute],
other,
});
};
// Reduce edges to any that match the rule
// Filter nodes by the resulting edges
const edgeRule = ({ attribute, operator, type, value: other }) =>
(nodes, edges) => {
let filteredEdges;
// If there is no attribute, we just care about filtering by type
if (!attribute) {
switch (operator) {
case operators.EXISTS:
filteredEdges = edges.filter(edge => edge.type === type);
break;
default:
filteredEdges = edges.filter(edge => edge.type !== type);
}
} else {
// If there is an attribute we check that, too.
filteredEdges = edges.filter(edge => edge.type === type && predicate(operator)({
value: edge[entityAttributesProperty][attribute],
other,
}));
}
const edgeMap = filteredEdges.flatMap(edge => [edge.from, edge.to]);
const filteredNodes = nodes.filter(node =>
edgeMap.includes(node[entityPrimaryKeyProperty]));
return {
nodes: filteredNodes,
edges: filteredEdges,
};
};
/**
* Creates an alter rule, which can be called with `rule(node)`
*
* @param {Object} options Rule configuration
* @param {string} options.type Which type of node we are interested in
* @param {string} options.attribute Which node attribute to assess
* @param {string} options.operator What predicate to apply to the attribute
* @param {string} options.value Value to compare the node attribute with
*
* Usage:
* ```
* // Check node is of type
* const rule = nodeRule({ type: 'person', operator: operators.EXISTS });
* const result = rule(node); // returns boolean
* ```
* ```
* // Check node is of type and has attribute that matches the expression
* const rule = nodeRule({
* type: 'person',
* operator: operators.EXISTS,
* attribute:'age',
* operator: 'GREATER_THAN',
* value: 20
* });
* const result = rule(node); // returns boolean
* ```
*/
const nodeRule = ({ attribute, operator, type, value: other }) =>
(nodes = [], edges = []) => {
let filteredNodes;
// If there is no attribute, we just care about filtering by type
if (!attribute) {
switch (operator) {
case operators.EXISTS:
filteredNodes = nodes.filter(node => node.type === type);
break;
default:
filteredNodes = nodes.filter(node => node.type !== type);
}
} else {
// If there is an attribute we check that, too.
filteredNodes = nodes.filter(node => node.type === type && predicate(operator)({
value: node[entityAttributesProperty][attribute],
other,
}));
}
const nodeIds = filteredNodes.map(node =>
node[entityPrimaryKeyProperty]);
const filteredEdges = edges.filter(edge =>
nodeIds.includes(edge.from) && nodeIds.includes(edge.to));
return {
nodes: filteredNodes,
edges: filteredEdges,
};
};
/**
* Creates an ego rule, which can be called with `rule(ego)`
*
* @param {Object} options Rule configuration
* @param {string} options.attribute Which ego attribute to assess
* @param {string} options.operator What predicate to apply to the attribute
* @param {string} options.value Value to compare the ego attribute with
*/
const egoRule = ({ attribute, operator, value: other }) =>
ego =>
predicate(operator)({
value: ego[entityAttributesProperty][attribute],
other,
});
/**
* Adds type parameter to rule function
* @param {string} type rule type
* @param {function} f rule method
*/
const createRule = (type, options, f) => {
const rule = f(options);
rule.type = type;
rule.options = options;
return rule;
};
/**
* Creates a configured rule function based on the ruleConfig
*
* @param {Object} ruleConfig
* @param {string} ruleConfig.type Which type of rule we need
* @param {Object} ruleConfig.options Configuration object for specific rule type
*
* Usage:
* ```
* const rule = getRule({ type: alter, options: { type: 'person', operator: operators.EXISTS } });
* const result = rule(node, edges); // returns boolean
* ```
*/
const getRule = ({ type, options }) => {
switch (type) {
case 'alter':
return createRule('alter', options, nodeRule);
case 'edge':
return createRule('edge', options, edgeRule);
case 'ego':
return createRule('ego', options, egoRule);
default:
return () => false;
}
};
// As above, but for rules matching single array or edge
const getSingleRule = ({ type, options }) => {
switch (type) {
case 'alter':
return createRule('alter', options, singleNodeRule);
case 'edge':
return createRule('edge', options, singleEdgeRule);
case 'ego':
return createRule('ego', options, egoRule);
default:
return () => false;
}
};
// Provides ES6 named + default imports via babel
Object.defineProperty(exports, '__esModule', {
value: true,
});
exports.default = getRule;
exports.getSingleRule = getSingleRule;
exports.alter = nodeRule;
exports.ego = egoRule;
exports.edge = edgeRule;