2018-06-08 10:26:52 +00:00
|
|
|
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 a RDF graph:
|
|
|
|
|
|
|
|
- `RDF.IRI`s
|
|
|
|
- `RDF.BlankNode`s
|
|
|
|
- `RDF.Literal`s
|
|
|
|
|
|
|
|
see <https://www.w3.org/TR/sparql11-query/#defn_RDFTerm>
|
|
|
|
"""
|
|
|
|
|
2018-09-02 03:25:20 +00:00
|
|
|
@type t :: RDF.IRI.t | RDF.BlankNode.t | RDF.Literal.t
|
|
|
|
|
|
|
|
|
2018-09-16 20:21:53 +00:00
|
|
|
@doc """
|
|
|
|
Checks if the given value is a RDF term.
|
|
|
|
|
|
|
|
Note: As opposed to `RDF.term?` this function returns false on atoms and does
|
|
|
|
not try resolves those 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
|
2020-05-05 21:58:44 +00:00
|
|
|
iex> RDF.Term.term?(RDF.XSD.integer(42))
|
2018-09-16 20:21:53 +00:00
|
|
|
true
|
|
|
|
iex> RDF.Term.term?(42)
|
|
|
|
false
|
|
|
|
"""
|
|
|
|
def term?(value)
|
|
|
|
|
2018-06-08 10:26:52 +00:00
|
|
|
@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.
|
|
|
|
|
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` 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)
|
|
|
|
|
2018-09-16 02:02:53 +00:00
|
|
|
@doc """
|
|
|
|
Converts a given value into a RDF term.
|
|
|
|
|
|
|
|
Returns `nil` if the given value is not convertible into any valid RDF.Term.
|
2018-10-04 22:58:34 +00:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> RDF.Term.coerce("foo")
|
|
|
|
~L"foo"
|
|
|
|
iex> RDF.Term.coerce(42)
|
2020-05-05 21:58:44 +00:00
|
|
|
RDF.XSD.integer(42)
|
2018-10-04 22:58:34 +00:00
|
|
|
|
2018-09-16 02:02:53 +00:00
|
|
|
"""
|
|
|
|
def coerce(value)
|
|
|
|
|
2018-10-04 22:58:34 +00:00
|
|
|
@doc """
|
|
|
|
Returns the native Elixir value of a RDF term.
|
|
|
|
|
|
|
|
Returns `nil` if the given value is not a a valid RDF term or a value convertible to a RDF term.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> RDF.Term.value(~I<http://example.com/>)
|
|
|
|
"http://example.com/"
|
|
|
|
iex> RDF.Term.value(~L"foo")
|
|
|
|
"foo"
|
2020-05-05 21:58:44 +00:00
|
|
|
iex> RDF.XSD.integer(42) |> RDF.Term.value()
|
2018-10-04 22:58:34 +00:00
|
|
|
42
|
|
|
|
|
|
|
|
"""
|
|
|
|
def value(term)
|
|
|
|
|
2018-06-08 10:26:52 +00:00
|
|
|
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)
|
2018-09-16 02:02:53 +00:00
|
|
|
def coerce(term), do: term
|
2018-10-04 22:58:34 +00:00
|
|
|
def value(term), do: term.value
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: true
|
2018-06-08 10:26:52 +00:00
|
|
|
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)
|
2018-09-16 02:02:53 +00:00
|
|
|
def coerce(term), do: term
|
2018-10-04 22:58:34 +00:00
|
|
|
def value(term), do: to_string(term)
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: true
|
2018-09-16 02:02:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defimpl RDF.Term, for: Reference do
|
2020-03-02 01:07:31 +00:00
|
|
|
@dialyzer {:nowarn_function, equal_value?: 2}
|
|
|
|
@dialyzer {:nowarn_function, coerce: 1}
|
2018-09-16 02:02:53 +00:00
|
|
|
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)
|
2018-10-04 22:58:34 +00:00
|
|
|
def value(term), do: term
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: false
|
2018-06-08 10:26:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defimpl RDF.Term, for: RDF.Literal do
|
2020-05-05 21:58:44 +00:00
|
|
|
def equal?(term1, term2), do: RDF.Literal.equal?(term1, term2)
|
2018-06-08 10:26:52 +00:00
|
|
|
def equal_value?(term1, term2), do: RDF.Literal.equal_value?(term1, term2)
|
2018-09-16 02:02:53 +00:00
|
|
|
def coerce(term), do: term
|
2020-04-10 21:40:33 +00:00
|
|
|
def value(term), do: RDF.Literal.value(term) || RDF.Literal.lexical(term)
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: true
|
2018-09-16 02:02:53 +00:00
|
|
|
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)
|
|
|
|
|
2020-05-05 21:58:44 +00:00
|
|
|
def coerce(true), do: RDF.XSD.true
|
|
|
|
def coerce(false), do: RDF.XSD.false
|
2020-05-16 22:53:36 +00:00
|
|
|
def coerce(nil), do: nil
|
|
|
|
def coerce(term) do
|
|
|
|
case RDF.Namespace.resolve_term(term) do
|
|
|
|
{:ok, iri} -> iri
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
2018-09-16 20:21:53 +00:00
|
|
|
|
2018-10-04 22:58:34 +00:00
|
|
|
def value(true), do: true
|
|
|
|
def value(false), do: false
|
2020-05-16 22:53:36 +00:00
|
|
|
def value(nil), do: nil
|
|
|
|
def value(term), do: RDF.Term.value(coerce(term))
|
2018-10-04 22:58:34 +00:00
|
|
|
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: false
|
2018-09-16 02:02:53 +00:00
|
|
|
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)
|
2020-04-10 21:40:33 +00:00
|
|
|
def coerce(term), do: RDF.XSD.String.new(term)
|
2018-10-04 22:58:34 +00:00
|
|
|
def value(term), do: term
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: false
|
2018-09-16 02:02:53 +00:00
|
|
|
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)
|
2020-04-10 21:40:33 +00:00
|
|
|
def coerce(term), do: RDF.XSD.Integer.new(term)
|
2018-10-04 22:58:34 +00:00
|
|
|
def value(term), do: term
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: false
|
2018-09-16 02:02:53 +00:00
|
|
|
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)
|
2020-04-10 21:40:33 +00:00
|
|
|
def coerce(term), do: RDF.XSD.Double.new(term)
|
2018-10-04 22:58:34 +00:00
|
|
|
def value(term), do: term
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: false
|
2018-09-16 02:02:53 +00:00
|
|
|
end
|
|
|
|
|
2018-09-16 13:07:41 +00:00
|
|
|
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)
|
2020-04-10 21:40:33 +00:00
|
|
|
def coerce(term), do: RDF.XSD.Decimal.new(term)
|
2018-10-04 22:58:34 +00:00
|
|
|
def value(term), do: term
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: false
|
2018-09-16 13:07:41 +00:00
|
|
|
end
|
|
|
|
|
2018-09-16 02:02:53 +00:00
|
|
|
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)
|
2020-04-10 21:40:33 +00:00
|
|
|
def coerce(term), do: RDF.XSD.DateTime.new(term)
|
2018-10-04 22:58:34 +00:00
|
|
|
def value(term), do: term
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: false
|
2018-09-16 02:02:53 +00:00
|
|
|
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)
|
2020-04-10 21:40:33 +00:00
|
|
|
def coerce(term), do: RDF.XSD.DateTime.new(term)
|
2018-10-04 22:58:34 +00:00
|
|
|
def value(term), do: term
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: false
|
2018-09-16 02:02:53 +00:00
|
|
|
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)
|
2020-04-10 21:40:33 +00:00
|
|
|
def coerce(term), do: RDF.XSD.Date.new(term)
|
2018-10-04 22:58:34 +00:00
|
|
|
def value(term), do: term
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: false
|
2018-09-16 02:02:53 +00:00
|
|
|
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)
|
2020-04-10 21:40:33 +00:00
|
|
|
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)
|
2018-10-04 22:58:34 +00:00
|
|
|
def value(term), do: term
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: false
|
2018-06-08 10:26:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defimpl RDF.Term, for: Any do
|
|
|
|
def equal?(term1, term2), do: term1 == term2
|
|
|
|
def equal_value?(_, _), do: nil
|
2018-09-16 02:02:53 +00:00
|
|
|
def coerce(_), do: nil
|
2018-10-04 22:58:34 +00:00
|
|
|
def value(_), do: nil
|
2018-09-16 20:21:53 +00:00
|
|
|
def term?(_), do: false
|
2018-06-08 10:26:52 +00:00
|
|
|
end
|