Coder Social home page Coder Social logo

csa.js's Introduction

Connection Scan Algorithm for JavaScript

Build Status

State:

  • basic csa without footpaths is functional

  • timespan csa without footpaths is functional

  • merger that combines different streams of connections is functional

The Connection Scan Algorithm (CSA) for Javascript takes a stream of "connections" and transforms it into a stream of solutions to get from a certain stop to a certain stop. The algorithm will find the earliest arrival times first, and will return alternatives as long as the stream runs.

Use it

You can install the library using npm:

npm install csa

And include it in your nodejs application

var csa = require('csa');
var planner = new csa.BasicCSA({departureStop: "...", arrivalStop: "...",departureTime:new Date()});
connectionsReadStream.pipe(planner);
planner.on("result", function (result) {
    console.log("Path found:",result);
    connectionsReadStream.close();
});
planner.on("data", function (connection) {
    //Access to a minimum spanning tree of connections being built up
    //May be useful for e.g., creating isochrone maps
});

Multiple connection streams

Instead of using one stream of connections, you can combine multiple streams as input by using MergeStream

var connectionsStreams = [
	[ 'stream1', connectionsReadStream1 ],
	[ 'stream2', connectionsReadStream2 ],
	...
];

var connectionsReadStream = new csa.MergeStream(connectionsStreams, query.departureTime);

You can add streams by setting an eventlistener on the MergeStream instance. To remove a stream, just end the stream itself.

connectionsReadStream.on("data", function (connection) {
	connectionsReadStream.addConnectionsStream('newStream', newConnectionsReadStream);
});

Timespan CSA

An expansion on basic csa is timespan csa, this creates an array of minimum spanning trees to create every possible route from a depart to destination within a certain departure timespan. (starting at departureTime to latestDepartTime)

var csa = require('csa');
var planner = new csa.TimespanCSA({
	"departureStop": "...", 
	"arrivalStop": "...",
	"departureTime": new Date(),
	"latestDepartTime": new Date(new Date().valueOf() + 720000),
	"minimumTransferTime": 6 * 60
});
connectionsReadStream.pipe(planner);
planner.on("result", function (result) {
	// This gets emitted for every found route
    console.log("Path found:",result);
    connectionsReadStream.close();
});
planner.on("data", function (connection) {
    // Access to a minimum spanning tree of connections being built up
    // May be useful for e.g., creating isochrone maps
});

Transfer times (optionally)

To specify the minimum transfer time you should add the ''minimumTransferTime'' argument to the query:

var query = {
	"arrivalStop":"..." , 
	"departureStop": "...", 
	"departureTime": "...",
	"minimumTransferTime": ...
}
var planner = new csa.BasicCSA(query);

To specify transfer times depending on the stops of the transfers you should create a transfer time fetcher:

//Define a class
var transferTimesFetcher = function() {};
//Define the get function
transferTimesFetcher.get = function (previousConnection, connection) {
	return new Promise(function (fulfill) {
		var transferTime = ...
		fulfill(transferTime)
        });
};
var planner = new csa.BasicCSA(query,transferTimesFetcher);

Protip: you can use browserify on this repo to use CSA in the browser

csa.js's People

Contributors

brechtvdv avatar sballieu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.