-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathabstraction.js
More file actions
276 lines (240 loc) · 6.98 KB
/
Copy pathabstraction.js
File metadata and controls
276 lines (240 loc) · 6.98 KB
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
/*
Allowing 2 users to collaborate on the same path.
This makes it write only for each user, but in a UI using below function you
can now see whatever was added latest collaboration
*/
async function getLatest (path,pubkeyOther) {
var path = path;
var refMe = gun.user();
var refBob = gun.user(pubkeyOther);
console.log('me', refMe);
console.log('bob', refBob);
while(path.length>0){
var step = path.shift();
refMe = refMe.get(step);
refBob = refBob.get(step);
}
var me = await refMe.promOnce();
console.log('me', me.data);
var bob = await refBob.promOnce();
console.log('bob', bob.data);
return Gun.state.node(me.data, bob.data);
}
/* Abstraction Layer to GunDB
* Functions to abstract the creation of a schema
*/
/* Schema definitions:
* Metadata should start with double-underscore
* all items should include __label and __type with the corresponding type
* __label = a name that visualGraph can display for the node
* __type = either 'node', 'edge' or 'index', this will become important
* when querying happens
*/
/* Schema for Nodes */
function newNode(object, label) {
object.__label = label;
object.__type = 'node';
var gunRef = nodes.set(object);
return gunRef;
}
/* Schema for Edges */
function newEdge(object, label) {
object.__label = label;
object.__type = 'edge';
var gunRef = edges.set(object);
return gunRef;
}
/* Create Index for Nodes and Edges */
var nodes = gun.get('nodes')//.put({'__type':'index','__label':'nodesIndex'}); //creates global nodes index, but not write protected
var edges = gun.get('edges')//.put({'__type':'index','__label':'edgesIndex'}); //same here
/* Tuple function */
/* Takes objects or references from Gun to create nodes */
var tuple = function (node, verb, object){
node.get('out').set(verb);
verb.get('source').put(node);
verb.get('target').put(object);
object.get('in').set(verb);
setTimeout(DFS.search('nodes','__label'), 1000);
}
var addNode = function (label, edgeR, nodeR) {
var obj = {label:label, edge:edgeR, node:nodeR};
nodes.map().once(function(obj, data, key){
if(data.__label == obj.label) {
var soul = Gun.node.soul(data);
var node = gun.get(soul).get('out').set(obj.edge);
console.log(node._.soul);
var node = gun.get(node._.soul);
obj.edge.get('source').put(node);
obj.edge.get('target').put(obj.node);
obj.node.get('in').set(obj.edge);
} else {
console.log(`${obj.label}, not found`);
}
}.bind(null, obj));
};
/* BFS Search for Pattern (Query) */
var QuerySearch = ( function(){
var query = {};
var tObj = {};
query.search = function(obj){
tObj = obj;
if(tObj.subject){
nodes.map().once(query.step);
} else {
throw 'no pattern defined';
}
};
query.step = function(node, key){
if(typeof node != 'string'){
var soul = Gun.node.soul(node)/*node['_']['#'] || node['#']*/;
console.log(soul);
if(node['__label'] == tObj.subject || tObj.subject[0] == '?'){
gun.get(soul).get('out').map().once(query.look.bind(null, soul))
}
}
};
query.look = function(parent, node, key) {
console.log('Qlook',key, node, parent);
var soul = Gun.node.soul(node)/*node['_']['#'] || node['#']*/;
if(node['__label']== tObj.predicate || tObj.predicate[0] == '?'){
var temp = (parent+'__'+soul);
gun.get(node.target['#']).once(query.find.bind(null,temp));
}
};
query.find = function(parent, node, key) {
console.log('Qfind',key, node, parent);
var soul = Gun.node.soul(node)/*node['_']['#'] || node['#']*/;
if(node['__label']== tObj.subject || tObj.subject[0] == '?'){
var temp = (parent+'__'+soul);
console.log('pushed',temp);
tObj.result.push(temp);
query.print();
}
};
query.print = function () {
if(!tObj.result){
console.log('no results');
return;
} else {
var i = 0;
var l = tObj.result.length;
for(i;i<l;i++){
var temp = tObj.result[i];
temp = temp.split('__');
var y = 0;
var l1 = temp.length;
for(y;y<l1;y++){
gun.get(temp[y]).once(query.nice.bind(null,y));
}
}
DFS.search('nodes','__label');
}
};
query.nice = function (item, node) {
console.log(item,node['__label']);
}
return query;
}
)(Gun, gun, nodes, edges);
var QueryFind = ( function(){
var query = {};
var tObj = {};
var util = {};
util.isMatch = function (is, toMatch) {
return toMatch.some(function (v) {
return is.indexOf(v) >= 0;
});
};
util.print = function () {
console.log('Found:');
var arr = Object.entries(tObj.result[0]);
var i = 0;
var l = arr.length;
for(i;i<l;i++){
console.log(arr[i][0]);
console.log(arr[i][1]);
}
DFS.search('nodes','__label');
}
query.find = function(obj){
tObj = obj;
nodes.map().once(query.match);
};
query.match = function(node, key){
if(typeof node != 'string'){
var keysQ = Object.keys(tObj.obj);
var keys = Object.keys(node);
var soul = node._.soul;
if(util.isMatch(keys,keysQ)){
for(var i=0;i<keys.length;i++){
for(var y=0;y<keysQ.length;y++){
var qK = keysQ[y];
var qV = tObj.obj[qK];
var k = keys[i];
var v = node[k];
if(qV == v && k == qK) {
tObj.result.push(node);
util.print();
}
}
}
} else {
console.log('no match!');
return;
}
} else {
return;
}
};
return query;
}
)(Gun, gun, nodes, edges);
/* Triple Traversal Object
* This stores options and is meant to become the transport object between
* functions that execute during Traversal
*/
var TripTrav = function(triple){
this.subject = triple.subject;
this.predicate = triple.predicate;
this.object = triple.object;
this.result = [];
}
var triple = {subject:'?p',predicate:'type',object:'Artist'};
var trav = new TripTrav(triple);
QuerySearch.search(trav);
/* Select an object to match from a node */
var SelectTrav = function(obj) {
this.obj = obj;
this.result = [];
}
var selection = {name:'Ice Cream'};
var travSel = new SelectTrav(selection);
QueryFind.find(travSel);
/* Local Graph Functions
* To build a graph for queries
* To build a graph to perform graph operations on
*/
var localGraph = function() {
this.map = new Map(),
this.add = function (item) {
var uuid = uuidv4();
this.map.set(uuid, item);
return uuid;
},
this.find = function(uuid) {
return this.map.get(uuid);
},
this.update = function(uuid, item) {
this.map.set(uuid, item);
}
}
function uuidv4 () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
})
}
var lGraph = new localGraph();
var test = lGraph.add({id:'test1'});
var obj = {name:'test', label:'person', birthdate:'07-05-1986', link: test};
var test0 = lGraph.add(obj)