-
Notifications
You must be signed in to change notification settings - Fork 24
/
components.go
123 lines (101 loc) · 3.77 KB
/
components.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
package sim
import (
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/notifiers"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/components"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/runtime"
)
// Input is a component that will emit signal from the given array – one
// Reading per tick. On subsequent ticks, it will emit Invalid readings.
type Input []runtime.Reading
// NewInput creates an input from an array of floats.
func NewInput(values []float64) *Input {
input := Input(ToRtReadings(NewReadings(values)))
return &input
}
// NewConstantInput creates a constant-value input.
func NewConstantInput(value float64) runtime.Component {
return components.NewConstantSignal(value)
}
// Name implements runtime.Component.
func (i *Input) Name() string { return "TestInput" }
// Type implements runtime.Component.
func (i *Input) Type() runtime.ComponentType { return runtime.ComponentTypeSource }
// ShortDescription implements runtime.Component.
func (i *Input) ShortDescription() string { return "" }
// IsActuator implements runtime.Component.
func (*Input) IsActuator() bool { return false }
// Execute implements runtime.Component.
func (i *Input) Execute(_ runtime.PortToReading, _ runtime.CircuitAPI) (runtime.PortToReading, error) {
return runtime.PortToReading{
"output": []runtime.Reading{i.execute()},
}, nil
}
func (i *Input) execute() runtime.Reading {
if len(*i) == 0 {
return runtime.InvalidReading()
}
reading := (*i)[0]
*i = (*i)[1:]
return reading
}
// DynamicConfigUpdate implements runtime.Component.
func (i *Input) DynamicConfigUpdate(_ notifiers.Event, _ config.Unmarshaller) {}
// Output is a sink component to capture signal within a test.
type output struct {
Readings []runtime.Reading
}
// Name implements runtime.Component.
func (o *output) Name() string { return "TestOutput" }
// Type implements runtime.Component.
func (o *output) Type() runtime.ComponentType { return runtime.ComponentTypeSink }
// ShortDescription implements runtime.Component.
func (o *output) ShortDescription() string { return "" }
// IsActuator implements runtime.Component.
func (*output) IsActuator() bool { return false }
// TakeReadings returns the list of readings since previous TakeReadings() call
// (or since start).
func (o *output) TakeReadings() []runtime.Reading {
readings := o.Readings
o.Readings = nil
return readings
}
// Execute implements runtime.Component.
func (o *output) Execute(ins runtime.PortToReading, _ runtime.CircuitAPI) (runtime.PortToReading, error) {
o.Readings = append(o.Readings, ins.ReadSingleReadingPort("input"))
return nil, nil
}
// DynamicConfigUpdate implements runtime.Component.
func (o *output) DynamicConfigUpdate(_ notifiers.Event, _ config.Unmarshaller) {}
/******** helpers ***********/
// ConfigureInputComponent takes an input component and creates a
// Component which outputs signal with given name on its "output"
// poruntime.
func ConfigureInputComponent(comp runtime.Component, signal runtime.SignalID) *runtime.ConfiguredComponent {
return &runtime.ConfiguredComponent{
Component: comp,
PortMapping: runtime.PortMapping{
Outs: map[string][]runtime.Signal{
"output": {{
SubCircuitID: signal.SubCircuitID,
SignalName: signal.SignalName,
}},
},
},
}
}
// ConfigureOutputComponent takes an output component and creates a
// Component which reads signal with a given name on its "input" port.
func ConfigureOutputComponent(signal runtime.SignalID, comp runtime.Component) *runtime.ConfiguredComponent {
return &runtime.ConfiguredComponent{
Component: comp,
PortMapping: runtime.PortMapping{
Ins: map[string][]runtime.Signal{
"input": {{
SubCircuitID: signal.SubCircuitID,
SignalName: signal.SignalName,
}},
},
},
}
}