-
Notifications
You must be signed in to change notification settings - Fork 0
/
place.go
133 lines (109 loc) · 2.62 KB
/
place.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
package petri
import "errors"
var _ Object = (*Place)(nil)
var _ Node = (*Place)(nil)
var _ Input = (*PlaceInput)(nil)
var _ Filter = (*PlaceFilter)(nil)
// Place represents a place.
type Place struct {
ID string `json:"_id"`
// Name is the name of the place
Name string `json:"name,omitempty"`
// Bound is the maximum number of tokens that can be in this place
Bound int `json:"bound,omitempty"`
// AcceptedTokens are the tokens that can be accepted by this place
AcceptedTokens []*TokenSchema `json:"acceptedTokens,omitempty"`
}
func (p *Place) PostInit() error {
return nil
}
// NewPlace creates a new place.
func NewPlace(name string, bound int, acceptedTokens ...*TokenSchema) *Place {
return &Place{
ID: ID(),
Name: name,
Bound: bound,
AcceptedTokens: acceptedTokens,
}
}
func acceptedTokenIDs(tokens []*TokenSchema) []*TokenSchema {
ids := make([]*TokenSchema, len(tokens))
for i, token := range tokens {
ids[i] = &TokenSchema{
ID: token.ID,
}
}
return ids
}
func (p *Place) Document() Document {
return Document{
"_id": p.ID,
"name": p.Name,
"bound": p.Bound,
"acceptedTokens": acceptedTokenIDs(p.AcceptedTokens),
}
}
func (p *Place) CanAccept(t *TokenSchema) bool {
for _, token := range p.AcceptedTokens {
if token.Name == t.Name {
return true
}
}
return false
}
func (p *Place) IsNode() {}
func (p *Place) Identifier() string {
return p.ID
}
func (p *Place) String() string {
return p.Name
}
type PlaceInput struct {
Name string
Bound int
AcceptedTokens []*TokenSchema
}
func (p *PlaceInput) Object() Object {
return NewPlace(p.Name, p.Bound, p.AcceptedTokens...)
}
func (p *PlaceInput) Kind() Kind {
return PlaceObject
}
type PlaceMask struct {
Name bool
Bound bool
AcceptedTokens bool
}
var ErrNotFound = errors.New("not found")
func (p *PlaceMask) IsMask() {}
type NodeFilter interface {
Filter
IsNodeFilter()
}
type PlaceFilter struct {
ID *StringSelector `json:"_id,omitempty"`
Name *StringSelector `json:"name,omitempty"`
Bound *IntSelector `json:"bound,omitempty"`
}
func (p *PlaceFilter) IsNodeFilter() {}
type PlaceUpdate struct {
Input *PlaceInput
Mask *PlaceMask
}
func (p *Place) Kind() Kind { return PlaceObject }
func (p *Place) Update(u Update) error {
update, ok := u.(*PlaceUpdate)
if !ok {
return ErrWrongUpdate
}
if update.Mask.Name {
p.Name = update.Input.Name
}
if update.Mask.Bound {
p.Bound = update.Input.Bound
}
return nil
}
func (p *PlaceInput) IsInput() {}
func (p *PlaceUpdate) IsUpdate() {}
func (p *PlaceFilter) IsFilter() {}