Skip to content

Commit

Permalink
Merge pull request #12 from N-J-Martin/usingNewConjureAasSetup
Browse files Browse the repository at this point in the history
Using new conjure aas setup and also bug fixing block output
  • Loading branch information
ozgurakgun authored Mar 24, 2024
2 parents ef66cd4 + 8bac286 commit fe8a0a2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 53 deletions.
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

0 comments on commit fe8a0a2

Please sign in to comment.