use of im.tny.segvault.subway.Transfer in project underlx by underlx.
the class Trip method persistConnectionPath.
public static String persistConnectionPath(Path path, String replaceTrip) {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
RealmList<StationUse> uses = new RealmList<>();
List<Connection> edgeList = path.getEdgeList();
int size = edgeList.size();
if (size == 0) {
StationUse use = new StationUse();
use.setType(StationUse.UseType.VISIT);
use.setStation(realm.where(RStation.class).equalTo("id", path.getStartVertex().getStation().getId()).findFirst());
use.setEntryDate(path.getEntryExitTimes(0).first);
use.setLeaveDate(path.getEntryExitTimes(0).second);
use.setManualEntry(path.getManualEntry(0));
uses.add(realm.copyToRealm(use));
} else if (size == 1 && edgeList.get(0) instanceof Transfer) {
Connection c = edgeList.get(0);
StationUse use = new StationUse();
use.setType(StationUse.UseType.VISIT);
use.setSourceLine(c.getSource().getLine().getId());
use.setTargetLine(c.getTarget().getLine().getId());
use.setEntryDate(path.getEntryExitTimes(0).first);
use.setLeaveDate(path.getEntryExitTimes(0).second);
use.setManualEntry(path.getManualEntry(0));
use.setStation(realm.where(RStation.class).equalTo("id", c.getSource().getStation().getId()).findFirst());
uses.add(realm.copyToRealm(use));
} else {
int timeIdx = 0;
StationUse startUse = new StationUse();
startUse.setType(StationUse.UseType.NETWORK_ENTRY);
startUse.setStation(realm.where(RStation.class).equalTo("id", path.getStartVertex().getStation().getId()).findFirst());
startUse.setEntryDate(path.getEntryExitTimes(timeIdx).first);
startUse.setLeaveDate(path.getEntryExitTimes(timeIdx).second);
startUse.setManualEntry(path.getManualEntry(timeIdx));
uses.add(realm.copyToRealm(startUse));
int i = 1;
timeIdx++;
if (edgeList.get(0) instanceof Transfer) {
i = 2;
timeIdx++;
}
for (; i < size; i++) {
Connection c = edgeList.get(i);
StationUse use = new StationUse();
if (c instanceof Transfer) {
if (i == size - 1)
break;
use.setType(StationUse.UseType.INTERCHANGE);
use.setSourceLine(c.getSource().getLine().getId());
use.setTargetLine(c.getTarget().getLine().getId());
use.setEntryDate(path.getEntryExitTimes(timeIdx).first);
timeIdx++;
use.setLeaveDate(path.getEntryExitTimes(timeIdx).second);
use.setManualEntry(path.getManualEntry(timeIdx));
timeIdx++;
// skip next station as then we'd have duplicate uses for the same station ID
i++;
} else {
use.setType(StationUse.UseType.GONE_THROUGH);
use.setEntryDate(path.getEntryExitTimes(timeIdx).first);
use.setLeaveDate(path.getEntryExitTimes(timeIdx).second);
use.setManualEntry(path.getManualEntry(timeIdx));
timeIdx++;
}
use.setStation(realm.where(RStation.class).equalTo("id", c.getSource().getStation().getId()).findFirst());
uses.add(realm.copyToRealm(use));
}
StationUse endUse = new StationUse();
endUse.setType(StationUse.UseType.NETWORK_EXIT);
endUse.setStation(realm.where(RStation.class).equalTo("id", path.getEndVertex().getStation().getId()).findFirst());
endUse.setEntryDate(path.getEntryExitTimes(timeIdx).first);
endUse.setLeaveDate(path.getEntryExitTimes(timeIdx).second);
endUse.setManualEntry(path.getManualEntry(timeIdx));
uses.add(realm.copyToRealm(endUse));
}
Trip trip;
if (replaceTrip != null) {
trip = realm.where(Trip.class).equalTo("id", replaceTrip).findFirst();
trip.getPath().deleteAllFromRealm();
trip.setUserConfirmed(true);
trip.setSynced(false);
} else {
trip = realm.createObject(Trip.class, UUID.randomUUID().toString());
trip.setUserConfirmed(false);
}
trip.setPath(uses);
String tripId = trip.getId();
realm.commitTransaction();
realm.close();
return tripId;
}
use of im.tny.segvault.subway.Transfer in project underlx by underlx.
the class Path method manualExtendStart.
public void manualExtendStart(Stop vertex) {
// start by reverting to the path with the start without user-made extensions
int edgesToRemove = 0;
while (manualEntry.get(0)) {
manualEntry.remove(0);
times.remove(0);
edgesToRemove++;
}
for (int i = 0; i < edgesToRemove; i++) {
edgeList.remove(0);
}
if (edgeList.size() > 0) {
startVertex = edgeList.get(0).getSource();
}
// now go from the program-made start
Date time = times.get(0).first;
List<Connection> cs = Route.getShortestPath(graph, vertex, startVertex, new LeastHopsWeighter()).getEdgeList();
int size = cs.size();
int insertPos = 0;
for (int i = 0; i < size; i++) {
// never add a transfer as the first step
if (i == 0 && cs.get(i) instanceof Transfer) {
continue;
}
times.add(insertPos, new Pair<Date, Date>(time, time));
manualEntry.add(insertPos, true);
edgeList.add(insertPos++, cs.get(i));
}
this.startVertex = vertex;
for (OnPathChangedListener l : listeners) {
l.onPathChanged(this);
}
}
use of im.tny.segvault.subway.Transfer in project underlx by underlx.
the class TripFragment method populatePathView.
public static void populatePathView(final Context context, final LayoutInflater inflater, final Path path, ViewGroup root, boolean showInfoIcons) {
root.removeAllViews();
List<Connection> el = path.getEdgeList();
final int stepviewPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, context.getResources().getDisplayMetrics());
if (el.size() == 0) {
View stepview = inflater.inflate(R.layout.path_station, root, false);
FrameLayout prevLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.prev_line_stripe_layout);
FrameLayout nextLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.next_line_stripe_layout);
prevLineStripeLayout.setVisibility(View.GONE);
nextLineStripeLayout.setVisibility(View.GONE);
TextView timeView = (TextView) stepview.findViewById(R.id.time_view);
if (path.getManualEntry(0)) {
timeView.setVisibility(View.INVISIBLE);
} else {
timeView.setText(DateUtils.formatDateTime(context, path.getEntryTime(0).getTime(), DateUtils.FORMAT_SHOW_TIME));
}
final Station station = path.getStartVertex().getStation();
stepview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, StationActivity.class);
intent.putExtra(StationActivity.EXTRA_STATION_ID, station.getId());
intent.putExtra(StationActivity.EXTRA_NETWORK_ID, station.getNetwork().getId());
context.startActivity(intent);
}
});
RouteFragment.populateStationView(context, station, stepview, showInfoIcons, false);
stepview.setPadding(stepviewPadding, 0, stepviewPadding, 0);
root.addView(stepview);
return;
}
boolean isFirst = true;
Connection c = el.get(0);
int i = 0;
for (; i < el.size(); i++) {
c = el.get(i);
if (i == 0 && c instanceof Transfer) {
// starting with a line change? ignore
continue;
}
View stepview = inflater.inflate(R.layout.path_station, root, false);
FrameLayout prevLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.prev_line_stripe_layout);
FrameLayout nextLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.next_line_stripe_layout);
if (isFirst) {
Line line = c.getSource().getLine();
int lineColor = line.getColor();
prevLineStripeLayout.setVisibility(View.GONE);
nextLineStripeLayout.setBackgroundColor(lineColor);
TextView timeView = (TextView) stepview.findViewById(R.id.time_view);
if (path.getManualEntry(i)) {
timeView.setVisibility(View.INVISIBLE);
} else {
timeView.setText(DateUtils.formatDateTime(context, path.getEntryTime(i).getTime(), DateUtils.FORMAT_SHOW_TIME));
}
isFirst = false;
} else {
Line targetLine = c.getTarget().getLine();
int prevLineColor = c.getSource().getLine().getColor();
int nextLineColor = targetLine.getColor();
prevLineStripeLayout.setBackgroundColor(prevLineColor);
nextLineStripeLayout.setBackgroundColor(nextLineColor);
TextView timeView = (TextView) stepview.findViewById(R.id.time_view);
// start of the path was extended with the first step getting replaced by a transfer
if (path.getManualEntry(i) && !(c instanceof Transfer && !path.getManualEntry(i + 1))) {
timeView.setVisibility(View.INVISIBLE);
} else {
timeView.setText(DateUtils.formatDateTime(context, path.getEntryTime(i).getTime(), DateUtils.FORMAT_SHOW_TIME));
}
}
final Station station = c.getSource().getStation();
stepview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, StationActivity.class);
intent.putExtra(StationActivity.EXTRA_STATION_ID, station.getId());
intent.putExtra(StationActivity.EXTRA_NETWORK_ID, station.getNetwork().getId());
context.startActivity(intent);
}
});
RouteFragment.populateStationView(context, station, stepview, showInfoIcons, false);
ImageView crossView = (ImageView) stepview.findViewById(R.id.station_cross_image);
if (c.getSource().getStation().isAlwaysClosed()) {
crossView.setVisibility(View.VISIBLE);
}
stepview.setPadding(stepviewPadding, 0, stepviewPadding, 0);
root.addView(stepview);
if (c instanceof Transfer && i != el.size() - 1) {
c = el.get(++i);
}
}
View stepview = inflater.inflate(R.layout.path_station, root, false);
int lineColor = c.getSource().getLine().getColor();
FrameLayout prevLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.prev_line_stripe_layout);
FrameLayout nextLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.next_line_stripe_layout);
prevLineStripeLayout.setBackgroundColor(lineColor);
nextLineStripeLayout.setVisibility(View.GONE);
TextView timeView = (TextView) stepview.findViewById(R.id.time_view);
if (path.getManualEntry(i)) {
timeView.setVisibility(View.INVISIBLE);
} else {
timeView.setText(DateUtils.formatDateTime(context, path.getEntryTime(i).getTime(), DateUtils.FORMAT_SHOW_TIME));
}
ImageView crossView = (ImageView) stepview.findViewById(R.id.station_cross_image);
if (c.getTarget().getStation().isAlwaysClosed()) {
crossView.setVisibility(View.VISIBLE);
}
final Station station = c.getTarget().getStation();
stepview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, StationActivity.class);
intent.putExtra(StationActivity.EXTRA_STATION_ID, station.getId());
intent.putExtra(StationActivity.EXTRA_NETWORK_ID, station.getNetwork().getId());
context.startActivity(intent);
}
});
RouteFragment.populateStationView(context, station, stepview, showInfoIcons, false);
stepview.setPadding(stepviewPadding, 0, stepviewPadding, 0);
root.addView(stepview);
}
use of im.tny.segvault.subway.Transfer in project underlx by underlx.
the class DisturbanceAwareWeighter method getEdgeWeight.
@Override
public double getEdgeWeight(Network network, Connection connection) {
double weight = super.getEdgeWeight(network, connection);
// make transferring to lines with disturbances much "heavier"
LineStatusCache.Status s = mainService.getLineStatusCache().getLineStatus(connection.getTarget().getLine().getId());
if (connection instanceof Transfer || isSource(connection.getSource())) {
if (!isTarget(connection.getTarget()) && s != null && (s.down || connection.getTarget().getLine().isExceptionallyClosed(new Date()))) {
// TODO adjust this according to disturbance severity, if possible
weight += 100000;
if (connection.getTarget().getLine().isExceptionallyClosed(new Date())) {
weight += 100000;
}
}
}
return weight;
}
use of im.tny.segvault.subway.Transfer in project underlx by underlx.
the class TopologyCache method loadNetwork.
public static Network loadNetwork(Context context, String id, String apiEndpoint) throws CacheException {
String filename = "net-" + id;
Topology t = null;
try {
FileInputStream fis = context.openFileInput(filename);
ObjectInputStream is = new ObjectInputStream(fis);
t = (Topology) is.readObject();
is.close();
fis.close();
} catch (FileNotFoundException e) {
throw new CacheException(e).addInfo("File " + filename + " not found");
} catch (IOException e) {
throw new CacheException(e).addInfo("IO exception");
} catch (ClassNotFoundException e) {
throw new CacheException(e).addInfo("Class not found");
} catch (Exception e) {
e.printStackTrace();
throw new CacheException(e).addInfo("Unforeseen exception");
}
Network net = new Network(t.network.id, t.network.mainLocale, t.network.names, t.network.typCars, t.network.holidays, t.network.timezone, t.network.newsURL);
for (API.POI poi : t.pois) {
net.addPOI(new POI(poi.id, poi.type, poi.worldCoord, poi.webURL, poi.mainLocale, poi.names));
}
for (String lineid : t.network.lines) {
API.Line l = t.lines.get(lineid);
Line line = new Line(net, l.mainLocale, l.names, new HashSet<Stop>(), l.id, l.typCars, l.order);
line.setColor(Color.parseColor("#" + l.color));
boolean isFirstStationInLine = true;
for (String sid : l.stations) {
API.Station s = t.stations.get(sid);
Station station = net.getStation(s.id);
if (station == null) {
Map<String, String> triviaURLs = new HashMap<>();
for (Map.Entry<String, String> entry : s.triviaURLs.entrySet()) {
triviaURLs.put(entry.getKey(), apiEndpoint + entry.getValue());
}
Map<String, Map<String, String>> connURLs = new HashMap<>();
for (Map.Entry<String, Map<String, String>> entry : s.connURLs.entrySet()) {
Map<String, String> urls = new HashMap<>();
for (Map.Entry<String, String> localeEntry : entry.getValue().entrySet()) {
urls.put(localeEntry.getKey(), apiEndpoint + localeEntry.getValue());
}
connURLs.put(entry.getKey(), urls);
}
station = new Station(net, s.id, s.name, s.altNames, new Station.Features(s.features.lift, s.features.bus, s.features.boat, s.features.train, s.features.airport), triviaURLs);
station.setConnectionURLs(connURLs);
// Lobbies
for (String lid : s.lobbies) {
API.Lobby alobby = t.lobbies.get(lid);
Lobby lobby = new Lobby(alobby.id, alobby.name);
for (API.Exit aexit : alobby.exits) {
Lobby.Exit exit = new Lobby.Exit(aexit.id, aexit.worldCoord, aexit.streets, aexit.type);
lobby.addExit(exit);
}
for (API.Schedule asched : alobby.schedule) {
Schedule sched = new Schedule(asched.holiday, asched.day, asched.open, asched.openTime * 1000, asched.duration * 1000);
lobby.addSchedule(sched);
}
station.addLobby(lobby);
}
// POIs
for (String poiId : s.pois) {
POI poi = net.getPOI(poiId);
if (poi != null) {
station.addPOI(poi);
}
}
}
Stop stop = new Stop(station, line);
line.addVertex(stop);
station.addVertex(stop);
if (isFirstStationInLine) {
line.setFirstStop(stop);
isFirstStationInLine = false;
}
// WiFi APs
for (API.WiFiAP w : s.wiFiAPs) {
// take line affinity into account
if (w.line.equals(line.getId())) {
WiFiLocator.addBSSIDforStop(stop, new BSSID(w.bssid));
}
}
}
for (API.Schedule asched : l.schedule) {
Schedule sched = new Schedule(asched.holiday, asched.day, asched.open, asched.openTime * 1000, asched.duration * 1000);
line.addSchedule(sched);
}
for (API.WorldPath apath : l.worldPaths) {
WorldPath path = new WorldPath(apath.id, apath.path);
line.addPath(path);
}
net.addLine(line);
}
// Connections are within stations in the same line
for (API.Connection c : t.connections) {
Set<Stop> sFrom = net.getStation(c.from).vertexSet();
Set<Stop> sTo = net.getStation(c.to).vertexSet();
Stop from = null, to = null;
for (Stop s : sFrom) {
for (Stop s2 : sTo) {
if (s.getLine().getId().equals(s2.getLine().getId())) {
from = s;
to = s2;
}
}
}
if (from != null && to != null) {
Connection newConnection = net.addEdge(from, to);
from.getLine().addEdge(from, to);
newConnection.setTimes(new Connection.Times(c.typWaitS, c.typStopS, c.typS));
newConnection.setWorldLength(c.worldLength);
net.setEdgeWeight(newConnection, c.typS);
}
}
for (API.Transfer tr : t.transfers) {
Transfer newTransfer = new Transfer();
// find stations with the right IDs for each line
Station station = net.getStation(tr.station);
for (Stop from : station.vertexSet()) {
for (Stop to : station.vertexSet()) {
if (from.getLine().getId().equals(tr.from) && to.getLine().getId().equals(tr.to)) {
net.addEdge(from, to, newTransfer);
net.setEdgeWeight(newTransfer, tr.typS);
newTransfer.setTimes(new Connection.Times(0, 0, tr.typS));
}
}
}
}
for (API.Schedule asched : t.network.schedule) {
Schedule sched = new Schedule(asched.holiday, asched.day, asched.open, asched.openTime * 1000, asched.duration * 1000);
net.addSchedule(sched);
}
net.setDatasetAuthors(t.info.authors);
net.setDatasetVersion(t.info.version);
return net;
}
Aggregations