-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.go
296 lines (248 loc) · 6.62 KB
/
entity.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package acmelib
import (
"fmt"
"slices"
"strings"
"time"
"github.com/jaevor/go-nanoid"
)
// EntityKind is the kind of an entity.
type EntityKind int
const (
// EntityKindNetwork represents a [Network] entity.
EntityKindNetwork EntityKind = iota
// EntityKindBus represents a [Bus] entity.
EntityKindBus
// EntityKindNode represents a [Node] entity.
EntityKindNode
// EntityKindMessage represents a [Message] entity.
EntityKindMessage
// EntityKindSignal represents a [Signal] entity.
EntityKindSignal
// EntityKindSignalType represents a [SignalType] entity.
EntityKindSignalType
// EntityKindSignalUnit represents a [SignalUnit] entity.
EntityKindSignalUnit
// EntityKindSignalEnum represents a [SignalEnum] entity.
EntityKindSignalEnum
// EntityKindSignalEnumValue represents a [SignalEnumValue] entity.
EntityKindSignalEnumValue
// EntityKindAttribute represents a [Attribute] entity.
EntityKindAttribute
// EntityKindCANIDBuilder represents a [CANIDBuilder] entity.
EntityKindCANIDBuilder
)
func (ek EntityKind) String() string {
switch ek {
case EntityKindNetwork:
return "network"
case EntityKindBus:
return "bus"
case EntityKindNode:
return "node"
case EntityKindMessage:
return "message"
case EntityKindSignal:
return "signal"
case EntityKindSignalType:
return "signal-type"
case EntityKindSignalUnit:
return "signal-unit"
case EntityKindSignalEnum:
return "signal-enum"
case EntityKindSignalEnumValue:
return "signal-enum-value"
case EntityKindAttribute:
return "attribute"
case EntityKindCANIDBuilder:
return "canid-builder"
default:
return "unknown"
}
}
// EntityID is the unique identifier of an entity.
type EntityID string
func newEntityID() EntityID {
gen, err := nanoid.Standard(21)
if err != nil {
panic(err)
}
return EntityID(gen())
}
func (id EntityID) String() string {
return string(id)
}
type entity struct {
entityID EntityID
entityKind EntityKind
name string
desc string
createTime time.Time
}
func newEntity(name string, kind EntityKind) *entity {
id := newEntityID()
createTime := time.Now()
return &entity{
entityID: id,
entityKind: kind,
name: name,
desc: "",
createTime: createTime,
}
}
// EntityID returns the unique identifier of the entity.
func (e *entity) EntityID() EntityID {
return e.entityID
}
// EntityKind returns the kind of the entity.
func (e *entity) EntityKind() EntityKind {
return e.entityKind
}
// Name returns the name of the entity.
func (e *entity) Name() string {
return e.name
}
// Desc returns the description of the entity.
func (e *entity) Desc() string {
return e.desc
}
// CreateTime returns the time when the entity was created.
func (e *entity) CreateTime() time.Time {
return e.createTime
}
// SetDesc sets the description of the entity.
func (e *entity) SetDesc(desc string) {
e.desc = desc
}
func (e *entity) stringify(b *strings.Builder, tabs int) {
tabStr := getTabString(tabs)
b.WriteString(fmt.Sprintf("%sentity_id: %s; entity_kind: %s\n", tabStr, e.entityID, e.entityKind))
b.WriteString(fmt.Sprintf("%sname: %s\n", tabStr, e.name))
if len(e.desc) > 0 {
b.WriteString(fmt.Sprintf("%sdesc: %s\n", tabStr, e.desc))
}
b.WriteString(fmt.Sprintf("%screate_time: %s\n", tabStr, e.createTime.Format(time.RFC3339)))
}
func (e *entity) clone() *entity {
return &entity{
entityID: newEntityID(),
entityKind: e.entityKind,
name: e.name,
desc: e.desc,
createTime: e.createTime,
}
}
type withAttributes struct {
attAssignments *set[EntityID, *AttributeAssignment]
}
func newWithAttributes() *withAttributes {
return &withAttributes{
attAssignments: newSet[EntityID, *AttributeAssignment](),
}
}
func (wa *withAttributes) addAttributeAssignment(attribute Attribute, ent AttributableEntity, val any) error {
if attribute == nil {
return &ArgumentError{
Name: "attribute",
Err: ErrIsNil,
}
}
switch v := val.(type) {
case int:
if attribute.Type() != AttributeTypeInteger {
return &AttributeValueError{Err: ErrInvalidType}
}
intAtt, err := attribute.ToInteger()
if err != nil {
panic(err)
}
if v < intAtt.min || v > intAtt.max {
return &AttributeValueError{Err: ErrOutOfBounds}
}
case float64:
if attribute.Type() != AttributeTypeFloat {
return &AttributeValueError{Err: ErrInvalidType}
}
floatAtt, err := attribute.ToFloat()
if err != nil {
panic(err)
}
if v < floatAtt.min || v > floatAtt.max {
return &AttributeValueError{Err: ErrOutOfBounds}
}
case string:
switch attribute.Type() {
case AttributeTypeString:
case AttributeTypeEnum:
enumAtt, err := attribute.ToEnum()
if err != nil {
panic(err)
}
if !enumAtt.values.hasKey(v) {
return &AttributeValueError{Err: ErrNotFound}
}
default:
return &AttributeValueError{Err: ErrInvalidType}
}
default:
return &AttributeValueError{Err: ErrInvalidType}
}
attAss := newAttributeAssignment(attribute, ent, val)
wa.attAssignments.add(attribute.EntityID(), attAss)
attribute.addRef(attAss)
return nil
}
func (wa *withAttributes) removeAttributeAssignment(attEntID EntityID) error {
attAss, err := wa.attAssignments.getValue(attEntID)
if err != nil {
return err
}
wa.attAssignments.remove(attEntID)
attAss.attribute.removeRef(attAss.EntityID())
return nil
}
// RemoveAllAttributeAssignments removes all the attribute assignments from the entity.
func (wa *withAttributes) RemoveAllAttributeAssignments() {
for _, attVal := range wa.attAssignments.entries() {
attVal.attribute.removeRef(attVal.EntityID())
}
wa.attAssignments.clear()
}
// AttributeAssignments returns a slice of all attribute assignments of the entity.
func (wa *withAttributes) AttributeAssignments() []*AttributeAssignment {
attSlice := wa.attAssignments.getValues()
slices.SortFunc(attSlice, func(a, b *AttributeAssignment) int {
return strings.Compare(a.attribute.Name(), b.attribute.Name())
})
return attSlice
}
func (wa *withAttributes) getAttributeAssignment(attributeEntityID EntityID) (*AttributeAssignment, error) {
attVal, err := wa.attAssignments.getValue(attributeEntityID)
if err != nil {
return nil, err
}
return attVal, nil
}
type referenceableEntity interface {
EntityID() EntityID
}
type withRefs[R referenceableEntity] struct {
refs *set[EntityID, R]
}
func newWithRefs[R referenceableEntity]() *withRefs[R] {
return &withRefs[R]{
refs: newSet[EntityID, R](),
}
}
func (t *withRefs[R]) addRef(ref R) {
t.refs.add(ref.EntityID(), ref)
}
func (t *withRefs[R]) removeRef(refID EntityID) {
t.refs.remove(refID)
}
func (t *withRefs[R]) ReferenceCount() int {
return t.refs.size()
}
func (t *withRefs[R]) References() []R {
return t.refs.getValues()
}