Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Commit

Permalink
Renames and refactoring around ends of line discover and handling. Re…
Browse files Browse the repository at this point in the history
…moved some deprecated code
  • Loading branch information
cartwrightian committed Mar 5, 2021
1 parent 8f716a8 commit dea4683
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 165 deletions.
5 changes: 0 additions & 5 deletions main/src/com/tramchester/domain/id/CompositeId.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ public String getGraphId() {
return idA.getGraphId()+DIVIDER+idB.getGraphId();
}

@Override
public boolean notValid() {
return !isValid();
}

@Override
public boolean isValid() {
return idA.isValid() && idB.isValid();
Expand Down
7 changes: 0 additions & 7 deletions main/src/com/tramchester/domain/id/IdFor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,5 @@ static <T extends GraphProperty> IdFor<T> invalid() {

String getGraphId();

/***
* use !isValid()
* @return
*/
@Deprecated
boolean notValid();

boolean isValid();
}
6 changes: 5 additions & 1 deletion main/src/com/tramchester/domain/id/IdSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static <T extends GraphProperty> IdSet<T> emptySet() {
return new IdSet<>(Collections.emptySet());
}

private IdSet<T> addAll(IdSet<T> other) {
public IdSet<T> addAll(IdSet<T> other) {
theSet.addAll(other.theSet);
return this;
}
Expand Down Expand Up @@ -155,4 +155,8 @@ public boolean equals(Object o) {
public int hashCode() {
return theSet.hashCode();
}

public boolean containsAll(IdSet<T> other) {
return theSet.containsAll(other.theSet);
}
}
5 changes: 0 additions & 5 deletions main/src/com/tramchester/domain/id/InvalidId.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ public String getGraphId() {
throw new RuntimeException("Not implemented");
}

@Override
public boolean notValid() {
return true;
}

@Override
public boolean isValid() {
return false;
Expand Down
5 changes: 0 additions & 5 deletions main/src/com/tramchester/domain/id/StringIdFor.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ public String getGraphId() {
return theId;
}

@Override
public boolean notValid() {
return theId.isEmpty();
}

@Override
public boolean isValid() {
return !theId.isEmpty();
Expand Down
83 changes: 83 additions & 0 deletions main/src/com/tramchester/graph/FindRouteEndPoints.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.tramchester.graph;

import com.tramchester.domain.id.IdSet;
import com.tramchester.domain.places.RouteStation;
import com.tramchester.domain.reference.TransportMode;
import com.tramchester.graph.graphbuild.GraphProps;
import com.tramchester.graph.graphbuild.StagedTransportGraphBuilder;
import org.jetbrains.annotations.NotNull;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;

public class FindRouteEndPoints {
private static final Logger logger = LoggerFactory.getLogger(FindRouteEndPoints.class);
private final GraphDatabase graphDatabase;

@Inject
public FindRouteEndPoints(GraphDatabase graphDatabase, StagedTransportGraphBuilder.Ready readyToken) {
this.graphDatabase = graphDatabase;
}

public IdSet<RouteStation> searchForStarts(TransportMode mode) {
logger.info("Find starts for " +mode);

String query = "MATCH (a:ROUTE_STATION)-[r1:ON_ROUTE]->(:ROUTE_STATION) " +
"WHERE $mode in r1.transport_mode " +
"AND " +
"NOT EXISTS { " +
"MATCH (:ROUTE_STATION)-[r2:ON_ROUTE]->(a:ROUTE_STATION) " +
"WHERE $mode in r2.transport_mode AND r1.route_id=r2.route_id" +
"}" +
" RETURN a";

IdSet<RouteStation> stationIds = getIdFors(mode, query);

logger.info("Found " + stationIds.size() + " starts ");
return stationIds;
}

public IdSet<RouteStation> searchForEnds(TransportMode mode) {
logger.info("Find ends for " +mode);

String query = "MATCH (:ROUTE_STATION)-[r1:ON_ROUTE]->(a:ROUTE_STATION) " +
"WHERE $mode in r1.transport_mode " +
"AND " +
"NOT EXISTS { " +
"MATCH (a:ROUTE_STATION)-[r2:ON_ROUTE]->(:ROUTE_STATION) " +
"WHERE $mode in r2.transport_mode AND r1.route_id=r2.route_id" +
"}" +
" RETURN a";

IdSet<RouteStation> stationIds = getIdFors(mode, query);
logger.info("Found " + stationIds.size() + " ends");
return stationIds;
}

@NotNull
private IdSet<RouteStation> getIdFors(TransportMode mode, String query) {
logger.debug("Query: '" + query + '"');

Map<String, Object> params = new HashMap<>();
params.put("mode", mode.getNumber());

IdSet<RouteStation> stationIds = new IdSet<>();
try (Transaction txn = graphDatabase.beginTx()) {
Result result = txn.execute(query, params);
while (result.hasNext()) {
Map<String, Object> row = result.next();
Node node = (Node) row.get("a");
stationIds.add(GraphProps.getRouteStationIdFrom(node));
}
result.close();
}
return stationIds;
}

}
56 changes: 0 additions & 56 deletions main/src/com/tramchester/graph/FindStationsAtEndsOfRoutes.java

This file was deleted.

23 changes: 8 additions & 15 deletions main/src/com/tramchester/graph/graphbuild/GraphBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,14 @@ public enum Labels implements Label
NEIGHBOURS_ENABLED;

public static Labels forMode(TransportMode mode) {
switch (mode) {
case Tram:
return TRAM_STATION;
case Bus:
return BUS_STATION;
case Train:
case RailReplacementBus:
return TRAIN_STATION;
case Ferry:
return FERRY_STATION;
case Subway:
return SUBWAY_STATION;
default:
throw new RuntimeException("Unsupported mode " + mode);
}
return switch (mode) {
case Tram -> TRAM_STATION;
case Bus -> BUS_STATION;
case Train, RailReplacementBus -> TRAIN_STATION;
case Ferry -> FERRY_STATION;
case Subway -> SUBWAY_STATION;
default -> throw new RuntimeException("Unsupported mode " + mode);
};
}

public static Set<Labels> forMode(Set<TransportMode> modes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ private void createRouteRelationship(Node from, Node to, Route route, int cost)
Relationship onRoute = from.createRelationshipTo(to, TransportRelationshipTypes.ON_ROUTE);
setProperty(onRoute, route);
setCostProp(onRoute, cost);
setProperty(onRoute, route.getTransportMode());
}
}

Expand Down
2 changes: 1 addition & 1 deletion main/src/com/tramchester/graph/search/MapPathToStages.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private void reset() {
protected Optional<WalkingToStationStage> beginTrip(Relationship relationship) {
IdFor<Trip> newTripId = GraphProps.getTripIdFrom(relationship);

if (tripId.notValid()) {
if (!tripId.isValid()) {
this.tripId = newTripId;
boardingTime = GraphProps.getTime(relationship);
} else if (!tripId.equals(newTripId)){
Expand Down
51 changes: 0 additions & 51 deletions main/src/com/tramchester/repository/EndsOfRoutesRepository.java

This file was deleted.

9 changes: 5 additions & 4 deletions main/src/com/tramchester/repository/RouteCallingStations.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ public void start() {
logger.info("start");
Collection<Route> routes = transportData.getRoutes();
logger.info("Populating for " + routes.size() + " routes");
routes.forEach(route -> populateFromServices(route, route.getServices()));
routes.forEach(this::populateFromServices);
logger.info("ready");
}

private void populateFromServices(Route route, Set<Service> services) {
private void populateFromServices(Route route) {
logger.debug("Populate calling stations for route " + HasId.asId(route));
Set<Trip> trips = services.stream().map(service -> service.getTripsFor(route)).flatMap(Collection::stream).collect(Collectors.toSet());
Set<Service> services = route.getServices();
Set<Trip> allTrips = services.stream().map(Service::getTrips).flatMap(Collection::stream).collect(Collectors.toSet());

// ASSUME: longest trips correspond to full end to end journeys on the whole route
Optional<Trip> longest = trips.stream().max(Comparator.comparingInt(a -> a.getStops().numberOfCallingPoints()));
Optional<Trip> longest = allTrips.stream().max(Comparator.comparingInt(a -> a.getStops().numberOfCallingPoints()));

longest.ifPresent(longestTrip -> {
StopCalls stops = longestTrip.getStops();
Expand Down
Loading

0 comments on commit dea4683

Please sign in to comment.