From 5821db3b55db7433b0b1450b4a4b11fa94502984 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Mon, 26 Jul 2021 20:11:02 +0200 Subject: [PATCH] Small refactoring and cleanup --- javascript/RuleSet.js | 64 ++++++++++++++++++----------------------- javascript/index.js | 8 +----- javascript/interpret.js | 32 ++++++++------------- javascript/package.json | 6 ++-- 4 files changed, 44 insertions(+), 66 deletions(-) diff --git a/javascript/RuleSet.js b/javascript/RuleSet.js index 04aebbc..d50fdd2 100644 --- a/javascript/RuleSet.js +++ b/javascript/RuleSet.js @@ -6,34 +6,25 @@ * @param values {object} Main data object */ class RuleSet { - constructor(name, defaultValue = 1, values) { - this.name = name; - this.defaultValue = defaultValue; - this.values = values; - this.score = this.defaultValue; - this.order = null; + constructor(config) { + delete config["name"] + delete config["unit"] + delete config["description"] + this.program = config } - /** - * toString - * Returns constructor values in string for display in the console - */ - toString() { - return `${this.name} | ${this.defaultValue} | ${this.values}`; - } /** * getScore calculates a score for the RuleSet * @param tags {object} Active tags to compare against */ - runProgram(tags, program = this.values) { - console.log("Running program", program) - if(typeof program !== "object"){ + runProgram(tags, program = this.program) { + if (typeof program !== "object") { return program; } - + let functionName /*: string*/ = undefined; - let functionArguments /*: any */= undefined + let functionArguments /*: any */ = undefined let otherValues = {} Object.entries(program).forEach( entry => { @@ -41,35 +32,27 @@ class RuleSet { if (key.startsWith("$")) { functionName = key functionArguments = value - }else{ + } else { otherValues[key] = value } } ) - if(functionName === undefined){ + if (functionName === undefined) { return this.interpretAsDictionary(program, tags) } - - - console.log(program) - if (functionName === '$multiply') { - this.score = this.multiplyScore(tags, functionArguments); - return `"${this.name.slice(8)}":"${this.score}"`; + if (functionName === '$multiply') { + return this.multiplyScore(tags, functionArguments); } else if (functionName === '$firstMatchOf') { this.order = keys; - const match = this.getFirstMatchScore(tags); - return `"${this.name.slice(8)}":"${match}"`; - + return this.getFirstMatchScore(tags); } else if (functionName === '$min') { - const minVal = this.getMinValue(tags, functionArguments); - return `"${this.name.slice(8)}":"${minVal}"`; - + return this.getMinValue(tags, functionArguments); } else if (functionName === '$max') { - const maxVal = this.getMaxValue(tags, functionArguments); - return `"${this.name.slice(8)}":"${maxVal}"`; - + return this.getMaxValue(tags, functionArguments); + } else if (functionName === '$default') { + return this.defaultV(functionArguments, otherValues, tags) } else { console.error(`Error: Program ${functionName} is not implemented yet. ${JSON.stringify(program)}`); } @@ -109,13 +92,22 @@ class RuleSet { if (propertyValue === undefined) { return undefined } - if(typeof propertyValue !== "object"){ + if (typeof propertyValue !== "object") { return propertyValue } return propertyValue[value] }); } + defaultV(subProgram, otherArgs, tags) { + const normalProgram = Object.entries(otherArgs)[0][1] + const value = this.runProgram(tags, normalProgram) + if (value !== undefined) { + return value; + } + return this.runProgram(tags, subProgram) + } + /** * Multiplies the default score with the proper values * @param tags {object} the active tags to check against diff --git a/javascript/index.js b/javascript/index.js index 6e977b2..6574b4a 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1,13 +1,7 @@ import interpret from './interpret.js'; import RuleSet from './RuleSet.js'; -const printMsg = () => { - console.info("BE-StressMap has been installed successfully!") - console.info("View the README file for instructors on how to use this module.") -} - export { interpret, - RuleSet, - printMsg, + RuleSet }; diff --git a/javascript/interpret.js b/javascript/interpret.js index d17e854..9319b86 100644 --- a/javascript/interpret.js +++ b/javascript/interpret.js @@ -2,42 +2,34 @@ * Import packages */ import fs from 'fs'; -import { argv } from 'process'; +import {argv} from 'process'; import RuleSet from './RuleSet.js'; const app = { - init () { + init() { if (!!argv && argv.length < 4) { console.info(`Invalid command. In order to run the JavaScript interpreter please use the following format: > node index.js [ruleset JSON file] [tags]`) } else if (!!argv && argv.length === 4) { const definitionFile = argv[2]; const tags = argv[3]; - this.interpret(definitionFile, tags); + const result = this.interpret(definitionFile, tags); + console.log(result) } }, /** - * Interpret JsonFile w/ Tags + * Interpret JsonFile and apply it as a tag. To use with CLI only * @param definitionFile {any} JSON input defining the score distribution * @param tags {any} OSM tags as key/value pairs */ - interpret (definitionFile, tags) { - tags = JSON.parse(tags) - const rawData = fs.readFileSync(definitionFile); - const ruleSet = JSON.parse(rawData); - const { name, $default, $multiply, $min, value} = ruleSet; - - if (!!value) { - const currentSet = new RuleSet(name, $default, value); - console.log(currentSet.runProgram(tags)); - } else if (!!$multiply) { - const currentSet = new RuleSet(name, $default, {$multiply}); - console.log(currentSet.runProgram(tags)); - } else if (!!$min) { - const currentSet = new RuleSet(name, $default, {$min}); - console.log(currentSet.runProgram(tags)); + interpret(definitionFile, tags) { + if (typeof tags === "string") { + tags = JSON.parse(tags) } - } + const rawData = fs.readFileSync(definitionFile); + const program = JSON.parse(rawData); + return new RuleSet(program).runProgram(tags) + } }; app.init(); diff --git a/javascript/package.json b/javascript/package.json index 380a004..67004b0 100644 --- a/javascript/package.json +++ b/javascript/package.json @@ -1,7 +1,7 @@ { - "name": "mapcomplete-stressmap", - "version": "1.0.0", - "description": "Calculate bicycle route stress score to display on MapComplete/OSM.", + "name": "aspected-routing", + "version": "0.2.0", + "description": "Calculates a score based on a .Json-file which contains an Aspected-Routing function. This is a (partial) javascript port", "main": "index.js", "type": "module", "scripts": {