Clean up the top-level RDF module and some other files

This commit is contained in:
Marcel Otto 2017-06-17 00:44:11 +02:00
parent ad1acfffc1
commit b3365d2f55
8 changed files with 86 additions and 115 deletions

View file

@ -1,5 +1,49 @@
defmodule RDF do
alias RDF.{Namespace, Literal, BlankNode, Triple, Quad}
alias RDF.{Namespace, Literal, BlankNode, Triple, Quad,
Description, Graph, Dataset}
@doc """
Checks if the given value is a RDF resource.
## Examples
iex> RDF.resource?(RDF.uri("http://example.com/resource"))
true
iex> RDF.resource?(EX.resource)
true
iex> RDF.resource?(RDF.bnode)
true
iex> RDF.resource?(42)
false
"""
def resource?(value)
def resource?(%URI{}), do: true
def resource?(atom) when is_atom(atom), do: resource?(Namespace.resolve_term(atom))
def resource?(%BlankNode{}), do: true
def resource?(_), do: false
@doc """
Checks if the given value is an URI.
## Examples
iex> RDF.uri?("http://www.example.com/foo")
true
iex> RDF.uri?("not a uri")
false
"""
def uri?(some_uri = %URI{}) do
# The following was suggested at http://stackoverflow.com/questions/30696761/check-if-a-url-is-valid-in-elixir
# TODO: Find a better way! Maybe <https://github.com/marcelog/ex_rfc3986>?
case some_uri do
%URI{scheme: nil} -> false
_uri -> true
end
end
def uri?(value) when is_binary(value), do: uri?(URI.parse(value))
def uri?(_), do: false
@doc """
Generator function for URIs from strings or term atoms of a `RDF.Namespace`.
@ -24,113 +68,41 @@ defmodule RDF do
def uri(atom) when is_atom(atom), do: Namespace.resolve_term(atom)
def uri(string) do
parsed_uri = URI.parse(string)
if uri?(parsed_uri) do
parsed_uri
else
raise RDF.InvalidURIError, ~s(string "#{string}" is not a valid URI)
with parsed_uri = URI.parse(string) do
if uri?(parsed_uri) do
parsed_uri
else
raise RDF.InvalidURIError, ~s(string "#{string}" is not a valid URI)
end
end
end
@doc """
Checks if the given value is an URI.
## Examples
defdelegate bnode(), to: BlankNode, as: :new
defdelegate bnode(id), to: BlankNode, as: :new
iex> RDF.uri?("http://www.example.com/foo")
true
iex> RDF.uri?("not a uri")
false
"""
def uri?(some_uri = %URI{}) do
# The following was a suggested at http://stackoverflow.com/questions/30696761/check-if-a-url-is-valid-in-elixir
# TODO: Find a better way! Maybe https://github.com/marcelog/ex_rfc3986 ?
case some_uri do
%URI{scheme: nil} -> false
_uri -> true
end
end
def uri?(value) when is_binary(value), do: uri?(URI.parse(value))
def uri?(_), do: false
defdelegate literal(value), to: Literal, as: :new
defdelegate literal(value, opts), to: Literal, as: :new
defdelegate triple(s, p, o), to: Triple, as: :new
defdelegate triple(tuple), to: Triple, as: :new
@doc """
Generator function for `RDF.Literal` values.
defdelegate quad(s, p, o, g), to: Quad, as: :new
defdelegate quad(tuple), to: Quad, as: :new
## Examples
defdelegate description(arg), to: Description, as: :new
defdelegate description(arg1, arg2), to: Description, as: :new
defdelegate description(arg1, arg2, arg3), to: Description, as: :new
iex> RDF.literal(42)
%RDF.Literal{value: 42, datatype: XSD.integer}
"""
def literal(value)
defdelegate graph(), to: Graph, as: :new
defdelegate graph(arg), to: Graph, as: :new
defdelegate graph(arg1, arg2), to: Graph, as: :new
defdelegate graph(arg1, arg2, arg3), to: Graph, as: :new
defdelegate graph(arg1, arg2, arg3, arg4), to: Graph, as: :new
def literal(lit = %Literal{}), do: lit
def literal(value), do: Literal.new(value)
def literal(value, opts), do: Literal.new(value, opts)
@doc """
Generator function for `RDF.Triple`s.
## Examples
iex> RDF.triple("http://example.com/S", "http://example.com/p", 42)
{RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42)}
iex> RDF.triple(EX.S, EX.p, 42)
{RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42)}
"""
def triple(subject, predicate, object), do: Triple.new(subject, predicate, object)
def triple(tuple), do: Triple.new(tuple)
@doc """
Generator function for `RDF.quad`s.
## Examples
iex> RDF.quad("http://example.com/S", "http://example.com/p", 42, "http://example.com/Graph")
{RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42), RDF.uri("http://example.com/Graph")}
iex> RDF.quad(EX.S, EX.p, 42, EX.Graph)
{RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42), RDF.uri("http://example.com/Graph")}
"""
def quad(subject, predicate, object, graph_context),
do: Quad.new(subject, predicate, object, graph_context)
def quad(tuple), do: Quad.new(tuple)
@doc """
Generator function for `RDF.BlankNode`s.
"""
def bnode, do: BlankNode.new
@doc """
Generator function for `RDF.BlankNode`s with a user-defined identity.
## Examples
iex> RDF.bnode(:foo)
%RDF.BlankNode{id: "foo"}
"""
def bnode(id), do: BlankNode.new(id)
@doc """
Checks if the given value is a RDF resource.
## Examples
iex> RDF.resource?(RDF.uri("http://example.com/resource"))
true
iex> RDF.resource?(EX.resource)
true
iex> RDF.resource?(RDF.bnode)
true
iex> RDF.resource?(42)
false
"""
def resource?(value)
def resource?(%URI{}), do: true
def resource?(atom) when is_atom(atom), do: resource?(Namespace.resolve_term(atom))
def resource?(%BlankNode{}), do: true
def resource?(_), do: false
defdelegate dataset(), to: Dataset, as: :new
defdelegate dataset(arg), to: Dataset, as: :new
defdelegate dataset(arg1, arg2), to: Dataset, as: :new
for term <- ~w[type subject predicate object first rest value]a do

View file

@ -42,6 +42,8 @@ defmodule RDF.Literal do
"""
def new(value)
def new(%RDF.Literal{} = literal), do: literal
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)

