Skip to content

Commit

Permalink
Merge pull request #61 from openplannerteam/revert-59-feature/error-c…
Browse files Browse the repository at this point in the history
…lasses

Revert "Error classes" on master
  • Loading branch information
SimonVanneste authored Jan 10, 2019
2 parents ef371d1 + 1e068f9 commit 29dfd6a
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 176 deletions.
5 changes: 0 additions & 5 deletions src/Context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @ts-ignore
import { EventEmitter, Listener } from "events";
import { Container, injectable } from "inversify";
import EventType from "./EventType";

/**
* The Context serves as event pass through and holder of the inversify container object.
Expand Down Expand Up @@ -36,10 +35,6 @@ export default class Context implements EventEmitter {
return this.emitter.emit(type, ...args);
}

public emitWarning(...args: any[]): boolean {
return this.emit(EventType.Warning, ...args);
}

public listenerCount(type: string | symbol): number {
return this.emitter.listenerCount(type);
}
Expand Down
9 changes: 2 additions & 7 deletions src/EventType.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
enum EventType {
Query = "query",
QueryExponential = "query-exponential",
AbortQuery = "abort-query",
InvalidQuery = "invalid-query",

QueryAbort = "query-abort",
LDFetchGet = "ldfetch-get",

Warning = "warning",

ConnectionScan = "connection-scan",
InitialReachableStops = "initial-reachable-stops",
FinalReachableStops = "final-reachable-stops",
InitialReachableStops = "initial-reachable-stops",
AddedNewTransferProfile = "added-new-transfer-profile",
}

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

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

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

return iterator;
} catch (e) {
if (e.eventType) {
this.context.emit(e.eventType, e.message);
}

return Promise.reject();
}
return iterator;
}

public addListener(type: string | symbol, listener: Listener): this {
Expand Down
74 changes: 29 additions & 45 deletions src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ export default async (logResults) => {
let scannedConnections = 0;

planner
.on(EventType.InvalidQuery, (error) => {
console.log("InvalidQuery", error);
})
.on(EventType.AbortQuery, (reason) => {
console.log("AbortQuery", reason);
})
.on(EventType.Query, () => {
console.log("Query");
.on(EventType.Query, (query) => {
console.log("Query", query);
})
.on(EventType.QueryExponential, (query) => {
const { minimumDepartureTime, maximumArrivalTime } = query;
Expand All @@ -34,52 +28,42 @@ export default async (logResults) => {
})
.on(EventType.ConnectionScan, (connection) => {
scannedConnections++;
})
.on(EventType.Warning, (e) => {
console.warn(e);
});
}

return new Promise((resolve, reject) => {
planner.query({
publicTransportOnly: true,
// from: "https://data.delijn.be/stops/201657",
// to: "https://data.delijn.be/stops/205910",
// from: "https://data.delijn.be/stops/200455", // Deinze weg op Grammene +456
// 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/008896925", // Ingelmunster
to: "http://irail.be/stations/NMBS/008892007", // Ghent-Sint-Pieters
minimumDepartureTime: new Date("2019-01-10T08:13:20.530Z"),
maximumTransferDuration: Units.fromHours(0.5),
})
.then((publicTransportResult) => {

let i = 0;
const publicTransportResult = await planner.query({
publicTransportOnly: true,
// from: "https://data.delijn.be/stops/201657",
// to: "https://data.delijn.be/stops/205910",
// from: "https://data.delijn.be/stops/200455", // Deinze weg op Grammene +456
// 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/008896925", // Ingelmunster
to: "http://irail.be/stations/NMBS/008892007", // Ghent-Sint-Pieters
minimumDepartureTime: new Date(),
maximumTransferDuration: Units.fromHours(0.5),
});

publicTransportResult.take(3)
.on("data", (path: IPath) => {
++i;
return new Promise((resolve, reject) => {
let i = 0;

if (logResults) {
console.log(i);
console.log(JSON.stringify(path, null, " "));
console.log("\n");
}
publicTransportResult.take(3)
.on("data", (path: IPath) => {
++i;

if (i === 3) {
resolve(true);
}
})
.on("end", () => {
resolve(false);
});
if (logResults) {
console.log(i);
console.log(JSON.stringify(path, null, " "));
console.log("\n");
}

if (i === 3) {
resolve(true);
}
})
.catch(() => {
.on("end", () => {
resolve(false);
});
});

};
5 changes: 0 additions & 5 deletions src/errors/InvalidQueryError.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/errors/LocationResolverError.ts

This file was deleted.

20 changes: 7 additions & 13 deletions src/planner/public-transport/JourneyExtractorDefault.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ArrayIterator, AsyncIterator } from "asynciterator";
import { inject, injectable } from "inversify";
import Context from "../../Context";
import IConnection from "../../fetcher/connections/IConnection";
import ILocation from "../../interfaces/ILocation";
import IPath from "../../interfaces/IPath";
Expand Down Expand Up @@ -29,14 +28,9 @@ export default class JourneyExtractorDefault implements IJourneyExtractor {
private readonly locationResolver: ILocationResolver;

private bestArrivalTime: number[][] = [];
private context: Context;

constructor(
@inject(TYPES.LocationResolver) locationResolver: ILocationResolver,
@inject(TYPES.Context)context?: Context,
) {
constructor(@inject(TYPES.LocationResolver) locationResolver: ILocationResolver) {
this.locationResolver = locationResolver;
this.context = context;
}

public async extractJourneys(
Expand Down Expand Up @@ -78,7 +72,7 @@ export default class JourneyExtractorDefault implements IJourneyExtractor {
transferProfile.arrivalTime,
);
} catch (e) {
this.context.emitWarning(e);
console.warn(e);
}
}
}
Expand Down Expand Up @@ -179,17 +173,17 @@ export default class JourneyExtractorDefault implements IJourneyExtractor {
// Get next profile based on the arrival time at the current location.
if (remainingTransfers >= 0) {
const currentProfiles: IProfile[] = profilesByStop[currentLocation.id];
let profileIndex: number = currentProfiles.length - 1;
let i: number = currentProfiles.length - 1;

currentTransferProfile = currentProfiles[profileIndex].transferProfiles[remainingTransfers];
currentTransferProfile = currentProfiles[i].transferProfiles[remainingTransfers];
departureTime = new Date(currentTransferProfile.departureTime);

while (profileIndex >= 0 && departureTime < exitConnection.arrivalTime) {
currentTransferProfile = currentProfiles[--profileIndex].transferProfiles[remainingTransfers];
while (i >= 0 && departureTime < exitConnection.arrivalTime) {
currentTransferProfile = currentProfiles[--i].transferProfiles[remainingTransfers];
departureTime = new Date(currentTransferProfile.departureTime);
}

if (profileIndex === -1) {
if (i === -1) {
// This should never happen.
return Promise.reject("Can't find next connection");
}
Expand Down
98 changes: 39 additions & 59 deletions src/planner/public-transport/PublicTransportPlannerCSAProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor
public async plan(query: IResolvedQuery): Promise<AsyncIterator<IPath>> {
this.query = query;

const departureLocation = this.query.from[0];
if (!departureLocation.id) {
departureLocation.id = "geo:" + departureLocation.latitude + "," + departureLocation.longitude;
departureLocation.name = "Departure location";
}

const arrivalLocation = this.query.to[0];
if (!arrivalLocation.id) {
arrivalLocation.id = "geo:" + arrivalLocation.latitude + "," + arrivalLocation.longitude;
arrivalLocation.name = "Arrival location";
}

this.setBounds();

return this.calculateJourneys();
Expand Down Expand Up @@ -212,7 +224,7 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor

private calculateEarliestArrivalTime(connection: IConnection): IArrivalTimeByTransfers {
const remainSeatedTime = this.remainSeated(connection);
if (connection["gtfs:dropOffType"] === DropOffType.NotAvailable) {
if (connection["gtfs:dropOffType"] === "gtfs:NotAvailable") {
return remainSeatedTime;
}

Expand All @@ -234,7 +246,7 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor
);

if (reachableStops.length === 0 && this.context) {
this.context.emit(EventType.AbortQuery, "No reachable stops at arrival location");
this.context.emit(EventType.QueryAbort);

return false;
}
Expand All @@ -244,18 +256,9 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor
}

for (const reachableStop of reachableStops) {
if (!this.query.to[0].id && reachableStop.duration === 0) {
this.query.to[0] = reachableStop.stop;
}

this.durationToTargetByStop[reachableStop.stop.id] = reachableStop.duration;
}

if (!this.query.to[0].id) {
this.query.to[0].id = "geo:" + this.query.to[0].latitude + "," + this.query.to[0].longitude;
this.query.to[0].name = "Arrival location";
}

return true;
}

Expand All @@ -269,21 +272,8 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor
this.query.minimumWalkingSpeed,
);

const stopIndex = 0;
while (stopIndex < this.initialReachableStops.length && !this.query.from[0].id) {
const reachableStop = this.initialReachableStops[stopIndex];
if (reachableStop.duration === 0) {
this.query.from[0] = reachableStop.stop;
}
}

if (!this.query.from[0].id) {
this.query.from[0].id = "geo:" + this.query.from[0].latitude + "," + this.query.from[0].longitude;
this.query.from[0].name = "Departure location";
}

if (this.initialReachableStops.length === 0 && this.context) {
this.context.emit(EventType.AbortQuery, "No reachable stops at departure location");
this.context.emit(EventType.QueryAbort);

return false;
}
Expand Down Expand Up @@ -412,45 +402,35 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor
);
}

try {
const departureStop: ILocation = await this.locationResolver.resolve(connection.departureStop);
const reachableStops: IReachableStop[] = await this.transferReachableStopsFinder.findReachableStops(
departureStop as IStop,
ReachableStopsFinderMode.Source,
this.query.maximumWalkingDuration,
this.query.minimumWalkingSpeed,
);

reachableStops.forEach((reachableStop: IReachableStop) => {
if (reachableStop.stop.id !== departureLocation.id) {
this.incorporateInProfile(
connection,
reachableStop.duration,
reachableStop.stop,
earliestArrivalTimeByTransfers,
);
}
});
const departureStop: ILocation = await this.locationResolver.resolve(connection.departureStop);
const reachableStops: IReachableStop[] = await this.transferReachableStopsFinder.findReachableStops(
departureStop as IStop,
ReachableStopsFinderMode.Source,
this.query.maximumWalkingDuration,
this.query.minimumWalkingSpeed,
);

} catch (e) {
this.context.emitWarning(e);
}
reachableStops.forEach((reachableStop: IReachableStop) => {
if (reachableStop.stop.id !== departureLocation.id) {
this.incorporateInProfile(
connection,
reachableStop.duration,
reachableStop.stop,
earliestArrivalTimeByTransfers,
);
}
});
}

private async emitTransferProfile(transferProfile: ITransferProfile, amountOfTransfers: number): Promise<void> {
try {
const departureStop = await this.locationResolver.resolve(transferProfile.enterConnection.departureStop);
const arrivalStop = await this.locationResolver.resolve(transferProfile.exitConnection.arrivalStop);
const departureStop = await this.locationResolver.resolve(transferProfile.enterConnection.departureStop);
const arrivalStop = await this.locationResolver.resolve(transferProfile.exitConnection.arrivalStop);

this.context.emit(EventType.AddedNewTransferProfile, {
departureStop,
arrivalStop,
amountOfTransfers,
});

} catch (e) {
this.context.emitWarning(e);
}
this.context.emit(EventType.AddedNewTransferProfile, {
departureStop,
arrivalStop,
amountOfTransfers,
});
}

private incorporateInProfile(
Expand Down
Loading

0 comments on commit 29dfd6a

Please sign in to comment.