-
Notifications
You must be signed in to change notification settings - Fork 25
/
logical.go
113 lines (91 loc) · 3.49 KB
/
logical.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
package components
import (
"go.uber.org/fx"
policylangv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/language/v1"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/notifiers"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/runtime"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/runtime/tristate"
)
// logicalCombinator is n-ary logical combinator used to implement And.
type logicalCombinator struct {
op func(tristate.Bool, tristate.Bool) tristate.Bool
name string
neutralElement tristate.Bool
}
// Name implements runtime.Component.
func (c *logicalCombinator) Name() string { return c.name }
// Type implements runtime.Component.
func (c *logicalCombinator) Type() runtime.ComponentType {
return runtime.ComponentTypeSignalProcessor
}
// ShortDescription implements runtime.Component.
func (c *logicalCombinator) ShortDescription() string { return "" }
// IsActuator implements runtime.Component.
func (*logicalCombinator) IsActuator() bool { return false }
// Execute implements runtime.Component.
func (c *logicalCombinator) Execute(inPortReadings runtime.PortToReading, circuitAPI runtime.CircuitAPI) (runtime.PortToReading, error) {
inputs := inPortReadings.ReadRepeatedReadingPort("inputs")
output := c.neutralElement
for _, input := range inputs {
output = c.op(output, tristate.FromReading(input))
}
return runtime.PortToReading{
"output": []runtime.Reading{output.ToReading()},
}, nil
}
// DynamicConfigUpdate is a no-op for logicalCombinator.
func (*logicalCombinator) DynamicConfigUpdate(notifiers.Event, config.Unmarshaller) {}
// NewAndAndOptions creates a new And Component.
func NewAndAndOptions(
_ *policylangv1.And,
_ runtime.ComponentID,
_ iface.Policy,
) (runtime.Component, fx.Option, error) {
return &logicalCombinator{
neutralElement: tristate.True,
op: tristate.Bool.And,
name: "And",
}, fx.Options(), nil
}
// NewOrAndOptions creates a new Or Component.
func NewOrAndOptions(
_ *policylangv1.Or,
_ runtime.ComponentID,
_ iface.Policy,
) (runtime.Component, fx.Option, error) {
return &logicalCombinator{
neutralElement: tristate.False,
op: tristate.Bool.Or,
name: "Or",
}, fx.Options(), nil
}
type inverter struct{}
// Name implements runtime.Component.
func (c *inverter) Name() string { return "Inverter" }
// Type implements runtime.Component.
func (c *inverter) Type() runtime.ComponentType { return runtime.ComponentTypeSignalProcessor }
// ShortDescription implements runtime.Component.
func (c *inverter) ShortDescription() string { return "" }
// IsActuator implements runtime.Component.
func (*inverter) IsActuator() bool { return false }
// Execute implements runtime.Component.
func (c *inverter) Execute(inPortReadings runtime.PortToReading, circuitAPI runtime.CircuitAPI) (runtime.PortToReading, error) {
input := inPortReadings.ReadSingleReadingPort("input")
return runtime.PortToReading{
"output": []runtime.Reading{
tristate.FromReading(input).Not().ToReading(),
},
}, nil
}
// DynamicConfigUpdate is a no-op for not component.
func (*inverter) DynamicConfigUpdate(notifiers.Event, config.Unmarshaller) {}
// NewInverterAndOptions creates a new Inverter Component.
func NewInverterAndOptions(
_ *policylangv1.Inverter,
_ runtime.ComponentID,
_ iface.Policy,
) (runtime.Component, fx.Option, error) {
return &inverter{}, fx.Options(), nil
}