2017-02-13 00:21:32 +00:00
|
|
|
defmodule RDF.Test.Case do
|
|
|
|
use ExUnit.CaseTemplate
|
|
|
|
|
2017-03-12 13:27:52 +00:00
|
|
|
use RDF.Vocabulary.Namespace
|
2020-06-29 08:37:42 +00:00
|
|
|
defvocab EX, base_iri: "http://example.com/", terms: [], strict: false
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
defvocab FOAF, base_iri: "http://xmlns.com/foaf/0.1/", terms: [], strict: false
|
2020-06-16 10:05:44 +00:00
|
|
|
|
2021-09-04 15:46:08 +00:00
|
|
|
alias RDF.{Dataset, Graph, Description, IRI, XSD}
|
2017-08-20 20:35:14 +00:00
|
|
|
import RDF, only: [iri: 1]
|
2021-09-04 15:46:08 +00:00
|
|
|
import RDF.Sigils
|
2017-02-13 00:21:32 +00:00
|
|
|
|
|
|
|
using do
|
|
|
|
quote do
|
2021-10-09 14:40:07 +00:00
|
|
|
alias RDF.{Dataset, Graph, Description, IRI, XSD, PrefixMap, PropertyMap, NS}
|
2020-10-28 10:51:00 +00:00
|
|
|
alias RDF.NS.{RDFS, OWL}
|
2020-06-16 10:05:44 +00:00
|
|
|
alias unquote(__MODULE__).{EX, FOAF}
|
2017-02-13 00:21:32 +00:00
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
import RDF, only: [iri: 1, literal: 1, bnode: 1]
|
2017-04-23 21:41:29 +00:00
|
|
|
import unquote(__MODULE__)
|
2017-06-04 02:20:08 +00:00
|
|
|
|
|
|
|
import RDF.Sigils
|
2020-04-04 22:53:54 +00:00
|
|
|
|
|
|
|
@compile {:no_warn_undefined, RDF.Test.Case.EX}
|
2020-06-16 10:05:44 +00:00
|
|
|
@compile {:no_warn_undefined, RDF.Test.Case.FOAF}
|
2017-02-13 00:21:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-05 10:41:55 +00:00
|
|
|
# TODO: Remove this when we dropped support for Elixir versions < 1.10
|
|
|
|
def struct_not_allowed_as_input_error do
|
|
|
|
if Version.match?(System.version(), "~> 1.10") do
|
|
|
|
FunctionClauseError
|
|
|
|
else
|
|
|
|
ArgumentError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-04 12:10:07 +00:00
|
|
|
def string_to_stream(string) do
|
|
|
|
{:ok, pid} = StringIO.open(string)
|
|
|
|
IO.binstream(pid, :line)
|
|
|
|
end
|
|
|
|
|
2020-11-04 21:01:43 +00:00
|
|
|
def stream_to_string(stream) do
|
|
|
|
stream
|
|
|
|
|> Enum.to_list()
|
|
|
|
|> IO.iodata_to_binary()
|
|
|
|
end
|
|
|
|
|
2021-09-04 15:46:08 +00:00
|
|
|
@iri ~I<http://example.com/Foo>
|
|
|
|
@bnode ~B<foo>
|
|
|
|
@valid_literal ~L"foo"
|
|
|
|
@invalid_literal XSD.integer("foo")
|
|
|
|
|
|
|
|
###############################
|
|
|
|
# RDF.Statement
|
|
|
|
|
2021-09-27 18:57:17 +00:00
|
|
|
@statement {RDF.iri(EX.S), RDF.iri(EX.P), RDF.literal("Foo")}
|
|
|
|
def statement(), do: @statement
|
|
|
|
|
|
|
|
@coercible_statement {EX.S, EX.P, "Foo"}
|
|
|
|
def coercible_statement(), do: @coercible_statement
|
|
|
|
|
2021-09-04 15:46:08 +00:00
|
|
|
@valid_triple {RDF.iri(EX.S), EX.p(), RDF.iri(EX.O)}
|
|
|
|
def valid_triple(), do: @valid_triple
|
|
|
|
|
|
|
|
@valid_triples [
|
|
|
|
@valid_triple,
|
|
|
|
{@iri, @iri, @iri},
|
|
|
|
{@bnode, @iri, @iri},
|
|
|
|
{@iri, @iri, @bnode},
|
|
|
|
{@bnode, @iri, @bnode},
|
|
|
|
{@iri, @iri, @valid_literal},
|
|
|
|
{@bnode, @iri, @valid_literal},
|
|
|
|
{@iri, @iri, @invalid_literal},
|
|
|
|
{@bnode, @iri, @invalid_literal}
|
|
|
|
]
|
|
|
|
|
|
|
|
def valid_triples(), do: @valid_triples
|
|
|
|
|
|
|
|
@valid_star_triples [
|
|
|
|
{@valid_triple, @iri, @iri},
|
|
|
|
{@iri, @iri, @valid_triple}
|
|
|
|
]
|
|
|
|
|
|
|
|
def valid_star_triples(), do: @valid_star_triples
|
|
|
|
|
|
|
|
@valid_quads [
|
|
|
|
{@iri, @iri, @iri, @iri},
|
|
|
|
{@bnode, @iri, @iri, @iri},
|
|
|
|
{@iri, @iri, @bnode, @iri},
|
|
|
|
{@bnode, @iri, @bnode, @iri},
|
|
|
|
{@iri, @iri, @valid_literal, @iri},
|
|
|
|
{@bnode, @iri, @valid_literal, @iri},
|
|
|
|
{@iri, @iri, @invalid_literal, @iri},
|
|
|
|
{@bnode, @iri, @invalid_literal, @iri}
|
|
|
|
]
|
|
|
|
def valid_quads(), do: @valid_quads
|
|
|
|
|
|
|
|
@valid_star_quads [
|
|
|
|
{@valid_triple, @iri, @iri, @iri},
|
|
|
|
{@iri, @iri, @valid_triple, @iri}
|
|
|
|
]
|
|
|
|
|
|
|
|
def valid_star_quads(), do: @valid_star_quads
|
|
|
|
|
|
|
|
@invalid_triples [
|
|
|
|
{@iri, @bnode, @iri},
|
|
|
|
{@valid_literal, @iri, @iri},
|
|
|
|
{@iri, @valid_literal, @iri}
|
|
|
|
]
|
|
|
|
|
|
|
|
def invalid_triples, do: @invalid_triples
|
|
|
|
|
|
|
|
@invalid_quads [
|
|
|
|
{@iri, @bnode, @iri, @iri},
|
|
|
|
{@iri, @iri, @iri, @bnode},
|
|
|
|
{@valid_literal, @iri, @iri, @iri},
|
|
|
|
{@iri, @valid_literal, @iri, @iri},
|
|
|
|
{@iri, @iri, @iri, @valid_literal}
|
|
|
|
]
|
|
|
|
|
|
|
|
def invalid_quads(), do: @invalid_quads
|
|
|
|
|
2017-02-13 00:21:32 +00:00
|
|
|
###############################
|
|
|
|
# RDF.Description
|
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
def description, do: Description.new(EX.Subject)
|
2017-02-13 00:21:32 +00:00
|
|
|
def description(content), do: Description.add(description(), content)
|
|
|
|
|
|
|
|
def description_of_subject(%Description{subject: subject}, subject),
|
|
|
|
do: true
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2017-02-13 00:21:32 +00:00
|
|
|
def description_of_subject(_, _),
|
|
|
|
do: false
|
|
|
|
|
|
|
|
def empty_description(%Description{predications: predications}),
|
|
|
|
do: predications == %{}
|
|
|
|
|
|
|
|
def description_includes_predication(desc, {predicate, object}) do
|
|
|
|
desc.predications
|
|
|
|
|> Map.get(predicate, %{})
|
|
|
|
|> Enum.member?({object, nil})
|
|
|
|
end
|
|
|
|
|
|
|
|
###############################
|
|
|
|
# RDF.Graph
|
|
|
|
|
|
|
|
def graph, do: unnamed_graph()
|
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
def unnamed_graph, do: Graph.new()
|
2017-02-13 00:21:32 +00:00
|
|
|
|
2019-03-28 23:34:22 +00:00
|
|
|
def named_graph(name \\ EX.GraphName), do: Graph.new(name: name)
|
2017-02-13 00:21:32 +00:00
|
|
|
|
|
|
|
def unnamed_graph?(%Graph{name: nil}), do: true
|
2020-06-29 08:37:42 +00:00
|
|
|
def unnamed_graph?(_), do: false
|
2017-02-13 00:21:32 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
def named_graph?(%Graph{name: %IRI{}}), do: true
|
|
|
|
def named_graph?(_), do: false
|
2017-02-13 00:21:32 +00:00
|
|
|
def named_graph?(%Graph{name: name}, name), do: true
|
2020-06-29 08:37:42 +00:00
|
|
|
def named_graph?(_, _), do: false
|
2017-02-13 00:21:32 +00:00
|
|
|
|
|
|
|
def empty_graph?(%Graph{descriptions: descriptions}),
|
|
|
|
do: descriptions == %{}
|
|
|
|
|
2017-02-18 20:35:27 +00:00
|
|
|
def graph_includes_statement?(graph, {subject, _, _} = statement) do
|
2021-09-27 18:57:17 +00:00
|
|
|
subject = if is_tuple(subject), do: subject, else: iri(subject)
|
|
|
|
|
2017-02-13 00:21:32 +00:00
|
|
|
graph.descriptions
|
2021-09-27 18:57:17 +00:00
|
|
|
|> Map.get(subject, %{})
|
2017-02-13 00:21:32 +00:00
|
|
|
|> Enum.member?(statement)
|
|
|
|
end
|
|
|
|
|
2017-02-18 20:35:27 +00:00
|
|
|
###############################
|
2019-03-28 23:34:22 +00:00
|
|
|
# RDF.Dataset
|
2017-02-18 20:35:27 +00:00
|
|
|
|
|
|
|
def dataset, do: unnamed_dataset()
|
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
def unnamed_dataset, do: Dataset.new()
|
2017-02-18 20:35:27 +00:00
|
|
|
|
2019-03-29 00:29:24 +00:00
|
|
|
def named_dataset(name \\ EX.DatasetName), do: Dataset.new(name: name)
|
2017-02-18 20:35:27 +00:00
|
|
|
|
|
|
|
def unnamed_dataset?(%Dataset{name: nil}), do: true
|
2020-06-29 08:37:42 +00:00
|
|
|
def unnamed_dataset?(_), do: false
|
2017-02-18 20:35:27 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
def named_dataset?(%Dataset{name: %IRI{}}), do: true
|
|
|
|
def named_dataset?(_), do: false
|
2017-02-18 20:35:27 +00:00
|
|
|
def named_dataset?(%Dataset{name: name}, name), do: true
|
2020-06-29 08:37:42 +00:00
|
|
|
def named_dataset?(_, _), do: false
|
2017-02-18 20:35:27 +00:00
|
|
|
|
|
|
|
def empty_dataset?(%Dataset{graphs: graphs}), do: graphs == %{}
|
|
|
|
|
|
|
|
def dataset_includes_statement?(dataset, {_, _, _} = statement) do
|
|
|
|
dataset
|
2020-06-29 08:37:42 +00:00
|
|
|
|> Dataset.default_graph()
|
2017-02-18 20:35:27 +00:00
|
|
|
|> graph_includes_statement?(statement)
|
|
|
|
end
|
|
|
|
|
|
|
|
def dataset_includes_statement?(dataset, {subject, predicate, objects, nil}),
|
|
|
|
do: dataset_includes_statement?(dataset, {subject, predicate, objects})
|
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
def dataset_includes_statement?(
|
|
|
|
dataset,
|
|
|
|
{subject, predicate, objects, graph_context}
|
|
|
|
) do
|
2017-02-18 20:35:27 +00:00
|
|
|
dataset.graphs
|
2017-08-20 20:35:14 +00:00
|
|
|
|> Map.get(iri(graph_context), named_graph(graph_context))
|
2017-02-18 20:35:27 +00:00
|
|
|
|> graph_includes_statement?({subject, predicate, objects})
|
|
|
|
end
|
2021-09-27 18:57:17 +00:00
|
|
|
|
|
|
|
###############################
|
|
|
|
# RDF.Star annotations
|
|
|
|
|
|
|
|
@star_statement {@statement, EX.ap(), EX.ao()}
|
|
|
|
def star_statement(), do: @star_statement
|
|
|
|
|
|
|
|
@empty_annotation Description.new(@statement)
|
|
|
|
def empty_annotation(), do: @empty_annotation
|
|
|
|
|
|
|
|
@annotation Description.new(@statement, init: {EX.ap(), EX.ao()})
|
|
|
|
def annotation(), do: @annotation
|
|
|
|
|
|
|
|
@object_annotation Description.new(EX.As, init: {EX.ap(), @statement})
|
|
|
|
def object_annotation(), do: @object_annotation
|
|
|
|
|
|
|
|
@graph_with_annotation Graph.new(init: @annotation)
|
|
|
|
def graph_with_annotation(), do: @graph_with_annotation
|
|
|
|
|
|
|
|
@graph_with_annotations Graph.new(init: [@annotation, @object_annotation])
|
|
|
|
def graph_with_annotations(), do: @graph_with_annotations
|
2017-02-13 00:21:32 +00:00
|
|
|
end
|