-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVarUtils.js
133 lines (107 loc) · 3.31 KB
/
VarUtils.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
var GenSym = require('./GenSym');
var GeneratorBlacklist = require('./GeneratorBlacklist');
var VarGen = require('./VarGen');
var NodeFactory = require('../rdf/NodeFactory');
var StringUtils = require('../util/StringUtils');
var VarUtils = {
// Some conveniently predefined variables
g: NodeFactory.createVar('g'),
s: NodeFactory.createVar('s'),
p: NodeFactory.createVar('p'),
o: NodeFactory.createVar('o'),
_s_: NodeFactory.createVar('_s_'),
_p_: NodeFactory.createVar('_p_'),
_o_: NodeFactory.createVar('_o_'),
v: NodeFactory.createVar('v'),
w: NodeFactory.createVar('w'),
x: NodeFactory.createVar('x'),
y: NodeFactory.createVar('y'),
z: NodeFactory.createVar('z'),
a: NodeFactory.createVar('a'),
b: NodeFactory.createVar('b'),
c: NodeFactory.createVar('c'),
/**
* Convert an array of variable names to variable objects
*
*/
createVars: function(varNames) {
var result = varNames.map(function(varName) {
return NodeFactory.createVar(varName);
});
return result;
},
getVarName: function(v) {
return v.getName();
},
/**
* Convert an array of variable objects into an array of variable names
*
*
*/
getVarNames: function(vars) {
var result = vars.map(function(v) {
return v.getName();
});
return result;
},
/**
* Create a generator which yields fresh variables that is not contained in the array 'vars'.
* The new var name will have the given prefix
*
*/
createVarGen: function(prefix, excludeVars) {
if (!prefix) {
prefix = 'v';
}
var excludeVarNames = this.getVarNames(excludeVars);
var generator = GenSym.create(prefix);
var genVarName = new GeneratorBlacklist(generator, excludeVarNames);
var result = new VarGen(genVarName);
return result;
},
varPattern: /\?(\w+)/g,
/**
* Extract SPARQL variables from a string
*
* @param {String} str
* @returns {Array}
*/
extractSparqlVars: function(str) {
var varNames = StringUtils.extractAllRegexMatches(this.varPattern, str, 1);
var result = [];
for (var i = 0; i < varNames.length; ++i) {
var varName = varNames[i];
var v = NodeFactory.createVar(varName);
result.push(v);
}
return result;
},
/**
* Create a generator for fresh variable names
*
*/
freshVarGen: function(baseVarName, blacklistVars) {
var blacklistNames = VarUtils.getVarNames(blacklistVars);
var genSym = GenSym.create(baseVarName);
var genFreshName = new GeneratorBlacklist(genSym, blacklistNames);
var result = new VarGen(genFreshName);
return result;
},
/**
* Allocate a single fresh variable
*/
freshVar: function(baseVarName, blacklistVars) {
var varGen = this.freshVarGen(baseVarName, blacklistVars);
var result = varGen.next();
return result;
},
// TODO Maybe add a freshVarName method
fnSubst: function(varMap) {
var result = function(v) {
var r = varMap.get(v); // [v.getName()];
return r;
};
return result;
},
};
module.exports = VarUtils;