-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
1,842 additions
and
521 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,88 @@ | ||
var util = require('util'); | ||
|
||
module.exports = function () { | ||
this.When(/^I request a travel time matrix I should get$/, (table, callback) => { | ||
var NO_ROUTE = 2147483647; // MAX_INT | ||
const durationsRegex = new RegExp(/^I request a travel time matrix I should get$/); | ||
const distancesRegex = new RegExp(/^I request a travel distance matrix I should get$/); | ||
|
||
var tableRows = table.raw(); | ||
const DURATIONS_NO_ROUTE = 2147483647; // MAX_INT | ||
const DISTANCES_NO_ROUTE = 3.40282e+38; // MAX_FLOAT | ||
|
||
if (tableRows[0][0] !== '') throw new Error('*** Top-left cell of matrix table must be empty'); | ||
this.When(durationsRegex, function(table, callback) {tableParse.call(this, table, DURATIONS_NO_ROUTE, 'durations', callback);}.bind(this)); | ||
this.When(distancesRegex, function(table, callback) {tableParse.call(this, table, DISTANCES_NO_ROUTE, 'distances', callback);}.bind(this)); | ||
}; | ||
|
||
var waypoints = [], | ||
columnHeaders = tableRows[0].slice(1), | ||
rowHeaders = tableRows.map((h) => h[0]).slice(1), | ||
symmetric = columnHeaders.length == rowHeaders.length && columnHeaders.every((ele, i) => ele === rowHeaders[i]); | ||
const durationsParse = function(v) { return isNaN(parseInt(v)); }; | ||
const distancesParse = function(v) { return isNaN(parseFloat(v)); }; | ||
|
||
if (symmetric) { | ||
columnHeaders.forEach((nodeName) => { | ||
var node = this.findNodeByName(nodeName); | ||
if (!node) throw new Error(util.format('*** unknown node "%s"', nodeName)); | ||
waypoints.push({ coord: node, type: 'loc' }); | ||
}); | ||
} else { | ||
columnHeaders.forEach((nodeName) => { | ||
var node = this.findNodeByName(nodeName); | ||
if (!node) throw new Error(util.format('*** unknown node "%s"', nodeName)); | ||
waypoints.push({ coord: node, type: 'dst' }); | ||
}); | ||
rowHeaders.forEach((nodeName) => { | ||
var node = this.findNodeByName(nodeName); | ||
if (!node) throw new Error(util.format('*** unknown node "%s"', nodeName)); | ||
waypoints.push({ coord: node, type: 'src' }); | ||
function tableParse(table, noRoute, annotation, callback) { | ||
|
||
const parse = annotation == 'distances' ? distancesParse : durationsParse; | ||
const params = this.queryParams; | ||
params.annotations = annotation == 'distances' ? 'distance' : 'duration'; | ||
|
||
var tableRows = table.raw(); | ||
|
||
if (tableRows[0][0] !== '') throw new Error('*** Top-left cell of matrix table must be empty'); | ||
|
||
var waypoints = [], | ||
columnHeaders = tableRows[0].slice(1), | ||
rowHeaders = tableRows.map((h) => h[0]).slice(1), | ||
symmetric = columnHeaders.length == rowHeaders.length && columnHeaders.every((ele, i) => ele === rowHeaders[i]); | ||
|
||
if (symmetric) { | ||
columnHeaders.forEach((nodeName) => { | ||
var node = this.findNodeByName(nodeName); | ||
if (!node) throw new Error(util.format('*** unknown node "%s"', nodeName)); | ||
waypoints.push({ coord: node, type: 'loc' }); | ||
}); | ||
} else { | ||
columnHeaders.forEach((nodeName) => { | ||
var node = this.findNodeByName(nodeName); | ||
if (!node) throw new Error(util.format('*** unknown node "%s"', nodeName)); | ||
waypoints.push({ coord: node, type: 'dst' }); | ||
}); | ||
rowHeaders.forEach((nodeName) => { | ||
var node = this.findNodeByName(nodeName); | ||
if (!node) throw new Error(util.format('*** unknown node "%s"', nodeName)); | ||
waypoints.push({ coord: node, type: 'src' }); | ||
}); | ||
} | ||
|
||
var actual = []; | ||
actual.push(table.headers); | ||
|
||
this.reprocessAndLoadData((e) => { | ||
if (e) return callback(e); | ||
// compute matrix | ||
|
||
this.requestTable(waypoints, params, (err, response) => { | ||
if (err) return callback(err); | ||
if (!response.body.length) return callback(new Error('Invalid response body')); | ||
|
||
var json = JSON.parse(response.body); | ||
|
||
var result = json[annotation].map(row => { | ||
var hashes = {}; | ||
row.forEach((v, i) => { hashes[tableRows[0][i+1]] = parse(v) ? '' : v; }); | ||
return hashes; | ||
}); | ||
} | ||
|
||
var actual = []; | ||
actual.push(table.headers); | ||
|
||
this.reprocessAndLoadData((e) => { | ||
if (e) return callback(e); | ||
// compute matrix | ||
var params = this.queryParams; | ||
|
||
this.requestTable(waypoints, params, (err, response) => { | ||
if (err) return callback(err); | ||
if (!response.body.length) return callback(new Error('Invalid response body')); | ||
|
||
var json = JSON.parse(response.body); | ||
|
||
var result = json['durations'].map(row => { | ||
var hashes = {}; | ||
row.forEach((v, i) => { hashes[tableRows[0][i+1]] = isNaN(parseInt(v)) ? '' : v; }); | ||
return hashes; | ||
}); | ||
|
||
var testRow = (row, ri, cb) => { | ||
for (var k in result[ri]) { | ||
if (this.FuzzyMatch.match(result[ri][k], row[k])) { | ||
result[ri][k] = row[k]; | ||
} else if (row[k] === '' && result[ri][k] === NO_ROUTE) { | ||
result[ri][k] = ''; | ||
} else { | ||
result[ri][k] = result[ri][k].toString(); | ||
} | ||
|
||
var testRow = (row, ri, cb) => { | ||
for (var k in result[ri]) { | ||
if (this.FuzzyMatch.match(result[ri][k], row[k])) { // for floating points, we should do an epsilon check which is find the difference and check > or < 0.000001 -- do this in fuzzy match | ||
result[ri][k] = row[k]; | ||
} else if (row[k] === '' && result[ri][k] === noRoute) { | ||
result[ri][k] = ''; | ||
} else { | ||
result[ri][k] = result[ri][k].toString(); | ||
} | ||
} | ||
|
||
result[ri][''] = row['']; | ||
cb(null, result[ri]); | ||
}; | ||
result[ri][''] = row['']; | ||
cb(null, result[ri]); | ||
}; | ||
|
||
this.processRowsAndDiff(table, testRow, callback); | ||
}); | ||
this.processRowsAndDiff(table, testRow, callback); | ||
}); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.