Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const node = (output) => ({
"node:fs/promises",
"node:path",
"node:os",
"node:crypto",
],
});

Expand Down
34 changes: 6 additions & 28 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,45 +81,30 @@ export class Model {
constructor() {
this.vfs = {};
this._toRun = [];
this.unnamedCount = 0;
}
clone() {
const clone = new Model();
clone.vfs = { ...this.vfs };
clone._toRun = [...this.toRun];
clone.unnamedCount = this.unnamedCount;
clone._toRun = [...this._toRun];
return clone;
}
addString(model) {
let filename = `_mzn_${this.unnamedCount++}.mzn`;
while (filename in this.vfs) {
filename = `_mzn_${this.unnamedCount++}.mzn`;
}
const filename = `_mzn_${crypto.randomUUID()}.mzn`;
this.addFile(filename, model);
return filename;
}
addDznString(dzn) {
let filename = `_dzn_${this.unnamedCount++}.dzn`;
while (filename in this.vfs) {
filename = `_dzn_${this.unnamedCount++}.dzn`;
}
const filename = `_dzn_${crypto.randomUUID()}.dzn`;
this.addFile(filename, dzn);
return filename;
}
addJson(data) {
let filename = `_json_${this.unnamedCount++}.json`;
while (filename in this.vfs) {
filename = `_json_${this.unnamedCount++}.json`;
}
const filename = `_json_${crypto.randomUUID()}.json`;
this.addFile(filename, JSON.stringify(data));
return filename;
}
addFile(filename, contents, use = true) {
if (typeof contents !== "string") {
if (filename in this.vfs) {
this._addToRun(filename, use);
return;
}
throw new Error("Missing file contents argument");
}
this.vfs[filename] = contents;
Expand All @@ -144,10 +129,7 @@ export class Model {
const preArgs = [];
let files = this.vfs;
if (options) {
let mpcFile = `_mzn_${this.unnamedCount++}.mpc`;
while (mpcFile in this.vfs) {
mpcFile = `_mzn_${this.unnamedCount++}.mpc`;
}
const mpcFile = `_mzn_${crypto.randomUUID()}.mpc`;
files = { ...this.vfs, [mpcFile]: JSON.stringify(options) };
preArgs.push(mpcFile);
}
Expand Down Expand Up @@ -228,11 +210,7 @@ export class Model {
}
compile(cfg) {
const config = { ...cfg };
let i = 0;
let out = `_fzn_${i++}.fzn`;
while (out in this.vfs) {
out = `_fzn_${i++}.fzn`;
}
const out = `_fzn_${crypto.randomUUID()}.fzn`;
const args = ["-c", "--fzn", out];
const { worker } = this._run(args, config.options, [out]);
// Don't reuse this worker, always create add a new one to the pool
Expand Down
26 changes: 6 additions & 20 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import rl from "node:readline";
import fs from "node:fs/promises";
import path from "node:path";
import os from "node:os";
import crypto from "node:crypto";

let settings = { minizinc: "minizinc", _executable: "minizinc" };

Expand Down Expand Up @@ -37,36 +38,25 @@ export class Model {
constructor() {
this.vfs = {};
this._toRun = [];
this.unnamedCount = 0;
}
clone() {
const clone = new Model();
clone.vfs = { ...this.vfs };
clone._toRun = [...this.toRun];
clone.unnamedCount = this.unnamedCount;
clone._toRun = [...this._toRun];
return clone;
}
addString(model) {
let filename = `_mzn_${this.unnamedCount++}.mzn`;
while (filename in this.vfs) {
filename = `_mzn_${this.unnamedCount++}.mzn`;
}
const filename = `_mzn_${crypto.randomUUID()}.mzn`;
this._addVirtual(filename, model);
return filename;
}
addDznString(dzn) {
let filename = `_dzn_${this.unnamedCount++}.dzn`;
while (filename in this.vfs) {
filename = `_dzn_${this.unnamedCount++}.dzn`;
}
const filename = `_dzn_${crypto.randomUUID()}.dzn`;
this._addVirtual(filename, dzn);
return filename;
}
addJson(data) {
let filename = `_json_${this.unnamedCount++}.json`;
while (filename in this.vfs) {
filename = `_json_${this.unnamedCount++}.json`;
}
const filename = `_json_${crypto.randomUUID()}.json`;
this._addVirtual(filename, JSON.stringify(data));
return filename;
}
Expand Down Expand Up @@ -109,11 +99,7 @@ export class Model {
const preArgs = ["--json-stream"];
const tempdir = await fs.mkdtemp(path.join(os.tmpdir(), "mzn"));
if (options) {
let mpcFile = `_mzn_${this.unnamedCount++}.mpc`;
while (mpcFile in this.vfs) {
mpcFile = `_mzn_${this.unnamedCount++}.mpc`;
}
mpcFile = path.join(tempdir, mpcFile);
const mpcFile = path.join(tempdir, `_mzn_${crypto.randomUUID()}.mpc`);
await fs.writeFile(mpcFile, JSON.stringify(options));
preArgs.push(mpcFile);
}
Expand Down