-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathContext.js
More file actions
161 lines (117 loc) · 4.83 KB
/
Context.js
File metadata and controls
161 lines (117 loc) · 4.83 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
var Class = require('../ext/Class');
var MappedConceptSource = require('./MappedConceptSource');
var MappedConcept = require('./MappedConcept');
var AggUtils = require('./AggUtils');
var ObjectUtils = require('../util/ObjectUtils');
var RefSpec = require('./RefSpec');
var BindingMapperExpr = require('./binding_mapper/BindingMapperExpr');
var ExprVar = require('../sparql/expr/ExprVar');
var AggMap = require('./agg/AggMap');
var forEach = require('lodash.foreach');
/**
* A sponate context is a container for mappings, prefixes
* and configuration options
*/
var Context = Class.create({
initialize: function(prefixMapping) {
this.prefixMapping = prefixMapping;
this.nameToSource = {};
// This is for the registration of templates
// Maybe should be moved to StoreFacade
//this.nameToMappedConcept = nameToMappedConcept || {};
},
/*
getMappedConcept: function(name) {
return this.nameToMappedConcept[name];
},
*/
getSource: function(name) {
return this.nameToSource[name];
},
// addMappedConcept: function() {
//
// },
createResolvedContext: function() {
var result = new Context(this.prefixMapping);
forEach(this.nameToSource, function(source, name) {
var mc = source.getMappedConcept();
var agg = mc.getAgg();
var aggClone = agg.clone();
var b = new MappedConcept(mc.getConcept(), aggClone);
var s = new MappedConceptSource(b, source.getSparqlService());
result.addSource(name, s);
});
forEach(result.nameToSource, function(source, name) {
result.processRefs(name, source);
});
return result;
},
processRefs: function(baseName, source) {
var mappedConcept = source.getMappedConcept();
var sparqlService = source.getSparqlService();
var agg = mappedConcept.getAgg();
var refs = AggUtils.getRefs(agg);
var self = this;
refs.forEach(function(ref) {
var refSpec = ref.getRefSpec();
//console.log('TEMPLATE REF SPEC ' + JSON.stringify(refSpec));
var target = refSpec.getTarget();
// Evaluate function targets
if(ObjectUtils.isFunction(target)) {
target = target();
}
if(target instanceof MappedConcept) {
// TODO mappedConcepts used as references are AggObject,
// however we expect AggMap's - so we have to wrap them
var aggMap = target.getAgg();
var aggObject = aggMap.getSubAgg();//target.getAgg();
//var aggObject = target.getAgg();
var attrToAgg = aggObject.getAttrToAgg();
var aggIdLiteral = AggUtils.unwrapAggTransform(attrToAgg.id);
var idMapper = aggIdLiteral.getBindingMapper();
aggMap = new AggMap(idMapper, aggObject);
var newTarget = new MappedConcept(target.getConcept(), aggMap);
// Allocate a new name and source for this anonymous mapped concept
var i = 0;
var name;
while(self.getSource(name = (baseName + '_fn_' + i))) {
++i;
}
ref.setRefSpec(new RefSpec(name, refSpec.getAttr()));
//console.log('NEW REF SPEC: ' + JSON.stringify(ref));
//refSpec.setTarget(name);
var bindingMapper = ref.getBindingMapper();
if(!bindingMapper) {
var c = mappedConcept.getConcept();//newTarget.getConcept();
var v = c.getVar();
bindingMapper = new BindingMapperExpr(new ExprVar(v));
ref.setBindingMapper(bindingMapper);
}
var newSource = new MappedConceptSource(newTarget, sparqlService);
self.nameToSource[name] = newSource;
//console.log('STATE: ' + JSON.stringify(self.nameToSource, null, 4));
}
else if(!ObjectUtils.isString(target)) {
throw new Error('Unknown target type: ', target);
}
});
},
// addTemplate: function(spec) {
// var name = spec.name;
// if(!name) {
// throw new Error('Sponate spec must have a name');
// }
//
// var mappedConcept = SponateUtils.parseSpec(spec, this.prefixMapping);
//
// this.nameToMappedConcept[name] = mappedConcept;
// },
//
addSource: function(name, source) {
this.nameToSource[name] = source;
//console.log('MAPPED CONCEPT ' + name + ': ' + JSON.stringify(mappedConcept, null, null));
// var source = new MappedConceptSource(mappedConcept, sparqlService);
//this.processRefs(name, source);
},
});
module.exports = Context;