-
Notifications
You must be signed in to change notification settings - Fork 28
/
cql2asp.ts
92 lines (78 loc) · 2.23 KB
/
cql2asp.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
const HOLE = '?';
export default function cql2asp(spec: any) {
const mark = subst_if_hole(spec.mark);
const facts = [];
if (mark) {
facts.push(`mark(${spec.mark}).`);
}
if ('data' in spec && 'url' in spec.data) {
facts.push(`data("${spec.data.url}").`);
}
for (let i = 0; i < spec.encodings.length; i++) {
const enc = spec.encodings[i];
const eid = `e${i}`;
facts.push(`encoding(${eid}).`);
let encFieldType = null;
let encZero = null;
let encBinned = null;
for (const field of Object.keys(enc)) {
const fieldContent = subst_if_hole(enc[field]);
if (!fieldContent) {
continue;
}
if (!remove_if_star(fieldContent)) {
continue;
}
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 if (fieldContent) {
facts.push(`:- not bin(${eid},_).`);
} else {
facts.push(`:- bin(${eid},_).`);
}
} 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;
}
function subst_if_hole(v: any) {
return v !== HOLE ? v : null;
}
function remove_if_star(v: any) {
return v !== '*' ? v : null;
}