/
report.go
252 lines (213 loc) Β· 5.01 KB
/
report.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
package deadlock
import (
"crypto/sha1"
"fmt"
"io"
"sort"
"strings"
"github.com/y-taka-23/ddsv-go/deadlock/rule"
"github.com/y-taka-23/ddsv-go/deadlock/rule/vars"
)
type StateId string
type StateSet map[StateId]State
type LocationSet map[ProcessId]rule.Location
// State represents a state of the system's each moment
// i.e. where processes are and what the value of variables are.
type State interface {
Id() StateId
Locations() LocationSet
SharedVars() vars.Shared
Upstream() TransitionId
}
type state struct {
locations LocationSet
sharedVars vars.Shared
upstream TransitionId
}
// assume that s.Id() is independent from s.upstream
func (s state) Id() StateId {
h := sha1.New()
serial := fmt.Sprintf("%+v%+v", s.locations, s.sharedVars)
h.Write([]byte(serial))
return StateId(fmt.Sprintf("%x", h.Sum(nil)))
}
func (s state) Locations() LocationSet {
return s.locations
}
func (s state) SharedVars() vars.Shared {
return s.sharedVars
}
func (s state) Upstream() TransitionId {
return s.upstream
}
type TransitionId string
type TransitionSet map[TransitionId]Transition
// Transition represents a transition from the Source state to the Target state.
type Transition interface {
Id() TransitionId
Process() ProcessId
Label() rule.Label
Source() StateId
Target() StateId
}
type transition struct {
process ProcessId
label rule.Label
source StateId
target StateId
}
func (t transition) Id() TransitionId {
h := sha1.New()
serial := fmt.Sprintf("%+v", t)
h.Write([]byte(serial))
return TransitionId(fmt.Sprintf("%x", h.Sum(nil)))
}
func (t transition) Process() ProcessId {
return t.process
}
func (t transition) Label() rule.Label {
return t.label
}
func (t transition) Source() StateId {
return t.source
}
func (t transition) Target() StateId {
return t.target
}
// Report contains the result of the state space searching
type Report interface {
Visited() StateSet
Transited() TransitionSet
Initial() StateId
Accepting() StateSet
Deadlocked() StateSet
Traces() TransitionSet
}
type report struct {
visited StateSet
transited TransitionSet
initial StateId
accepting StateSet
deadlocked StateSet
traces TransitionSet
}
func (rp report) Visited() StateSet {
return rp.visited
}
func (rp report) Transited() TransitionSet {
return rp.transited
}
func (rp report) Initial() StateId {
return rp.initial
}
func (rp report) Accepting() StateSet {
return rp.accepting
}
func (rp report) Deadlocked() StateSet {
return rp.deadlocked
}
func (rp report) Traces() TransitionSet {
return rp.traces
}
// Printer outputs reports in Graphviz's dot notation
type Printer struct {
writer io.Writer
}
func NewPrinter(w io.Writer) Printer {
return Printer{writer: w}
}
func (pr Printer) Print(rp Report) (int, error) {
written, err := fmt.Fprintln(pr.writer, "digraph {")
if err != nil {
return written, err
}
for _, s := range rp.Visited() {
n := 0
if s.Id() == rp.Initial() {
n, err = pr.printInitial(s)
} else if _, ok := rp.Accepting()[s.Id()]; ok {
n, err = pr.printAccepting(s)
} else if _, ok := rp.Deadlocked()[s.Id()]; ok {
n, err = pr.printDeadlocked(s)
} else {
n, err = pr.printState(s)
}
written += n
if err != nil {
return written, err
}
}
for _, t := range rp.Transited() {
n := 0
if _, ok := rp.Traces()[t.Id()]; ok {
n, err = pr.printTrace(t)
} else {
n, err = pr.printTransition(t)
}
written += n
if err != nil {
return written, err
}
}
n, err := fmt.Fprintln(pr.writer, "}")
written += n
if err != nil {
return written, err
}
return written, nil
}
func (pr Printer) printState(s State) (int, error) {
return fmt.Fprintf(
pr.writer,
" \"%s\" [label=\"%s\"]\n",
s.Id(), stateLabel(s),
)
}
func (pr Printer) printInitial(s State) (int, error) {
return fmt.Fprintf(
pr.writer,
" \"%s\" [label=\"%s\", fillcolor=\"#AAFFFF\", style=\"solid,filled\"];\n",
s.Id(), stateLabel(s),
)
}
func (pr Printer) printAccepting(s State) (int, error) {
return fmt.Fprintf(
pr.writer,
" \"%s\" [label=\"%s\", peripheries=2];\n",
s.Id(), stateLabel(s),
)
}
func (pr Printer) printDeadlocked(s State) (int, error) {
return fmt.Fprintf(
pr.writer,
" \"%s\" [label=\"%s\", fillcolor=\"#FFAAAA\", style=\"solid,filled\"];\n",
s.Id(), stateLabel(s),
)
}
func (pr Printer) printTransition(t Transition) (int, error) {
return fmt.Fprintf(
pr.writer,
" \"%s\" -> \"%s\" [label=\"%s.%s\"];\n",
t.Source(), t.Target(), t.Process(), t.Label(),
)
}
func (pr Printer) printTrace(t Transition) (int, error) {
return fmt.Fprintf(
pr.writer,
" \"%s\" -> \"%s\" [label=\"%s.%s\", color=\"#FF0000\", fontcolor=\"#FF0000\"];\n",
t.Source(), t.Target(), t.Process(), t.Label(),
)
}
func stateLabel(s State) string {
ss := []string{}
for pid, l := range s.Locations() {
ss = append(ss, fmt.Sprintf("%s @ %s", pid, l))
}
vs := []string{}
for x, n := range s.SharedVars() {
vs = append(vs, fmt.Sprintf("%s = %d", x, n))
}
sort.Strings(ss)
sort.Strings(vs)
return strings.Join(ss, ", ") + "\\n" + strings.Join(vs, ", ")
}