Small refactoring and cleanup
This commit is contained in:
parent
c4cd9d1806
commit
5821db3b55
4 changed files with 44 additions and 66 deletions
|
@ -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}"`;
|
||||
|
||||
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
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
|
|
@ -2,41 +2,33 @@
|
|||
* 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) {
|
||||
interpret(definitionFile, tags) {
|
||||
if (typeof tags === "string") {
|
||||
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));
|
||||
}
|
||||
const rawData = fs.readFileSync(definitionFile);
|
||||
const program = JSON.parse(rawData);
|
||||
return new RuleSet(program).runProgram(tags)
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -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": {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue