-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
executable file
·379 lines (306 loc) · 9.25 KB
/
index.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import * as yargs from 'yargs';
import * as util from 'util';
import {exec} from "child_process";
import * as stream from "stream";
import * as fs from "fs";
import {ranking} from "./ranking";
import {constraints} from "./constraints";
import {assert, eq, not, or, iteFromDictFlipKeyValue, capitalizeFirstLetter} from "./helpers";
import { FIELDS, QUERIES, Fields, Query } from './queries';
import {PRIMITIVE_MARKS} from 'vega-lite/build/src/mark';
import {CHANNELS} from 'vega-lite/build/src/channel';
import {AGGREGATE_OPS} from 'vega-lite/build/src/aggregate';
// parse args
const argv = yargs.argv;
const types = `
(set-option :produce-unsat-cores true)
; data related types
(declare-datatypes () ((RawType
StringType FloatType IntegerType DateType BooleanType
)))
(declare-datatypes () ((Field
(mk-field (name String) (type RawType) (cardinality Int))
)))
; encoding related types
(declare-datatypes () ((Marktype
${PRIMITIVE_MARKS.map(mark => capitalizeFirstLetter(mark) + 'Mark').join(' ')}
)))
(declare-datatypes () ((Channel
${CHANNELS.map(channel => capitalizeFirstLetter(channel) + 'Channel').join(' ')}
)))
(declare-datatypes () ((EncodingType
Quantitative Ordinal Nominal
)))
(declare-datatypes () ((AggFunc
; TODO: add 'Aggregate' postfix
None ${AGGREGATE_OPS.map(agg => capitalizeFirstLetter(agg)).join(' ')}
)))
(declare-datatypes () ((Scale
(mk-scale (zero bool) (log bool))
)))
(declare-datatypes () ((Encoding
(mk-enc (channel Channel) (field Field) (type EncodingType) (binned Bool) (agg AggFunc) (scale Scale))
)))
`;
const nullEnc = `
(declare-const nullEnc Encoding)
`;
const countField = `
(declare-const countField Field)
${assert(eq("(name countField)", '"*"'))}
${assert(eq("(type countField)", "IntegerType"))}
`
const markDeclaration = `
(declare-const mark Marktype)
`
function addGetXGetYEnc(encs){
const getXEncDict = {};
encs.forEach((enc, i) => {
getXEncDict[enc] = `(channel ${enc})`;
});
let dim = "X";
const getXEncFunc = `(define-fun get${dim}Enc () Encoding
${iteFromDictFlipKeyValue(dim + "Channel", getXEncDict, "nullEnc")}
)`;
dim = "Y";
const getYEncFunc = `(define-fun get${dim}Enc () Encoding
${iteFromDictFlipKeyValue(dim + "Channel", getXEncDict, "nullEnc")}
)`;
return getXEncFunc + '\n' + getYEncFunc;
}
function solve(getUnsatCore: boolean, encs: string[]){
let solve = `
; get output
(check-sat)
`;
if (getUnsatCore){
solve+=`
(echo "Unsat Core:")
(get-unsat-core)`
} else {
solve+=`
${encs.map(e => `(get-value (${e}))`).join("\n")}
(get-value (mark))
(get-model)
(echo "Spec:")
(get-value (mark))
${encs.map(e => `(get-value (${e}))`).join("\n")}
`;
}
return solve;
}
//function debugRanking(fields: Fields, query: Query, encs, produceUnsatCore: boolean){
function debugRanking(){
//return encs.map((e) => ` (get-value (`)
return "(get-value (pen mpen gx gy nullEnc))";
}
function callZ3(program: string, callback: (output: string) => void) {
console.time("z3");
// execute in z3
const child = exec("z3 /dev/fd/0", function (err, stdout, stderr) {
if (err) {
console.error(err);
}
if (stderr) {
console.error(stderr);
}
if (argv["v"]) {
console.timeEnd("z3");
}
callback(stdout);
});
const stdinStream = new stream.Readable();
stdinStream.push(program); // Add data to the internal queue for users of the stream to consume
stdinStream.push(null); // Signals the end of the stream (EOF)
stdinStream.pipe(child.stdin);
}
function buildProgram(fields: Fields, query: Query, produceUnsatCore: boolean) {
let program = "";
program += types;
program += markDeclaration;
program += countField;
program += nullEnc;
// add fields
fields.forEach(f => {
const name = f.name + "Field";
program += `(declare-const ${name} Field)
`;
program += assert(eq(`(name ${name})`, `"${f.name}"`)) + "\n";
program += assert(eq(`(type ${name})`, `${f.type}Type`)) + "\n";
program += assert(eq(`(cardinality ${name})`, `${f.cardinality}`)) + "\n";
});
// add null field
const nullname = "nullField";
program += `(declare-const ${nullname} Field)
`;
program += assert(eq(`(name ${nullname})`, `"nullfield"`)) + "\n";
program += assert(eq(`(field nullEnc)`, nullname)) + "\n";
// add mark type constraint
if (query.mark) {
program += assert(eq("mark", `${capitalizeFirstLetter(query.mark)}Mark`));
}
// add encodings
const encs: string[] = [];
if (query.encodings) {
query.encodings.forEach((e, i) => {
const enc = `e${i}`;
program += `(declare-const ${enc} Encoding)`;
program += assert(not(eq(enc, "nullEnc")));
if (e.field) {
program += assert(eq(`(name (field ${enc}))`, `"${e.field}"`));
}
if (e.type) {
program += assert(eq(`(type ${enc})`, `${capitalizeFirstLetter(e.type)}`));
}
if (e.channel) {
program += assert(eq(`(channel ${enc})`, `${capitalizeFirstLetter(e.channel)}Channel`));
}
if (e.aggregate) {
program += assert(eq(`(agg ${enc})`, `${capitalizeFirstLetter(e.aggregate)}`));
}
if (e.binned !== undefined) {
if (e.binned) {
program += assert(`(binned ${enc})`);
} else {
program += assert(not(`(binned ${enc})`));
}
}
if (e.scale !== undefined) {
if (e.scale.log === true) {
program += assert(`(log (scale ${enc}))`);
} else if (e.scale.log === false) {
program += assert(not(`(log (scale ${enc}))`));
}
if (e.scale.zero === true) {
program += assert(`(zero (scale ${enc}))`);
} else if (e.scale.zero === false) {
program += assert(not(`(zero (scale ${enc}))`));
}
}
encs.push(enc);
});
}
// add constraints
if (encs.length === 0) {
// we need at least one channel
program += assert("false");
} else {
program += addGetXGetYEnc(encs);
const {hard, soft} = constraints(encs, fields.map(f => f.name));
program += hard.join(" ");
if (!produceUnsatCore) {
program += soft.join(" ");
}
}
if (!produceUnsatCore) {
const [defs, minimizeStmt] = ranking(fields, query, encs)
program += defs;
program += minimizeStmt;
}
program += solve(produceUnsatCore, encs);
program += debugRanking()
return program;
} // END buildProgram
function makeIterator(array: string[]) {
let nextIndex = 0;
return {
next: () => {
return nextIndex < array.length ?
{value: array[nextIndex++], done: false} :
{value: '', done: true};
}
};
}
function parse(stdout) {
const lines = makeIterator(stdout.split('\n'));
let marktype = '';
let encoding = {};
let next = lines.next();
// find marktype def
while (next.value !== "Spec:" && !next.done) {
next = lines.next();
}
if (next.done) {
throw new Error("No marktype found");
}
next = lines.next();
(next.value.replace as any)(
/\(\(mark (\w+)Mark\)\)/gi,
(_, $1) => {
marktype = $1.toLowerCase();
}
)
next = lines.next();
while (!next.done) {
let line = next.value;
next = lines.next();
while (!next.done && !next.value.startsWith("((e")) {
line += ` ${next.value.trim()}`;
next = lines.next();
}
(line.replace as any)(
// ((e0 (mk-enc XChannel (mk-field "str1" String 3) Ordinal false Min (mk-scale true false))))
// $1 $2 $3 $4 $5 $6 $7 $8 $9
/\(\(e\d* \(mk-enc (\w+)Channel \(mk-field "(\w+|\*)" (\w+) (\d+)\) (\w+) (\w+) (\w+) \(mk-scale (\w+) (\w+)\)\)\)\)/gi,
(_, $1, $2, $3, $4, $5, $6, $7, $8, $9) => {
const enc: any = {
field: $2,
type: $5.toLowerCase()
}
if ($6 === "true") {
enc.bin = true;
}
if ($7 !== "None") {
enc.aggregate = $7.toLowerCase()
}
enc.scale = {};
if ($8 === "true") {
enc.scale.zero = true;
}
if ($9 === "true") {
enc.scale.log = true;
}
if (Object.keys(enc.scale).length === 0) {
delete enc.scale;
}
encoding[$1.toLowerCase()] = enc;
}
)
}
const spec = {
mark: marktype,
encoding: encoding
};
return spec;
}
const query = QUERIES[parseInt(argv.query) || 0];
function run(produceUnsatCore) {
const program = buildProgram(FIELDS, query, produceUnsatCore);
if (argv["d"]) {
console.log("Writing out.z3");
fs.writeFile("out.z3", program, () => {});
}
callZ3(program, (output) => {
if (!produceUnsatCore && output.startsWith("unsat")) {
console.warn("Program was unsat");
return run(true);
}
if (argv["v"]) {
console.log("Output from z3:");
console.log(output);
}
if (!produceUnsatCore) {
const vl = argv["vl"]
if (vl) {
const spec: any = parse(output);
spec.data = query.data;
if (argv["v"]) {
console.log(`Writing ${vl}`);
console.log(util.inspect(spec, false, null));
}
fs.writeFile(vl, JSON.stringify(spec, null, ' '), () => {});
}
}
});
}
run(false);