2016-10-15 16:26:56 +00:00
|
|
|
defmodule RDF do
|
2017-06-18 19:12:51 +00:00
|
|
|
@moduledoc """
|
|
|
|
The top-level module of RDF.ex.
|
|
|
|
|
|
|
|
RDF.ex consists of:
|
|
|
|
|
|
|
|
- modules for the nodes of an RDF graph
|
2018-09-16 01:52:07 +00:00
|
|
|
- `RDF.Term`
|
2017-08-20 20:35:14 +00:00
|
|
|
- `RDF.IRI`
|
2017-06-18 19:12:51 +00:00
|
|
|
- `RDF.BlankNode`
|
|
|
|
- `RDF.Literal`
|
2020-06-16 10:05:44 +00:00
|
|
|
- the `RDF.Literal.Datatype` system
|
2017-06-18 19:12:51 +00:00
|
|
|
- a facility for the mapping of URIs of a vocabulary to Elixir modules and
|
|
|
|
functions: `RDF.Vocabulary.Namespace`
|
|
|
|
- modules for the construction of statements
|
|
|
|
- `RDF.Triple`
|
|
|
|
- `RDF.Quad`
|
|
|
|
- `RDF.Statement`
|
|
|
|
- modules for collections of statements
|
|
|
|
- `RDF.Description`
|
|
|
|
- `RDF.Graph`
|
|
|
|
- `RDF.Dataset`
|
|
|
|
- `RDF.Data`
|
2017-08-20 20:35:14 +00:00
|
|
|
- `RDF.List`
|
2019-11-27 08:10:57 +00:00
|
|
|
- `RDF.Diff`
|
2020-06-16 10:05:44 +00:00
|
|
|
- functions to construct and execute basic graph pattern queries: `RDF.Query`
|
2018-03-09 20:15:43 +00:00
|
|
|
- functions for working with RDF serializations: `RDF.Serialization`
|
|
|
|
- behaviours for the definition of RDF serialization formats
|
|
|
|
- `RDF.Serialization.Format`
|
2017-06-18 19:12:51 +00:00
|
|
|
- `RDF.Serialization.Decoder`
|
|
|
|
- `RDF.Serialization.Encoder`
|
2017-08-20 20:35:14 +00:00
|
|
|
- and the implementation of various RDF serialization formats
|
2017-06-18 19:12:51 +00:00
|
|
|
- `RDF.NTriples`
|
|
|
|
- `RDF.NQuads`
|
2017-08-20 20:35:14 +00:00
|
|
|
- `RDF.Turtle`
|
2017-06-18 19:12:51 +00:00
|
|
|
|
|
|
|
This top-level module provides shortcut functions for the construction of the
|
|
|
|
basic elements and structures of RDF and some general helper functions.
|
|
|
|
|
2019-04-06 00:24:22 +00:00
|
|
|
For a general introduction you may refer to the guides on the [homepage](https://rdf-elixir.dev).
|
2017-06-18 19:12:51 +00:00
|
|
|
"""
|
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
alias RDF.{
|
|
|
|
IRI,
|
|
|
|
Namespace,
|
|
|
|
Literal,
|
|
|
|
BlankNode,
|
|
|
|
Triple,
|
|
|
|
Quad,
|
2021-05-10 19:41:48 +00:00
|
|
|
Statement,
|
2020-06-29 08:37:42 +00:00
|
|
|
Description,
|
|
|
|
Graph,
|
|
|
|
Dataset,
|
|
|
|
PrefixMap
|
|
|
|
}
|
2019-03-27 23:12:14 +00:00
|
|
|
|
2020-05-06 16:04:19 +00:00
|
|
|
import RDF.Guards
|
2020-04-10 21:40:33 +00:00
|
|
|
import RDF.Utils.Bootstrapping
|
|
|
|
|
2019-08-03 22:13:13 +00:00
|
|
|
defdelegate default_base_iri(), to: RDF.IRI, as: :default_base
|
|
|
|
|
2019-03-27 23:12:14 +00:00
|
|
|
@standard_prefixes PrefixMap.new(
|
2020-06-29 08:37:42 +00:00
|
|
|
xsd: xsd_iri_base(),
|
|
|
|
rdf: rdf_iri_base(),
|
2020-04-10 21:40:33 +00:00
|
|
|
rdfs: rdfs_iri_base()
|
2019-03-27 23:12:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
A fixed set prefixes that will always be part of the `default_prefixes/0`.
|
|
|
|
|
|
|
|
```elixir
|
|
|
|
#{inspect(@standard_prefixes, pretty: true)}
|
|
|
|
```
|
|
|
|
|
|
|
|
See `default_prefixes/0`, if you don't want these standard prefixes to be part
|
|
|
|
of the default prefixes.
|
|
|
|
"""
|
|
|
|
def standard_prefixes(), do: @standard_prefixes
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
A user-defined `RDF.PrefixMap` of prefixes to IRI namespaces.
|
|
|
|
|
|
|
|
This prefix map will be used implicitly wherever a prefix map is expected, but
|
|
|
|
not provided. For example, when you don't pass a prefix map to the Turtle serializer,
|
|
|
|
this prefix map will be used.
|
|
|
|
|
|
|
|
By default the `standard_prefixes/0` are part of this prefix map, but you can
|
|
|
|
define additional default prefixes via the `default_prefixes` compile-time
|
|
|
|
configuration.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
config :rdf,
|
|
|
|
default_prefixes: %{
|
|
|
|
ex: "http://example.com/"
|
|
|
|
}
|
|
|
|
|
|
|
|
If you don't want the `standard_prefixes/0` to be part of the default prefixes,
|
|
|
|
or you want to map the standard prefixes to different namespaces (strongly discouraged!),
|
|
|
|
you can set the `use_standard_prefixes` compile-time configuration flag to `false`.
|
|
|
|
|
|
|
|
config :rdf,
|
|
|
|
use_standard_prefixes: false
|
|
|
|
|
|
|
|
"""
|
|
|
|
@default_prefixes Application.get_env(:rdf, :default_prefixes, %{}) |> PrefixMap.new()
|
|
|
|
if Application.get_env(:rdf, :use_standard_prefixes, true) do
|
|
|
|
def default_prefixes() do
|
|
|
|
PrefixMap.merge!(@standard_prefixes, @default_prefixes)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
def default_prefixes() do
|
|
|
|
@default_prefixes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the `default_prefixes/0` with additional prefix mappings.
|
|
|
|
|
|
|
|
The `prefix_mappings` can be given in any format accepted by `RDF.PrefixMap.new/1`.
|
|
|
|
"""
|
|
|
|
def default_prefixes(prefix_mappings) do
|
|
|
|
default_prefixes() |> PrefixMap.merge!(prefix_mappings)
|
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-11-16 12:23:43 +00:00
|
|
|
defdelegate read_string(string, opts), to: RDF.Serialization
|
|
|
|
defdelegate read_string!(string, opts), to: RDF.Serialization
|
|
|
|
defdelegate read_stream(stream, opts \\ []), to: RDF.Serialization
|
|
|
|
defdelegate read_stream!(stream, opts \\ []), to: RDF.Serialization
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate read_file(filename, opts \\ []), to: RDF.Serialization
|
|
|
|
defdelegate read_file!(filename, opts \\ []), to: RDF.Serialization
|
2020-11-16 12:23:43 +00:00
|
|
|
defdelegate write_string(data, opts), to: RDF.Serialization
|
|
|
|
defdelegate write_string!(data, opts), to: RDF.Serialization
|
|
|
|
defdelegate write_stream(data, opts), to: RDF.Serialization
|
|
|
|
defdelegate write_file(data, filename, opts \\ []), to: RDF.Serialization
|
|
|
|
defdelegate write_file!(data, filename, opts \\ []), to: RDF.Serialization
|
2018-03-09 20:15:43 +00:00
|
|
|
|
2016-10-15 16:26:56 +00:00
|
|
|
@doc """
|
2017-06-16 22:44:11 +00:00
|
|
|
Checks if the given value is a RDF resource.
|
2017-04-10 01:06:20 +00:00
|
|
|
|
2016-10-15 16:26:56 +00:00
|
|
|
## Examples
|
|
|
|
|
2018-09-16 20:28:46 +00:00
|
|
|
Supposed `EX` is a `RDF.Vocabulary.Namespace` and `Foo` is not.
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
iex> RDF.resource?(RDF.iri("http://example.com/resource"))
|
2017-06-16 22:44:11 +00:00
|
|
|
true
|
|
|
|
iex> RDF.resource?(EX.resource)
|
|
|
|
true
|
2018-09-16 20:28:46 +00:00
|
|
|
iex> RDF.resource?(EX.Resource)
|
|
|
|
true
|
|
|
|
iex> RDF.resource?(Foo.Resource)
|
|
|
|
false
|
2017-06-16 22:44:11 +00:00
|
|
|
iex> RDF.resource?(RDF.bnode)
|
|
|
|
true
|
2020-05-05 21:58:44 +00:00
|
|
|
iex> RDF.resource?(RDF.XSD.integer(42))
|
2018-09-16 01:52:07 +00:00
|
|
|
false
|
2017-06-16 22:44:11 +00:00
|
|
|
iex> RDF.resource?(42)
|
|
|
|
false
|
2016-10-15 16:26:56 +00:00
|
|
|
"""
|
2017-06-16 22:44:11 +00:00
|
|
|
def resource?(value)
|
2020-05-08 22:09:31 +00:00
|
|
|
def resource?(%IRI{}), do: true
|
|
|
|
def resource?(%BlankNode{}), do: true
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2020-05-06 16:04:19 +00:00
|
|
|
def resource?(qname) when maybe_ns_term(qname) do
|
2020-05-08 22:09:31 +00:00
|
|
|
case Namespace.resolve_term(qname) do
|
|
|
|
{:ok, iri} -> resource?(iri)
|
|
|
|
_ -> false
|
|
|
|
end
|
2018-09-16 20:28:46 +00:00
|
|
|
end
|
|
|
|
|
2020-05-08 22:09:31 +00:00
|
|
|
def resource?(_), do: false
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2018-09-16 01:52:07 +00:00
|
|
|
@doc """
|
|
|
|
Checks if the given value is a RDF term.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-09-16 20:28:46 +00:00
|
|
|
Supposed `EX` is a `RDF.Vocabulary.Namespace` and `Foo` is not.
|
|
|
|
|
2018-09-16 01:52:07 +00:00
|
|
|
iex> RDF.term?(RDF.iri("http://example.com/resource"))
|
|
|
|
true
|
|
|
|
iex> RDF.term?(EX.resource)
|
|
|
|
true
|
2018-09-16 20:28:46 +00:00
|
|
|
iex> RDF.term?(EX.Resource)
|
|
|
|
true
|
|
|
|
iex> RDF.term?(Foo.Resource)
|
|
|
|
false
|
2018-09-16 01:52:07 +00:00
|
|
|
iex> RDF.term?(RDF.bnode)
|
|
|
|
true
|
2020-05-05 21:58:44 +00:00
|
|
|
iex> RDF.term?(RDF.XSD.integer(42))
|
2018-09-16 01:52:07 +00:00
|
|
|
true
|
|
|
|
iex> RDF.term?(42)
|
|
|
|
false
|
|
|
|
"""
|
|
|
|
def term?(value)
|
|
|
|
def term?(%Literal{}), do: true
|
2020-06-29 08:37:42 +00:00
|
|
|
def term?(value), do: resource?(value)
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
defdelegate uri?(value), to: IRI, as: :valid?
|
|
|
|
defdelegate iri?(value), to: IRI, as: :valid?
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate uri(value), to: IRI, as: :new
|
|
|
|
defdelegate iri(value), to: IRI, as: :new
|
2017-08-20 20:35:14 +00:00
|
|
|
defdelegate uri!(value), to: IRI, as: :new!
|
|
|
|
defdelegate iri!(value), to: IRI, as: :new!
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2017-07-21 22:21:28 +00:00
|
|
|
@doc """
|
|
|
|
Checks if the given value is a blank node.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> RDF.bnode?(RDF.bnode)
|
|
|
|
true
|
2017-08-20 20:35:14 +00:00
|
|
|
iex> RDF.bnode?(RDF.iri("http://example.com/resource"))
|
2017-07-21 22:21:28 +00:00
|
|
|
false
|
|
|
|
iex> RDF.bnode?(42)
|
|
|
|
false
|
|
|
|
"""
|
|
|
|
def bnode?(%BlankNode{}), do: true
|
|
|
|
def bnode?(_), do: false
|
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate bnode(), to: BlankNode, as: :new
|
2017-06-16 22:44:11 +00:00
|
|
|
defdelegate bnode(id), to: BlankNode, as: :new
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-05-15 15:13:31 +00:00
|
|
|
@doc """
|
|
|
|
Checks if the given value is a RDF literal.
|
|
|
|
"""
|
|
|
|
def literal?(%Literal{}), do: true
|
|
|
|
def literal?(_), do: false
|
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate literal(value), to: Literal, as: :new
|
2017-06-16 22:44:11 +00:00
|
|
|
defdelegate literal(value, opts), to: Literal, as: :new
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate triple(s, p, o), to: Triple, as: :new
|
|
|
|
defdelegate triple(tuple), to: Triple, as: :new
|
2016-10-30 18:36:46 +00:00
|
|
|
|
2017-06-16 22:44:11 +00:00
|
|
|
defdelegate quad(s, p, o, g), to: Quad, as: :new
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate quad(tuple), to: Quad, as: :new
|
2016-10-30 18:36:46 +00:00
|
|
|
|
2021-05-10 19:41:48 +00:00
|
|
|
defdelegate statement(s, p, o), to: Statement, as: :new
|
|
|
|
defdelegate statement(s, p, o, g), to: Statement, as: :new
|
|
|
|
defdelegate statement(tuple), to: Statement, as: :new
|
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
defdelegate description(subject, opts \\ []), to: Description, as: :new
|
2016-11-02 02:19:19 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate graph(), to: Graph, as: :new
|
|
|
|
defdelegate graph(arg), to: Graph, as: :new
|
|
|
|
defdelegate graph(arg1, arg2), to: Graph, as: :new
|
2016-11-02 02:19:19 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate dataset(), to: Dataset, as: :new
|
|
|
|
defdelegate dataset(arg), to: Dataset, as: :new
|
|
|
|
defdelegate dataset(arg1, arg2), to: Dataset, as: :new
|
2016-11-02 02:19:19 +00:00
|
|
|
|
2019-11-20 00:45:18 +00:00
|
|
|
defdelegate diff(arg1, arg2), to: RDF.Diff
|
|
|
|
|
2017-07-27 19:49:35 +00:00
|
|
|
defdelegate list?(resource, graph), to: RDF.List, as: :node?
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate list?(description), to: RDF.List, as: :node?
|
2017-07-27 19:49:35 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
def list(native_list), do: RDF.List.from(native_list)
|
2018-04-07 20:59:03 +00:00
|
|
|
def list(head, %Graph{} = graph), do: RDF.List.new(head, graph)
|
2020-06-29 08:37:42 +00:00
|
|
|
def list(native_list, opts), do: RDF.List.from(native_list, opts)
|
2018-04-07 20:59:03 +00:00
|
|
|
|
2019-03-25 23:31:43 +00:00
|
|
|
defdelegate prefix_map(prefixes), to: RDF.PrefixMap, as: :new
|
2020-10-08 20:49:45 +00:00
|
|
|
defdelegate property_map(property_map), to: RDF.PropertyMap, as: :new
|
2019-03-25 23:31:43 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate langString(value, opts), to: RDF.LangString, as: :new
|
2020-05-05 21:58:44 +00:00
|
|
|
defdelegate lang_string(value, opts), to: RDF.LangString, as: :new
|
|
|
|
|
2017-06-10 21:08:49 +00:00
|
|
|
for term <- ~w[type subject predicate object first rest value]a do
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate unquote(term)(), to: RDF.NS.RDF
|
2020-06-01 13:43:38 +00:00
|
|
|
@doc false
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate unquote(term)(s, o), to: RDF.NS.RDF
|
2020-06-01 13:43:38 +00:00
|
|
|
@doc false
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate unquote(term)(s, o1, o2), to: RDF.NS.RDF
|
2020-06-01 13:43:38 +00:00
|
|
|
@doc false
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate unquote(term)(s, o1, o2, o3), to: RDF.NS.RDF
|
2020-06-01 13:43:38 +00:00
|
|
|
@doc false
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate unquote(term)(s, o1, o2, o3, o4), to: RDF.NS.RDF
|
2020-06-01 13:43:38 +00:00
|
|
|
@doc false
|
2017-06-10 21:08:49 +00:00
|
|
|
defdelegate unquote(term)(s, o1, o2, o3, o4, o5), to: RDF.NS.RDF
|
|
|
|
end
|
2017-05-29 21:12:50 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
defdelegate langString(), to: RDF.NS.RDF
|
|
|
|
defdelegate lang_string(), to: RDF.NS.RDF, as: :langString
|
2017-06-10 21:08:49 +00:00
|
|
|
defdelegate unquote(nil)(), to: RDF.NS.RDF
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2018-09-14 22:34:09 +00:00
|
|
|
defdelegate __base_iri__(), to: RDF.NS.RDF
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|