Skip to content

Commit

Permalink
Issue #1.
Browse files Browse the repository at this point in the history
  • Loading branch information
highsource committed Jul 13, 2015
1 parent b9d84bd commit aa0561e
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ public class StopTimeVertex extends TemporalVertex {
public StopTimeVertex(Stop stop, int time) {
super(stop, time);
}

@Override
public boolean isEntryVertex() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public int getTime() {
return time;
}

public abstract boolean isEntryVertex();

@Override
public int hashCode() {
final int prime = 31;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public Trip getTrip() {
return trip;
}

@Override
public boolean isEntryVertex() {
return false;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.hisrc.gtfs.graph.service.jgrapht;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableSet;

import org.hisrc.gtfs.graph.model.edge.TransitionEdge;
import org.hisrc.gtfs.graph.model.path.Path;
import org.hisrc.gtfs.graph.model.util.TimeAwareComparator;
import org.hisrc.gtfs.graph.model.vertex.StopTimeVertex;
import org.hisrc.gtfs.graph.model.vertex.TemporalVertex;
import org.hisrc.gtfs.graph.service.GraphService;
import org.hisrc.util.ComparableComparator;
import org.jgrapht.DirectedGraph;
import org.onebusaway.gtfs.model.Stop;

import com.google.common.collect.TreeMultimap;

public class JGraphTGraphService implements GraphService {

private final DirectedGraph<TemporalVertex, TransitionEdge> graph;
private final Map<String, Stop> stopsById = new HashMap<String, Stop>();
private final TreeMultimap<String, TemporalVertex> stopTimeVerticesByStopId = TreeMultimap
.create(ComparableComparator.<String> create(),
TimeAwareComparator.create());

public JGraphTGraphService(
DirectedGraph<TemporalVertex, TransitionEdge> graph) {
this.graph = graph;
for (TemporalVertex vertex : graph.vertexSet()) {
if (vertex.isEntryVertex()) {
final Stop stop = vertex.getStop();
final String stopId = stop.getId().toString();
stopTimeVerticesByStopId.put(stopId, vertex);
stopsById.put(stopId, stop);
}
}
}

private Stop findStopById(String stopId) {
final Stop stop = this.stopsById.get(stopId);
return stop;
}

@Override
public TemporalVertex findLatestTemporalVertexByStopIdBefore(String stopId,
int time) {
final Stop stop = findStopById(stopId);
if (stop == null) {
return null;
} else {
final StopTimeVertex stopTimeVertex = new StopTimeVertex(stop, time);
final NavigableSet<TemporalVertex> stopTimeVertices = this.stopTimeVerticesByStopId
.get(stopId);
return stopTimeVertices.floor(stopTimeVertex);
}
}

@Override
public TemporalVertex findEarliestTemporalVertexByStopIdBefore(
String stopId, int time) {
final Stop stop = findStopById(stopId);
if (stop == null) {
return null;
} else {
final StopTimeVertex stopTimeVertex = new StopTimeVertex(stop, time);
final NavigableSet<TemporalVertex> stopTimeVertices = this.stopTimeVerticesByStopId
.get(stopId);
return stopTimeVertices.ceiling(stopTimeVertex);
}
}

@Override
public Path findShortestPathStartingAfter(String fromStopId,
String toStopId, int time) {
throw new UnsupportedOperationException();
}

@Override
public Path findShortestPathEndingBefore(String fromStopId,
String toStopId, int time) {
throw new UnsupportedOperationException();
}

@Override
public Path findShortestPath(Collection<TemporalVertex> startVertices,
Collection<TemporalVertex> endVertices) {
throw new UnsupportedOperationException();
}

@Override
public Collection<TemporalVertex> findTemporalVerticesByStopIdBefore(
String stopId, int time) {
throw new UnsupportedOperationException();
}

@Override
public Collection<TemporalVertex> findTemporalVerticesByStopIdAfter(
String stopId, int time) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.hisrc.gtfs.graph.builder;
package org.hisrc.gtfs.graph.servicebuilder;

import org.hisrc.gtfs.graph.model.edge.TransitionEdge;
import org.hisrc.gtfs.graph.model.vertex.TemporalVertex;
import org.hisrc.gtfs.graph.service.GraphService;
import org.onebusaway.gtfs.model.Stop;
import org.onebusaway.gtfs.model.Trip;

public interface GraphBuilder {
public interface GraphServiceBuilder {

public TemporalVertex addTripStopTimeVertex(Trip trip, Stop stop, int time);

Expand Down Expand Up @@ -35,6 +36,6 @@ public TransitionEdge addTransferEdge(TemporalVertex sourceVertex,
public TransitionEdge addWaitEdge(TemporalVertex sourceVertex,
TemporalVertex targetVertex, int waitTime);

public void build();
public GraphService build();

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.hisrc.gtfs.graph.builder.jgrapht;
package org.hisrc.gtfs.graph.servicebuilder.jgrapht;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.Map.Entry;

import org.hisrc.gtfs.graph.builder.GraphBuilder;
import org.hisrc.gtfs.graph.model.edge.BoardEdge;
import org.hisrc.gtfs.graph.model.edge.ChildParentEdge;
import org.hisrc.gtfs.graph.model.edge.ParentChildEdge;
Expand All @@ -18,6 +16,9 @@
import org.hisrc.gtfs.graph.model.vertex.StopTimeVertex;
import org.hisrc.gtfs.graph.model.vertex.TemporalVertex;
import org.hisrc.gtfs.graph.model.vertex.TripStopTimeVertex;
import org.hisrc.gtfs.graph.service.GraphService;
import org.hisrc.gtfs.graph.service.jgrapht.JGraphTGraphService;
import org.hisrc.gtfs.graph.servicebuilder.GraphServiceBuilder;
import org.hisrc.gtfs.onebusaway.model.util.StopComparator;
import org.jgrapht.DirectedGraph;
import org.jgrapht.EdgeFactory;
Expand All @@ -30,9 +31,10 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.TreeMultimap;

public class JGraphTGraphBuilder implements GraphBuilder {
public class JGraphTGraphServiceBuilder implements GraphServiceBuilder {

private Logger logger = LoggerFactory.getLogger(JGraphTGraphBuilder.class);
private Logger logger = LoggerFactory
.getLogger(JGraphTGraphServiceBuilder.class);

private DirectedGraph<TemporalVertex, TransitionEdge> graph = new DirectedMultigraph<TemporalVertex, TransitionEdge>(
new EdgeFactory<TemporalVertex, TransitionEdge>() {
Expand Down Expand Up @@ -145,13 +147,8 @@ private void addWaitEdges() {
}

@Override
public void build() {
// TODO Auto-generated method stub
public GraphService build() {
addWaitEdges();
logger.info(MessageFormat.format(
"Created a graph with [{0}] vertices.", graph.vertexSet()
.size()));
logger.info(MessageFormat.format("Created a graph with [{0}] edges.",
graph.edgeSet().size()));
return new JGraphTGraphService(this.graph);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import java.util.Set;

import org.apache.commons.lang3.Validate;
import org.hisrc.gtfs.graph.builder.GraphBuilder;
import org.hisrc.gtfs.graph.model.vertex.TemporalVertex;
import org.hisrc.gtfs.graph.servicebuilder.GraphServiceBuilder;
import org.onebusaway.gtfs.impl.GtfsDaoImpl;
import org.onebusaway.gtfs.model.AgencyAndId;
import org.onebusaway.gtfs.model.Stop;
Expand All @@ -29,11 +29,11 @@ public abstract class AbstractGraphBuildingGtfsDao extends GtfsDaoImpl {

protected abstract boolean isRelevantTrip(final Trip trip);

protected final GraphBuilder graphBuilder;
protected final GraphServiceBuilder graphBuilder;
private Multimap<Stop, Transfer> incomingTransfersByStop = HashMultimap
.create();

public AbstractGraphBuildingGtfsDao(GraphBuilder graphBuilder) {
public AbstractGraphBuildingGtfsDao(GraphServiceBuilder graphBuilder) {
this.graphBuilder = Validate.notNull(graphBuilder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Set;

import org.apache.commons.lang3.Validate;
import org.hisrc.gtfs.graph.builder.GraphBuilder;
import org.hisrc.gtfs.graph.servicebuilder.GraphServiceBuilder;
import org.joda.time.LocalDate;
import org.onebusaway.gtfs.model.AgencyAndId;
import org.onebusaway.gtfs.model.ServiceCalendar;
Expand All @@ -18,7 +18,7 @@ public class SingleDayGraphBuildingGtfsDao extends AbstractGraphBuildingGtfsDao
protected final int dayOfWeek;
protected final Set<AgencyAndId> availableServiceIds = new HashSet<AgencyAndId>();

public SingleDayGraphBuildingGtfsDao(GraphBuilder graphBuilder, int year,
public SingleDayGraphBuildingGtfsDao(GraphServiceBuilder graphBuilder, int year,
int month, int day) {
super(graphBuilder);
this.serviceDate = new ServiceDate(year, month, day);
Expand Down
30 changes: 30 additions & 0 deletions core/src/main/java/org/hisrc/util/ComparableComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.hisrc.util;

import java.util.Comparator;

public class ComparableComparator<C extends Comparable<C>> implements
Comparator<C> {

@Override
public int compare(C o1, C o2) {
if (o1 == o2) {
return 0;
} else if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
} else {
return o1.compareTo(o2);
}
}

@SuppressWarnings("rawtypes")
private static Comparator INSTANCE = new ComparableComparator();

public static <T extends Comparable<T>> Comparator<T> create() {
@SuppressWarnings("unchecked")
final Comparator<T> comparator = (Comparator<T>) INSTANCE;
return comparator;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import java.net.URISyntaxException;
import java.util.zip.ZipFile;

import org.hisrc.gtfs.graph.builder.GraphBuilder;
import org.hisrc.gtfs.graph.builder.jgrapht.JGraphTGraphBuilder;
import org.hisrc.gtfs.graph.model.vertex.TemporalVertex;
import org.hisrc.gtfs.graph.service.GraphService;
import org.hisrc.gtfs.graph.servicebuilder.GraphServiceBuilder;
import org.hisrc.gtfs.graph.servicebuilder.jgrapht.JGraphTGraphServiceBuilder;
import org.hisrc.gtfs.serialization.onebusaway.GtfsReader;
import org.hisrc.gtfs.serialization.onebusaway.services.SingleDayGraphBuildingGtfsDao;
import org.junit.Assert;
import org.junit.Test;
import org.onebusaway.csv_entities.ZipFileCsvInputSource;
import org.onebusaway.gtfs.model.StopTime;
Expand All @@ -26,12 +29,19 @@ public void parsesSWU() throws IOException, URISyntaxException {
zipFile);
gtfsReader.setInputSource(csvInputSource);

final GraphBuilder graphBuilder = new JGraphTGraphBuilder();
final GraphServiceBuilder graphBuilder = new JGraphTGraphServiceBuilder();
final GtfsMutableDao dao = new SingleDayGraphBuildingGtfsDao(
graphBuilder, 2015, 07, 10);
gtfsReader.setEntityStore(dao);
gtfsReader.run();
graphBuilder.build();
final GraphService graphService = graphBuilder.build();

final int startTime = 12 * 60 * 60;
final TemporalVertex start = graphService
.findLatestTemporalVertexByStopIdBefore("SWU_9001070",
startTime);
Assert.assertNotNull(start);
Assert.assertTrue(start.getTime() <= startTime);
}

// @Test
Expand Down

0 comments on commit aa0561e

Please sign in to comment.