-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_expression_composed.go
126 lines (109 loc) · 3.28 KB
/
model_expression_composed.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
// © 2019-present nextmv.io inc
package nextroute
import (
"fmt"
)
// ComposedPerVehicleTypeExpression is an expression that uses an expression for
// each vehicle type.
type ComposedPerVehicleTypeExpression interface {
ModelExpression
// DefaultExpression returns the default expression that is used if no
// expression is defined for a specific vehicle type.
DefaultExpression() ModelExpression
// Get returns the expression that is defined for the given vehicle type. If
// no expression is defined for the given vehicle type, the default
// expression is returned.
Get(vehicleType ModelVehicleType) ModelExpression
// Set sets the expression for the given vehicle type.
Set(vehicleType ModelVehicleType, expression ModelExpression)
}
// NewComposedPerVehicleTypeExpression returns a new ComposedPerVehicleTypeExpression.
func NewComposedPerVehicleTypeExpression(
defaultExpression ModelExpression,
) ComposedPerVehicleTypeExpression {
i := NewModelExpressionIndex()
return &composedPerVehicleTypeExpressionImpl{
index: i,
defaultExpression: defaultExpression,
name: fmt.Sprintf("composed_per_vehicle_type[%v] ",
i,
),
}
}
type composedPerVehicleTypeExpressionImpl struct {
defaultExpression ModelExpression
expressions []ModelExpression
name string
index int
}
func (t *composedPerVehicleTypeExpressionImpl) Get(vehicleType ModelVehicleType) ModelExpression {
idx := vehicleType.Index()
if idx >= 0 && idx < len(t.expressions) {
if expression := t.expressions[idx]; expression != nil {
return expression
}
}
return t.defaultExpression
}
func (t *composedPerVehicleTypeExpressionImpl) Set(
vehicleType ModelVehicleType,
expression ModelExpression,
) {
idx := vehicleType.Index()
// we have to grow the slice in case the index is out of bounds
if idx >= len(t.expressions) {
newExpressions := make([]ModelExpression, idx+1)
copy(newExpressions, t.expressions)
t.expressions = newExpressions
}
t.expressions[idx] = expression
}
func (t *composedPerVehicleTypeExpressionImpl) HasNegativeValues() bool {
if t.defaultExpression.HasNegativeValues() {
return true
}
for _, expression := range t.expressions {
if expression.HasNegativeValues() {
return true
}
}
return false
}
func (t *composedPerVehicleTypeExpressionImpl) HasPositiveValues() bool {
if t.defaultExpression.HasPositiveValues() {
return true
}
for _, expression := range t.expressions {
if expression.HasPositiveValues() {
return true
}
}
return false
}
func (t *composedPerVehicleTypeExpressionImpl) String() string {
return t.Name()
}
func (t *composedPerVehicleTypeExpressionImpl) Index() int {
return t.index
}
func (t *composedPerVehicleTypeExpressionImpl) Name() string {
return t.name
}
func (t *composedPerVehicleTypeExpressionImpl) SetName(n string) {
t.name = n
}
func (t *composedPerVehicleTypeExpressionImpl) DefaultExpression() ModelExpression {
return t.defaultExpression
}
func (t *composedPerVehicleTypeExpressionImpl) Value(
vehicleType ModelVehicleType,
from, to ModelStop,
) float64 {
idx := vehicleType.Index()
if idx >= 0 && idx < len(t.expressions) {
if expression := t.expressions[idx]; expression != nil {
return expression.Value(vehicleType, from, to)
}
}
return t.defaultExpression.Value(vehicleType, from, to)
}