model

package
v0.0.0-...-5682995 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package model represents the static Model of a scenario and provides the Init function to load such a scenario.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Assignment

type Assignment struct {
	Name      string
	Line      *Line
	Departure Time
	WayPoints []WayPoint
}

Assignment is a task for a Bus to do.

type Bus

type Bus struct {
	Id          BusId
	Name        string
	Assignments []Assignment
}

Bus describes a vehicle can can load passengers.

type BusId

type BusId string

BusId is used to identify a Bus.

type BusModel

type BusModel interface {
	Buses() []Bus
	Bus(BusId) (*Bus, bool)
}

BusModel is a model designed for all bus stuff.

type BusPosition

type BusPosition struct {
	BusId     BusId      `json:"id"`
	Location  [2]float64 `json:"loc"`
	StopId    *StopId    `json:"stopId,omitempty"`
	Departure Time       `json:"departure,omitempty"`
}

BusPosition describes the position of a certain bus at the current moment. BusPosition is meant to be sent to subscribers, possible over network. Thus, we keep this struct small.

type Coordinate

type Coordinate interface {
	Lat() float64
	Lon() float64
}

Coordinate is an interface providing latitude and longitude data.

type Line

type Line struct {
	Id   LineId
	Name string

	DefinitionIndex int
	Color           string
	// contains filtered or unexported fields
}

Line represents a predefined path and departures times for buses.

func (*Line) StartTimes

func (l *Line) StartTimes() []Time

func (*Line) Stops

func (l *Line) Stops() []*WayPoint

Stops returns all way points of the lines that are real stops.

func (*Line) String

func (l *Line) String() string

func (*Line) TourTimes

func (l *Line) TourTimes(start Time) []Time

TourTimes returns all departure times of the tour starting at start. If no tour of this line starts at the given time, then nil is returned. If the line is not well defined (e.g. no waypoints, no adequate departures) then the behaviour of this method is not well defined. It will most likely panic.

Example
stopA := StopId("stopA")
stopB := StopId("stopB")
stopC := StopId("stopC")
stopD := StopId("stopD")
stops := []*WayPoint{{Id: &stopA}, {Id: &stopB}, {Id: &stopC}, {Id: &stopD}}
baseTime, _ := ParseTime("16:35")
departures := map[StopId][]Time{
	"stopA": {baseTime, baseTime.Add(7 * time.Minute), baseTime.Add(14 * time.Minute)},
	"stopB": {baseTime.Add(2 * time.Minute), baseTime.Add(9 * time.Minute), baseTime.Add(16 * time.Minute)},
	"stopC": {baseTime.Add(3 * time.Minute), baseTime.Add(12 * time.Minute), baseTime.Add(19 * time.Minute)},
	"stopD": {baseTime.Add(1 * time.Minute), baseTime.Add(13 * time.Minute), baseTime.Add(20 * time.Minute)},
}
line := Line{waypoints: stops, departures: departures}
fmt.Printf("Departures for second tour: %v\n", line.TourTimes(baseTime.Add(7*time.Minute)))
fmt.Printf("Returns nil if tour not found: %v\n", line.TourTimes(baseTime.Add(2*time.Minute)) == nil)
Output:

Departures for second tour: [16:42 16:44 16:47 16:48]
Returns nil if tour not found: true

func (*Line) WayPoints

func (l *Line) WayPoints() []*WayPoint

WayPoints returns all waypoints of the line (including way points that are no stops).

type LineId

type LineId string

LineId is used to identify a Line.

type LineModel

type LineModel interface {
	Lines() []Line
	Line(LineId) (Line, bool)
}

LineModel is model designed for line management

type Model

type Model interface {
	BusModel
	LineModel
	Start() Time
}

Model represent the static data of a scenario. The model does not change over time, i.e. bus positions etc. are not stored in the model.

func Init

func Init(directory string) (Model, error)

Init loads the scenario from the provided directory and parses it.

type Publisher

type Publisher func(position BusPosition)

Publisher is a function taking care to broadcast BusPosition updates.

type RouteService

type RouteService func(...Coordinate) ([]Coordinate, float64, error)

RouteService is a function capable of computing detailed waypoints between the provided waypoints.

type StopId

type StopId string

StopId is used to identify a Stop.

type Ticker

type Ticker struct {
	HeartBeat <-chan Time
	// contains filtered or unexported fields
}

Ticker is similar to time.Ticker as it contains a channel that produces a Time in specified intervals. The difference to time.Ticker is that this Ticker has a specified start time and operates on Time, rather than on time.Time. New tickers should be created with NewTicker.

func NewTicker

func NewTicker(start Time, frequency float64, warp float64) Ticker

NewTicker creates and starts a new ticker. The start parameter specifies the first Time to be emitted by the ticker. The frequency denotes the frequency of consecutive emits by the ticker. The duration between two ticks is multiplied by the warp argument. This third interval may not be accurate (see time.NewTicker).

Example
start, _ := ParseTime("7:34")
ticker := NewTicker(start, 1000, 60*1000)
time1 := <-ticker.HeartBeat
time2 := <-ticker.HeartBeat
time3 := <-ticker.HeartBeat
ticker.Stop()
_ = <-ticker.HeartBeat
fmt.Printf("%v - %v - %v", time1, time2, time3)
Output:

07:34 - 07:35 - 07:36

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop prevents the ticker from emitting more events and closes the writing channel.

type Time

type Time int

Time specifies the time of the day in milliseconds. The difference to time.Time is that Time does not specify the date. A Time can be parsed from a kitchen clock string such as "15:04" with ParseTime.

func MustParseTime

func MustParseTime(timeString string) Time

MustParseTime haves nearly identical to ParseTime. The only difference is that MustParseTime will panic if the timeString cannot be parsed, whereas ParseTime returns a non-nil error.

func ParseTime

func ParseTime(timeString string) (Time, error)

ParseTime creates a new Time from a string. The time string must be a time given in time.Kitchen 24hour format, such as "15:04". If the time string is not parsable, then an error is returned. In this case, the returned time is 0.

Example
moment, _ := ParseTime("15:04")
fmt.Printf("Milliseconds since midnight: %d\n", moment)
hour, minute := moment.HourMinute()
fmt.Printf("Hour: %d, Minute: %d\n", hour, minute)
later := moment.Add(86 * time.Minute)
fmt.Printf("%v is later than %v: %v (difference: %d minutes)", later, moment, moment.Before(later), later.Sub(moment)/time.Minute)
Output:

Milliseconds since midnight: 54240000
Hour: 15, Minute: 4
16:30 is later than 15:04: true (difference: 86 minutes)

func (Time) Add

func (t Time) Add(duration time.Duration) Time

Add creates a new Time which represents this time plus the provided duration.

func (Time) Before

func (t Time) Before(other Time) bool

Before returns true if this time is strictly before the other time.

func (Time) HourMinute

func (t Time) HourMinute() (int, int)

HourMinute returns the hour and the minute of the time.

func (Time) String

func (t Time) String() string

func (Time) Sub

func (t Time) Sub(other Time) time.Duration

Sub computes the difference between this time and the given time. It is negative if this time is before (see Time.Before) the other time.

type WayPoint

type WayPoint struct {
	Departure Time
	Id        *StopId
	Name      string
	Latitude  float64
	Longitude float64
}

WayPoint is a part of an assignment.

func (WayPoint) Lat

func (w WayPoint) Lat() float64

func (WayPoint) Lon

func (w WayPoint) Lon() float64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL