<commit_before>package nl.ovapi.rid.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import nl.ovapi.bison.model.KV17cvlinfo;
import nl.ovapi.bison.model.KV17cvlinfo.Mutation;
import nl.ovapi.bison.model.KV17cvlinfo.Mutation.MutationType;
import nl.ovapi.bison.model.KV6posinfo;
import nl.ovapi.bison.model.KV6posinfo.Type;
import nl.ovapi.exceptions.StopNotFoundException;
import nl.ovapi.exceptions.TooEarlyException;
import nl.ovapi.exceptions.UnknownKV6PosinfoType;
import nl.ovapi.rid.model.JourneyPattern.JourneyPatternPoint;
import nl.ovapi.rid.model.TimeDemandGroup.TimeDemandGroupPoint;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;
import com.google.transit.realtime.GtfsRealtime.TripDescriptor;
import com.google.transit.realtime.GtfsRealtime.TripDescriptor.ScheduleRelationship;
import com.google.transit.realtime.GtfsRealtime.TripUpdate;
import com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeEvent;
import com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate;
import com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdateOrBuilder;
import com.google.transit.realtime.GtfsRealtime.VehicleDescriptor;
@ToString()
public class Journey {
	@Getter
	@Setter
	private Long id;
	@Getter
	@Setter
	private JourneyPattern journeypattern;
	@Getter
	@Setter
	private TimeDemandGroup timedemandgroup;
	@Getter
	@Setter
	private Map<Integer, Long> realizedArrivals;
	@Getter
	@Setter
	private Map<Integer, Long> realizedDepartures;

	@Getter
	@Setter
	private Integer departuretime;
	@Getter
	@Setter
	private Boolean wheelchairaccessible;
	@Getter
	@Setter
	private String agencyId;
	@Getter
	@Setter
	private String operatingDay;

	@Getter
	private KV6posinfo posinfo;

	@Getter
	@Setter
	private boolean isCanceled;

	private Map<Integer, ArrayList<Mutation>> mutations;

	@Getter
	@Setter
	private Map<Integer, KV6posinfo> reinforcements;

	public Journey() {
		mutations = Maps.newHashMap();
		reinforcements = Maps.newHashMap();
		realizedArrivals = Maps.newHashMap();
		realizedDepartures = Maps.newHashMap();
	}

	public void clearKV6(){
		posinfo = null;
	}

	private static final boolean RECORD_TIMES = true;

	private static final int PUNCTUALITY_FLOOR = 15; // seconds
	private static final int DEFAULT_SPEED = (int) (80 / 3.6); // meters per
	// seconds
	private static final int LONGHAUL_SPEED = (int) (100 / 3.6); // meters per
	// seconds
	private static final int SHORTHAUL_SPEED = (int) (60 / 3.6); // meters per
	// seconds

	private static final int MIN_PUNCTUALITY = -300; // Minimum allowed
	// punctuality.

	private static final Logger _log = LoggerFactory.getLogger(Journey.class);

	public TripDescriptor.Builder tripDescriptor(){
		TripDescriptor.Builder tripDescriptor = TripDescriptor.newBuilder();
		tripDescriptor.setStartDate(operatingDay);
		tripDescriptor.setTripId(id.toString());
		tripDescriptor.setScheduleRelationship(isCanceled ? ScheduleRelationship.CANCELED : ScheduleRelationship.SCHEDULED);
		return tripDescriptor;
	}

	public StopTimeEvent.Builder stopTimeEventArrival(JourneyPatternPoint pt, int punctuality){
		StopTimeEvent.Builder stopTimeEvent = StopTimeEvent.newBuilder();
		stopTimeEvent.setDelay(punctuality);
		return stopTimeEvent;
	}

	public StopTimeEvent.Builder stopTimeEventArrival(JourneyPatternPoint pt, long time){
		StopTimeEvent.Builder stopTimeEvent = StopTimeEvent.newBuilder();
		stopTimeEvent.setTime(time);
		return stopTimeEvent;
	}

	public boolean hasMutations(){
		return mutations.size() > 0 || isCanceled;
	}


