Skip to content

Commit

Permalink
Merge pull request #54 from openplannerteam/feature/abort-query
Browse files Browse the repository at this point in the history
1. Abort event when query is impossible #53
  • Loading branch information
maximtmartin authored Dec 21, 2018
2 parents 5869088 + db68853 commit c7df6b3
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 70 deletions.
3 changes: 2 additions & 1 deletion src/Defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Units from "./util/Units";
export default class Defaults {
public static readonly defaultMinimumWalkingSpeed = 3;
public static readonly defaultMaximumWalkingSpeed = 6;
public static readonly defaultWalkingDuration = Units.fromSeconds(10 * 60);
public static readonly defaultMinimumTransferDuration = Units.fromSeconds(60);
public static readonly defaultMaximumTransferDuration = Units.fromHours(.4);
public static readonly defaultMaximumTransfers = 8;
public static readonly defaultMaximumTransfers = 4;
}
5 changes: 5 additions & 0 deletions src/EventType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
enum EventType {
Query = "query",
QueryExponential = "query-exponential",
QueryAbort = "query-abort",
LDFetchGet = "ldfetch-get",
ConnectionScan = "connection-scan",
FinalReachableStops = "final-reachable-stops",
InitialReachableStops = "initial-reachable-stops",
AddedNewTransferProfile = "added-new-transfer-profile",
}

export default EventType;
8 changes: 7 additions & 1 deletion src/Planner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ export default class Planner implements EventEmitter {
public async query(query: IQuery): Promise<AsyncIterator<IPath>> {
this.emit(EventType.Query, query);

return this.queryRunner.run(query);
const iterator = await this.queryRunner.run(query);

this.once(EventType.QueryAbort, () => {
iterator.close();
});

return iterator;
}

public addListener(type: string | symbol, listener: Listener): this {
Expand Down
23 changes: 16 additions & 7 deletions src/demo.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import EventType from "./EventType";
import Planner from "./index";
import IPath from "./interfaces/IPath";
import IStep from "./interfaces/IStep";
import Units from "./util/Units";

export default async (logResults) => {

const planner = new Planner();

if (logResults) {
let scannedPages = 0;
let scannedConnections = 0;

planner
.on(EventType.Query, (query) => {
console.log("Query", query);
})
.on(EventType.QueryExponential, (query) => {
const { minimumDepartureTime, maximumArrivalTime } = query;

console.log("Total scanned pages", scannedPages);
console.log("Total scanned connections", scannedConnections);
console.log("[Subquery]", minimumDepartureTime, maximumArrivalTime, maximumArrivalTime - minimumDepartureTime);
})
.on(EventType.LDFetchGet, (url, duration) => {
scannedPages++;
console.log(`[GET] ${url} (${duration}ms)`);
})
.on(EventType.ConnectionScan, (connection) => {
scannedConnections++;
});
}

Expand All @@ -31,10 +39,10 @@ export default async (logResults) => {
// to: "https://data.delijn.be/stops/502481", // Tielt Metaalconstructie Goossens
// from: "https://data.delijn.be/stops/509927", // Tield Rameplein perron 1
// to: "https://data.delijn.be/stops/200455", // Deinze weg op Grammene +456
from: "http://irail.be/stations/NMBS/008896008", // Kortrijk
from: "http://irail.be/stations/NMBS/008896925", // Ingelmunster
to: "http://irail.be/stations/NMBS/008892007", // Ghent-Sint-Pieters
minimumDepartureTime: new Date(),
maximumTransferDuration: Units.fromHours(.01),
maximumTransferDuration: Units.fromHours(0.5),
});

return new Promise((resolve, reject) => {
Expand All @@ -45,16 +53,17 @@ export default async (logResults) => {
++i;

if (logResults) {
console.log(++i);
path.steps.forEach((step: IStep) => {
console.log(JSON.stringify(step, null, " "));
});
console.log(i);
console.log(JSON.stringify(path, null, " "));
console.log("\n");
}

if (i === 3) {
resolve(true);
}
})
.on("end", () => {
resolve(false);
});
});
};
24 changes: 12 additions & 12 deletions src/fetcher/connections/ld-fetch/ConnectionsIteratorLazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,22 @@ export default class ConnectionsIteratorLazy extends BufferedIterator<IConnectio
}

private pushCurrentPage(): void {
const { connections } = this.currentPage;
const { connections } = this.currentPage;

if (this.config.backward) {
let c = connections.length - 1;
if (this.config.backward) {
let c = connections.length - 1;

while (c >= 0) {
this._push(connections[c]);
c--;
}
while (c >= 0) {
this._push(connections[c]);
c--;
}

// Forwards
} else {
for (const connection of connections) {
this._push(connection);
}
// Forwards
} else {
for (const connection of connections) {
this._push(connection);
}
}
}

}
4 changes: 3 additions & 1 deletion src/interfaces/IQuery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ILocation from "./ILocation";
import { DurationMs, SpeedkmH } from "./units";
import { DistanceM, DurationMs, SpeedkmH } from "./units";

export default interface IQuery {
from?: string | string[] | ILocation | ILocation[];
Expand All @@ -11,6 +11,8 @@ export default interface IQuery {
walkingSpeed?: SpeedkmH;
minimumWalkingSpeed?: SpeedkmH;
maximumWalkingSpeed?: SpeedkmH;
maximumWalkingDuration?: DurationMs;
maximumWalkingDistance?: DistanceM;
minimumTransferDuration?: DurationMs;
maximumTransferDuration?: DurationMs;
maximumTransfers?: number;
Expand Down
25 changes: 17 additions & 8 deletions src/planner/public-transport/CSA/util/ProfileUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import DropOffType from "../../../../fetcher/connections/DropOffType";
import IConnection from "../../../../fetcher/connections/IConnection";
import { DurationMs } from "../../../../interfaces/units";
import IArrivalTimeByTransfers from "../data-structure/IArrivalTimeByTransfers";
import IProfilesByStop from "../data-structure/stops/IProfilesByStop";

Expand All @@ -23,21 +25,28 @@ export default class ProfileUtil {
public static getTransferTimes(
profilesByStop: IProfilesByStop,
connection: IConnection,
maxLegs,
minimumTransferDuration,
maxLegs: number,
minimumTransferDuration: DurationMs,
maximumTransferDuration: DurationMs,
): IArrivalTimeByTransfers {
const { arrivalStop, arrivalTime } = connection;
const trip = connection["gtfs:trip"];

if (connection["gtfs:dropOffType"] !== "gtfs:NotAvailable") {
if (connection["gtfs:dropOffType"] !== DropOffType.NotAvailable) {

let i = profilesByStop[arrivalStop].length - 1;
while (i >= 0) {
if (profilesByStop[arrivalStop][i].departureTime >= arrivalTime.getTime() + minimumTransferDuration) {
const arrivalTimeByTransfers = profilesByStop[arrivalStop][i].getArrivalTimeByTransfers(trip);
let profileIndex = profilesByStop[arrivalStop].length - 1;
while (profileIndex >= 0) {

const departure: number = profilesByStop[arrivalStop][profileIndex].departureTime;
const arrival: number = arrivalTime.getTime();

const transferDuration = departure - arrival;

if (transferDuration >= minimumTransferDuration && transferDuration <= maximumTransferDuration) {
const arrivalTimeByTransfers = profilesByStop[arrivalStop][profileIndex].getArrivalTimeByTransfers(trip);
return arrivalTimeByTransfers.slice() as IArrivalTimeByTransfers;
}
i--;
profileIndex--;
}

}
Expand Down
Loading

0 comments on commit c7df6b3

Please sign in to comment.