-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping.ts
272 lines (248 loc) · 7.5 KB
/
mapping.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
import {Schema} from '@opvious/api/sdk';
import {assert} from '@opvious/stl-errors';
import {isIndicator, Label, TensorOutline} from '../common.js';
import {A1, Range, rangeA1} from '../spreadsheet/index.js';
import {Header, newHeader, Table} from '../table.js';
/**
* Associates model data with table columns. The returned mapping may be partial
* (missing parameters and/or variables).
*/
export function computeInputMapping(
tables: ReadonlyArray<Table>,
out: Schema<'Outline'>
): InputMapping {
validateNoHeaderCollisions(out);
const reg = new ItemRangeRegistry();
const params: TensorMapping[] = [];
for (const tsr of out.parameters) {
if (tsr.derivation != null) {
// This is a derived parameter, already captured.
continue;
}
const mapping = tensorMapping(tables, tsr, reg);
if (!mapping) {
throw new Error('parameter not found ' + tsr.label);
}
params.push(mapping);
}
const variables: TensorMapping[] = [];
for (const tsr of out.variables) {
const mapping = tensorMapping(tables, tsr);
if (mapping) {
variables.push(mapping);
}
}
for (const dim of out.dimensions) {
const {label} = dim;
const header = newHeader(label);
for (const t of tables) {
const block = t.blocks.get(header);
if (!block) {
continue;
}
if (block.kind !== 'slim') {
throw new Error('Wide dimension block ' + label);
}
reg.addRange(label, block.bodyRange);
}
}
const mapping = {
dimensions: out.dimensions.map((d) => ({
label: d.label,
isNumeric: d.isNumeric,
itemRanges: reg.ranges(d.label),
})),
parameters: params,
variables,
};
return mapping;
}
function tensorMapping(
tables: ReadonlyArray<Table>,
tsr: TensorOutline,
reg?: ItemRangeRegistry
): TensorMapping | undefined {
const {label} = tsr;
let mapping: TensorMapping | undefined;
for (const table of tables) {
const builder = TensorMappingBuilder.ifCompatible(table, tsr, reg);
if (!builder) {
continue;
}
if (mapping) {
throw new Error('Duplicate tensor ' + label);
}
mapping = builder.build();
}
return mapping;
}
class TensorMappingBuilder {
private isProjected = false;
private readonly usedBlocks = new Set<Header>();
private constructor(
private readonly tensor: TensorOutline,
private valueRange: Range,
private keyBoxes: ReadonlyMap<Header, KeyBox>,
private readonly itemRangeRegistry?: ItemRangeRegistry
) {}
static ifCompatible(
table: Table,
tsr: TensorOutline,
reg?: ItemRangeRegistry
): TensorMappingBuilder | undefined {
const block = table.blocks.get(newHeader(tsr.label));
if (!block) {
return undefined;
}
const keyBoxes = new Map<Header, KeyBox>([
[block.header, {kind: 'value', range: block.bodyRange}],
]);
if (block.kind === 'wide') {
keyBoxes.set(block.nestedHeader, {kind: 'row', range: block.headRange});
}
for (const block of table.blocks.values()) {
if (block.kind !== 'slim') {
continue;
}
keyBoxes.set(block.header, {kind: 'column', range: block.bodyRange});
}
return new TensorMappingBuilder(tsr, block.bodyRange, keyBoxes, reg);
}
build(): TensorMapping {
const keyBoxes: KeyBox[] = [];
for (const binding of this.tensor.bindings) {
keyBoxes.push(this.bindingKeyBox(binding));
}
return {
label: this.tensor.label,
keyBoxes,
valueRange: this.isProjected ? undefined : this.valueRange,
};
}
private bindingKeyBox(binding: Schema<'SourceBinding'>): KeyBox {
const {keyBoxes, tensor, usedBlocks} = this;
const {dimensionLabel: dim, qualifier: qual} = binding;
if (dim == null && qual == null) {
throw new Error('Underqualified tensor ' + tensor.label);
}
let header = qual ? newHeader(qual) : undefined;
let box = header ? keyBoxes.get(header) : undefined;
if (dim != null) {
if (!box) {
header = newHeader(dim);
box = keyBoxes.get(header);
}
}
if (box) {
assert(header != null, 'Null header');
if (usedBlocks.has(header)) {
throw new Error('Reused binding header ' + header);
}
usedBlocks.add(header);
} else {
if (this.isProjected || !isIndicator(tensor)) {
throw new Error('Missing binding ' + header);
}
this.isProjected = true;
box = {kind: 'value', range: this.valueRange};
}
if (dim != null) {
this.itemRangeRegistry?.addRange(dim, box.range);
}
return box;
}
}
class ItemRangeRegistry {
private readonly byDimension = new Map<Label, Map<A1, Range>>();
addRange(dim: Label, rg: Range): void {
let rgs = this.byDimension.get(dim);
if (!rgs) {
rgs = new Map();
this.byDimension.set(dim, rgs);
}
rgs.set(rangeA1(rg), rg);
}
ranges(dim: Label): ReadonlyArray<Range> {
const rgs = this.byDimension.get(dim);
if (!rgs) {
throw new Error('Dimension not found ' + dim);
}
return [...rgs.values()];
}
}
// Mappings must be JSON-serializable.
export interface InputMapping {
readonly dimensions: ReadonlyArray<DimensionMapping>;
readonly parameters: ReadonlyArray<TensorMapping>;
readonly variables: ReadonlyArray<TensorMapping>;
}
export interface DimensionMapping {
readonly label: Label;
readonly isNumeric: boolean;
readonly itemRanges: ReadonlyArray<Range>;
}
export interface TensorMapping {
readonly label: Label;
readonly keyBoxes: ReadonlyArray<KeyBox>;
readonly valueRange?: Range; // Can be absent for indicators.
}
export type KeyBoxKind = 'column' | 'row' | 'value';
export interface KeyBox {
readonly kind: KeyBoxKind;
readonly range: Range;
}
function validateNoHeaderCollisions(out: Schema<'Outline'>): void {
const byHeader = new Map<Header, Label>();
for (const dim of out.dimensions) {
addLabel(dim.label);
}
for (const param of out.parameters) {
addLabel(param.label);
}
for (const variable of out.variables) {
addLabel(variable.label);
}
for (const param of out.parameters) {
checkQualifiers(param);
}
for (const variable of out.variables) {
checkQualifiers(variable);
}
function addLabel(next: Label): void {
const h = newHeader(next);
const prev = byHeader.get(h);
if (prev) {
throw new Error(`Header collision ${h} (${prev}, ${next})`);
}
byHeader.set(h, next);
}
function checkQualifiers(tsr: TensorOutline): void {
const quals = new Set<Header>();
for (const {qualifier: qual} of tsr.bindings) {
if (!qual) {
continue;
}
const h = newHeader(qual);
if (quals.has(h) || byHeader.has(h)) {
throw new Error('Conflicting qualifier ' + qual);
}
quals.add(h);
}
}
}