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
|
* @param values {object} Main data object
|
||||||
*/
|
*/
|
||||||
class RuleSet {
|
class RuleSet {
|
||||||
constructor(name, defaultValue = 1, values) {
|
constructor(config) {
|
||||||
this.name = name;
|
delete config["name"]
|
||||||
this.defaultValue = defaultValue;
|
delete config["unit"]
|
||||||
this.values = values;
|
delete config["description"]
|
||||||
this.score = this.defaultValue;
|
this.program = config
|
||||||
this.order = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
* getScore calculates a score for the RuleSet
|
||||||
* @param tags {object} Active tags to compare against
|
* @param tags {object} Active tags to compare against
|
||||||
*/
|
*/
|
||||||
runProgram(tags, program = this.values) {
|
runProgram(tags, program = this.program) {
|
||||||
console.log("Running program", program)
|
if (typeof program !== "object") {
|
||||||
if(typeof program !== "object"){
|
|
||||||
return program;
|
return program;
|
||||||
}
|
}
|
||||||
|
|
||||||
let functionName /*: string*/ = undefined;
|
let functionName /*: string*/ = undefined;
|
||||||
let functionArguments /*: any */= undefined
|
let functionArguments /*: any */ = undefined
|
||||||
let otherValues = {}
|
let otherValues = {}
|
||||||
Object.entries(program).forEach(
|
Object.entries(program).forEach(
|
||||||
entry => {
|
entry => {
|
||||||
|
@ -41,35 +32,27 @@ class RuleSet {
|
||||||
if (key.startsWith("$")) {
|
if (key.startsWith("$")) {
|
||||||
functionName = key
|
functionName = key
|
||||||
functionArguments = value
|
functionArguments = value
|
||||||
}else{
|
} else {
|
||||||
otherValues[key] = value
|
otherValues[key] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
if(functionName === undefined){
|
if (functionName === undefined) {
|
||||||
return this.interpretAsDictionary(program, tags)
|
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') {
|
} else if (functionName === '$firstMatchOf') {
|
||||||
this.order = keys;
|
this.order = keys;
|
||||||
const match = this.getFirstMatchScore(tags);
|
return this.getFirstMatchScore(tags);
|
||||||
return `"${this.name.slice(8)}":"${match}"`;
|
|
||||||
|
|
||||||
} else if (functionName === '$min') {
|
} else if (functionName === '$min') {
|
||||||
const minVal = this.getMinValue(tags, functionArguments);
|
return this.getMinValue(tags, functionArguments);
|
||||||
return `"${this.name.slice(8)}":"${minVal}"`;
|
|
||||||
|
|
||||||
} else if (functionName === '$max') {
|
} else if (functionName === '$max') {
|
||||||
const maxVal = this.getMaxValue(tags, functionArguments);
|
return this.getMaxValue(tags, functionArguments);
|
||||||
return `"${this.name.slice(8)}":"${maxVal}"`;
|
} else if (functionName === '$default') {
|
||||||
|
return this.defaultV(functionArguments, otherValues, tags)
|
||||||
} else {
|
} else {
|
||||||
console.error(`Error: Program ${functionName} is not implemented yet. ${JSON.stringify(program)}`);
|
console.error(`Error: Program ${functionName} is not implemented yet. ${JSON.stringify(program)}`);
|
||||||
}
|
}
|
||||||
|
@ -109,13 +92,22 @@ class RuleSet {
|
||||||
if (propertyValue === undefined) {
|
if (propertyValue === undefined) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
if(typeof propertyValue !== "object"){
|
if (typeof propertyValue !== "object") {
|
||||||
return propertyValue
|
return propertyValue
|
||||||
}
|
}
|
||||||
return propertyValue[value]
|
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
|
* Multiplies the default score with the proper values
|
||||||
* @param tags {object} the active tags to check against
|
* @param tags {object} the active tags to check against
|
||||||
|
|
|
@ -1,13 +1,7 @@
|
||||||
import interpret from './interpret.js';
|
import interpret from './interpret.js';
|
||||||
import RuleSet from './RuleSet.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 {
|
export {
|
||||||
interpret,
|
interpret,
|
||||||
RuleSet,
|
RuleSet
|
||||||
printMsg,
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,42 +2,34 @@
|
||||||
* Import packages
|
* Import packages
|
||||||
*/
|
*/
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { argv } from 'process';
|
import {argv} from 'process';
|
||||||
import RuleSet from './RuleSet.js';
|
import RuleSet from './RuleSet.js';
|
||||||
|
|
||||||
const app = {
|
const app = {
|
||||||
init () {
|
init() {
|
||||||
if (!!argv && argv.length < 4) {
|
if (!!argv && argv.length < 4) {
|
||||||
console.info(`Invalid command. In order to run the JavaScript interpreter please use the following format:
|
console.info(`Invalid command. In order to run the JavaScript interpreter please use the following format:
|
||||||
> node index.js [ruleset JSON file] [tags]`)
|
> node index.js [ruleset JSON file] [tags]`)
|
||||||
} else if (!!argv && argv.length === 4) {
|
} else if (!!argv && argv.length === 4) {
|
||||||
const definitionFile = argv[2];
|
const definitionFile = argv[2];
|
||||||
const tags = argv[3];
|
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 definitionFile {any} JSON input defining the score distribution
|
||||||
* @param tags {any} OSM tags as key/value pairs
|
* @param tags {any} OSM tags as key/value pairs
|
||||||
*/
|
*/
|
||||||
interpret (definitionFile, tags) {
|
interpret(definitionFile, tags) {
|
||||||
tags = JSON.parse(tags)
|
if (typeof tags === "string") {
|
||||||
const rawData = fs.readFileSync(definitionFile);
|
tags = JSON.parse(tags)
|
||||||
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)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
app.init();
|
app.init();
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "mapcomplete-stressmap",
|
"name": "aspected-routing",
|
||||||
"version": "1.0.0",
|
"version": "0.2.0",
|
||||||
"description": "Calculate bicycle route stress score to display on MapComplete/OSM.",
|
"description": "Calculates a score based on a .Json-file which contains an Aspected-Routing function. This is a (partial) javascript port",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue