-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel_vehicle.go
193 lines (162 loc) · 4.25 KB
/
model_vehicle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// © 2019-present nextmv.io inc
package nextroute
import (
"fmt"
"strings"
"time"
)
// ModelVehicle is a vehicle in the model. A vehicle is a sequence of stops.
type ModelVehicle interface {
ModelData
// AddStop adds a stop to the vehicle. The stop is added to the end of the
// vehicle, before the last stop. If fixed is true stop will be fixed and
// can not be unplanned.
AddStop(stop ModelStop, fixed bool) error
// First returns the first stop of the vehicle.
First() ModelStop
// ID returns the identifier of the vehicle.
ID() string
// Index returns the index of the vehicle.
Index() int
// Last returns the last stop of the vehicle.
Last() ModelStop
// Model returns the model of the vehicle.
Model() Model
// SetID sets the identifier of the vehicle. This identifier is not used by
// nextroute, and therefore it does not have to be unique for nextroute
// internally. However, to make this ID useful for debugging and reporting it
// should be made unique.
SetID(string)
// Start returns the start time of the vehicle.
Start() time.Time
// Stops returns the stops of the vehicle that are provided as a start
// assignment. The first and last stop of the vehicle are not included in
// the returned slice.
Stops() ModelStops
// VehicleType returns the vehicle type of the vehicle.
VehicleType() ModelVehicleType
}
// ModelVehicles is a slice of ModelVehicle.
type ModelVehicles []ModelVehicle
type modelVehicleImpl struct {
start time.Time
modelDataImpl
vehicleType ModelVehicleType
id string
stops ModelStops
index int
}
func newModelVehicle(
index int,
vehicleType ModelVehicleType,
start time.Time,
first ModelStop,
last ModelStop,
) (ModelVehicle, error) {
if first.HasPlanStopsUnit() {
return nil,
fmt.Errorf("first stop %s already has a plan unit", first)
}
if last.HasPlanStopsUnit() {
return nil,
fmt.Errorf("last stop %s already has a plan unit", last)
}
first.(*stopImpl).firstOrLast = true
first.(*stopImpl).fixed = true
last.(*stopImpl).fixed = true
last.(*stopImpl).firstOrLast = true
return &modelVehicleImpl{
modelDataImpl: newModelDataImpl(),
index: index,
vehicleType: vehicleType,
stops: ModelStops{first, last},
start: start,
}, nil
}
func (v *modelVehicleImpl) VehicleType() ModelVehicleType {
return v.vehicleType
}
func (v *modelVehicleImpl) Index() int {
return v.index
}
func (v *modelVehicleImpl) First() ModelStop {
return v.stops[0]
}
func (v *modelVehicleImpl) Last() ModelStop {
return v.stops[len(v.stops)-1]
}
func (v *modelVehicleImpl) Stops() ModelStops {
result := make(ModelStops, len(v.stops)-2)
if len(v.stops) > 2 {
copy(result, v.stops[1:len(v.stops)-1])
}
return result
}
func (v *modelVehicleImpl) AddStop(
stop ModelStop,
fixed bool,
) error {
message := "can not add a stop `%v` to vehicle `%v`, "
if v.Model().IsLocked() {
return fmt.Errorf(message+
"the model is locked, this happens once a"+
"solution has been created using this model",
stop.ID(),
v.ID(),
)
}
if stop == nil {
return fmt.Errorf("can not add a nil stop to vehicle `%v`",
v.ID(),
)
}
if stop.IsFirstOrLast() {
return fmt.Errorf(message+
"the stop is first or last",
stop.ID(),
v.ID(),
)
}
if !stop.HasPlanStopsUnit() {
return fmt.Errorf(message+
"the stop does not have a plan unit",
stop.ID(),
v.ID(),
)
}
if vIdx, stopAddedToVehicle := v.Model().(*modelImpl).stopVehicles[stop.Index()]; stopAddedToVehicle {
return fmt.Errorf("can not add a stop `%v` to vehicle `%v` "+
"the stop is already added to vehicle `%v`",
stop.ID(),
v.ID(),
v.Model().Vehicles()[vIdx].ID(),
)
}
stop.(*stopImpl).model.stopVehicles[stop.Index()] = v.Index()
stop.(*stopImpl).fixed = fixed
v.stops = append(v.stops, v.stops[len(v.stops)-1])
v.stops[len(v.stops)-2] = stop.(*stopImpl)
return nil
}
func (v *modelVehicleImpl) Model() Model {
return v.vehicleType.Model()
}
func (v *modelVehicleImpl) Start() time.Time {
return v.start
}
func (v *modelVehicleImpl) ID() string {
return v.id
}
func (v *modelVehicleImpl) SetID(id string) {
v.id = id
}
func (v *modelVehicleImpl) String() string {
var sb strings.Builder
_, _ = fmt.Fprintf(
&sb,
"%v [%v]",
v.id,
v.index,
)
return sb.String()
}