Skip to content

Commit

Permalink
1. Added maximumWalkingDuration and maximumWalkingDistance #42
Browse files Browse the repository at this point in the history
2. close iterators.
  • Loading branch information
Maxim Martin committed Dec 21, 2018
1 parent 733d4cd commit db68853
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 42 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;
}
3 changes: 3 additions & 0 deletions src/EventType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ enum EventType {
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;
5 changes: 1 addition & 4 deletions src/demo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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) => {
Expand Down Expand Up @@ -42,10 +41,8 @@ export default async (logResults) => {
// 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
/* from: {longitude: 3.707352, latitude: 51.011877},
to: {longitude: 3.707353, latitude: 51.011877},*/
minimumDepartureTime: new Date(),
maximumTransferDuration: Units.fromHours(.01),
maximumTransferDuration: Units.fromHours(0.5),
});

return new Promise((resolve, reject) => {
Expand Down
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
64 changes: 48 additions & 16 deletions src/planner/public-transport/PublicTransportPlannerCSAProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { AsyncIterator } from "asynciterator";
import { inject, injectable, tagged } from "inversify";
import Context from "../../Context";
import EventType from "../../EventType";
import DropOffType from "../../fetcher/connections/DropOffType";
import IConnection from "../../fetcher/connections/IConnection";
import IConnectionsProvider from "../../fetcher/connections/IConnectionsProvider";
import PickupType from "../../fetcher/connections/PickupType";
import IStop from "../../fetcher/stops/IStop";
import ILocation from "../../interfaces/ILocation";
import IPath from "../../interfaces/IPath";
Expand Down Expand Up @@ -125,19 +127,24 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor
const self = this;

return new Promise((resolve, reject) => {

const done = () => {
self.journeyExtractor.extractJourneys(self.profilesByStop, self.query)
.then((resultIterator) => {
resolve(resultIterator);
});
let isDone = false;
const done = () => {
if (!isDone) {
self.connectionsIterator.close();

self.journeyExtractor.extractJourneys(self.profilesByStop, self.query)
.then((resultIterator) => {
resolve(resultIterator);
});
isDone = true;
}
};

this.connectionsIterator.on("readable", () =>
this.connectionsIterator.on("readable", () =>
self.processNextConnection(done),
);

this.connectionsIterator.on("end", () => done());
this.connectionsIterator.on("end", () => done());

}) as Promise<AsyncIterator<IPath>>;
}
Expand Down Expand Up @@ -234,7 +241,7 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor
.findReachableStops(
arrivalStop,
ReachableStopsFinderMode.Target,
this.query.maximumTransferDuration,
this.query.maximumWalkingDuration,
this.query.minimumWalkingSpeed,
);

Expand All @@ -244,6 +251,10 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor
return false;
}

if (this.context) {
this.context.emit(EventType.FinalReachableStops, reachableStops);
}

for (const reachableStop of reachableStops) {
this.durationToTargetByStop[reachableStop.stop.id] = reachableStop.duration;
}
Expand All @@ -257,7 +268,7 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor
this.initialReachableStops = await this.initialReachableStopsFinder.findReachableStops(
fromLocation,
ReachableStopsFinderMode.Source,
this.query.maximumTransferDuration,
this.query.maximumWalkingDuration,
this.query.minimumWalkingSpeed,
);

Expand All @@ -267,6 +278,10 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor
return false;
}

if (this.context) {
this.context.emit(EventType.InitialReachableStops, this.initialReachableStops);
}

return true;
}

Expand Down Expand Up @@ -321,7 +336,8 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor
connection,
this.query.maximumTransfers,
this.query.minimumTransferDuration,
);
this.query.maximumTransferDuration,
);

return Vectors.shiftVector<IArrivalTimeByTransfers>(
transferTimes,
Expand Down Expand Up @@ -374,7 +390,7 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor

const initialReachableStop: IReachableStop = this.initialReachableStops.find(
(reachable: IReachableStop) =>
reachable.stop.id === connection.departureStop,
reachable.stop.id === connection.departureStop,
);

if (initialReachableStop) {
Expand All @@ -390,7 +406,7 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor
const reachableStops: IReachableStop[] = await this.transferReachableStopsFinder.findReachableStops(
departureStop as IStop,
ReachableStopsFinderMode.Source,
this.query.maximumTransferDuration,
this.query.maximumWalkingDuration,
this.query.minimumWalkingSpeed,
);

Expand All @@ -406,12 +422,23 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor
});
}

private async emitTransferProfile(transferProfile: ITransferProfile, amountOfTransfers: number): Promise<void> {
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,
});
}

