aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2021-06-23 08:58:59 +0200
committerarie <arie@alleycat.cc>2021-06-28 21:06:19 +0200
commit1de330a393293aa737b47ee836e46b03244e6ad8 (patch)
treece791f8d192f750d1ac40e6857f2e789081de9b8
parent13eab97abf658192313a4c05c7150b5f184bb2d5 (diff)
Rdf.Graph: add to_nested_seq
-rw-r--r--lib/core/graph.ml20
-rw-r--r--lib/core/rdf.mli6
2 files changed, 22 insertions, 4 deletions
diff --git a/lib/core/graph.ml b/lib/core/graph.ml
index e8b6705..49c161f 100644
--- a/lib/core/graph.ml
+++ b/lib/core/graph.ml
@@ -69,9 +69,6 @@ let remove (triple:Triple.t) graph =
(fun pos_opt -> Option.bind pos_opt (remove_p p o))
graph
-let subjects graph =
- TermMap.to_seq graph
- |> Seq.map (fun (s, _pos) -> s)
(* Helpers to cast to Triple positional terms *)
@@ -103,6 +100,7 @@ let _os_seq_to_triples s =
(_to_predicate p)
(_to_object o)))
+
(* Helper to create a stream of triples from a stream of predicate-object-set
maps *)
let _pos_seq_to_triples =
@@ -111,6 +109,10 @@ let _pos_seq_to_triples =
|> _os_seq_to_triples s
)
+let subjects graph =
+ TermMap.to_seq graph
+ |> Seq.map (fun (s, _pos) -> _to_subject s)
+
let to_triples graph =
TermMap.to_seq graph
|> _pos_seq_to_triples
@@ -130,3 +132,15 @@ let pp ppf graph =
Fmt.pf ppf "%a"
(Fmt.seq Triple.pp)
(to_triples graph)
+
+let to_nested_seq graph =
+ TermMap.to_seq graph
+ |> Seq.map (fun (s, pos) ->
+ _to_subject s,
+ pos
+ |> TermMap.to_seq
+ |> Seq.map (fun (p, os) ->
+ _to_predicate p,
+ os
+ |> TermSet.to_seq
+ |> Seq.map _to_object))
diff --git a/lib/core/rdf.mli b/lib/core/rdf.mli
index fa2d2f8..9c17ca0 100644
--- a/lib/core/rdf.mli
+++ b/lib/core/rdf.mli
@@ -251,7 +251,7 @@ module Graph : sig
val remove: Triple.t -> t -> t
(** [remove_triple triple graph] removes [triple] from [graph]. *)
- val subjects : t -> Term.t Seq.t
+ val subjects : t -> Triple.Subject.t Seq.t
(** [subjects graph] returns a sequence of subjects that appear in the graph. *)
val to_triples: t -> Triple.t Seq.t
@@ -269,4 +269,8 @@ module Graph : sig
[@@ocaml.toplevel_printer]
(** [pp ppf t] will output a human readable version of the Graph [t] to
the formatter [ppf] *)
+
+ val to_nested_seq : t -> (Triple.Subject.t * (Triple.Predicate.t * Triple.Object.t Seq.t) Seq.t ) Seq.t
+ (** [to_nested_seq graph] returns a sequence of nested sequences
+ containing all triples of [graph]. This is useful for serializing a graph. *)
end