forked from veith/petrinet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpetrinet.go
More file actions
206 lines (171 loc) · 5.57 KB
/
petrinet.go
File metadata and controls
206 lines (171 loc) · 5.57 KB
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
// petrinet is a simple petri net execution library
package petrinet
import (
"errors"
"fmt"
"github.com/antonmedv/expr"
"github.com/antonmedv/expr/vm"
"sync"
)
type Net struct {
InputMatrix [][]int `json:"-"` // Input Matrix
OutputMatrix [][]int `json:"-"` // Output Matrix
ConditionMatrix [][]string `json:"-"` // Condition Matrix
State []int `json:"-"` // State
TokenIds [][]int `json:"token-identifier"` // State
Variables map[string]interface{} `json:"variables"` // variablen die mit dem Prozess mitlaufen
EnabledTransitions []int `json:"enabled_transitions"` // list of transitions which can be fired
compiledConditionMatrix [][]*vm.Program `json:"-"` // Precompiled Condition Matrix
}
var tokenID int // token id counter
func (net *Net) Init() {
tokenID = 0
// tokenIDs vergeben
net.TokenIds = make([][]int, len(net.InputMatrix[0]))
// token ids for initial state
for i, tokens := range net.State {
for n := 0; n < tokens; n++ {
net.TokenIds[i] = append(net.TokenIds[i], net.nextTokenID())
}
}
// precompile conditions
if len(net.ConditionMatrix) > 0 {
net.compiledConditionMatrix = make([][]*vm.Program, len(net.ConditionMatrix))
for transitionIndex, _ := range net.ConditionMatrix {
net.compiledConditionMatrix[transitionIndex] = make([]*vm.Program, len(net.ConditionMatrix[transitionIndex]))
for i, condition := range net.ConditionMatrix[transitionIndex] {
prog, err := expr.Compile(condition, expr.Env(net.Variables))
if err != nil {
panic(err)
}
net.compiledConditionMatrix[transitionIndex][i] = prog
}
}
}
net.EnabledTransitions = net.evaluateNextPossibleTransitions()
}
func (net *Net) nextTokenID() int {
tokenID++
return tokenID
}
func (net *Net) FireWithTokenId(transition int, tokenID int) error {
for p, _ := range net.InputMatrix[transition] {
if len(net.TokenIds[p]) > 0 { // nur wenn es elemente hat prüfen
if net.TokenIds[p][0] != tokenID {
for index, tokenIDVal := range net.TokenIds[p] {
// tokenID finden und TokenID an erste stelle setzen
if tokenIDVal == tokenID {
a := net.TokenIds[p][index]
b := net.TokenIds[p][0]
net.TokenIds[p][index] = b
net.TokenIds[p][0] = a
return net.Fire(transition)
}
}
} else {
return net.Fire(transition)
}
}
}
return errors.New("TokenID not found")
}
// fires an enabled transition.
func (f *Net) Fire(transition int) error {
var err error
var mutex = &sync.Mutex{}
if f.TransitionEnabled(transition) {
mutex.Lock()
f.EnabledTransitions = f.fastfire(transition)
mutex.Unlock()
return err
} else {
err = errors.New(fmt.Sprintf("Transition %v not enabled", transition))
return err
}
}
func (net *Net) TransitionEnabled(t int) bool {
for _, b := range net.EnabledTransitions {
if b == t {
return true
}
}
return false
}
// prüfe ob Transition ungeachtet der arc bedingungen gefeuert werden könnte
// prüft auch, ob genügend tokens vorhanden sind.
func (net *Net) fastCheck(transition int) bool {
for place, p := range net.InputMatrix[transition] {
if p != 0 && net.State[place]-p < 0 {
return false
}
}
return true
}
// finde die möglichen nächsten Transitionen und löse automatische Transitionen direkt aus
// Timed Transitionen werden gestartet
func (net *Net) evaluateNextPossibleTransitions() []int {
var possibleTransitions []int
// mögliche transitionen finden
for t := 0; t < len(net.InputMatrix); t++ {
if net.fastCheck(t) {
possibleTransitions = append(possibleTransitions, t)
}
}
var lockedTransitions []int
// conditions prüfen
for t := 0; t < len(possibleTransitions); t++ {
// sobald eine Bedingung auf einem arc zu einer Transition nicht erfüllt ist, ist die Transition nicht mehr feuerbar
// conditionen sind als string eingetragen
if !net.proveConditions(possibleTransitions[t]) {
lockedTransitions = append(lockedTransitions, t)
possibleTransitions = removeFromIntFromArray(possibleTransitions, t)
}
}
return possibleTransitions
}
func removeFromIntFromArray(l []int, item int) []int {
for i, other := range l {
if other == item {
return append(l[:i], l[i+1:]...)
}
}
return l
}
// Prüft eine Transitionsbedingung
func (net *Net) proveConditions(transitionIndex int) bool {
if len(net.ConditionMatrix) > 0 {
for _, prog := range net.compiledConditionMatrix[transitionIndex] {
result, err := expr.Run(prog, net.Variables)
if err != nil || !result.(bool) {
return false
}
}
}
return true
}
func (net *Net) UpdateVariable(name string, value interface{}) {
net.Variables[name] = value
// conditions are not the same, maybe a Transition is now possible
net.EnabledTransitions = net.evaluateNextPossibleTransitions()
}
// fire ohne Check
func (net *Net) fastfire(transition int) []int {
for place, step := range net.InputMatrix[transition] {
net.State[place] = net.State[place] - step
// id tokens entfernen (an erster stelle)
for n := 0; n < step; n++ {
// pop
net.TokenIds[place] = net.TokenIds[place][1:]
}
}
for place, step := range net.OutputMatrix[transition] {
net.State[place] = net.State[place] + step
// id tokens erzeugen (an letzter stelle)
for n := 0; n < step; n++ {
// push
nextID := net.nextTokenID()
net.TokenIds[place] = append(net.TokenIds[place], nextID)
}
}
return net.evaluateNextPossibleTransitions()
}