|
| 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 | +} |
0 commit comments