Skip to content

Commit 06d0ab2

Browse files
committed
log minizinc solutions
1 parent a244abe commit 06d0ab2

3 files changed

Lines changed: 25 additions & 12 deletions

File tree

src/livecodes/languages/minizinc/lang-minizinc-script.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ let hasRun = false;
3232
const pageLoaded = new Promise((resolve) => onLoad(resolve));
3333
const modPromise = import(minizincUrl);
3434

35+
const getOutput = (solution: any) =>
36+
solution?.output?.default ??
37+
solution?.output?.dzn ??
38+
solution?.output?.json ??
39+
solution?.output?.raw ??
40+
solution?.output ??
41+
'';
42+
3543
const run = (data: MiniZincData = {}) => {
3644
if (!MiniZinc) {
3745
throw new Error('MiniZinc is not initialized. await livecodes.minizinc.init() first.');
@@ -67,12 +75,22 @@ livecodes.minizinc = {
6775
await livecodes.minizinc.init();
6876
return new Promise((resolve) => {
6977
const solve = run(data);
78+
let lastOutput = '';
7079
const errors: any[] = [];
7180
solve.on('error', (error: any) => {
81+
const msg = `MiniZinc: ${error?.what ?? 'Error'}: ${error.message}`;
7282
// eslint-disable-next-line no-console
73-
console.error(error);
83+
console.error(msg);
7484
errors.push(error);
7585
});
86+
solve.on('solution', (solution: any) => {
87+
const output = getOutput(solution);
88+
const line = '----------';
89+
const msg = typeof output === 'string' ? output + line : output;
90+
// eslint-disable-next-line no-console
91+
console.log(msg);
92+
lastOutput = output;
93+
});
7694
solve.on('exit', (msg: any) => {
7795
if (msg.code === 0) return;
7896
if (errors.length) {
@@ -103,13 +121,8 @@ livecodes.minizinc = {
103121
ERROR: '=====ERROR=====',
104122
};
105123
const status = statusMap[result.status] || '';
106-
const output =
107-
result.solution?.output?.default ??
108-
result.solution?.output?.dzn ??
109-
result.solution?.output?.json ??
110-
result.solution?.output?.raw ??
111-
result.solution?.output ??
112-
'';
124+
const resultOutput = getOutput(result.solution);
125+
const output = lastOutput === resultOutput ? '' : resultOutput;
113126
const msg = typeof output === 'string' ? output + status : output;
114127
// eslint-disable-next-line no-console
115128
console.log(msg);

src/livecodes/templates/starter/minizinc-starter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ cocoa = 500;
156156
},
157157
script: {
158158
language: 'minizinc',
159-
content: `
159+
content: String.raw`
160160
% from https://docs.minizinc.dev/en/stable/part_2_tutorial.html
161161
% Baking cakes for the school fete (with data file)
162162
@@ -195,8 +195,8 @@ constraint 75 * c <= cocoa;
195195
solve maximize 400 * b + 450 * c;
196196
197197
output [
198-
"no. of banana cakes = \\(b)\\n",
199-
"no. of chocolate cakes = \\(c)\\n"];
198+
"no. of banana cakes = \(b)\n",
199+
"no. of chocolate cakes = \(c)\n"];
200200
`.trimStart(),
201201
},
202202
};

src/livecodes/utils/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ export const onLoad = /* @__PURE__ */ (fn: (...args: any[]) => any) => {
689689
if (document.readyState === 'complete' || document.readyState === 'interactive') {
690690
fn();
691691
} else {
692-
window.addEventListener('load', fn);
692+
window.addEventListener('load', fn, { once: true });
693693
}
694694
};
695695

0 commit comments

Comments
 (0)