RDF.Literal (RDF.ex v0.9.0) View Source

RDF literals are leaf nodes of a RDF graph containing raw data, like strings, numbers etc.

A literal is a struct consisting of a literal field holding either a RDF.Literal.Datatype struct for the respective known datatype of the literal or a RDF.Literal.Generic struct if the datatype is unknown, i.e. has no RDF.Literal.Datatype implementation.

Link to this section Summary

Functions

Transforms the given literal into its canonical form.

Returns if the lexical of the given literal has the canonical form.

Returns the canonical lexical of the given literal.

Coerces a new RDF.Literal from another value.

Returns if the given value is a RDF.Literal or RDF.Literal.Datatype struct.

Returns the IRI of datatype of the given literal.

Checks if two literals are equal.

Checks if two literals have equal values.

Returns if the literal uses the RDF.Literal.Generic datatype or on of the dedicated builtin or custom RDF.Literal.Datatypes.

Checks if the first of two RDF.Literals is greater then the other.

Returns if a literal is a datatyped literal.

Returns if a literal is a language-tagged literal.

Checks if 'literal' is literal with the given datatype.

Returns the language of the given literal if present.

Checks if the first of two RDF.Literals is smaller then the other.

Returns the lexical form of the given literal.

Matches the lexical form of the given RDF.Literal against a XPath and XQuery regular expression pattern with flags.

Creates a new RDF.Literal of the given value and tries to infer an appropriate XSD datatype.

Creates a new RDF.Literal with the given datatype or language tag.

Creates a new RDF.Literal, but fails if it's not valid.

Returns if a literal is a plain literal.

Returns if a literal is a simple literal.

Updates the value of a RDF.Literal without changing everything else.

Returns if the given literal is valid with respect to its datatype.

Returns the value of the given literal.

Link to this section Types

Specs

t() :: %RDF.Literal{literal: RDF.Literal.Datatype.literal()}

Link to this section Functions

Specs

canonical(t()) :: t()

Transforms the given literal into its canonical form.

Specs

canonical?(t()) :: boolean()

Returns if the lexical of the given literal has the canonical form.

Link to this function

canonical_lexical(literal)

View Source

Specs

canonical_lexical(t()) :: String.t() | nil

Returns the canonical lexical of the given literal.

Coerces a new RDF.Literal from another value.

The following mapping of Elixir types to XSD datatypes is applied:

Elixir datatypeXSD datatype
stringxsd:string
booleanxsd:boolean
integerxsd:integer
floatxsd:double
Decimalxsd:decimal
Timexsd:time
Datexsd:date
DateTimexsd:dateTime
NaiveDateTimexsd:dateTime
URIxsd:AnyURI

When an RDF.Literal can not be coerced, nil is returned (as opposed to new/1 which fails in this case).

Examples

iex> RDF.Literal.coerce(42)
%RDF.Literal{literal: %RDF.XSD.Integer{value: 42}}

Specs

compare(t(), t()) ::
  RDF.Literal.Datatype.comparison_result() | :indeterminate | nil

Returns if the given value is a RDF.Literal or RDF.Literal.Datatype struct.

If you simply want to check for a RDF.Literal use pattern matching or RDF.literal?/1. This function is a bit slower than those and most of the time only needed when implementing RDF.Literal.Datatypes where you have to deal with the raw, i.e. unwrapped RDF.Literal.Datatype structs.

Specs

datatype_id(t()) :: RDF.IRI.t()

Returns the IRI of datatype of the given literal.

Specs

equal?(any(), any()) :: boolean()

Checks if two literals are equal.

Two literals are equal if they have the same datatype, value and lexical form.

Link to this function

equal_value?(left, right)

View Source

Specs

equal_value?(t(), t() | any()) :: boolean()

Checks if two literals have equal values.

Specs

generic?(t()) :: boolean()

Returns if the literal uses the RDF.Literal.Generic datatype or on of the dedicated builtin or custom RDF.Literal.Datatypes.

Link to this function

greater_than?(left, right)

View Source

Specs

greater_than?(t(), t()) :: boolean()

Checks if the first of two RDF.Literals is greater then the other.

