Пример #1
0
func (h *RequestHandler) HandleDeparturesBetween(w http.ResponseWriter, r *http.Request) error {
	params := mux.Vars(r)
	from := params["fromid"]
	to := params["toid"]
	trains, err := h.trainView.DeparturesBetween(services.StationID(from), services.StationID(to))
	return writeJsonResponse(w, trains, err)
}
Пример #2
0
func TestDijkstraMultipleDepartures(t *testing.T) {
	assert := assert.New(t)

	time1 := time.Now()
	e1 := NewTestEvent("A", time1, "1")

	time2 := next(next(time1))
	e2 := NewTestEvent("A", time2, "1")

	time3 := next(next(time2))
	e3 := NewTestEvent("A", time3, "1")

	time4 := next(next(time3))
	e4 := NewTestEvent("A", time4, "2")

	time5 := next(next(time4))
	e5 := NewTestEvent("B", time5, "2")

	time6 := next(next(time5))
	e6 := NewTestEvent("B", time6, "2")

	time7 := next(next(time6))
	e7 := NewTestEvent("C", time7, "2")

	time8 := next(next(time7))
	e8 := NewTestEvent("D", time8, "3")

	time9 := next(next(time8))
	e9 := NewTestEvent("E", time9, "3")

	in := &models.TrainEventList{[]models.TrainEvent{*e1, *e2, *e3, *e4, *e5, *e6, *e7, *e8, *e9}}

	out1, err1 := Dijkstra(in, e1, services.StationID("C-id"))
	assert.Nil(err1)
	assert.NotNil(out1)
	assert.Equal(5, len(out1.Events))

	out2, err2 := Dijkstra(in, e8, services.StationID("E-id"))
	assert.Nil(err2)
	assert.NotNil(out2)
	assert.Equal(2, len(out2.Events))

	out3, err3 := Dijkstra(in, e1, services.StationID("E-id"))
	assert.NotNil(err3)
	assert.Nil(out3)

	/*
		for _, e := range out.Events {
			logger.Printf("$$$ %s", e.String())
		}
	*/
}
Пример #3
0
func (h *RequestHandler) HandleDeparturesForStation(w http.ResponseWriter, r *http.Request) error {
	params := mux.Vars(r)
	from := params["fromid"]
	logger.Println("Handling request for departure times at %s", from)
	trains, err := h.trainView.TrainsFromStation(services.StationID(from))
	return writeJsonResponse(w, trains, err)
}
Пример #4
0
func (s *trainService) joinStationName(list *models.TrainEventList) *models.TrainEventList {
	for idx, _ := range list.Events {
		if station, err := s.stationProvider(services.StationID(list.Events[idx].StationUic)); err == nil {
			list.Events[idx].StationName = station.Name
		}
	}
	return list
}
Пример #5
0
func (s *stationService) Station(stationID services.StationID) (*models.Station, error) {
	if all, err := s.AllStations(); err == nil {
		for _, s := range all.Stations {
			if services.StationID(s.UIC) == stationID {
				return &s, nil
			}
		}
	}
	return nil, services.NewServiceValidationError(fmt.Sprintf("Unknown station id: %", string(stationID)))
}
Пример #6
0
// Dijkstra implements https://en.wikipedia.org/wiki/Dijkstra's_algorithm
func Dijkstra(in *models.TrainEventList, src *models.TrainEvent, dst services.StationID) (*models.TrainEventList, error) {
	logger.Println("Building Graph")
	verticesList := ToVertices(in.Events)
	outEdges := ToEdgeMap(verticesList)
	logger.Printf("Exec Dijkstra's with |V| = %d and |E| = %d", len(verticesList), len(outEdges))

	targets := []*Vertex{}

	if _, err := seedVertex(src, outEdges); err != nil {
		logger.Println("Failed to seed an apropriate vertex for %v", src)
		return nil, err
	}

	q := FiFoQueue(verticesList)
	logger.Printf("|Q| = %d", len(q))
	for len(q) > 0 {
		u := q.Min()
		edges := outEdges[u.HashKey()]
		for _, e := range edges {
			e.Validate()
			v := e.to

			if q.Contains(v) {
				alt := utils.AddWithoutOverflow(u.TimeFromSource(), e.Price())
				if v.TimeFromSource() > alt {
					v.SetTimeFromSource(alt)
					v.SetPrev(u)
					v.inEdge = e
				}
			}
		}

		if dst == services.StationID(u.event.StationUic) && u.timeFromSource < MAX_DISTANCE {
			targets = append(targets, u)
		}
	}

	logger.Printf("Done! - %d targets found", len(targets))
	if result, err := route(targets); err == nil {
		return result, nil
	} else {
		msg := fmt.Sprintf("Could not produce route from %s at %s to %s", src.StationUic, src.ScheduledDeparture, dst)
		return nil, errors.New(msg)
	}
}
Пример #7
0
func TestDijkstra(t *testing.T) {
	assert := assert.New(t)

	data := loadData(t)

	//fromid := "8600020" // Aalborg
	toid := services.StationID("8600029") // Arden

	var from *models.TrainEvent
	for idx, _ := range data.Events {
		e := &data.Events[idx]
		if e.ID == "b24ba020-7545-43b0-8135-48fffe742345" { // Aalborg departure
			if from == nil {
				logger.Printf("Trying src %s %v", data.Events[idx].ID, data.Events[idx])
				from = e
			}
		}
	}

	out, err := Dijkstra(data, from, toid)
	assert.Nil(err)
	assert.NotNil(out)
}
Пример #8
0
func (h *RequestHandler) HandleStationsDetailRequest(w http.ResponseWriter, r *http.Request) error {
	params := mux.Vars(r)
	stationid := params["stationid"]
	station, err := h.stationView.GetStation(services.StationID(stationid))
	return writeJsonResponse(w, station, err)
}