From fd3a20116bc7e2e2dd88287923c79e429b6719a5 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Fri, 31 Mar 2017 16:04:39 +0200 Subject: [PATCH] core: creating a new graph from another graph --- lib/rdf/graph.ex | 12 ++++++++++++ test/unit/graph_test.exs | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/rdf/graph.ex b/lib/rdf/graph.ex index e904c50..17cb714 100644 --- a/lib/rdf/graph.ex +++ b/lib/rdf/graph.ex @@ -38,6 +38,12 @@ defmodule RDF.Graph do def new(%RDF.Description{} = description), do: new() |> add(description) + @doc """ + Creates an unnamed `RDF.Graph` from another `RDF.Graph`. + """ + def new(%RDF.Graph{descriptions: descriptions}), + do: %RDF.Graph{descriptions: descriptions} + @doc """ Creates an empty unnamed `RDF.Graph`. """ @@ -68,6 +74,12 @@ defmodule RDF.Graph do def new(name, %RDF.Description{} = description), do: new(name) |> add(description) + @doc """ + Creates a named `RDF.Graph` from another `RDF.Graph`. + """ + def new(name, %RDF.Graph{descriptions: descriptions}), + do: %RDF.Graph{new(name) | descriptions: descriptions} + @doc """ Creates an unnamed `RDF.Graph` with initial triples. """ diff --git a/test/unit/graph_test.exs b/test/unit/graph_test.exs index 95a8b48..68d5bf8 100644 --- a/test/unit/graph_test.exs +++ b/test/unit/graph_test.exs @@ -77,6 +77,26 @@ defmodule RDF.GraphTest do assert unnamed_graph?(g) assert graph_includes_statement?(g, {EX.Subject, EX.predicate, EX.Object}) end + + test "creating a named graph from another graph" do + g = Graph.new(EX.GraphName, Graph.new({EX.Subject, EX.predicate, EX.Object})) + assert named_graph?(g, uri(EX.GraphName)) + assert graph_includes_statement?(g, {EX.Subject, EX.predicate, EX.Object}) + + g = Graph.new(EX.GraphName, Graph.new(EX.OtherGraphName, {EX.Subject, EX.predicate, EX.Object})) + assert named_graph?(g, uri(EX.GraphName)) + assert graph_includes_statement?(g, {EX.Subject, EX.predicate, EX.Object}) + end + + test "creating an unnamed graph from another graph" do + g = Graph.new(Graph.new({EX.Subject, EX.predicate, EX.Object})) + assert unnamed_graph?(g) + assert graph_includes_statement?(g, {EX.Subject, EX.predicate, EX.Object}) + + g = Graph.new(Graph.new(EX.OtherGraphName, {EX.Subject, EX.predicate, EX.Object})) + assert unnamed_graph?(g) + assert graph_includes_statement?(g, {EX.Subject, EX.predicate, EX.Object}) + end end describe "adding triples" do