-
Notifications
You must be signed in to change notification settings - Fork 8
/
boolean.ex
245 lines (191 loc) · 7 KB
/
boolean.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
defmodule RDF.XSD.Boolean do
@moduledoc """
`RDF.XSD.Datatype` for XSD booleans.
"""
@type valid_value :: boolean
@type input_value :: RDF.Literal.t() | valid_value | number | String.t() | any
use RDF.XSD.Datatype.Primitive,
name: "boolean",
id: RDF.Utils.Bootstrapping.xsd_iri("boolean")
alias RDF.XSD
def_applicable_facet XSD.Facets.Pattern
@doc false
def pattern_conform?(pattern, _value, lexical) do
XSD.Facets.Pattern.conform?(pattern, lexical)
end
@impl XSD.Datatype
def lexical_mapping(lexical, _) do
with lexical do
cond do
lexical in ~W[true 1] -> true
lexical in ~W[false 0] -> false
true -> @invalid_value
end
end
end
@impl XSD.Datatype
@spec elixir_mapping(valid_value | integer | any, Keyword.t()) :: value
def elixir_mapping(value, _)
def elixir_mapping(value, _) when is_boolean(value), do: value
def elixir_mapping(1, _), do: true
def elixir_mapping(0, _), do: false
def elixir_mapping(_, _), do: @invalid_value
@impl RDF.Literal.Datatype
def do_cast(value)
def do_cast(%XSD.String{} = xsd_string) do
xsd_string.value |> new() |> canonical()
end
def do_cast(literal) do
cond do
XSD.Decimal.datatype?(literal) ->
!Decimal.equal?(literal.value, 0) |> new()
XSD.Numeric.datatype?(literal) ->
new(literal.value not in [0, 0.0, :nan])
true ->
super(literal)
end
end
@doc """
Returns an Effective Boolean Value (EBV).
The Effective Boolean Value is an algorithm to coerce values to a `RDF.XSD.Boolean`.
It is specified and used in the SPARQL query language and is based upon XPath's
`fn:boolean`. Other than specified in these specs any value which can not be
converted into a boolean results in `nil`.
see
- <https://www.w3.org/TR/xpath-31/#id-ebv>
- <https://www.w3.org/TR/sparql11-query/#ebv>
"""
@spec ebv(input_value) :: RDF.Literal.t() | nil
def ebv(value)
def ebv(%RDF.Literal{literal: literal}), do: ebv(literal)
def ebv(true), do: XSD.Boolean.Value.true()
def ebv(false), do: XSD.Boolean.Value.false()
def ebv(%__MODULE__{value: nil}), do: XSD.Boolean.Value.false()
def ebv(%__MODULE__{} = value), do: literal(value)
def ebv(%XSD.String{} = string) do
if String.length(string.value) == 0,
do: XSD.Boolean.Value.false(),
else: XSD.Boolean.Value.true()
end
def ebv(%datatype{} = literal) do
if XSD.Numeric.datatype?(datatype) do
if datatype.valid?(literal) and
not (datatype.value(literal) == 0 or datatype.value(literal) == :nan),
do: XSD.Boolean.Value.true(),
else: XSD.Boolean.Value.false()
end
end
def ebv(value) when is_binary(value) or is_number(value) do
value |> RDF.Literal.coerce() |> ebv()
end
def ebv(_), do: nil
@doc """
Alias for `ebv/1`.
"""
@spec effective(input_value) :: RDF.Literal.t() | nil
def effective(value), do: ebv(value)
@doc """
Returns `RDF.XSD.true` if the effective boolean value of the given argument is `RDF.XSD.false`, or `RDF.XSD.false` if it is `RDF.XSD.true`.
Otherwise it returns `nil`.
## Examples
iex> RDF.XSD.Boolean.fn_not(RDF.XSD.true)
RDF.XSD.false
iex> RDF.XSD.Boolean.fn_not(RDF.XSD.false)
RDF.XSD.true
iex> RDF.XSD.Boolean.fn_not(true)
RDF.XSD.false
iex> RDF.XSD.Boolean.fn_not(false)
RDF.XSD.true
iex> RDF.XSD.Boolean.fn_not(42)
RDF.XSD.false
iex> RDF.XSD.Boolean.fn_not("")
RDF.XSD.true
iex> RDF.XSD.Boolean.fn_not(nil)
nil
see <https://www.w3.org/TR/xpath-functions/#func-not>
"""
@spec fn_not(input_value) :: t() | nil
def fn_not(value)
def fn_not(%RDF.Literal{literal: literal}), do: fn_not(literal)
def fn_not(value) do
case ebv(value) do
%RDF.Literal{literal: %__MODULE__{value: true}} -> XSD.Boolean.Value.false()
%RDF.Literal{literal: %__MODULE__{value: false}} -> XSD.Boolean.Value.true()
nil -> nil
end
end
@doc """
Returns the logical `AND` of the effective boolean value of the given arguments.
It returns `nil` if only one argument is `nil` and the other argument is
`RDF.XSD.true` and `RDF.XSD.false` if the other argument is `RDF.XSD.false`.
## Examples
iex> RDF.XSD.Boolean.logical_and(RDF.XSD.true, RDF.XSD.true)
RDF.XSD.true
iex> RDF.XSD.Boolean.logical_and(RDF.XSD.true, RDF.XSD.false)
RDF.XSD.false
iex> RDF.XSD.Boolean.logical_and(RDF.XSD.true, nil)
nil
iex> RDF.XSD.Boolean.logical_and(nil, RDF.XSD.false)
RDF.XSD.false
iex> RDF.XSD.Boolean.logical_and(nil, nil)
nil
see <https://www.w3.org/TR/sparql11-query/#func-logical-and>
"""
@spec logical_and(input_value, input_value) :: t() | nil
def logical_and(left, right)
def logical_and(%RDF.Literal{literal: left}, right), do: logical_and(left, right)
def logical_and(left, %RDF.Literal{literal: right}), do: logical_and(left, right)
def logical_and(left, right) do
case ebv(left) do
%RDF.Literal{literal: %__MODULE__{value: false}} ->
XSD.Boolean.Value.false()
%RDF.Literal{literal: %__MODULE__{value: true}} ->
case ebv(right) do
%RDF.Literal{literal: %__MODULE__{value: true}} -> XSD.Boolean.Value.true()
%RDF.Literal{literal: %__MODULE__{value: false}} -> XSD.Boolean.Value.false()
nil -> nil
end
nil ->
if match?(%RDF.Literal{literal: %__MODULE__{value: false}}, ebv(right)) do
XSD.Boolean.Value.false()
end
end
end
@doc """
Returns the logical `OR` of the effective boolean value of the given arguments.
It returns `nil` if only one argument is `nil` and the other argument is
`RDF.XSD.false` and `RDF.XSD.true` if the other argument is `RDF.XSD.true`.
## Examples
iex> RDF.XSD.Boolean.logical_or(RDF.XSD.true, RDF.XSD.false)
RDF.XSD.true
iex> RDF.XSD.Boolean.logical_or(RDF.XSD.false, RDF.XSD.false)
RDF.XSD.false
iex> RDF.XSD.Boolean.logical_or(RDF.XSD.true, nil)
RDF.XSD.true
iex> RDF.XSD.Boolean.logical_or(nil, RDF.XSD.false)
nil
iex> RDF.XSD.Boolean.logical_or(nil, nil)
nil
see <https://www.w3.org/TR/sparql11-query/#func-logical-or>
"""
@spec logical_or(input_value, input_value) :: t() | nil
def logical_or(left, right)
def logical_or(%RDF.Literal{literal: left}, right), do: logical_or(left, right)
def logical_or(left, %RDF.Literal{literal: right}), do: logical_or(left, right)
def logical_or(left, right) do
case ebv(left) do
%RDF.Literal{literal: %__MODULE__{value: true}} ->
XSD.Boolean.Value.true()
%RDF.Literal{literal: %__MODULE__{value: false}} ->
case ebv(right) do
%RDF.Literal{literal: %__MODULE__{value: true}} -> XSD.Boolean.Value.true()
%RDF.Literal{literal: %__MODULE__{value: false}} -> XSD.Boolean.Value.false()
nil -> nil
end
nil ->
if match?(%RDF.Literal{literal: %__MODULE__{value: true}}, ebv(right)) do
XSD.Boolean.Value.true()
end
end
end
end