Skip to content

Commit

Permalink
Merge pull request #308 from sberkley/add_networks_model
Browse files Browse the repository at this point in the history
Add networks.txt model
  • Loading branch information
leonardehrenfried authored Dec 3, 2024
2 parents 99682a5 + 9b10e5b commit 9cfd8bb
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public <T> T getEntityForId(Class<T> type, Serializable id) {

@Override
public List<Agency> getAllAgencies() {
return _ops.find("from Agency");
return _ops.find("FROM Agency");
}

@Override
public List<Block> getAllBlocks() {
return _ops.find("from Block");
return _ops.find("FROM Block");
}

@Override
Expand Down Expand Up @@ -294,7 +294,7 @@ public Trip getTripForId(AgencyAndId id) {

@Override
public Collection<Area> getAllAreas() {
return _ops.find("from Area");
return _ops.find("FROM Area");
}

@Deprecated
Expand Down Expand Up @@ -327,7 +327,7 @@ public Collection<LocationGroup> getAllLocationGroups() {
}
@Override
public Collection<StopArea> getAllStopAreas() {
return _ops.find("from StopArea");
return _ops.find("FROM StopArea");
}
@Override
public Collection<Location> getAllLocations() {
Expand All @@ -336,12 +336,17 @@ public Collection<Location> getAllLocations() {

@Override
public Collection<BookingRule> getAllBookingRules() {
return _ops.find("from BookingRule");
return _ops.find("FROM BookingRule");
}

@Override
public Collection<Translation> getAllTranslations() {
return _ops.find("from Translation");
return _ops.find("FROM Translation");
}

@Override
public Collection<Network> getAllNetworks() {
return _ops.find("FROM Network");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ public Collection<Ridership> getAllRiderships() {
return getAllEntitiesForType(Ridership.class);
}

public Collection<Vehicle> getAllVehicles() { return getAllEntitiesForType(Vehicle.class); }
public Collection<Vehicle> getAllVehicles() {
return getAllEntitiesForType(Vehicle.class);
}

public Collection<Level> getAllLevels() {
return getAllEntitiesForType(Level.class);
Expand Down Expand Up @@ -257,6 +259,9 @@ public Vehicle getVehicleForId(AgencyAndId id) {
return getEntityForId(Vehicle.class, id);
}

public Collection<Network> getAllNetworks() {
return getAllEntitiesForType(Network.class);
}


public Facility getFacilityForId(AgencyAndId id) { return getEntityForId(Facility.class, id);}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ public Collection<Translation> getAllTranslations() {
return _dao.getAllTranslations();
}

@Override
public Collection<Network> getAllNetworks() {
return _dao.getAllNetworks();
}

@Override
public List<String> getOptionalMetadataFilenames() {
return _dao.getOptionalMetadataFilenames();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (C) 2024 Sound Transit <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onebusaway.gtfs.model;

import org.onebusaway.csv_entities.schema.annotations.CsvField;
import org.onebusaway.csv_entities.schema.annotations.CsvFields;
import org.onebusaway.gtfs.serialization.mappings.DefaultAgencyIdFieldMappingFactory;

@CsvFields(filename = "networks.txt", required = false)
public final class Network extends IdentityBean<AgencyAndId> {

private static final long serialVersionUID = 1L;

@CsvField(name = "network_id", mapping = DefaultAgencyIdFieldMappingFactory.class)
private AgencyAndId id;

@CsvField(name = "network_name", optional = true)
private String name;

@Override
public AgencyAndId getId() {
return id;
}

@Override
public void setId(AgencyAndId id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "<Network " + this.id + ">";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public static List<Class<?>> getEntityClasses() {
entityClasses.add(DirectionEntry.class);
entityClasses.add(AlternateStopNameException.class);
entityClasses.add(Icon.class);
entityClasses.add(Network.class);
return entityClasses;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public GtfsReader() {
_entityClasses.add(WrongWayConcurrency.class);
_entityClasses.add(DirectionEntry.class);
_entityClasses.add(AlternateStopNameException.class);
_entityClasses.add(Network.class);

CsvTokenizerStrategy tokenizerStrategy = new CsvTokenizerStrategy();
tokenizerStrategy.getCsvParser().setTrimInitialWhitespace(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ public interface GtfsDao extends GenericDao {

public Collection<Translation> getAllTranslations();

/****
* {@link Network} Methods
****/

public Collection<Network> getAllNetworks();

public Collection<DirectionEntry> getAllDirectionEntries();

public Collection<WrongWayConcurrency> getAllWrongWayConcurrencies();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* Copyright (C) 2024 Sound Transit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onebusaway.gtfs.model;

import java.io.File;
import java.io.IOException;
import java.util.*;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.onebusaway.gtfs.serialization.GtfsWriter;
import org.onebusaway.gtfs.serialization.GtfsWriterTest;
import org.onebusaway.gtfs.services.MockGtfs;
import org.onebusaway.gtfs.services.GtfsRelationalDao;

import static org.junit.jupiter.api.Assertions.*;

public class NetworkTest {

private MockGtfs _gtfs;

private File _tmpDirectory;

@BeforeEach
public void before() throws IOException {
_gtfs = MockGtfs.create();

//make temp directory for gtfs writing output
_tmpDirectory = File.createTempFile("NetworkTest-", "-tmp");
if (_tmpDirectory.exists())
GtfsWriterTest.deleteFileRecursively(_tmpDirectory);
_tmpDirectory.mkdirs();
}

@Test
public void testBasicNetworks() throws IOException {
_gtfs.putMinimal();
_gtfs.putDefaultTrips();
_gtfs.putDefaultStops();
_gtfs.putLines("networks.txt",
"network_id,network_name",
"rail_network,Rail Network", "bus_network,Bus Network");

GtfsRelationalDao dao = _gtfs.read();
assertEquals(2, dao.getAllNetworks().size());

GtfsWriter writer = new GtfsWriter();
writer.setOutputLocation(_tmpDirectory);
writer.run(dao);

Scanner scan = new Scanner(new File(_tmpDirectory + "/networks.txt"));
Set<String> expectedNetworkNames = new HashSet<String>();
Set<String> foundNetworkNames = new HashSet<String>();
expectedNetworkNames.add("Rail Network");
expectedNetworkNames.add("Bus Network");
boolean onHeader = true;
while(scan.hasNext()){
String line = scan.nextLine();
if (onHeader) {
onHeader = false;
} else {
String[] lineParts = line.split(",");

if (lineParts.length > 1) {
foundNetworkNames.add(lineParts[1]);
}
}
}
scan.close();

assertEquals(expectedNetworkNames, foundNetworkNames, "Did not find network names in output");
}

@Test
public void testPutMinimal() throws IOException {
_gtfs.putMinimal();
// Just make sure it parses without throwing an error.
_gtfs.read();
}

@AfterEach
public void teardown() {
deleteFileRecursively(_tmpDirectory);
}

public static void deleteFileRecursively(File file) {

if (!file.exists())
return;

if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File child : files)
deleteFileRecursively(child);
}
}

file.delete();
}

}

0 comments on commit 9cfd8bb

Please sign in to comment.