From 9c31a92abd4005d654199d8408955870164443d6 Mon Sep 17 00:00:00 2001 From: pgm-chardelv1 Date: Mon, 19 Jul 2021 16:12:00 +0200 Subject: [PATCH] Adds first version for min & max | Needs review --- javascript/RuleSet.js | 55 ++++++++++++++++++++++++++++++ javascript/interpret.js | 75 +++++++++++++++++++++++++---------------- 2 files changed, 101 insertions(+), 29 deletions(-) diff --git a/javascript/RuleSet.js b/javascript/RuleSet.js index 367b33c..09b8a63 100644 --- a/javascript/RuleSet.js +++ b/javascript/RuleSet.js @@ -1,3 +1,4 @@ + /** * RuleSet Class * Constructor @@ -30,6 +31,7 @@ class RuleSet { const [ [program, keys], values ] = Object.entries(initValues); + console.log(program) if (program === '$multiply') { this.scoreValues = keys; @@ -41,6 +43,16 @@ class RuleSet { this.order = keys; const match = this.getFirstMatchScore(tags); return `"${this.name.slice(8)}":"${match}"`; + + } else if (program === '$min') { + this.scoreValues = keys; + const minVal = this.getMinValue(tags); + return `"${this.name.slice(8)}":"${minVal}"`; + + } else if (program === '$max') { + this.scoreValues = keys; + const maxVal = this.getMaxValue(tags); + return `"${this.name.slice(8)}":"${maxVal}"`; } else { console.error(`Error: Program ${program} is not implemented yet. ${JSON.stringify(keys)}`); @@ -74,8 +86,10 @@ class RuleSet { getFirstMatchScore (tags) { let matchFound = false; let match = ""; + let i = 0; for (let key of this.order) { + i++; for (let entry of Object.entries(tags)) { const [tagKey, tagValue] = entry; @@ -111,6 +125,47 @@ class RuleSet { return null; } + getMinValue (tags) { + const minArr = this.scoreValues.map(part => { + if (typeof(part) === 'object') { + return this.getMin(part, tags) + } else { + return parseInt(part); + } + }); + let absMin = Math.min(...minArr); + return absMin; + } + + getMin (part, tags) { + let min; + const [ group ] = Object.entries(part); + const [,compareVals] = group; + const minArr = Object.values(compareVals).map(v => parseInt(v)); + min = Math.min(...minArr); + return min; + } + + getMaxValue (tags) { + const maxArr = this.scoreValues.map(part => { + if (typeof(part) === 'object') { + return this.getMax(part, tags) + } else { + return parseInt(part); + } + }); + let absMax = Math.max(...maxArr); + return absMax; + } + + getMax (part, tags) { + let max; + const [ group ] = Object.entries(part); + const [,compareVals] = group; + const maxArr = Object.values(compareVals).map(v => parseInt(v)); + max = Math.max(...maxArr); + return max; + } } diff --git a/javascript/interpret.js b/javascript/interpret.js index 53a564d..32de744 100644 --- a/javascript/interpret.js +++ b/javascript/interpret.js @@ -9,12 +9,12 @@ import RuleSet from './RuleSet.js'; * Example tags */ const tags1 = { - "highway": "residential", // 1 + "highway": "residential", // 1 // Expect "yes" "surface": "paved", // 0.99 } const tags2 = { - "bicycle": "yes", + "bicycle": "yes", // Expect "yes" "cycleway": "lane", "highway": "secondary", "maxspeed": "50", @@ -22,68 +22,73 @@ const tags2 = { const tags3 = { "cyclestreet": "yes", - "highway": "residential", + "highway": "residential", // Expect "yes" "maxspeed": "30", "surface": "asphalt" } const tags4 = { - "highway": "track", + "highway": "track", // Expect "yes" "surface": "asphalt", "incline": "10%" } const tags5 = { - "access": "no", + "access": "no", // Expect "no" "bicycle": "official", "area": "yes" } +const tags6 = { + "surface":"dirt", + "highway":"track", + /* + "surface":"paved", + "highway":"path" + */ +} + /** * Interpret JsonFile w/ Tags * @param definitionFile {any} JSON input defining the score distribution * @param tags {any} OSM tags as key/value pairs */ const interpret = (definitionFile, tags) => { - const rawData = fs.readFileSync(definitionFile); - const ruleSet = JSON.parse(rawData); - - const { name, $default, $multiply, value} = ruleSet; + const { name, $default, $multiply, $min, value} = ruleSet; if (!!value) { const currentSet = new RuleSet(name, $default, value); console.log(currentSet.runProgram(tags)); - } else { + } else if (!!$multiply) { const currentSet = new RuleSet(name, $default, {$multiply}); - // currentSet.toString(); + console.log(currentSet.runProgram(tags)); + } else if (!!$min) { + const currentSet = new RuleSet(name, $default, {$min}); console.log(currentSet.runProgram(tags)); } }; /* console.info('Comfort') -interpret("../Examples/bicycle/aspects/bicycle.comfort.json", tags1); -interpret("../Examples/bicycle/aspects/bicycle.comfort.json", tags2); -interpret("../Examples/bicycle/aspects/bicycle.comfort.json", tags3); -interpret("../Examples/bicycle/aspects/bicycle.comfort.json", tags4); +interpret("./.routeExamples/bicycle.comfort.json", tags1); +interpret("./.routeExamples/bicycle.comfort.json", tags2); +interpret("./.routeExamples/bicycle.comfort.json", tags3); +interpret("./.routeExamples/bicycle.comfort.json", tags4); console.log('*******************') console.info('Safety') -interpret("../Examples/bicycle/aspects/bicycle.safety.json", tags1); -interpret("../Examples/bicycle/aspects/bicycle.safety.json", tags2); -interpret("../Examples/bicycle/aspects/bicycle.safety.json", tags3); -interpret("../Examples/bicycle/aspects/bicycle.safety.json", tags4); +interpret("./.routeExamples/bicycle.safety.json", tags1); +interpret("./.routeExamples/bicycle.safety.json", tags2); +interpret("./.routeExamples/bicycle.safety.json", tags3); +interpret("./.routeExamples/bicycle.safety.json", tags4); console.log('*******************') */ -console.info('Legal Access') -interpret("../Examples/bicycle/aspects/bicycle.legal_access.json", tags1); -interpret("../Examples/bicycle/aspects/bicycle.legal_access.json", tags2); -interpret("../Examples/bicycle/aspects/bicycle.legal_access.json", tags3); -interpret("../Examples/bicycle/aspects/bicycle.legal_access.json", tags4); -interpret("../Examples/bicycle/aspects/bicycle.legal_access.json", tags5); - -// !TODO: Add default value = "no" as a fallback. Fix logic - -console.log('*******************') +/* console.info('Legal Access') +interpret("./.routeExamples/bicycle.legal_access.json", tags1); +interpret("./.routeExamples/bicycle.legal_access.json", tags2); +interpret("./.routeExamples/bicycle.legal_access.json", tags3); +interpret("./.routeExamples/bicycle.legal_access.json", tags4); +interpret("./.routeExamples/bicycle.legal_access.json", tags5); */ +// console.log('*******************') /* console.info('Speed Factor') interpret("./.routeExamples/bicycle.speed_factor.json", tags1); @@ -92,4 +97,16 @@ interpret("./.routeExamples/bicycle.speed_factor.json", tags3); interpret("./.routeExamples/bicycle.speed_factor.json", tags4); console.log('*******************') */ +console.info('Test min') +/* interpret("./.routeExamples/bicycle.min_test.json", tags1); +interpret("./.routeExamples/bicycle.min_test.json", tags2); +interpret("./.routeExamples/bicycle.min_test.json", tags3); +interpret("./.routeExamples/bicycle.min_test.json", tags4); */ +// interpret("./.routeExamples/bicycle.min_test.json", tags6); +const rawData = fs.readFileSync("./.routeExamples/bicycle.min_test.json"); +const ruleSet = JSON.parse(rawData); +interpret(ruleSet, tags6); + +console.log('*******************') + export default interpret;