cond_and.gno
1.21 Kb ยท 55 lines
1package daocond
2
3import (
4 "strings"
5)
6
7type andCond struct {
8 conditions []Condition
9}
10
11func And(conditions ...Condition) Condition {
12 if len(conditions) < 2 {
13 panic("at least two conditions are required")
14 }
15 return &andCond{conditions: conditions}
16}
17
18// Eval implements Condition.
19func (c *andCond) Eval(ballot Ballot) bool {
20 for _, condition := range c.conditions {
21 if !condition.Eval(ballot) {
22 return false
23 }
24 }
25 return true
26}
27
28// Signal implements Condition.
29func (c *andCond) Signal(ballot Ballot) float64 {
30 totalSignal := 0.0
31 for _, condition := range c.conditions {
32 totalSignal += condition.Signal(ballot)
33 }
34 return totalSignal / float64(len(c.conditions))
35}
36
37// Render implements Condition.
38func (c *andCond) Render() string {
39 renders := []string{}
40 for _, condition := range c.conditions {
41 renders = append(renders, condition.Render())
42 }
43 return "[" + strings.Join(renders, " AND ") + "]"
44}
45
46// RenderWithVotes implements Condition.
47func (c *andCond) RenderWithVotes(ballot Ballot) string {
48 renders := []string{}
49 for _, condition := range c.conditions {
50 renders = append(renders, condition.RenderWithVotes(ballot))
51 }
52 return "[" + strings.Join(renders, " AND ") + "]"
53}
54
55var _ Condition = (*andCond)(nil)