-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.go
73 lines (63 loc) · 1.4 KB
/
graph.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
package rdf
type Graph struct {
triples []*Triple
}
func NewGraph(ts ...*Triple) *Graph {
return &Graph{triples: ts}
}
func (g *Graph) Add(s, p, o Node) {
t := &Triple{s, p, o}
g.triples = append(g.triples, t)
}
// Find returns the first triple matching the given pattern.
func (g *Graph) Find(s, p, o Node) *Triple {
for t := range g.iter() {
if (s == nil || t.Subject == s) &&
(p == nil || t.Predicate == p) &&
(o == nil || t.Object == o) {
return t
}
}
return nil
}
// FindAll returns all triples matching the given pattern.
func (g *Graph) FindAll(s, p, o Node) []*Triple {
var triples []*Triple
for t := range g.iter() {
if (s == nil || t.Subject == s) &&
(p == nil || t.Predicate == p) &&
(o == nil || t.Object == o) {
triples = append(triples, t)
}
}
return triples
}
func (g *Graph) Remove(t *Triple) {
for i, other := range g.triples {
if t.Equal(other) {
g.triples = append(g.triples[:i], g.triples[i+1:]...)
return
}
}
}
func (g *Graph) iter() chan *Triple {
ch := make(chan *Triple)
go func() {
for _, t := range g.triples {
ch <- t
}
close(ch)
}()
return ch
}
type Triple struct {
Subject Node
Predicate Node
Object Node
}
func NewTriple(s, p, o Node) *Triple {
return &Triple{s, p, o}
}
func (t *Triple) Equal(other *Triple) bool {
return t.Subject.Equal(other.Subject) && t.Predicate.Equal(other.Predicate) && t.Object.Equal(other.Object)
}