Skip to content

Commit 0e704b5

Browse files
committed
More playtesting
1 parent c99a1dd commit 0e704b5

File tree

3 files changed

+187
-1
lines changed

3 files changed

+187
-1
lines changed

test/converting.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
let input = document.getElementById('file');
2+
let svg = document.getElementById('svg');
3+
input.onchange = event => {
4+
const f = event.target.files[0];
5+
const r = new FileReader();
6+
r.onload = e => {
7+
const t = e.target.result;
8+
setInstance(new alloy.AlloyInstance(t));
9+
};
10+
r.readAsText(f);
11+
};
12+
13+
14+
function setInstance (instance) {
15+
16+
let { sets, relations } = toSets(instance);
17+
18+
/**
19+
* [[Matrix$0]]
20+
*/
21+
let matrixSig = sets['this/Matrix'];
22+
23+
/**
24+
* [[{id: 'Matrix$0}]]
25+
*/
26+
let matrixObj = matrixSig.map(tuple => tuple.map(atom => ({id: atom.name()})));
27+
28+
console.log(matrixObj);
29+
30+
}
31+
32+
function toSets (instance) {
33+
34+
let sets = instance.signatures()
35+
.map(sig => ({
36+
id: sig.id(),
37+
data: sig.atoms().map(atom => [atom])
38+
}))
39+
.reduce((acc, curr) => {
40+
acc[curr.id] = curr.data;
41+
return acc;
42+
}, {});
43+
44+
let relations = instance.fields()
45+
.map(fld => ({
46+
id: fld.id(),
47+
data: fld.tuples().map(tuple => tuple.atoms())
48+
}))
49+
.reduce((acc, curr) => {
50+
acc[curr.id] = curr.data;
51+
return acc;
52+
}, {});
53+
54+
return { sets, relations };
55+
56+
}

test/selecting.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
let input = document.getElementById('file');
2+
let svg = document.getElementById('svg');
3+
input.onchange = event => {
4+
const f = event.target.files[0];
5+
const r = new FileReader();
6+
r.onload = e => {
7+
const t = e.target.result;
8+
setInstance(new alloy.AlloyInstance(t));
9+
};
10+
r.readAsText(f);
11+
};
12+
13+
function setInstance (instance) {
14+
15+
let { sets, relations } = simplify(instance);
16+
17+
let rowsRelation = relations['this/Matrix<:rows'];
18+
let colsRelation = relations['this/Matrix<:cols'];
19+
let valsRelation = relations['this/Matrix<:vals'];
20+
21+
let matrices = select(sets['this/Matrix']);
22+
matrices.property('rows', rowsRelation, accessDifference);
23+
matrices.property('cols', colsRelation, accessDifference);
24+
matrices.property('vals', valsRelation, accessDifference);
25+
// matrices.print();
26+
matrices.each(object => console.log(object));
27+
28+
}
29+
30+
function select (set, builder) {
31+
32+
builder = builder ? builder : () => ({});
33+
const objects = set.map(builder);
34+
return new Selection(set.slice(), objects);
35+
36+
}
37+
38+
function Selection (data, objects) {
39+
this._data = data;
40+
this._objects = objects;
41+
}
42+
43+
Selection.prototype = {
44+
each: each,
45+
print: print,
46+
property: property
47+
};
48+
49+
function property (name, relation, accessor) {
50+
51+
this._data.forEach((datum, index) => {
52+
const tuples = match(datum, relation);
53+
this._objects[index][name] = select(accessor(datum, tuples));
54+
});
55+
56+
// this._items.forEach(item => {
57+
//
58+
// const tuples = match(item.datum, relation);
59+
// item[name] = select(accessor(item, tuples));
60+
//
61+
// });
62+
63+
}
64+
65+
// Accessors
66+
67+
function accessDifference (item, tuples) {
68+
return tuples.map(tuple => difference(tuple, item));
69+
}
70+
71+
// Helpers
72+
73+
function difference (a, b) {
74+
return a.filter(value => !b.includes(value));
75+
}
76+
77+
function each (callback) {
78+
this._objects.forEach((object, index) => {
79+
callback(object, this._data[index])
80+
});
81+
}
82+
83+
function intersect (a, b) {
84+
return a.filter(value => b.includes(value));
85+
}
86+
87+
function match (group, tuples) {
88+
return tuples
89+
.filter(tuple => intersect(tuple, group).length !== 0);
90+
}
91+
92+
function print (depth=0) {
93+
94+
this._items.forEach(item => {
95+
console.log(`${' '.repeat(depth)}<${item.datum.join(', ')}>`);
96+
for (let prop in item) {
97+
if (item.hasOwnProperty(prop) && prop !== 'datum') {
98+
console.log(`${' '.repeat(depth+1)}${prop}:`);
99+
item[prop].print(depth+2);
100+
}
101+
}
102+
});
103+
104+
}
105+
106+
function simplify (instance) {
107+
108+
let sets = instance.signatures()
109+
.map(sig => ({
110+
id: sig.id(),
111+
data: sig.atoms(true).map(atom => [atom.name()])
112+
}))
113+
.reduce((acc, curr) => {
114+
acc[curr.id] = curr.data;
115+
return acc;
116+
}, {});
117+
118+
let relations = instance.fields()
119+
.map(fld => ({
120+
id: fld.id(),
121+
data: fld.tuples().map(tuple => tuple.atoms().map(atom => atom.name()))
122+
}))
123+
.reduce((acc, curr) => {
124+
acc[curr.id] = curr.data;
125+
return acc;
126+
}, {});
127+
128+
return { sets, relations };
129+
130+
}

test/test.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
<body>
2222
<input id='file' type='file'>
2323
<svg id='svg'/>
24-
<script src='play.js'></script>
24+
<script src='converting.js'></script>
2525
</body>
2626
</html>

0 commit comments

Comments
 (0)