-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVarNode.js
170 lines (134 loc) · 3.79 KB
/
VarNode.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
var forEach = require('lodash.foreach');
var Class = require('../ext/Class');
var VarNode = require('./VarNode');
var Step = require('./Step');
var HashMap = require('../util/collection/HashMap');
var ObjectUtils = require('../util/ObjectUtils');
/**
* A class for generating variables for step-ids.
* So this class does not care about the concrete step taken.
*
* @param variableName
* @param generator
* @param parent
* @param root
* @returns {ns.VarNode}
*/
var VarNode = Class.create({
initialize: function(variableName, generator, step, parent, root) {
this.variableName = variableName;
this.generator = generator;
//this.stepId = stepId; // Null for root
this.step = step; // Null for root
this.parent = parent;
this.root = root;
//console.log('VarNode status' , this);
if(!this.root) {
if(this.parent) {
this.root = parent.root;
}
else {
this.root = this;
}
}
this.idToChild = new HashMap();
},
hashCode: function() {
var result = ObjectUtils.hashCodeStr(this.variableName) * (this.step ? this.step.hashCode() : 7);
return result;
},
equals: function(that) {
throw new Error('Should not be called');
},
isRoot: function() {
var result = this.parent ? false : true;
return result;
},
/*
getSourceVarName: function() {
var result = this.root.variableName;
return result;
},
*/
getVariableName: function() {
return this.variableName;
},
/*
forPath: function(path) {
var steps = path.getSteps();
var result;
if(steps.length === 0) {
result = this;
} else {
var step = steps[0];
// TODO Allow steps back
result = forStep(step);
}
return result;
},
*/
// getIdStr: function() {
// var tmp = this.parent ? this.parent.getIdStr() : '';
//
// var result = tmp + this.variableName;
// return result;
// },
getStop: function(step) {
return this.step;
},
// getStepId: function(step) {
// return '' + JSON.stringify(step);
// },
getSteps: function() {
return this.steps;
},
/**
* Convenience method, uses forStep
*
* @param propertyUri
* @param isInverse
* @returns
*/
forProperty: function(propertyUri, isInverse) {
var step = new Step(propertyUri, isInverse);
var result = this.forStep(step);
return result;
},
forStep: function(step) {
var child = this.idToChild.get(step);
if(!child) {
var subName = this.generator.next();
child = new VarNode(subName, this.generator, step, this);
//Unless we change something
// we do not add the node to the parent
this.idToChild.put(step, child);
}
return child;
},
/*
* Recursively scans the tree, returning the first node
* whose varName matches. Null if none found.
*
* TODO: Somehow cache the variable -> node mapping
*/
findNodeByVarName: function(varName) {
if(this.variableName === varName) {
return this;
}
var children = [];
forEach(this.idToChild, function(child) {
children.push(child);
});
//var children = _.values(this.idToChild);
//forEach(this.idToChild, function(child) {
for(var i = 0; i < children.length; ++i) {
var child = children[i];
var tmp = child.findNodeByVarName(varName);
if(tmp) {
return tmp;
}
}
return null;
}
});
module.exports = VarNode;