-
Notifications
You must be signed in to change notification settings - Fork 11
/
rdf.go
150 lines (130 loc) · 2.63 KB
/
rdf.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
// Package triplestore provides APIs to manage, store and query triples, sources and RDFGraphs
package triplestore
// Triple consists of a subject, a predicate and a object
type Triple interface {
Subject() string
Predicate() string
Object() Object
Equal(Triple) bool
}
// Object is a resource (i.e. IRI), a literal or a blank node.
type Object interface {
Literal() (Literal, bool)
Resource() (string, bool)
Bnode() (string, bool)
Equal(Object) bool
}
// Literal is a unicode string associated with a datatype (ex: string, integer, ...).
type Literal interface {
Type() XsdType
Value() string
Lang() string
}
type triple struct {
sub, pred string
isSubBnode bool
obj object
triKey string
}
func (t *triple) Object() Object {
return t.obj
}
func (t *triple) Subject() string {
return t.sub
}
func (t *triple) Predicate() string {
return t.pred
}
func (t *triple) key() string {
if t.triKey == "" {
var sub string
if t.isSubBnode {
sub = "_:" + t.sub
} else {
sub = "<" + t.sub + ">"
}
t.triKey = sub + "<" + t.pred + ">" + t.obj.key()
return t.triKey
}
return t.triKey
}
func (t *triple) clone() *triple {
return &triple{
sub: t.sub,
pred: t.pred,
obj: t.obj,
triKey: t.triKey,
}
}
func (t *triple) Equal(other Triple) bool {
switch {
case t == nil:
return other == nil
case other == nil:
return false
default:
otherT, ok := other.(*triple)
if !ok {
return false
}
return t.key() == otherT.key()
}
}
type object struct {
isLit, isBnode bool
resource, bnode string
lit literal
}
func (o object) Literal() (Literal, bool) {
return o.lit, o.isLit
}
func (o object) Resource() (string, bool) {
return o.resource, !o.isLit
}
func (o object) Bnode() (string, bool) {
return o.bnode, o.isBnode
}
func (o object) key() string {
if o.isLit {
if o.lit.langtag != "" {
return "\"" + o.lit.val + "\"@" + o.lit.langtag
}
return "\"" + o.lit.val + "\"^^<" + string(o.lit.typ) + ">"
}
if o.isBnode {
return "_:" + o.bnode
}
return "<" + o.resource + ">"
}
func (o object) Equal(other Object) bool {
lit, ok := o.Literal()
otherLit, otherOk := other.Literal()
if ok != otherOk {
return false
}
if ok {
return lit.Type() == otherLit.Type() && lit.Value() == otherLit.Value()
}
res, ok := o.Resource()
otherRes, otherOk := other.Resource()
if ok != otherOk {
return false
}
if ok {
return res == otherRes
}
return true
}
type literal struct {
typ XsdType
val, langtag string
}
func (l literal) Type() XsdType {
return l.typ
}
func (l literal) Value() string {
return l.val
}
func (l literal) Lang() string {
return l.langtag
}