-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathQuadUtils.js
77 lines (54 loc) · 1.88 KB
/
QuadUtils.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
var HashMap = require('../util/collection/HashMap');
var Triple = require('../rdf/Triple');
var ElementGroup = require('./element/ElementGroup');
var ElementTriplesBlock = require('./element/ElementTriplesBlock');
var ElementNamedGraph= require('./element/ElementNamedGraph');
var ElementUtils = require('./ElementUtils');
var Quad = require('./Quad');
var QuadUtils = {
triplesToQuads: function(triples, graphNode) {
var result = triples.map(function(triple) {
var r = new Quad.createFromTriple(graphNode, triple);
return r;
});
return result;
},
quadsToElement: function(quads) {
var map = this.groupByGraph(quads);
var result = this.groupToElement(map);
return result;
},
/**
* Returns a map from quad to an array of quads
*/
groupByGraph: function(quads) {
var result = new HashMap();
quads.forEach(function(quad) {
//console.log('got item ' + quad);
var g = quad.getGraph();
var items = result.getOrCreate(g, []);
items.push(quad);
});
return result;
},
groupToElement: function(graphToQuads) {
var entries = graphToQuads.entries();
var elements = entries.map(function(entry) {
var graph = entry.key;
var quads = entry.val;
var triples = quads.map(function(quad) {
return quad.asTriple();
});
var etb = new ElementTriplesBlock(triples);
var r = graph == null || graph.equals(Quad.defaultGraphNodeGenerated)
? etb
: new ElementNamedGraph(graph, etb)
;
return r;
});
//var result = ElementUtils.groupIfNeeded(elements);
var result = new ElementGroup(elements);
return result;
}
};
module.exports = QuadUtils;