/
petriparse.go
358 lines (334 loc) · 8.39 KB
/
petriparse.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package mxgraph
import (
"bytes"
"fmt"
"html"
"io"
"log"
"os"
_ "strconv"
"strings"
)
var logger *log.Logger
type Component map[string]interface{}
type Options map[string]string
func (o Options) toString() string {
s := make([]string, 0, len(o))
for k, v := range o {
s = append(s, k+" = "+v)
}
return strings.Join(s, ", ")
}
type PetriParser struct{}
func (p *PetriParser) ParseXML(data []byte) ([]byte, error) {
logger = log.New(os.Stdout, "[MxGraph] ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
logger.SetOutput(io.Discard)
nodes := make(map[string]MxElement)
arcs := make(map[string]MxElement)
labels := make([]MxElement, 0)
edgelabels := make([]MxElement, 0)
pre := make([]MxElement, 0)
post := make([]MxElement, 0)
if elems, err := GetGraphModel(data); err == nil {
for _, x := range elems {
switch x.getObj() {
case "place", "imm", "exp", "gen":
nodes[x.Id] = x
case "arc", "harc":
arcs[x.Id] = x
case "label":
labels = append(labels, x)
case "edgeLabel":
edgelabels = append(edgelabels, x)
case "pre":
pre = append(pre, x)
case "post":
post = append(post, x)
default:
}
}
// label matching
matchingNode(nodes, labels)
for _, x := range edgelabels {
a, ok := arcs[x.Parent]
if ok {
a.Properties["multi"] = x.Value
}
}
var buf bytes.Buffer
write(&buf, nodes, arcs, pre, post)
return buf.Bytes(), nil
} else {
return nil, err
}
}
func matchingNode(nodes map[string]MxElement, labels []MxElement) {
dists := make(map[string][]float64)
for i, x := range nodes {
dists[i] = make([]float64, len(labels))
for j, y := range labels {
dists[i][j] = dist(x, y)
}
}
// matching
for len(dists) > 0 {
mink := ""
minj := -1
smin := 1.0e+200
for k, x := range dists {
for j, y := range x {
if y < smin {
smin = y
minj = j
mink = k
}
}
}
if smin == 1.0e+200 {
for k, _ := range dists {
nodes[k].Properties["label"] = strings.Replace("id_"+nodes[k].Id, "-", "_", -1)
}
break
}
nodes[mink].Properties["label"] = labels[minj].Value
delete(dists, mink)
for _, x := range dists {
x[minj] = 1.0e+200
}
}
}
func (c *MxElement) getObj() string {
switch c.Type {
case "place", "imm", "exp", "gen", "pre", "post":
return c.Type
case "vertex":
return c.getNode()
case "edge":
return c.getArc()
default:
logger.Printf("Skip object id: %s type: %s", c.Id, c.Type)
return ""
}
}
func (c *MxElement) getNode() string {
s := c.Properties
_, ellipse := s["ellipse"]
color, fillcolor := s["fillColor"]
_, dash := s["dashed"]
_, group := s["group"]
_, text := s["text"]
_, edgeLabel := s["edgeLabel"]
switch {
case group == true:
logger.Printf("mxCell %s is detected as Group", c.Id)
c.Type = "group"
case text == true:
logger.Printf("mxCell %s is detected as Label", c.Id)
c.Type = "label"
case edgeLabel == true:
logger.Printf("mxCell %s is detected as EdgeLabel", c.Id)
c.Type = "edgeLabel"
case ellipse == true && fillcolor == false:
logger.Printf("mxCell %s is detected as Place", c.Id)
c.Type = "place"
case ellipse == false && group == false && dash == true:
logger.Printf("mxCell %s is detected as IMM", c.Id)
c.Type = "imm"
case ellipse == false && group == false && dash == false && fillcolor == true && color == "#000000":
logger.Printf("mxCell %s is detected as GEN", c.Id)
c.Type = "gen"
case ellipse == false && group == false && dash == false && fillcolor == false:
logger.Printf("mxCell %s is detected as EXP", c.Id)
c.Type = "exp"
default:
logger.Println("Cannot detect mxCell as a node: id=", c.Id)
c.Type = ""
}
return c.Type
}
func (c *MxElement) getArc() string {
s := c.Properties
arrow, endarrow := s["endArrow"]
src := s["source"]
dest := s["target"]
switch {
case src != "" && dest != "" && endarrow == true && arrow == "oval":
logger.Printf("mxCell %s is detected as HARC", c.Id)
c.Type = "harc"
case src != "" && dest != "":
logger.Printf("mxCell %s is detected as ARC", c.Id)
c.Type = "arc"
default:
logger.Println("Cannot detect mxCell as an arc: id=", c.Id)
c.Type = ""
}
return c.Type
}
func dist(x MxElement, y MxElement) float64 {
if x.Parent != y.Parent {
return 1.0e+200
} else {
v1 := []float64{x.Geometry.X, x.Geometry.Y, x.Geometry.Width, x.Geometry.Height}
v2 := []float64{y.Geometry.X, y.Geometry.Y, y.Geometry.Width, y.Geometry.Height}
s := 0.0
s += ((v1[0] + v1[2]/2) - (v2[0] + v2[2]/2)) * ((v1[0] + v1[2]/2) - (v2[0] + v2[2]/2))
s += ((v1[1] + v1[3]/2) - (v2[1] + v2[3]/2)) * ((v1[1] + v1[3]/2) - (v2[1] + v2[3]/2))
return s
}
}
func write(w io.Writer, nodes map[string]MxElement, arcs map[string]MxElement, pre, post []MxElement) {
// options
optionlist := map[string][]string{
"place": []string{"init", "max"},
"imm": []string{"weight", "guard", "priority", "vanishable"},
"exp": []string{"rate", "guard", "priority", "vanishable"},
"gen": []string{"dist", "guard", "policy", "priority", "vanishable"},
"arc": []string{"multi"},
"harc": []string{"multi"},
}
fmt.Fprintln(w, "// Begin: This part has been generated automatically from XML file.")
// pre
fmt.Fprintln(w)
for _, x := range pre {
fmt.Fprintln(w, toText(x.Value))
}
// place
fmt.Fprintln(w)
fmt.Fprintln(w, "// place")
for _, x := range nodes {
if x.Type == "place" {
// make label
label := x.Properties["label"]
// make options
options := Options{}
for _, k := range optionlist[x.Type] {
if x.Properties[k] != "" {
options[k] = x.Properties[k]
}
}
if x.Value != "" {
options["init"] = x.Value
}
// write
if len(options) == 0 {
fmt.Fprintf(w, "place %s\n", label)
} else {
fmt.Fprintf(w, "place %s (%s)\n", label, options.toString())
}
}
}
// trans
fmt.Fprintln(w)
fmt.Fprintln(w, "// trans")
for _, x := range nodes {
if x.Type == "imm" {
// make label
label := x.Properties["label"]
// make options
options := Options{}
for _, k := range optionlist[x.Type] {
if x.Properties[k] != "" {
options[k] = x.Properties[k]
}
}
// write
if len(options) == 0 {
fmt.Fprintf(w, "imm %s\n", label)
} else {
fmt.Fprintf(w, "imm %s (%s)\n", label, options.toString())
}
}
}
for _, x := range nodes {
if x.Type == "exp" {
// make label
label := x.Properties["label"]
// make options
options := Options{}
for _, k := range optionlist[x.Type] {
if x.Properties[k] != "" {
options[k] = x.Properties[k]
}
}
// write
if len(options) == 0 {
fmt.Fprintf(w, "exp %s\n", label)
} else {
fmt.Fprintf(w, "exp %s (%s)\n", label, options.toString())
}
}
}
for _, x := range nodes {
if x.Type == "gen" {
// make label
label := x.Properties["label"]
// make options
options := Options{}
for _, k := range optionlist[x.Type] {
if x.Properties[k] != "" {
options[k] = x.Properties[k]
}
}
// write
if len(options) == 0 {
fmt.Fprintf(w, "gen %s\n", label)
} else {
fmt.Fprintf(w, "gen %s (%s)\n", label, options.toString())
}
}
}
// arc
fmt.Fprintln(w)
fmt.Fprintln(w, "// arc")
for _, x := range arcs {
switch {
case x.Type == "arc":
// make labels
src := nodes[x.Properties["source"]].Properties["label"]
dest := nodes[x.Properties["target"]].Properties["label"]
// make options
options := Options{}
for _, k := range optionlist[x.Type] {
if x.Properties[k] != "" {
options[k] = x.Properties[k]
}
}
// write
if len(options) == 0 {
fmt.Fprintf(w, "arc %s to %s\n", src, dest)
} else {
fmt.Fprintf(w, "arc %s to %s (%s)\n", src, dest, options.toString())
}
case x.Type == "harc":
// make labels
src := nodes[x.Properties["source"]].Properties["label"]
dest := nodes[x.Properties["target"]].Properties["label"]
// make options
options := Options{}
for _, k := range optionlist[x.Type] {
if x.Properties[k] != "" {
options[k] = x.Properties[k]
}
}
// write
if len(options) == 0 {
fmt.Fprintf(w, "harc %s to %s\n", src, dest)
} else {
fmt.Fprintf(w, "harc %s to %s (%s)\n", src, dest, options.toString())
}
}
}
// post
fmt.Fprintln(w)
for _, x := range post {
fmt.Fprintln(w, toText(x.Value))
}
// end
fmt.Fprintln(w)
fmt.Fprintln(w, "// End: This part has been generated automatically from XML file.")
}
func toText(x string) string {
a := html.UnescapeString(x)
b := strings.Replace(a, `<br>`, "\n", -1)
return b
}