-
Notifications
You must be signed in to change notification settings - Fork 28
/
vl2asp.ts
75 lines (67 loc) · 2.1 KB
/
vl2asp.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
import { TopLevelUnitSpec } from 'vega-lite/build/src/spec/unit';
import { Field } from 'vega-lite/build/src/channeldef';
/**
* Convert from Vega-Lite to ASP.
*/
export default function vl2asp(spec: TopLevelUnitSpec<Field>): string[] {
const facts = [`mark(${spec.mark}).`];
if ('data' in spec && 'url' in spec.data) {
facts.push(`data("${spec.data.url}").`);
}
const encoding = spec.encoding || {};
let i = 0;
for (const channel of Object.keys(encoding)) {
const eid = `e${i++}`;
facts.push(`encoding(${eid}).`);
facts.push(`channel(${eid},${channel}).`);
let encFieldType = null;
let encZero = null;
let encBinned = null;
// translate encodings
for (const field of Object.keys(encoding[channel])) {
const fieldContent = encoding[channel][field];
if (field === 'type') {
encFieldType = fieldContent;
}
if (field === 'bin') {
encBinned = fieldContent;
}
if (field === 'scale') {
// translate two boolean fields
if ('zero' in fieldContent) {
encZero = fieldContent.zero;
if (fieldContent.zero) {
facts.push(`zero(${eid}).`);
} else {
facts.push(`:- zero(${eid}).`);
}
}
if ('log' in fieldContent) {
if (fieldContent.log) {
facts.push(`log(${eid}).`);
} else {
facts.push(`:-log(${eid}).`);
}
}
} else if (field === 'bin') {
if (fieldContent.maxbins) {
facts.push(`${field}(${eid},${fieldContent.maxbins}).`);
} else {
facts.push(`${field}(${eid},10).`);
}
} else if (field === 'field') {
// fields can have spaces and start with capital letters
facts.push(`${field}(${eid},"${fieldContent}").`);
} else {
// translate normal fields
if (field !== 'bin') {
facts.push(`${field}(${eid},${fieldContent}).`);
}
}
}
if (encFieldType === 'quantitative' && encZero === null && encBinned === null) {
facts.push(`zero(${eid}).`);
}
}
return facts;
}