private incorporateInProfile(
connection: IConnection,
duration: DurationMs,
stop: IStop,
arrivalTimeByTransfers: IArrivalTimeByTransfers,
) {
): void {
const departureTime = connection.departureTime.getTime() - duration;

if (departureTime < this.query.minimumDepartureTime.getTime()) {
Expand Down Expand Up @@ -447,12 +474,17 @@ export default class PublicTransportPlannerCSAProfile implements IPublicTranspor

if (
arrivalTimeByTransfers[amountOfTransfers].arrivalTime < transferProfile.arrivalTime &&
connection["gtfs:pickupType"] !== "gtfs:NotAvailable" &&
possibleExitConnection["gtfs:dropOfType"] !== "gtfs:NotAvailable"
connection["gtfs:pickupType"] !== PickupType.NotAvailable &&
possibleExitConnection["gtfs:dropOfType"] !== DropOffType.NotAvailable
) {
newTransferProfile.enterConnection = connection;
newTransferProfile.exitConnection = possibleExitConnection;
newTransferProfile.departureTime = departureTime;

if (this.context && this.context.listenerCount(EventType.AddedNewTransferProfile) > 0) {
this.emitTransferProfile(newTransferProfile, amountOfTransfers);
}

} else {
newTransferProfile.enterConnection = transferProfile.enterConnection;
newTransferProfile.exitConnection = transferProfile.exitConnection;
Expand Down
1 change: 1 addition & 0 deletions src/query-runner/IResolvedQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default interface IResolvedQuery {
publicTransportOnly?: boolean;
minimumWalkingSpeed?: SpeedkmH;
maximumWalkingSpeed?: SpeedkmH;
maximumWalkingDuration?: DurationMs;
minimumTransferDuration?: DurationMs;
maximumTransferDuration?: DurationMs;
maximumTransfers?: number;
Expand Down
4 changes: 4 additions & 0 deletions src/query-runner/QueryRunnerDefault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import IPath from "../interfaces/IPath";
import IQuery from "../interfaces/IQuery";
import IPublicTransportPlanner from "../planner/public-transport/IPublicTransportPlanner";
import TYPES from "../types";
import Units from "../util/Units";
import ILocationResolver from "./ILocationResolver";
import IQueryRunner from "./IQueryRunner";
import IResolvedQuery from "./IResolvedQuery";
Expand Down Expand Up @@ -54,6 +55,7 @@ export default class QueryRunnerDefault implements IQueryRunner {
const {
from, to,
minimumWalkingSpeed, maximumWalkingSpeed, walkingSpeed,
maximumWalkingDuration, maximumWalkingDistance,
minimumTransferDuration, maximumTransferDuration, maximumTransfers,
minimumDepartureTime, maximumArrivalTime,
...other
Expand All @@ -78,6 +80,8 @@ export default class QueryRunnerDefault implements IQueryRunner {
resolvedQuery.to = await this.resolveEndpoint(to);
resolvedQuery.minimumWalkingSpeed = minimumWalkingSpeed || walkingSpeed || Defaults.defaultMinimumWalkingSpeed;
resolvedQuery.maximumWalkingSpeed = maximumWalkingSpeed || walkingSpeed || Defaults.defaultMaximumWalkingSpeed;
resolvedQuery.maximumWalkingDuration = maximumWalkingDuration ||
Units.toDuration(maximumWalkingDistance, resolvedQuery.minimumWalkingSpeed) || Defaults.defaultWalkingDuration;

resolvedQuery.minimumTransferDuration = minimumTransferDuration || Defaults.defaultMinimumTransferDuration;
resolvedQuery.maximumTransferDuration = maximumTransferDuration || Defaults.defaultMaximumTransferDuration;
Expand Down
4 changes: 4 additions & 0 deletions src/query-runner/exponential/QueryRunnerExponential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import IQuery from "../../interfaces/IQuery";
import IPublicTransportPlanner from "../../planner/public-transport/IPublicTransportPlanner";
import TYPES from "../../types";
import Emiterator from "../../util/iterators/Emiterator";
import Units from "../../util/Units";
import ILocationResolver from "../ILocationResolver";
import IQueryRunner from "../IQueryRunner";
import IResolvedQuery from "../IResolvedQuery";
Expand Down Expand Up @@ -88,6 +89,7 @@ export default class QueryRunnerExponential implements IQueryRunner {
const {
from, to,
minimumWalkingSpeed, maximumWalkingSpeed, walkingSpeed,
maximumWalkingDuration, maximumWalkingDistance,
minimumTransferDuration, maximumTransferDuration, maximumTransfers,
minimumDepartureTime,
...other
Expand All @@ -102,6 +104,8 @@ export default class QueryRunnerExponential implements IQueryRunner {
resolvedQuery.to = await this.resolveEndpoint(to);
resolvedQuery.minimumWalkingSpeed = minimumWalkingSpeed || walkingSpeed || Defaults.defaultMinimumWalkingSpeed;
resolvedQuery.maximumWalkingSpeed = maximumWalkingSpeed || walkingSpeed || Defaults.defaultMaximumWalkingSpeed;
resolvedQuery.maximumWalkingDuration = maximumWalkingDuration ||
Units.toDuration(maximumWalkingDistance, resolvedQuery.minimumWalkingSpeed) || Defaults.defaultWalkingDuration;

resolvedQuery.minimumTransferDuration = minimumTransferDuration || Defaults.defaultMinimumTransferDuration;
resolvedQuery.maximumTransferDuration = maximumTransferDuration || Defaults.defaultMaximumTransferDuration;
Expand Down
8 changes: 8 additions & 0 deletions src/util/iterators/MergeIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export default class MergeIterator<T> extends BufferedIterator<T> {
});
}

public close() {
for (const iterator of this.sourceIterators) {
iterator.close();
}

super.close();
}

private fillFirstValues(done) {
this.values = Array(this.sourceIterators.length).fill(undefined);
let filledValues = 0;
Expand Down

0 comments on commit db68853

Please sign in to comment.