Skip to content

Commit 6d63649

Browse files
committed
chore: clean code
1 parent 352cf3f commit 6d63649

File tree

3 files changed

+48
-72
lines changed

3 files changed

+48
-72
lines changed

action.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
type arithmetic uint8
99

1010
const (
11-
EFFECT_ARITHMETIC_SET arithmetic = iota
12-
EFFECT_ARITHMETIC_ADD
13-
EFFECT_ARITHMETIC_SUBSTRACT
14-
EFFECT_ARITHMETIC_MULTIPLY
15-
EFFECT_ARITHMETIC_DIVIDE
11+
SET arithmetic = iota
12+
ADD
13+
SUBSTRACT
14+
MULTIPLY
15+
DIVIDE
1616
)
1717

1818
type Action struct {
@@ -51,13 +51,13 @@ type EffectInterface interface {
5151

5252
type Effect[T Numeric] struct {
5353
Key StateKey
54-
Value T
5554
Operator arithmetic
55+
Value T
5656
}
5757

5858
func (effect Effect[T]) check(states states) bool {
5959
// Other operators than '=' mean the effect will have an impact of the states
60-
if effect.Operator != EFFECT_ARITHMETIC_SET {
60+
if effect.Operator != SET {
6161
return false
6262
}
6363

@@ -77,10 +77,10 @@ func (effect Effect[T]) check(states states) bool {
7777
func (effect Effect[T]) apply(data statesData) error {
7878
k := data.GetIndex(effect.Key)
7979
if k < 0 {
80-
if slices.Contains([]arithmetic{EFFECT_ARITHMETIC_SET, EFFECT_ARITHMETIC_ADD}, effect.Operator) {
80+
if slices.Contains([]arithmetic{SET, ADD}, effect.Operator) {
8181
data = append(data, State[T]{Value: effect.Value})
8282
return nil
83-
} else if slices.Contains([]arithmetic{EFFECT_ARITHMETIC_SUBSTRACT}, effect.Operator) {
83+
} else if slices.Contains([]arithmetic{SUBSTRACT}, effect.Operator) {
8484
data = append(data, State[T]{Value: -effect.Value})
8585
return nil
8686
}
@@ -92,15 +92,15 @@ func (effect Effect[T]) apply(data statesData) error {
9292

9393
state := data[k].(State[T])
9494
switch effect.Operator {
95-
case EFFECT_ARITHMETIC_SET:
95+
case SET:
9696
state.Value = effect.Value
97-
case EFFECT_ARITHMETIC_ADD:
97+
case ADD:
9898
state.Value += effect.Value
99-
case EFFECT_ARITHMETIC_SUBSTRACT:
99+
case SUBSTRACT:
100100
state.Value -= effect.Value
101-
case EFFECT_ARITHMETIC_MULTIPLY:
101+
case MULTIPLY:
102102
state.Value *= effect.Value
103-
case EFFECT_ARITHMETIC_DIVIDE:
103+
case DIVIDE:
104104
state.Value /= effect.Value
105105
}
106106

@@ -117,7 +117,7 @@ type EffectBool struct {
117117

118118
func (effectBool EffectBool) check(states states) bool {
119119
// Other operators than '=' is not allowed
120-
if effectBool.Operator != EFFECT_ARITHMETIC_SET {
120+
if effectBool.Operator != SET {
121121
return false
122122
}
123123

@@ -135,7 +135,7 @@ func (effectBool EffectBool) check(states states) bool {
135135
}
136136

137137
func (effectBool EffectBool) apply(data statesData) error {
138-
if effectBool.Operator != EFFECT_ARITHMETIC_SET {
138+
if effectBool.Operator != SET {
139139
return fmt.Errorf("operation %v not allowed on bool type", effectBool.Operator)
140140
}
141141

@@ -176,7 +176,7 @@ func (effectString EffectString) check(states states) bool {
176176
}
177177

178178
func (effectString EffectString) apply(data statesData) error {
179-
if !slices.Contains([]arithmetic{EFFECT_ARITHMETIC_SET, EFFECT_ARITHMETIC_ADD}, effectString.Operator) {
179+
if !slices.Contains([]arithmetic{SET, ADD}, effectString.Operator) {
180180
return fmt.Errorf("arithmetic operation %v not allowed on string type", effectString.Operator)
181181
}
182182

@@ -191,9 +191,9 @@ func (effectString EffectString) apply(data statesData) error {
191191

192192
state := data[k].(State[string])
193193
switch effectString.Operator {
194-
case EFFECT_ARITHMETIC_SET:
194+
case SET:
195195
state.Value = effectString.Value
196-
case EFFECT_ARITHMETIC_ADD:
196+
case ADD:
197197
state.Value = fmt.Sprint(state.Value, effectString.Value)
198198
}
199199
data[k] = state

benchmark/goapai_test.go

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,62 +22,38 @@ func BenchmarkGoapAI(b *testing.B) {
2222
actions := goapai.Actions{}
2323

2424
actions.AddAction("action1", 1, true, goapai.Conditions{
25-
&goapai.Condition[int]{Key: ATTRIBUTE_1, Value: 0, Operator: goapai.STATE_OPERATOR_UPPER},
25+
&goapai.Condition[int]{Key: ATTRIBUTE_1, Operator: goapai.UPPER, Value: 0},
2626
}, goapai.Effects{
27-
goapai.Effect[int]{
28-
Key: ATTRIBUTE_1,
29-
Value: 50,
30-
Operator: goapai.EFFECT_ARITHMETIC_SUBSTRACT,
31-
},
32-
goapai.Effect[int]{
33-
Key: ATTRIBUTE_2,
34-
Value: 5,
35-
Operator: goapai.EFFECT_ARITHMETIC_SUBSTRACT,
36-
},
27+
goapai.Effect[int]{Key: ATTRIBUTE_1, Operator: goapai.SUBSTRACT, Value: 50},
28+
goapai.Effect[int]{Key: ATTRIBUTE_2, Operator: goapai.SUBSTRACT, Value: 5},
3729
})
3830
actions.AddAction("action2", 1, true, goapai.Conditions{
39-
&goapai.Condition[int]{Key: ATTRIBUTE_3, Value: 50, Operator: goapai.STATE_OPERATOR_LOWER},
31+
&goapai.Condition[int]{Key: ATTRIBUTE_3, Operator: goapai.LOWER, Value: 50},
4032
}, goapai.Effects{
41-
goapai.Effect[int]{
42-
Key: ATTRIBUTE_3,
43-
Value: 20,
44-
Operator: goapai.EFFECT_ARITHMETIC_ADD,
45-
},
46-
goapai.Effect[int]{
47-
Key: ATTRIBUTE_2,
48-
Value: 10,
49-
Operator: goapai.EFFECT_ARITHMETIC_ADD,
50-
},
51-
goapai.Effect[int]{
52-
Key: ATTRIBUTE_1,
53-
Value: 5,
54-
Operator: goapai.EFFECT_ARITHMETIC_ADD,
55-
},
33+
goapai.Effect[int]{Key: ATTRIBUTE_3, Operator: goapai.ADD, Value: 20},
34+
goapai.Effect[int]{Key: ATTRIBUTE_2, Operator: goapai.ADD, Value: 10},
35+
goapai.Effect[int]{Key: ATTRIBUTE_1, Operator: goapai.ADD, Value: 5},
5636
})
5737
actions.AddAction("action3", 1, true, goapai.Conditions{
58-
&goapai.Condition[int]{Key: ATTRIBUTE_3, Value: 30, Operator: goapai.STATE_OPERATOR_UPPER},
38+
&goapai.Condition[int]{Key: ATTRIBUTE_3, Operator: goapai.UPPER, Value: 30},
5939
}, goapai.Effects{
60-
goapai.Effect[int]{
61-
Key: ATTRIBUTE_3,
62-
Value: 30,
63-
Operator: goapai.EFFECT_ARITHMETIC_SUBSTRACT,
64-
},
40+
goapai.Effect[int]{Key: ATTRIBUTE_3, Operator: goapai.SUBSTRACT, Value: 30},
6541
})
6642

6743
entity := Entity{attributes: Attributes{}}
6844

6945
goals := goapai.Goals{
7046
"goal1": {
7147
Conditions: goapai.Conditions{
72-
&goapai.Condition[int]{Key: ATTRIBUTE_2, Value: 80, Operator: goapai.STATE_OPERATOR_UPPER},
48+
&goapai.Condition[int]{Key: ATTRIBUTE_2, Value: 80, Operator: goapai.UPPER},
7349
},
7450
PriorityFn: func(sensors goapai.Sensors) float64 {
7551
return 1.0
7652
},
7753
},
7854
"goal2": {
7955
Conditions: goapai.Conditions{
80-
&goapai.Condition[int]{Key: ATTRIBUTE_2, Value: 100, Operator: goapai.STATE_OPERATOR_EQUAL},
56+
&goapai.Condition[int]{Key: ATTRIBUTE_2, Value: 100, Operator: goapai.EQUAL},
8157
},
8258
PriorityFn: func(sensors goapai.Sensors) float64 {
8359
return 0.0

state.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ import (
1111
type operator uint8
1212

1313
const (
14-
STATE_OPERATOR_EQUAL operator = iota
15-
STATE_OPERATOR_NOT_EQUAL
16-
STATE_OPERATOR_LOWER_OR_EQUAL
17-
STATE_OPERATOR_LOWER
18-
STATE_OPERATOR_UPPER_OR_EQUAL
19-
STATE_OPERATOR_UPPER
14+
EQUAL operator = iota
15+
NOT_EQUAL
16+
LOWER_OR_EQUAL
17+
LOWER
18+
UPPER_OR_EQUAL
19+
UPPER
2020
)
2121

2222
type Numeric interface {
2323
~int8 | ~int |
24-
~uint8 | ~uint64 |
25-
~float64
24+
~uint8 | ~uint64 |
25+
~float64
2626
}
2727

2828
type StateInterface interface {
@@ -176,27 +176,27 @@ func (condition *Condition[T]) Check(states states) bool {
176176
s := states.data[k]
177177
if state, ok := s.(State[T]); ok {
178178
switch condition.Operator {
179-
case STATE_OPERATOR_EQUAL:
179+
case EQUAL:
180180
if state.Value == condition.Value {
181181
return true
182182
}
183-
case STATE_OPERATOR_NOT_EQUAL:
183+
case NOT_EQUAL:
184184
if state.Value != condition.Value {
185185
return true
186186
}
187-
case STATE_OPERATOR_LOWER_OR_EQUAL:
187+
case LOWER_OR_EQUAL:
188188
if state.Value <= condition.Value {
189189
return true
190190
}
191-
case STATE_OPERATOR_LOWER:
191+
case LOWER:
192192
if state.Value < condition.Value {
193193
return true
194194
}
195-
case STATE_OPERATOR_UPPER_OR_EQUAL:
195+
case UPPER_OR_EQUAL:
196196
if state.Value >= condition.Value {
197197
return true
198198
}
199-
case STATE_OPERATOR_UPPER:
199+
case UPPER:
200200
if state.Value > condition.Value {
201201
return true
202202
}
@@ -224,11 +224,11 @@ func (conditionBool *ConditionBool) Check(states states) bool {
224224
s := states.data[k]
225225
if state, ok := s.(State[bool]); ok {
226226
switch conditionBool.Operator {
227-
case STATE_OPERATOR_EQUAL:
227+
case EQUAL:
228228
if state.Value == conditionBool.Value {
229229
return true
230230
}
231-
case STATE_OPERATOR_NOT_EQUAL:
231+
case NOT_EQUAL:
232232
if state.Value != conditionBool.Value {
233233
return true
234234
}
@@ -258,11 +258,11 @@ func (conditionString *ConditionString) Check(states states) bool {
258258
s := states.data[k]
259259
if state, ok := s.(State[string]); ok {
260260
switch conditionString.Operator {
261-
case STATE_OPERATOR_EQUAL:
261+
case EQUAL:
262262
if state.Value == conditionString.Value {
263263
return true
264264
}
265-
case STATE_OPERATOR_NOT_EQUAL:
265+
case NOT_EQUAL:
266266
if state.Value != conditionString.Value {
267267
return true
268268
}

0 commit comments

Comments
 (0)