Skip to content

Commit

Permalink
Allow visualisations to re-solve, fix some modal bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
cyderize committed Jun 22, 2023
1 parent c8d7dd4 commit 275718f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 37 deletions.
14 changes: 5 additions & 9 deletions src/lib/ModelModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@
const dispatch = createEventDispatcher();
export let modelFiles;
export let selectedModelFile = null;
export let active = false;
let selectedModel = null;
$: init(selectedModelFile);
function init(selectedModelFile) {
selectedModel =
selectedModelFile === null || selectedModelFile === undefined
? modelFiles.length > 0
? modelFiles[0]
: null
: selectedModelFile;
$: init(modelFiles);
function init(modelFiles) {
if (!selectedModel && modelFiles && modelFiles.length > 0) {
selectedModel = modelFiles[0];
}
}
function accept() {
Expand Down
10 changes: 2 additions & 8 deletions src/lib/ParameterModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@
export let active = false;
export let parameters;
export let dataFiles;
export let selectedDataFiles = [];
export let dataTab = true;
let dataTab = true;
let selectedFiles = [];
let parameterValues = [];
$: createSelectedFiles(selectedDataFiles);
$: createParameterValues(parameters);
$: hasDataFiles = dataFiles.length > 0;
$: dataTabActive = hasDataFiles && dataTab;
function createSelectedFiles(selectedDataFiles) {
selectedFiles = [...selectedDataFiles];
}
function createParameterValues(parameters) {
parameterValues = Object.keys(parameters)
.sort()
Expand Down
82 changes: 63 additions & 19 deletions src/lib/Playground.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@
let needsModel = false;
let needsData = null;
let dataFileTab = true;
$: state = currentFile ? currentFile.state : null;
$: canRun =
Expand Down Expand Up @@ -175,7 +174,6 @@
.filter((f) => f.name.endsWith('.dzn') || f.name.endsWith('.json'))
.map((f) => f.name);
let modelModalModel = null;
let parameterModalDataFiles = [];
let parameterModalParameters = {};
Expand Down Expand Up @@ -361,11 +359,13 @@
let getModelResolve = null;
async function getModel(addChecker) {
busyState++;
currentFile.state = editor.getState();
let modelFile = isModel ? currentFile : null;
if (!modelFile) {
if (modelFiles.length === 0) {
// No models to run
busyState--;
return false;
} else if (modelFiles.length === 1) {
// Only one model, so use it
Expand All @@ -378,6 +378,7 @@
});
if (!result) {
// Cancelled
busyState--;
return false;
}
modelFile = files.find((f) => f.name === result.modelFile);
Expand Down Expand Up @@ -442,6 +443,7 @@
});
if (!result) {
// Cancelled
busyState--;
return false;
}
if (result.parameters) {
Expand All @@ -452,7 +454,6 @@
}
}
model.addDznString(dzn);
dataFileTab = false;
parameterModalParameters = result.parameters;
} else {
for (const file of result.dataFiles) {
Expand All @@ -461,7 +462,6 @@
fileList.push(file);
}
}
dataFileTab = true;
parameterModalDataFiles = result.dataFiles;
}
} finally {
Expand All @@ -472,18 +472,22 @@
// Ignore and just run
console.error(e);
}
busyState--;
return { model, fileList };
}
async function run() {
busyState++;
const mznModel = await getModel(true);
if (!mznModel) {
// Cancelled
busyState--;
return;
}
const { model, fileList } = mznModel;
const options = solverConfig.getSolvingConfiguration(currentSolver.id);
await runWith(model, fileList, options);
}
async function runWith(model, fileList, options) {
const startTime = Date.now();
if (autoClearOutput) {
output = [];
Expand All @@ -496,14 +500,10 @@
},
];
minizinc = model.solve({
options: solverConfig.getSolvingConfiguration(currentSolver.id),
options,
jsonOutput: false,
});
busyState--;
if (visualisation) {
visualisation.reset();
}
hasVisualisation = false;
resetVisualisation();
minizinc.on('error', addOutput);
minizinc.on('warning', addOutput);
minizinc.on('solution', (v) => addOutput(v, Date.now() - startTime));
Expand Down Expand Up @@ -533,11 +533,9 @@
}
async function compile() {
busyState++;
const mznModel = await getModel(true);
if (!mznModel) {
// Cancelled
busyState--;
return;
}
const { model, fileList } = mznModel;
Expand All @@ -557,7 +555,6 @@
minizinc = model.compile({
options: solverConfig.getCompilationConfiguration(currentSolver.id),
});
busyState--;
minizinc.on('error', addOutput);
minizinc.on('warning', addOutput);
minizinc.on('statistics', addOutput);
Expand Down Expand Up @@ -603,6 +600,14 @@
output = output; // Force update
}
function resetVisualisation() {
if (visualisation) {
visualisation.reset();
}
hasVisualisation = false;
visualisationOpen = false;
}
async function processVisMessage(value, time) {
if (value.type === 'trace' && value.section === 'vis_json') {
if (!hasVisualisation) {
Expand Down Expand Up @@ -798,6 +803,45 @@
let visualisation;
let hasVisualisation = false;
let visualisationOpen = false;
function visReSolve(cfg) {
if (minizinc) {
stop();
}
const fileList = [cfg.modelFile];
const modelFileName = cfg.modelFile.substring(
0,
cfg.modelFile.length - 4
);
const checker = files.find(
(f) =>
f.name === `${modelFileName}.mzc` ||
f.name === `${modelFileName}.mzc.mzn`
);
if (checker) {
fileList.push(checker.name);
}
if (cfg.dataFiles) {
for (const dzn of cfg.dataFiles) {
fileList.push(dzn);
}
}
const model = new MiniZinc.Model();
for (const file of files) {
model.addFile(
file.name,
file.state.doc.toString(),
fileList.indexOf(file.name) !== -1
);
}
runWith(
model,
fileList,
cfg.options ||
solverConfig.getSolvingConfiguration(currentSolver.id)
);
}
</script>

<div class="mzn-playground">
Expand Down Expand Up @@ -1046,7 +1090,10 @@
class="tab-window"
class:visible={visualisationOpen}
>
<Visualisation bind:this={visualisation} />
<Visualisation
bind:this={visualisation}
on:solve={(e) => visReSolve(e.detail)}
/>
</div>
<div
class="tab-window"
Expand Down Expand Up @@ -1127,16 +1174,13 @@
<ModelModal
active={needsModel}
{modelFiles}
selectedModelFile={modelModalModel}
on:accept={(e) => getModelResolve(e.detail)}
on:cancel={() => getModelResolve(false)}
/>

<ParameterModal
active={needsData}
{dataFiles}
selectedDataFiles={parameterModalDataFiles}
dataTab={dataFileTab}
parameters={parameterModalParameters}
on:accept={(e) => getModelResolve(e.detail)}
on:cancel={() => getModelResolve(false)}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Visualisation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
for (let i = 0; i < visualisations.length; i++) {
const payload = {
time,
data: solution[i],
data: solution ? solution[i] : null,
};
visualisations[i].solutions.push(payload);
sendMessage({ event: 'solution', payload }, visualisations[i]);
Expand Down

0 comments on commit 275718f

Please sign in to comment.