aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2021-06-23 08:59:17 +0200
committerarie <arie@alleycat.cc>2021-06-28 21:06:21 +0200
commit184396b507e4bff859ffb5212caccfecf341d2ed (patch)
treea486daafa22b76d21163a5c8c76d800a735b13ec
parent1de330a393293aa737b47ee836e46b03244e6ad8 (diff)
Rdf_turtle: add initial encode
-rw-r--r--lib/turtle/rdf_turtle.ml46
-rw-r--r--lib/turtle/rdf_turtle.mli10
2 files changed, 51 insertions, 5 deletions
diff --git a/lib/turtle/rdf_turtle.ml b/lib/turtle/rdf_turtle.ml
index b69ca0d..ef64ae3 100644
--- a/lib/turtle/rdf_turtle.ml
+++ b/lib/turtle/rdf_turtle.ml
@@ -227,6 +227,51 @@ let decode str =
| Error e -> raise (Failure (Fmt.str "eror: %s@." e)))
+let encode graph =
+ let encode_iri iri =
+ Ast.Iri.of_iriref @@ Iri.to_string iri
+ in
+ let encode_subject subject =
+ Triple.Subject.map subject
+ (fun iri -> Ast.Sub_iri (encode_iri iri))
+ (fun bnode -> Ast.Sub_blank_node (Blank_node.identifier bnode))
+ in
+ let encode_predicate predicate =
+ Triple.Predicate.map predicate
+ (fun iri -> Ast.Predicate.of_iri (encode_iri iri))
+ in
+ let encode_object object' =
+ Triple.Object.map object'
+ (fun iri -> Ast.Obj_iri (encode_iri iri))
+ (fun bnode -> Ast.Obj_blank_node (Blank_node.identifier bnode))
+ (fun literal -> Ast.Obj_literal (Ast.Literal.make
+ (Literal.canonical literal)
+ (encode_iri (Literal.datatype literal))))
+ in
+ let encode_objects os_seq =
+ os_seq
+ |> Seq.map encode_object
+ |> List.of_seq
+ in
+ let encode_pos pos_seq =
+ pos_seq
+ |> Seq.map (fun (p, os_seq) ->
+ encode_predicate p,
+ encode_objects os_seq)
+ |> List.of_seq
+ in
+ Graph.to_nested_seq graph
+ |> Seq.map (fun (subject, pos_seq) ->
+ Ast.Triples.of_subject_and_predobjs
+ (encode_subject subject)
+ (encode_pos pos_seq)
+ |> Ast.Statement.of_triples)
+ |> List.of_seq
+ |> Fmt.str "%a" Ast.Turtle.pp
+
+
+
+
(* SUBJECT COLLECTIONS *)
(* <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> <http://a.example/p> <http://a.example/o> . *)
(* *)
@@ -281,4 +326,3 @@ let decode str =
(* 4 *)
(* ) ; *)
(* ns0:p ns0:o . *)
-
diff --git a/lib/turtle/rdf_turtle.mli b/lib/turtle/rdf_turtle.mli
index 27590ca..b013be1 100644
--- a/lib/turtle/rdf_turtle.mli
+++ b/lib/turtle/rdf_turtle.mli
@@ -83,10 +83,10 @@ module Ast : sig
end
-(* The language option indicates a language, the iri option inidicates the data type *)
-(* Question: if the data type is absent in the literal, the default is xsd:string. *)
-(* So we could decide to always have a datatype (so to not use an Iri option). Zoggy uses *)
-(* an Iri option though, maybe because that's closer to the exact specification of a turtle? *)
+ (* The language option indicates a language, the iri option inidicates the data type *)
+ (* Question: if the data type is absent in the literal, the default is xsd:string. *)
+ (* So we could decide to always have a datatype (so to not use an Iri option). Zoggy uses *)
+ (* an Iri option though, maybe because that's closer to the exact specification of a turtle? *)
module Literal : sig
type t
@@ -280,3 +280,5 @@ type ctxG = Ast.parser_state * Rdf.Graph.t
val apply_statement : ctxG -> Ast.Statement.t -> ctxG
val decode : string -> Rdf.Graph.t
+
+val encode : Rdf.Graph.t -> string