	public StopTimeEvent.Builder stopTimeEventDeparture(JourneyPatternPoint pt, int punctuality){
		StopTimeEvent.Builder stopTimeEvent = StopTimeEvent.newBuilder();
		if (mutations.containsKey(pt.getPointorder())){
			for (Mutation m : mutations.get(pt.getPointorder())){
				if (m.getMutationtype() == MutationType.LAG){
					punctuality = Math.max(punctuality, m.getLagtime());
				}
			}
		}
		stopTimeEvent.setDelay(punctuality);
		return stopTimeEvent;
	}

	public StopTimeEvent.Builder stopTimeEventDeparture(JourneyPatternPoint pt, long time){
		StopTimeEvent.Builder stopTimeEvent = StopTimeEvent.newBuilder();
		stopTimeEvent.setTime(time);
		return stopTimeEvent;
	}

	public long getEndEpoch(){
		try {
			Calendar c = Calendar.getInstance();
			c.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(getOperatingDay()));
			c.set(Calendar.HOUR, 0);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			c.set(Calendar.MILLISECOND, 0);
			c.add(Calendar.SECOND, getDeparturetime());
			c.add(Calendar.SECOND, timedemandgroup.points.get(timedemandgroup.points.size()-1).getTotaldrivetime());
			if (posinfo != null && posinfo.getPunctuality() != null){
				c.add(Calendar.SECOND, Math.abs(posinfo.getPunctuality()));
			}
			return c.getTimeInMillis();
		} catch (ParseException e) {
			return -1;
		}
	}

	public long getDepartureEpoch(){
		try {
			Calendar c = Calendar.getInstance();
			c.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(getOperatingDay()));
			c.set(Calendar.HOUR, 0);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			c.set(Calendar.MILLISECOND, 0);
			c.add(Calendar.SECOND, getDeparturetime());
			return c.getTimeInMillis();
		} catch (ParseException e) {
			return -1;
		}
	}

	private StopTimeUpdate.Builder recordedTimes(JourneyPatternPoint pt){
		if (!RECORD_TIMES)
			return null;
		StopTimeUpdate.Builder stopTimeUpdate = StopTimeUpdate.newBuilder();
		stopTimeUpdate.setStopSequence(pt.getPointorder());
		stopTimeUpdate.setStopId(pt.getPointref().toString());
		if (!pt.isScheduled())
			return null; // Dummy point
		if (realizedArrivals.containsKey(pt.getPointorder()))
			stopTimeUpdate.setArrival(stopTimeEventArrival(pt,realizedArrivals.get(pt.getPointorder())));
		if (realizedDepartures.containsKey(pt.getPointorder()))
			stopTimeUpdate.setDeparture(stopTimeEventDeparture(pt,realizedDepartures.get(pt.getPointorder())));
		if (stopTimeUpdate.hasArrival() || stopTimeUpdate.hasDeparture()){
			return stopTimeUpdate;
		}
		return null;
	}

	public TripUpdate.Builder filter(TripUpdate.Builder tripUpdate){
		if (tripUpdate.getStopTimeUpdateCount() == 0)
			return tripUpdate;
		tripUpdate.getStopTimeUpdateOrBuilderList();
		long lastTime = -1;
		for (Iterator<StopTimeUpdate.Builder> iter = tripUpdate.getStopTimeUpdateBuilderList().iterator(); iter.hasNext();){
			StopTimeUpdate.Builder update = iter.next();
			if (update.getScheduleRelationship() == StopTimeUpdate.ScheduleRelationship.NO_DATA){
				continue; //No stoptime updates
			}
			if (update.hasArrival()){
				if (update.getArrivalOrBuilder().hasTime()){
					long eta = 	update.getArrivalOrBuilder().getTime();
					if (lastTime == -1 || eta > lastTime){
						lastTime = eta;
					}else{
						System.out.println(tripUpdate.build());
						update.clearArrival();
					}
				}
			}
			if (update.hasDeparture()){
				if (update.getDepartureOrBuilder().hasTime()){
					long etd = 	update.getDepartureOrBuilder().getTime();
					if (lastTime == -1 || etd >= lastTime){
						lastTime = etd;
					}else{
						update.clearArrival();
					}
				}
			}
			if (!update.hasArrival() && !update.hasDeparture()){
				iter.remove();<commit_msg>Better debugging than system.out<commit_after>package nl.ovapi.rid.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import nl.ovapi.bison.model.KV17cvlinfo;
import nl.ovapi.bison.model.KV17cvlinfo.Mutation;
import nl.ovapi.bison.model.KV17cvlinfo.Mutation.MutationType;
import nl.ovapi.bison.model.KV6posinfo;
import nl.ovapi.bison.model.KV6posinfo.Type;
import nl.ovapi.exceptions.StopNotFoundException;
import nl.ovapi.exceptions.TooEarlyException;
import nl.ovapi.exceptions.UnknownKV6PosinfoType;
import nl.ovapi.rid.model.JourneyPattern.JourneyPatternPoint;
import nl.ovapi.rid.model.TimeDemandGroup.TimeDemandGroupPoint;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;
import com.google.transit.realtime.GtfsRealtime.TripDescriptor;
import com.google.transit.realtime.GtfsRealtime.TripDescriptor.ScheduleRelationship;
import com.google.transit.realtime.GtfsRealtime.TripUpdate;
import com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeEvent;
import com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate;
import com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdateOrBuilder;
import com.google.transit.realtime.GtfsRealtime.VehicleDescriptor;
@ToString()
public class Journey {
	@Getter
	@Setter
	private Long id;
	@Getter
	@Setter
	private JourneyPattern journeypattern;
	@Getter
	@Setter
	private TimeDemandGroup timedemandgroup;
	@Getter
	@Setter
	private Map<Integer, Long> realizedArrivals;
	@Getter
	@Setter
	private Map<Integer, Long> realizedDepartures;

	@Getter
	@Setter
	private Integer departuretime;
	@Getter
	@Setter
	private Boolean wheelchairaccessible;
	@Getter
	@Setter
	private String agencyId;
	@Getter
	@Setter
	private String operatingDay;

	@Getter
	private KV6posinfo posinfo;

	@Getter
	@Setter
	private boolean isCanceled;

	private Map<Integer, ArrayList<Mutation>> mutations;

	@Getter
	@Setter
	private Map<Integer, KV6posinfo> reinforcements;

	public Journey() {
		mutations = Maps.newHashMap();
		reinforcements = Maps.newHashMap();
		realizedArrivals = Maps.newHashMap();
		realizedDepartures = Maps.newHashMap();
	}

	public void clearKV6(){
		posinfo = null;
	}

	private static final boolean RECORD_TIMES = true;

	private static final int PUNCTUALITY_FLOOR = 15; // seconds
	private static final int DEFAULT_SPEED = (int) (80 / 3.6); // meters per
	// seconds
	private static final int LONGHAUL_SPEED = (int) (100 / 3.6); // meters per
	// seconds
	private static final int SHORTHAUL_SPEED = (int) (60 / 3.6); // meters per
	// seconds

	private static final int MIN_PUNCTUALITY = -300; // Minimum allowed
	// punctuality.

	private static final Logger _log = LoggerFactory.getLogger(Journey.class);

	public TripDescriptor.Builder tripDescriptor(){
		TripDescriptor.Builder tripDescriptor = TripDescriptor.newBuilder();
		tripDescriptor.setStartDate(operatingDay);
		tripDescriptor.setTripId(id.toString());
		tripDescriptor.setScheduleRelationship(isCanceled ? ScheduleRelationship.CANCELED : ScheduleRelationship.SCHEDULED);
		return tripDescriptor;
	}

	public StopTimeEvent.Builder stopTimeEventArrival(JourneyPatternPoint pt, int punctuality){
		StopTimeEvent.Builder stopTimeEvent = StopTimeEvent.newBuilder();
		stopTimeEvent.setDelay(punctuality);
		return stopTimeEvent;
	}

	public StopTimeEvent.Builder stopTimeEventArrival(JourneyPatternPoint pt, long time){
		StopTimeEvent.Builder stopTimeEvent = StopTimeEvent.newBuilder();
		stopTimeEvent.setTime(time);
		return stopTimeEvent;
	}

	public boolean hasMutations(){
		return mutations.size() > 0 || isCanceled;
	}


	public StopTimeEvent.Builder stopTimeEventDeparture(JourneyPatternPoint pt, int punctuality){
		StopTimeEvent.Builder stopTimeEvent = StopTimeEvent.newBuilder();
		if (mutations.containsKey(pt.getPointorder())){
			for (Mutation m : mutations.get(pt.getPointorder())){
				if (m.getMutationtype() == MutationType.LAG){
					punctuality = Math.max(punctuality, m.getLagtime());
				}
			}
		}
		stopTimeEvent.setDelay(punctuality);
		return stopTimeEvent;
	}

	public StopTimeEvent.Builder stopTimeEventDeparture(JourneyPatternPoint pt, long time){
		StopTimeEvent.Builder stopTimeEvent = StopTimeEvent.newBuilder();
		stopTimeEvent.setTime(time);
		return stopTimeEvent;
	}

	public long getEndEpoch(){
		try {
			Calendar c = Calendar.getInstance();
			c.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(getOperatingDay()));
			c.set(Calendar.HOUR, 0);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			c.set(Calendar.MILLISECOND, 0);
			c.add(Calendar.SECOND, getDeparturetime());
			c.add(Calendar.SECOND, timedemandgroup.points.get(timedemandgroup.points.size()-1).getTotaldrivetime());
			if (posinfo != null && posinfo.getPunctuality() != null){
				c.add(Calendar.SECOND, Math.abs(posinfo.getPunctuality()));
			}
			return c.getTimeInMillis();
		} catch (ParseException e) {
			return -1;
		}
	}

	public long getDepartureEpoch(){
		try {
			Calendar c = Calendar.getInstance();
			c.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(getOperatingDay()));
			c.set(Calendar.HOUR, 0);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			c.set(Calendar.MILLISECOND, 0);
			c.add(Calendar.SECOND, getDeparturetime());
			return c.getTimeInMillis();
		} catch (ParseException e) {
			return -1;
		}
	}

	private StopTimeUpdate.Builder recordedTimes(JourneyPatternPoint pt){
		if (!RECORD_TIMES)
			return null;
		StopTimeUpdate.Builder stopTimeUpdate = StopTimeUpdate.newBuilder();
		stopTimeUpdate.setStopSequence(pt.getPointorder());
		stopTimeUpdate.setStopId(pt.getPointref().toString());
		if (!pt.isScheduled())
			return null; // Dummy point
		if (realizedArrivals.containsKey(pt.getPointorder()))
			stopTimeUpdate.setArrival(stopTimeEventArrival(pt,realizedArrivals.get(pt.getPointorder())));
		if (realizedDepartures.containsKey(pt.getPointorder()))
			stopTimeUpdate.setDeparture(stopTimeEventDeparture(pt,realizedDepartures.get(pt.getPointorder())));
		if (stopTimeUpdate.hasArrival() || stopTimeUpdate.hasDeparture()){
			return stopTimeUpdate;
		}
		return null;
	}

	public TripUpdate.Builder filter(TripUpdate.Builder tripUpdate){
		if (tripUpdate.getStopTimeUpdateCount() == 0)
			return tripUpdate;
		tripUpdate.getStopTimeUpdateOrBuilderList();
		long lastTime = -1;
		for (Iterator<StopTimeUpdate.Builder> iter = tripUpdate.getStopTimeUpdateBuilderList().iterator(); iter.hasNext();){
			StopTimeUpdate.Builder update = iter.next();
			if (update.getScheduleRelationship() == StopTimeUpdate.ScheduleRelationship.NO_DATA){
				continue; //No stoptime updates
			}
			if (update.hasArrival()){
				if (update.getArrivalOrBuilder().hasTime()){
					long eta = 	update.getArrivalOrBuilder().getTime();
					if (lastTime == -1 || eta > lastTime){
						lastTime = eta;
					}else{
						_log.trace("Non-sequential trip-update filterd {}",tripUpdate.build());
						update.clearArrival();
					}
				}
			}
			if (update.hasDeparture()){
				if (update.getDepartureOrBuilder().hasTime()){
					long etd = 	update.getDepartureOrBuilder().getTime();
					if (lastTime == -1 || etd >= lastTime){
						lastTime = etd;
					}else{
						update.clearArrival();
					}
				}
			}
			if (!update.hasArrival() && !update.hasDeparture()){
				iter.remove();
{'fraction_non_alphanumeric': 0.11783205085312813, 'fraction_numerical': 0.006423553027768485, 'mean_word_length': 5.073140999593661, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3945583', 'n_tokens_mistral': 5553, 'n_tokens_neox': 4991, 'n_words': 893}