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
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
alias RDF.{Dataset, Graph, Description, IRI}
|
|
|
|
import RDF, only: [iri: 1]
|
2017-02-13 00:21:32 +00:00
|
|
|
|
|
|
|
using do
|
|
|
|
quote do
|
2020-05-05 21:58:44 +00:00
|
|
|
alias RDF.{Dataset, Graph, Description, IRI, XSD}
|
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
|
|
|
|
|
|
|
|
###############################
|
|
|
|
# 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
|
2017-02-13 00:21:32 +00:00
|
|
|
graph.descriptions
|
2017-08-20 20:35:14 +00:00
|
|
|
|> Map.get(iri(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
|
2017-02-13 00:21:32 +00:00
|
|
|
end
|