Add RDF.Graph.name/1 and RDF.Graph.change_name/2

This commit is contained in:
Marcel Otto 2020-09-28 10:39:04 +02:00
parent a8c71df20b
commit c306700991
6 changed files with 33 additions and 8 deletions

View file

@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
### Added
- `RDF.Description.subject/1` and `RDF.Description.change_subject/2`
- `RDF.Graph.name/1` and `RDF.Graph.change_name/2`
### Changed

View file

@ -79,11 +79,13 @@ defmodule RDF.Description do
@doc """
Returns the subject IRI or blank node of a description.
"""
@spec subject(t) :: RDF.Statement.subject()
def subject(%__MODULE__{} = description), do: description.subject
@doc """
Changes the subject of a description.
"""
@spec change_subject(t, RDF.Statement.coercible_subject()) :: t
def change_subject(%__MODULE__{} = description, new_subject) do
%__MODULE__{description | subject: coerce_subject(new_subject)}
end

View file

@ -17,6 +17,8 @@ defmodule RDF.Graph do
import RDF.Utils
alias RDF.{Description, IRI, PrefixMap, Statement}
defstruct name: nil, descriptions: %{}, prefixes: nil, base_iri: nil
@type graph_description :: %{Statement.subject() => Description.t()}
@type t :: %__MODULE__{
@ -45,8 +47,6 @@ defmodule RDF.Graph do
@type get_and_update_description_fun :: (Description.t() -> {Description.t(), input} | :pop)
defstruct name: nil, descriptions: %{}, prefixes: nil, base_iri: nil
@doc """
Creates an empty unnamed `RDF.Graph`.
"""
@ -138,6 +138,20 @@ defmodule RDF.Graph do
%__MODULE__{graph | descriptions: %{}}
end
@doc """
Returns the graph name IRI of `graph`.
"""
@spec name(t) :: RDF.Statement.graph_name()
def name(%__MODULE__{} = graph), do: graph.name
@doc """
Changes the graph name of `graph`.
"""
@spec change_name(t, RDF.Statement.coercible_graph_name()) :: t
def change_name(%__MODULE__{} = graph, new_name) do
%__MODULE__{graph | name: coerce_graph_name(new_name)}
end
@doc """
Adds triples to a `RDF.Graph`.

View file

@ -3,7 +3,7 @@ defmodule RDF.NQuads.Encoder do
use RDF.Serialization.Encoder
alias RDF.{Dataset, Graph, Quad, Statement, Triple}
alias RDF.{Dataset, Graph, Statement}
@impl RDF.Serialization.Encoder
@callback encode(Graph.t() | Dataset.t(), keyword | map) :: {:ok, String.t()} | {:error, any}
@ -17,18 +17,17 @@ defmodule RDF.NQuads.Encoder do
{:ok, if(result == "", do: result, else: "#{result}\n")}
end
@spec statement({Statement.subject(), Statement.predicate(), Statement.object(), nil}) ::
String.t()
@spec statement(Statement.t()) :: String.t()
def statement(statement)
def statement({subject, predicate, object, nil}) do
statement({subject, predicate, object})
end
@spec statement(Quad.t()) :: String.t()
def statement({subject, predicate, object, graph}) do
"#{term(subject)} #{term(predicate)} #{term(object)} #{term(graph)} ."
end
@spec statement(Triple.t()) :: String.t()
def statement({subject, predicate, object}) do
"#{term(subject)} #{term(predicate)} #{term(object)} ."
end

View file

@ -11,7 +11,7 @@ defmodule RDF.Statement do
@type subject :: IRI.t() | BlankNode.t()
@type predicate :: IRI.t() | BlankNode.t()
@type object :: IRI.t() | BlankNode.t() | Literal.t()
@type graph_name :: IRI.t() | BlankNode.t()
@type graph_name :: IRI.t() | BlankNode.t() | nil
@type coercible_subject :: subject | atom | String.t()
@type coercible_predicate :: predicate | atom | String.t()

View file

@ -165,6 +165,15 @@ defmodule RDF.GraphTest do
|> Graph.clear() == Graph.new(opts)
end
test "name/1" do
assert Graph.name(graph()) == graph().name
end
test "change_name/2" do
assert Graph.change_name(graph(), EX.NewGraph).name == iri(EX.NewGraph)
assert Graph.change_name(named_graph(), nil).name == nil
end
describe "add/2" do
test "a proper triple" do
assert Graph.add(graph(), {iri(EX.Subject), EX.predicate(), iri(EX.Object)})