View file

@ -6,7 +6,7 @@ defmodule RDF.Quad do
RDF values for subject, predicate, object and a graph context.
"""
alias RDF.{BlankNode, Statement}
alias RDF.Statement
@doc """
Creates a `RDF.Quad` with proper RDF values.
@ -19,6 +19,8 @@ defmodule RDF.Quad do
iex> RDF.Quad.new("http://example.com/S", "http://example.com/p", 42, "http://example.com/Graph")
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42), ~I<http://example.com/Graph>}
iex> RDF.Quad.new(EX.S, EX.p, 42, EX.Graph)
{RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42), RDF.uri("http://example.com/Graph")}
"""
def new(subject, predicate, object, graph_context) do
{
@ -40,6 +42,8 @@ defmodule RDF.Quad do
iex> RDF.Quad.new {"http://example.com/S", "http://example.com/p", 42, "http://example.com/Graph"}
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42), ~I<http://example.com/Graph>}
iex> RDF.Quad.new {EX.S, EX.p, 42, EX.Graph}
{RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42), RDF.uri("http://example.com/Graph")}
"""
def new({subject, predicate, object, graph_context}),
do: new(subject, predicate, object, graph_context)

View file

@ -6,7 +6,7 @@ defmodule RDF.Triple do
RDF values for subject, predicate and object.
"""
alias RDF.{BlankNode, Statement}
alias RDF.Statement
@doc """
Creates a `RDF.Triple` with proper RDF values.
@ -19,6 +19,8 @@ defmodule RDF.Triple do
iex> RDF.Triple.new("http://example.com/S", "http://example.com/p", 42)
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42)}
iex> RDF.Triple.new(EX.S, EX.p, 42)
{RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42)}
"""
def new(subject, predicate, object) do
{
@ -39,6 +41,8 @@ defmodule RDF.Triple do
iex> RDF.Triple.new {"http://example.com/S", "http://example.com/p", 42}
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42)}
iex> RDF.Triple.new {EX.S, EX.p, 42}
{RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42)}
"""
def new({subject, predicate, object}), do: new(subject, predicate, object)

View file

@ -412,7 +412,7 @@ defmodule RDF.Vocabulary.Namespace do
IO.warn "capitalized alias '#{term}' for a property"
end
defp case_violation_warning(:lowercased_alias, term, _, base_uri) do
defp case_violation_warning(:lowercased_alias, term, _, _) do
IO.warn "lowercased alias '#{term}' for a non-property resource"
end

View file

@ -1,7 +1,5 @@
defmodule RDF.QuadTest do
use ExUnit.Case
import RDF.Sigils
use RDF.Test.Case
doctest RDF.Quad
end

View file

@ -1,11 +1,5 @@
defmodule RDFTest do
use ExUnit.Case
use RDF.Vocabulary.Namespace
defvocab EX, base_uri: "http://example.com/", terms: [], strict: false
alias RDF.NS.XSD
use RDF.Test.Case
doctest RDF
end

View file

@ -1,8 +1,5 @@
defmodule RDF.TripleTest do
use ExUnit.Case
import RDF.Sigils
use RDF.Test.Case
doctest RDF.Triple
end