-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathjson2constraints.ts
52 lines (45 loc) · 1.13 KB
/
json2constraints.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
import { Constraint } from './constraints2json';
export interface ConstraintAsp {
definitions: string;
weights?: string;
assigns?: string;
}
export default function json2constraints(json: Constraint[]): ConstraintAsp {
const type = json[0].type;
// tslint:disable-next-line
json.forEach((constraint) => {
if (constraint.type !== type) {
throw new Error(`constraints not all of type ${type}`);
}
});
let definitions = '';
let weights;
let assigns;
if (type === 'soft') {
weights = '';
assigns = '';
}
for (const constraint of json) {
const def = `% @constraint ${constraint.description}
${constraint.asp}`;
definitions += def;
definitions += '\n\n';
if (type === 'soft') {
const weight = `#const ${constraint.name}_weight = ${constraint.weight}.`;
weights += weight;
weights += '\n';
const assign = `soft_weight(${constraint.name}, ${constraint.name}_weight).`;
assigns += assign;
assigns += '\n';
}
}
if (type === 'hard') {
return { definitions };
} else {
return {
definitions,
weights,
assigns,
};
}
}