Specs

has_datatype?(t()) :: boolean()

Returns if a literal is a datatyped literal.

For historical reasons, this excludes xsd:string and rdf:langString.

see http://www.w3.org/TR/rdf-concepts/#dfn-typed-literal

Specs

has_language?(t()) :: boolean()

Returns if a literal is a language-tagged literal.

see http://www.w3.org/TR/rdf-concepts/#dfn-plain-literal

Link to this function

is_a?(literal, datatype)

View Source

Checks if 'literal' is literal with the given datatype.

datatype can be one of the following:

  • a RDF.Literal.Datatype module which checks if the literal is of this datatype or derived from it
  • RDF.XSD.Numeric which checks if the literal is one of the numeric XSD datatypes or derived of one of them
  • RDF.XSD.Datatype which checks if the literal is a XSD datatype or derived of one of them

Specs

language(t()) :: String.t() | nil

Returns the language of the given literal if present.

Specs

less_than?(t(), t()) :: boolean()

Checks if the first of two RDF.Literals is smaller then the other.

Specs

lexical(t()) :: String.t()

Returns the lexical form of the given literal.

Link to this function

matches?(value, pattern, flags \\ "")

View Source

Specs

matches?(
  t() | String.t(),
  pattern :: t() | String.t(),
  flags :: t() | String.t()
) :: boolean()

Matches the lexical form of the given RDF.Literal against a XPath and XQuery regular expression pattern with flags.

The regular expression language is defined in XQuery 1.0 and XPath 2.0 Functions and Operators.

see https://www.w3.org/TR/xpath-functions/#func-matches

Specs

new(t() | any()) :: t() | nil

Creates a new RDF.Literal of the given value and tries to infer an appropriate XSD datatype.

See coerce/1 for applied mapping of Elixir types to XSD datatypes.

Note: The RDF.literal function is a shortcut to this function.

Examples

iex> RDF.Literal.new(42)
%RDF.Literal{literal: %RDF.XSD.Integer{value: 42}}

Specs

new(t() | any(), keyword()) :: t() | nil

Creates a new RDF.Literal with the given datatype or language tag.

Specs

new!(t() | any(), keyword()) :: t()

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("foo")
%RDF.Literal{literal: %RDF.XSD.String{value: "foo"}}

iex> RDF.Literal.new!("foo", datatype: RDF.NS.XSD.integer)
** (RDF.Literal.InvalidError) invalid RDF.Literal: %RDF.Literal{literal: %RDF.XSD.Integer{value: nil, lexical: "foo"}, valid: false}

iex> RDF.Literal.new!("foo", datatype: RDF.langString)
** (RDF.Literal.InvalidError) invalid RDF.Literal: %RDF.Literal{literal: %RDF.LangString{language: nil, value: "foo"}, valid: false}

Specs

plain?(t()) :: boolean()

Returns if a literal is a plain literal.

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

Specs

simple?(t()) :: boolean()

Returns if a literal is a simple literal.

A simple literal has no datatype or language.

see http://www.w3.org/TR/sparql11-query/#simple_literal

Link to this function

update(literal, fun, opts \\ [])

View Source

Updates the value of a RDF.Literal without changing everything else.

The optional second argument allows to specify what will be passed to fun with the :as option, eg. with as: :lexical the lexical is passed to the function.

Example

iex> RDF.XSD.integer(42) |> RDF.Literal.update(fn value -> value + 1 end)
RDF.XSD.integer(43)
iex> ~L"foo"de |> RDF.Literal.update(fn _ -> "bar" end)
~L"bar"de
iex> RDF.literal("foo", datatype: "http://example.com/dt") |> RDF.Literal.update(fn _ -> "bar" end)
RDF.literal("bar", datatype: "http://example.com/dt")
iex> RDF.XSD.integer(42) |> RDF.XSD.Integer.update(
...>   fn value -> value <> "1" end, as: :lexical)
RDF.XSD.integer(421)

Specs

valid?(t() | any()) :: boolean()

Returns if the given literal is valid with respect to its datatype.

Specs

value(t()) :: any()

Returns the value of the given literal.