forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gml.go
101 lines (88 loc) · 1.76 KB
/
gml.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
// Package gml provides an encoder for Graph Modeling Format
package gml
import (
"fmt"
"io"
"strings"
"github.com/cayleygraph/cayley/quad"
)
func init() {
quad.RegisterFormat(quad.Format{
Name: "gml",
Ext: []string{".gml"},
Writer: func(w io.Writer) quad.WriteCloser { return NewWriter(w) },
})
}
func NewWriter(w io.Writer) *Writer {
return &Writer{w: w}
}
type Writer struct {
w io.Writer
written bool
err error
nodes map[string]int
cur int
}
func (w *Writer) writeNode(s string) int {
if w.err != nil {
return -1
}
i, ok := w.nodes[s]
if ok {
return i
}
i = w.cur
w.cur++
w.nodes[s] = i
_, w.err = fmt.Fprintf(w.w, "\tnode [ id %d label %s ]\n", i, escape(s))
if w.err != nil {
return -1
}
return i
}
var escaper = strings.NewReplacer( // TODO: ISO 8859-1?
`&`, `&`,
`"`, `"`,
// `<`,`<`,
// `>`, `>`,
)
func escape(s string) string {
return `"` + escaper.Replace(s) + `"`
}
func (w *Writer) WriteQuad(q quad.Quad) error {
if w.err != nil {
return w.err
}
if !w.written {
if _, err := w.w.Write([]byte(header)); err != nil {
return err
}
w.written = true
w.nodes = make(map[string]int)
}
s := w.writeNode(q.Subject.String())
o := w.writeNode(q.Object.String())
if w.err != nil {
return w.err
}
_, w.err = fmt.Fprintf(w.w, "\tedge [ source %d target %d label %s ]\n",
s, o, escape(q.Predicate.String()))
return w.err
}
func (w *Writer) Close() error {
if w.err != nil {
return w.err
}
if !w.written {
if _, w.err = w.w.Write([]byte(header)); w.err != nil {
return w.err
}
}
if _, w.err = w.w.Write([]byte(footer)); w.err != nil {
return w.err
}
w.err = fmt.Errorf("closed")
return nil
}
const header = "Creator \"Cayley\"\ngraph [ directed 1\n"
const footer = "]\n"