-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.js
More file actions
74 lines (64 loc) · 2.13 KB
/
model.js
File metadata and controls
74 lines (64 loc) · 2.13 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
function XmlModel(xml) {
if (!(this instanceof XmlModel))
return new XmlModel(xml);
function getRoot(xml) {
var struct = $(xml).find("featureModel struct").get();
if (struct.length !== 1)
throw "model does not have exactly one struct";
var children = $(struct[0]).children().get();
if (children.length !== 1)
throw "model does not have exactly one root";
return $(children[0]);
}
function getRules() {
global.ruless = []
return $(xml).find("constraints rule").map(function() {
var children = $(this).children(":not(description)").get();
if (children.length !== 1)
throw "rule does not have exactly one child";
return children[0];
});
}
this.xml = xml;
this.root = getRoot(xml);
this.rules = getRules();
for(var i=0;i<this.rules.length;i++){
global.ruless[i]= this.rules[i].innerHTML
}
}
XmlModel.prototype.traverse = function(fn, pushFn, popFn) {
function traverse(node, parent, level) {
if (["feature", "and", "or", "alt"].includes(node.prop("tagName")))
fn(node, parent, level);
if (node.children().length > 0) {
if (pushFn)
pushFn();
node.children().get().forEach(function(child) {
traverse($(child), node, level + 1);
});
if (popFn)
popFn();
}
}
if (pushFn)
pushFn();
traverse(this.root, null, 0);
if (popFn)
popFn();
}
function Model(xmlModel) {
if (!(this instanceof Model))
return new Model(xmlModel);
function buildFeatureList(xmlModel) {
var features = [];
xmlModel.traverse(function(node, parent) {
features.push(new Feature(node, parent, node.children()));
});
return features;
}
this.xmlModel = xmlModel;
this.features = buildFeatureList(xmlModel);
this.rootFeature = this.features[0];
this.getFeature = featureGetter("features");
this.constraintSolver = new ConstraintSolver(this);
}