-
Notifications
You must be signed in to change notification settings - Fork 8
/
term.ex
229 lines (185 loc) · 6.37 KB
/
term.ex
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
defprotocol RDF.Term do
@moduledoc """
Shared behaviour for all RDF terms.
A `RDF.Term` is anything which can be an element of RDF statements of an RDF graph:
- `RDF.IRI`s
- `RDF.BlankNode`s
- `RDF.Literal`s
see <https://www.w3.org/TR/sparql11-query/#defn_RDFTerm>
"""
@type t :: RDF.Resource.t() | RDF.Literal.t()
@doc """
Checks if the given value is an RDF term.
Note: As opposed to `RDF.term?` this function returns `false` on atoms and does
not try to resolve them to IRIs.
## Examples
iex> RDF.Term.term?(RDF.iri("http://example.com/resource"))
true
iex> RDF.Term.term?(EX.Resource)
false
iex> RDF.Term.term?(RDF.bnode)
true
iex> RDF.Term.term?(RDF.XSD.integer(42))
true
iex> RDF.Term.term?(42)
false
"""
def term?(value)
@doc """
Tests for term equality.
see <http://www.w3.org/TR/rdf-sparql-query/#func-sameTerm>
"""
@fallback_to_any true
def equal?(term1, term2)
@doc """
Tests for equality of values.
Non-RDF terms are tried to be coerced via `RDF.Term.coerce/1` before comparison.
Returns `nil` if the given terms are not comparable.
see <http://www.w3.org/TR/rdf-sparql-query/#func-RDFterm-equal>
and the value equality semantics of the different literal datatypes here:
<https://www.w3.org/TR/sparql11-query/#OperatorMapping>
"""
@fallback_to_any true
def equal_value?(term1, term2)
@doc """
Converts a given value into an RDF term.
Returns `nil` if the given value is not convertible into any valid RDF.Term.
## Examples
iex> RDF.Term.coerce("foo")
~L"foo"
iex> RDF.Term.coerce(42)
RDF.XSD.integer(42)
"""
def coerce(value)
@doc """
Returns the native Elixir value of an RDF term.
Returns `nil` if the given value is not a valid RDF term or a value convertible to an RDF term.
## Examples
iex> RDF.Term.value(~I<http://example.com/>)
"http://example.com/"
iex> RDF.Term.value(~L"foo")
"foo"
iex> RDF.XSD.integer(42) |> RDF.Term.value()
42
"""
def value(term)
end
defimpl RDF.Term, for: RDF.IRI do
def equal?(term1, term2), do: term1 == term2
def equal_value?(term1, term2), do: RDF.IRI.equal_value?(term1, term2)
def coerce(term), do: term
def value(term), do: term.value
def term?(_), do: true
end
defimpl RDF.Term, for: RDF.BlankNode do
def equal?(term1, term2), do: term1 == term2
def equal_value?(term1, term2), do: RDF.BlankNode.equal_value?(term1, term2)
def coerce(term), do: term
def value(term), do: to_string(term)
def term?(_), do: true
end
defimpl RDF.Term, for: Reference do
@dialyzer {:nowarn_function, equal_value?: 2}
@dialyzer {:nowarn_function, coerce: 1}
def equal?(term1, term2), do: term1 == term2
def equal_value?(term1, term2), do: RDF.Term.equal_value?(coerce(term1), term2)
def coerce(term), do: RDF.BlankNode.new(term)
def value(term), do: term
def term?(_), do: false
end
defimpl RDF.Term, for: RDF.Literal do
def equal?(term1, term2), do: RDF.Literal.equal?(term1, term2)
def equal_value?(term1, term2), do: RDF.Literal.equal_value?(term1, term2)
def coerce(term), do: term
def value(term), do: RDF.Literal.value(term) || RDF.Literal.lexical(term)
def term?(_), do: true
end
defimpl RDF.Term, for: Atom do
def equal?(term1, term2), do: term1 == term2
def equal_value?(nil, _), do: nil
def equal_value?(term1, term2), do: RDF.Term.equal_value?(coerce(term1), term2)
def coerce(true), do: RDF.XSD.true()
def coerce(false), do: RDF.XSD.false()
def coerce(nil), do: nil
def coerce(term) do
case RDF.Namespace.resolve_term(term) do
{:ok, iri} -> iri
_ -> nil
end
end
def value(true), do: true
def value(false), do: false
def value(nil), do: nil
def value(term), do: RDF.Term.value(coerce(term))
def term?(_), do: false
end
defimpl RDF.Term, for: BitString do
def equal?(term1, term2), do: term1 == term2
def equal_value?(term1, term2), do: RDF.Term.equal_value?(coerce(term1), term2)
def coerce(term), do: RDF.XSD.String.new(term)
def value(term), do: term
def term?(_), do: false
end
defimpl RDF.Term, for: Integer do
def equal?(term1, term2), do: term1 == term2
def equal_value?(term1, term2), do: RDF.Term.equal_value?(coerce(term1), term2)
def coerce(term), do: RDF.XSD.Integer.new(term)
def value(term), do: term
def term?(_), do: false
end
defimpl RDF.Term, for: Float do
def equal?(term1, term2), do: term1 == term2
def equal_value?(term1, term2), do: RDF.Term.equal_value?(coerce(term1), term2)
def coerce(term), do: RDF.XSD.Double.new(term)
def value(term), do: term
def term?(_), do: false
end
defimpl RDF.Term, for: Decimal do
def equal?(term1, term2), do: term1 == term2
def equal_value?(term1, term2), do: RDF.Term.equal_value?(coerce(term1), term2)
def coerce(term), do: RDF.XSD.Decimal.new(term)
def value(term), do: term
def term?(_), do: false
end
defimpl RDF.Term, for: DateTime do
def equal?(term1, term2), do: term1 == term2
def equal_value?(term1, term2), do: RDF.Term.equal_value?(coerce(term1), term2)
def coerce(term), do: RDF.XSD.DateTime.new(term)
def value(term), do: term
def term?(_), do: false
end
defimpl RDF.Term, for: NaiveDateTime do
def equal?(term1, term2), do: term1 == term2
def equal_value?(term1, term2), do: RDF.Term.equal_value?(coerce(term1), term2)
def coerce(term), do: RDF.XSD.DateTime.new(term)
def value(term), do: term
def term?(_), do: false
end
defimpl RDF.Term, for: Date do
def equal?(term1, term2), do: term1 == term2
def equal_value?(term1, term2), do: RDF.Term.equal_value?(coerce(term1), term2)
def coerce(term), do: RDF.XSD.Date.new(term)
def value(term), do: term
def term?(_), do: false
end
defimpl RDF.Term, for: Time do
def equal?(term1, term2), do: term1 == term2
def equal_value?(term1, term2), do: RDF.Term.equal_value?(coerce(term1), term2)
def coerce(term), do: RDF.XSD.Time.new(term)
def value(term), do: term
def term?(_), do: false
end
defimpl RDF.Term, for: URI do
def equal?(term1, term2), do: term1 == term2
def equal_value?(term1, term2), do: RDF.Term.equal_value?(coerce(term1), term2)
def coerce(term), do: RDF.XSD.AnyURI.new(term)
def value(term), do: term
def term?(_), do: false
end
defimpl RDF.Term, for: Any do
def equal?(term1, term2), do: term1 == term2
def equal_value?(_, _), do: nil
def coerce(_), do: nil
def value(_), do: nil
def term?(_), do: false
end