From b85b2ccc25755f981e8a2b4dea77ed8ce416ff65 Mon Sep 17 00:00:00 2001 From: kohlros Date: Tue, 5 Sep 2023 14:57:29 +0200 Subject: [PATCH] Fix/improve normal dist block (#238) * encapsulate normal sample in its own function * add normal dist block to download --- static/scripts/modules/export.js | 10 +++++++++ static/scripts/normalBlockBehaviour.js | 28 +++++++++++++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/static/scripts/modules/export.js b/static/scripts/modules/export.js index 629cb20..f5acafa 100644 --- a/static/scripts/modules/export.js +++ b/static/scripts/modules/export.js @@ -36,6 +36,7 @@ async function downloadWorkspaceAsJS() { let jszip = await readFile("./scripts/jszip.js"); let fileutils = await prepareFileUtils(); let plotHandler = await preparePlotting(); + let gaussian = await prepareGaussian(); // Check if everything worked out if ( ![ @@ -63,6 +64,7 @@ async function downloadWorkspaceAsJS() { zip.file("PlotHandler.mjs", plotHandler); zip.folder("modules"); zip.file("modules/fileUtils.mjs", fileutils); + zip.file("gaussian.js", gaussian); let zipFile = await zip.generateAsync({ type: "blob" }); saveFile(zipFile, "elea.zip"); } @@ -102,6 +104,7 @@ function prepareAlgorithm() { `const {parentPort, Worker} = require("worker_threads");\n` + `const {Handler, consolelog, saveInCSV, plot, consoleerror, Message, RecvRequest} = require("./MessageHandler.js");\n` + `const {cpus} = require("os");\n` + + `const {gaussian} = require("./gaussian.js");\n` + `Handler.setParentPort(parentPort);\n`; let js = getCode(); @@ -162,6 +165,13 @@ async function preparePlotting() { return code; } +async function prepareGaussian() { + let file, code; + if (!(file = await readFile("./scripts/gaussian.js"))) return false; + code = file + `module.exports = {\n` + `gaussian \n` + `};`; + return code; +} + function removeFirstLine(file) { let lines = file.split("\n"); lines.shift(); diff --git a/static/scripts/normalBlockBehaviour.js b/static/scripts/normalBlockBehaviour.js index c02310e..317b362 100644 --- a/static/scripts/normalBlockBehaviour.js +++ b/static/scripts/normalBlockBehaviour.js @@ -595,21 +595,31 @@ Blockly.JavaScript["sample_normal_positive"] = function (block) { let variableMean = Blockly.JavaScript.valueToCode( block, "mean", - Blockly.JavaScript.ORDER_ATOMIC, + Blockly.JavaScript.ORDER_ATOMIC ); let variableVariance = Blockly.JavaScript.valueToCode( block, "variance", - Blockly.JavaScript.ORDER_ATOMIC, + Blockly.JavaScript.ORDER_ATOMIC ); - let code = `(function() { - const distribution = gaussian(${variableMean}, ${variableVariance}); - let sample = 0; - while (sample <= 0) sample = Math.round(distribution.ppf(Math.random())); - return sample; -})()`; - return [code, Blockly.JavaScript.ORDER_NONE]; + var functionName = Blockly.JavaScript.provideFunction_( + "sampleNormalPositive", + [ + "function " + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + "() {", + " const distribution = gaussian(" + + variableMean + + "," + + variableVariance + + ");", + " let sample = 0;", + " while (sample <= 0) sample = Math.round(distribution.ppf(Math.random()));", + " return sample;", + "}", + ] + ); + var code = functionName + "()"; + return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; Blockly.JavaScript["ea_debug_all"] = function () {