Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using new conjure aas setup and also bug fixing block output #12

Merged
merged 3 commits into from
Mar 24, 2024
Merged
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
2 changes: 2 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* EDITED by N-J-Martin*/

body {
margin: 0;
max-width: 100vw;
Expand Down
2 changes: 2 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<!DOCTYPE html>
<!-- EDITED by N-J-Martin-->
<html>
<head>
<meta charset="utf-8" />
<title>Essence Block Editor</title>
<script src="https://conjure-aas.cs.st-andrews.ac.uk/client.js"></script>
</head>
<body>
<div id="pageContainer">
Expand Down
109 changes: 56 additions & 53 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import * as Blockly from 'blockly';
import {TypedVariableModal} from '@blockly/plugin-typed-variable-modal';
//import {blocks} from './blocks/text';
import {blocks} from './blocks/essence';
import {jsonBlocks} from './blocks/json';
import {essenceGenerator} from './generators/essence';
Expand All @@ -18,7 +17,6 @@ import {save, load} from './serialization';
import {toolbox} from './toolbox';
import {jsonToolbox} from './jsonToolbox';
import './index.css';
import { variables } from 'blockly/blocks';

// Register the blocks and generator with Blockly
Blockly.common.defineBlocks(blocks);
Expand All @@ -36,7 +34,7 @@ let startBlock = dataWS.newBlock("object");
startBlock.initSvg();
dataWS.render()

const blockOut = Blockly.inject(document.getElementById('blocklyDiv2'), {readOnly:true});
const blockOut = Blockly.inject(document.getElementById('blocklyDiv2'), {readOnly: true});

//variable category using https://www.npmjs.com/package/@blockly/plugin-typed-variable-modal.
// much of the code below is from the usage instructions
Expand Down Expand Up @@ -203,60 +201,65 @@ async function get(currentJobid) {
}

// Runs essence code in conjure, outputs solution logs
// from https://conjure-aas.cs.st-andrews.ac.uk/submitDemo.html
// from https://conjure-aas.cs.st-andrews.ac.uk/
async function getSolution() {
// gets the data from the data input workspace
let data = jsonGenerator.workspaceToCode(dataWS) + "\n";
console.log("data" + data);
solutionText.innerHTML = "Solving..."
// waits for code to be submitted to conjure, until jobID returned
const currentJobid = await submit(data);
// get solution for our job. Need to wait until either solution found, code failed, or timed out
var solution = await get(currentJobid);
while (solution.status == 'wait'){
solution = await get(currentJobid);
}
// outputs text solution
solutionText.innerHTML = JSON.stringify(solution, undefined, 2);

// if solved, create relevant blocks and add to output workspace
if (solution.status == "ok"){
for (let sol of solution.solution){
for (let v in sol){
blockOut.createVariable(v);
let varBlock = blockOut.newBlock('variables_set');
varBlock.setFieldValue(blockOut.getVariable(v).getId(), 'VAR');
let valueBlock;
switch (typeof(sol[v])){
case("bigint"):
case("number"): {
valueBlock = blockOut.newBlock('math_number');
valueBlock.setFieldValue(sol[v], "NUM");
break;
}
case("string"): {
console.log("enum");
valueBlock = blockOut.newBlock('text');
valueBlock.setFieldValue(sol[v], "TEXT");
break;
}
default:{
console.log("idk");
valueBlock = null;
// gets the data from the data input workspace
let data = jsonGenerator.workspaceToCode(dataWS);
console.log("data " + data);
let code = essenceGenerator.workspaceToCode(ws);
console.log("code " + code);
const client = new ConjureClient("conjure-blocks");
client.solve(code, {data : data})
.then(result => outputSolution(result));
};

// outputs the solution in blocks, and outputs the log
function outputSolution(solution) {
// make writable, so blocks line up nicely
blockOut.options = new Blockly.Options({readOnly: false});
solutionText.innerHTML = JSON.stringify(solution, undefined, 2);
// clear any blocks from previous runs
blockOut.clear();
// if solved, create relevant blocks and add to output workspace
if (solution.status == "ok"){
for (let sol of solution.solution){
for (let v in sol){
blockOut.createVariable(v);
let varBlock = blockOut.newBlock('variables_set');
varBlock.setFieldValue(blockOut.getVariable(v).getId(), 'VAR');
let valueBlock;
switch (typeof(sol[v])){
case("bigint"):
case("number"): {
console.log("number");
valueBlock = blockOut.newBlock('math_number');
valueBlock.setFieldValue(sol[v], "NUM");
break;
}

};
varBlock.getInput("VALUE").connection.connect(valueBlock.outputConnection);
//let addVarBlock = new Blockly.Events.BlockCreate(varBlock);
//addVarBlock.run(true);
varBlock.initSvg();
valueBlock.initSvg();
blockOut.render();
}
}
case("string"): {
console.log("enum");
valueBlock = blockOut.newBlock('text');
valueBlock.setFieldValue(sol[v], "TEXT");
break;
}
default:{
console.log("idk");
valueBlock = null;
break;
}

};
varBlock.getInput("VALUE").connection.connect(valueBlock.outputConnection);
varBlock.initSvg();
valueBlock.initSvg();
blockOut.cleanUp();
blockOut.render();
}

}
}
blockOut.options = new Blockly.Options({readOnly: true});
}

}

// generate essence file from generated code
Expand Down