2016-10-15 16:26:56 +00:00
|
|
|
defmodule RDF.Literal do
|
|
|
|
@moduledoc """
|
|
|
|
RDF literals are leaf nodes of a RDF graph containing raw data, like strings and numbers.
|
|
|
|
"""
|
2017-06-09 22:18:39 +00:00
|
|
|
|
2017-04-23 21:41:29 +00:00
|
|
|
defstruct [:value, :uncanonical_lexical, :datatype, :language]
|
2016-10-15 16:26:56 +00:00
|
|
|
|
|
|
|
@type t :: module
|
|
|
|
|
2017-04-16 21:13:39 +00:00
|
|
|
alias RDF.Datatype.NS.XSD
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2018-09-07 19:42:38 +00:00
|
|
|
# to be able to pattern-match on plain types; we can't use RDF.Literal.Guards here since these aren't compiled here yet
|
2017-04-20 21:09:55 +00:00
|
|
|
@xsd_string XSD.string
|
2017-08-20 20:35:14 +00:00
|
|
|
@lang_string RDF.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString")
|
2017-04-20 21:09:55 +00:00
|
|
|
@plain_types [@xsd_string, @lang_string]
|
|
|
|
|
2016-10-15 16:26:56 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a new `RDF.Literal` of the given value and tries to infer an appropriate XSD datatype.
|
|
|
|
|
|
|
|
Note: The `RDF.literal` function is a shortcut to this function.
|
|
|
|
|
|
|
|
The following mapping of Elixir types to XSD datatypes is applied:
|
|
|
|
|
2017-06-09 22:18:39 +00:00
|
|
|
| Elixir datatype | XSD datatype |
|
|
|
|
| :-------------- | :------------- |
|
|
|
|
| `string` | `xsd:string` |
|
|
|
|
| `boolean` | `xsd:boolean` |
|
|
|
|
| `integer` | `xsd:integer` |
|
|
|
|
| `float` | `xsd:double` |
|
|
|
|
| `Time` | `xsd:time` |
|
|
|
|
| `Date` | `xsd:date` |
|
|
|
|
| `DateTime` | `xsd:dateTime` |
|
|
|
|
| `NaiveDateTime` | `xsd:dateTime` |
|
2016-10-15 16:26:56 +00:00
|
|
|
|
|
|
|
|
2017-06-16 22:27:05 +00:00
|
|
|
## Examples
|
2016-10-15 16:26:56 +00:00
|
|
|
|
|
|
|
iex> RDF.Literal.new(42)
|
2017-04-23 21:41:29 +00:00
|
|
|
%RDF.Literal{value: 42, datatype: XSD.integer}
|
2016-10-15 16:26:56 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
def new(value)
|
|
|
|
|
2017-06-16 22:44:11 +00:00
|
|
|
def new(%RDF.Literal{} = literal), do: literal
|
|
|
|
|
2017-04-16 21:13:39 +00:00
|
|
|
def new(value) when is_binary(value), do: RDF.String.new(value)
|
|
|
|
def new(value) when is_boolean(value), do: RDF.Boolean.new(value)
|
|
|
|
def new(value) when is_integer(value), do: RDF.Integer.new(value)
|
|
|
|
def new(value) when is_float(value), do: RDF.Double.new(value)
|
|
|
|
|
2017-05-01 16:06:53 +00:00
|
|
|
def new(%Date{} = value), do: RDF.Date.new(value)
|
2017-05-01 14:19:03 +00:00
|
|
|
def new(%Time{} = value), do: RDF.Time.new(value)
|
2017-04-30 12:38:26 +00:00
|
|
|
def new(%DateTime{} = value), do: RDF.DateTime.new(value)
|
|
|
|
def new(%NaiveDateTime{} = value), do: RDF.DateTime.new(value)
|
|
|
|
|
2016-10-15 16:26:56 +00:00
|
|
|
|
|
|
|
def new(value) do
|
2017-08-20 20:35:14 +00:00
|
|
|
raise RDF.Literal.InvalidError, "#{inspect value} not convertible to a RDF.Literal"
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2018-03-14 10:46:11 +00:00
|
|
|
@doc """
|
|
|
|
Creates a new `RDF.Literal` with the given datatype or language tag.
|
|
|
|
"""
|
|
|
|
def new(value, opts)
|
|
|
|
|
2017-04-02 21:15:07 +00:00
|
|
|
def new(value, opts) when is_list(opts),
|
|
|
|
do: new(value, Map.new(opts))
|
|
|
|
|
2018-08-26 03:46:18 +00:00
|
|
|
def new(value, %{language: nil} = opts),
|
|
|
|
do: new(value, Map.delete(opts, :language))
|
|
|
|
|
2018-08-26 22:31:02 +00:00
|
|
|
def new(value, %{language: _} = opts) do
|
2017-04-23 21:41:29 +00:00
|
|
|
if is_binary(value) do
|
2017-06-10 21:08:49 +00:00
|
|
|
if opts[:datatype] in [nil, @lang_string] do
|
2017-04-23 21:41:29 +00:00
|
|
|
RDF.LangString.new(value, opts)
|
|
|
|
else
|
|
|
|
raise ArgumentError, "datatype with language must be rdf:langString"
|
|
|
|
end
|
2017-04-16 21:13:39 +00:00
|
|
|
else
|
2017-04-23 21:41:29 +00:00
|
|
|
new(value, Map.delete(opts, :language)) # Should we raise a warning?
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
2016-10-30 18:36:46 +00:00
|
|
|
end
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
def new(value, %{datatype: %RDF.IRI{} = id} = opts) do
|
2017-04-20 21:09:55 +00:00
|
|
|
case RDF.Datatype.get(id) do
|
2017-04-23 21:41:29 +00:00
|
|
|
nil -> %RDF.Literal{value: value, datatype: id}
|
|
|
|
datatype -> datatype.new(value, opts)
|
2016-10-30 18:36:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-16 21:13:39 +00:00
|
|
|
def new(value, %{datatype: datatype} = opts),
|
2017-08-20 20:35:14 +00:00
|
|
|
do: new(value, %{opts | datatype: RDF.iri(datatype)})
|
2017-04-16 21:13:39 +00:00
|
|
|
|
|
|
|
def new(value, opts) when is_map(opts) and map_size(opts) == 0,
|
|
|
|
do: new(value)
|
|
|
|
|
|
|
|
|
2018-03-14 10:46:11 +00:00
|
|
|
@doc """
|
|
|
|
Creates a new `RDF.Literal`, but fails if it's not valid.
|
|
|
|
|
|
|
|
Note: Validation is only possible if an `RDF.Datatype` with an implementation of
|
|
|
|
`RDF.Datatype.valid?/1` exists.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> RDF.Literal.new!("3.14", datatype: XSD.double) == RDF.Literal.new("3.14", datatype: XSD.double)
|
|
|
|
true
|
|
|
|
|
|
|
|
iex> RDF.Literal.new!("invalid", datatype: "http://example/unkown_datatype") == RDF.Literal.new("invalid", datatype: "http://example/unkown_datatype")
|
|
|
|
true
|
|
|
|
|
|
|
|
iex> RDF.Literal.new!("foo", datatype: XSD.integer)
|
|
|
|
** (RDF.Literal.InvalidError) invalid RDF.Literal: %RDF.Literal{value: nil, lexical: "foo", datatype: ~I<http://www.w3.org/2001/XMLSchema#integer>}
|
|
|
|
|
|
|
|
iex> RDF.Literal.new!("foo", datatype: RDF.langString)
|
|
|
|
** (RDF.Literal.InvalidError) invalid RDF.Literal: %RDF.Literal{value: "foo", datatype: ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>, language: nil}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def new!(value, opts \\ %{}) do
|
|
|
|
with %RDF.Literal{} = literal <- new(value, opts) do
|
|
|
|
if valid?(literal) do
|
|
|
|
literal
|
|
|
|
else
|
|
|
|
raise RDF.Literal.InvalidError, "invalid RDF.Literal: #{inspect literal}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
invalid ->
|
|
|
|
raise RDF.Literal.InvalidError, "invalid result of RDF.Literal.new: #{inspect invalid}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-09 22:18:39 +00:00
|
|
|
@doc """
|
2018-06-08 10:26:52 +00:00
|
|
|
Returns the lexical representation of the given literal according to its datatype.
|
2017-06-09 22:18:39 +00:00
|
|
|
"""
|
2017-04-23 21:41:29 +00:00
|
|
|
def lexical(%RDF.Literal{value: value, uncanonical_lexical: nil, datatype: id} = literal) do
|
|
|
|
case RDF.Datatype.get(id) do
|
|
|
|
nil -> to_string(value)
|
|
|
|
datatype -> datatype.lexical(literal)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def lexical(%RDF.Literal{uncanonical_lexical: lexical}), do: lexical
|
|
|
|
|
2017-06-09 22:18:39 +00:00
|
|
|
@doc """
|
|
|
|
Returns the given literal in its canonical lexical representation.
|
|
|
|
"""
|
2017-04-23 21:41:29 +00:00
|
|
|
def canonical(%RDF.Literal{uncanonical_lexical: nil} = literal), do: literal
|
|
|
|
def canonical(%RDF.Literal{datatype: id} = literal) do
|
|
|
|
case RDF.Datatype.get(id) do
|
|
|
|
nil -> literal
|
|
|
|
datatype -> datatype.canonical(literal)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-06-09 22:18:39 +00:00
|
|
|
@doc """
|
|
|
|
Returns if the given literal is in its canonical lexical representation.
|
|
|
|
"""
|
2017-04-23 21:41:29 +00:00
|
|
|
def canonical?(%RDF.Literal{uncanonical_lexical: nil}), do: true
|
|
|
|
def canonical?(_), do: false
|
|
|
|
|
|
|
|
|
2017-06-09 22:18:39 +00:00
|
|
|
@doc """
|
|
|
|
Returns if the value of the given literal is a valid according to its datatype.
|
|
|
|
"""
|
2017-04-23 21:41:29 +00:00
|
|
|
def valid?(%RDF.Literal{datatype: id} = literal) do
|
|
|
|
case RDF.Datatype.get(id) do
|
|
|
|
nil -> true
|
|
|
|
datatype -> datatype.valid?(literal)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-04-16 21:13:39 +00:00
|
|
|
@doc """
|
2017-06-09 22:18:39 +00:00
|
|
|
Returns if a literal is a simple literal.
|
2017-04-16 21:13:39 +00:00
|
|
|
|
|
|
|
A simple literal has no datatype or language.
|
|
|
|
|
|
|
|
see <http://www.w3.org/TR/sparql11-query/#simple_literal>
|
|
|
|
"""
|
2017-04-20 21:09:55 +00:00
|
|
|
def simple?(%RDF.Literal{datatype: @xsd_string}), do: true
|
2017-04-26 00:48:49 +00:00
|
|
|
def simple?(_), do: false
|
2016-10-30 18:36:46 +00:00
|
|
|
|
2017-04-23 21:41:29 +00:00
|
|
|
|
2017-04-16 21:13:39 +00:00
|
|
|
@doc """
|
2017-06-09 22:18:39 +00:00
|
|
|
Returns if a literal is a language-tagged literal.
|
2017-04-16 21:13:39 +00:00
|
|
|
|
|
|
|
see <http://www.w3.org/TR/rdf-concepts/#dfn-plain-literal>
|
|
|
|
"""
|
2017-04-20 21:09:55 +00:00
|
|
|
def has_language?(%RDF.Literal{datatype: @lang_string}), do: true
|
|
|
|
def has_language?(_), do: false
|
2016-10-30 18:36:46 +00:00
|
|
|
|
2017-04-23 21:41:29 +00:00
|
|
|
|
2017-04-16 21:13:39 +00:00
|
|
|
@doc """
|
2017-06-09 22:18:39 +00:00
|
|
|
Returns if a literal is a datatyped literal.
|
2017-04-16 21:13:39 +00:00
|
|
|
|
|
|
|
For historical reasons, this excludes `xsd:string` and `rdf:langString`.
|
|
|
|
|
|
|
|
see <http://www.w3.org/TR/rdf-concepts/#dfn-typed-literal>
|
|
|
|
"""
|
|
|
|
def has_datatype?(literal) do
|
|
|
|
not plain?(literal) and not has_language?(literal)
|
2017-04-02 21:15:07 +00:00
|
|
|
end
|
|
|
|
|
2017-04-23 21:41:29 +00:00
|
|
|
|
2017-04-16 21:13:39 +00:00
|
|
|
@doc """
|
2017-06-09 22:18:39 +00:00
|
|
|
Returns if a literal is a plain literal.
|
2017-04-16 21:13:39 +00:00
|
|
|
|
|
|
|
A plain literal may have a language, but may not have a datatype.
|
|
|
|
For all practical purposes, this includes `xsd:string` literals too.
|
|
|
|
|
|
|
|
see <http://www.w3.org/TR/rdf-concepts/#dfn-plain-literal>
|
|
|
|
"""
|
2017-04-20 21:09:55 +00:00
|
|
|
def plain?(%RDF.Literal{datatype: datatype})
|
|
|
|
when datatype in @plain_types, do: true
|
|
|
|
def plain?(_), do: false
|
2017-04-16 21:13:39 +00:00
|
|
|
|
2017-04-20 21:09:55 +00:00
|
|
|
def typed?(literal), do: not plain?(literal)
|
|
|
|
|
2018-06-08 10:26:52 +00:00
|
|
|
|
|
|
|
@doc """
|
2018-11-02 21:00:48 +00:00
|
|
|
Checks if two `RDF.Literal`s are equal.
|
2018-06-08 10:26:52 +00:00
|
|
|
|
2018-09-16 02:02:53 +00:00
|
|
|
Non-RDF terms are tried to be coerced via `RDF.Term.coerce/1` before comparison.
|
|
|
|
|
2018-06-08 10:26:52 +00:00
|
|
|
Returns `nil` when the given arguments are not comparable as Literals.
|
|
|
|
|
|
|
|
see <https://www.w3.org/TR/rdf-concepts/#section-Literal-Equality>
|
|
|
|
"""
|
|
|
|
def equal_value?(left, right)
|
|
|
|
|
|
|
|
def equal_value?(%RDF.Literal{datatype: id1} = literal1, %RDF.Literal{datatype: id2} = literal2) do
|
|
|
|
case RDF.Datatype.get(id1) do
|
|
|
|
nil ->
|
|
|
|
if id1 == id2 do
|
|
|
|
literal1.value == literal2.value
|
|
|
|
end
|
|
|
|
datatype ->
|
|
|
|
datatype.equal_value?(literal1, literal2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Handle AnyURI in its own RDF.Datatype implementation
|
|
|
|
@xsd_any_uri "http://www.w3.org/2001/XMLSchema#anyURI"
|
|
|
|
|
|
|
|
def equal_value?(%RDF.Literal{datatype: %RDF.IRI{value: @xsd_any_uri}} = left, right),
|
|
|
|
do: RDF.IRI.equal_value?(left, right)
|
|
|
|
|
|
|
|
def equal_value?(left, %RDF.Literal{datatype: %RDF.IRI{value: @xsd_any_uri}} = right),
|
|
|
|
do: RDF.IRI.equal_value?(left, right)
|
|
|
|
|
2018-09-16 02:02:53 +00:00
|
|
|
def equal_value?(%RDF.Literal{} = left, right) when not is_nil(right) do
|
2018-09-16 20:21:53 +00:00
|
|
|
unless RDF.Term.term?(right) do
|
2018-09-16 02:02:53 +00:00
|
|
|
equal_value?(left, RDF.Term.coerce(right))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-08 10:26:52 +00:00
|
|
|
def equal_value?(_, _), do: nil
|
|
|
|
|
2018-11-02 21:00:48 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Checks if the first of two `RDF.Literal`s is smaller then the other.
|
|
|
|
|
|
|
|
Returns `nil` when the given arguments are not comparable datatypes.
|
|
|
|
|
|
|
|
"""
|
|
|
|
def less_than?(literal1, literal2) do
|
|
|
|
case compare(literal1, literal2) do
|
|
|
|
:lt -> true
|
|
|
|
nil -> nil
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Checks if the first of two `RDF.Literal`s is greater then the other.
|
|
|
|
|
|
|
|
Returns `nil` when the given arguments are not comparable datatypes.
|
|
|
|
|
|
|
|
"""
|
|
|
|
def greater_than?(literal1, literal2) do
|
|
|
|
case compare(literal1, literal2) do
|
|
|
|
:gt -> true
|
|
|
|
nil -> nil
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Compares two `RDF.Literal`s.
|
|
|
|
|
|
|
|
Returns `:gt` if first literal is greater than the second in terms of their datatype
|
|
|
|
and `:lt` for vice versa. If the two literals are equal `:eq` is returned.
|
2018-11-04 14:54:49 +00:00
|
|
|
For datatypes with only partial ordering `:indeterminate` is returned when the
|
|
|
|
order of the given literals is not defined.
|
2018-11-02 21:00:48 +00:00
|
|
|
|
|
|
|
Returns `nil` when the given arguments are not comparable datatypes.
|
|
|
|
|
|
|
|
"""
|
|
|
|
def compare(left, right)
|
|
|
|
|
|
|
|
def compare(%RDF.Literal{datatype: id1} = literal1, %RDF.Literal{datatype: id2} = literal2) do
|
|
|
|
case RDF.Datatype.get(id1) do
|
|
|
|
nil ->
|
|
|
|
if id1 == id2 do
|
|
|
|
cond do
|
|
|
|
literal1.value == literal2.value -> :eq
|
|
|
|
literal1.value < literal2.value -> :lt
|
|
|
|
true -> :gt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
datatype ->
|
|
|
|
datatype.compare(literal1, literal2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def compare(_, _), do: nil
|
|
|
|
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defimpl String.Chars, for: RDF.Literal do
|
2017-04-23 21:41:29 +00:00
|
|
|
def to_string(literal) do
|
|
|
|
RDF.Literal.lexical(literal)
|
2017-04-20 21:09:55 +00:00
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|