From 7a42ceacec2a333feee1f28f4e24b087e944ae95 Mon Sep 17 00:00:00 2001 From: Florian Hatat Date: Sun, 16 Mar 2014 14:03:22 +0100 Subject: [PATCH] Only output one for each node The following RDF/XML part: OCaml-RDF OCaml-RDF is an OCaml library to manipulate RDF graphs and execute Sparql queries. describes exactly the same graph as: OCaml-RDF OCaml-RDF is an OCaml library to manipulate RDF graphs and execute Sparql queries. The latter is less verbose, more idiomatic and more human-readable. This patch changes Rdf_xml.output so that only one is created for each subject node in the graph. --- src/rdf_xml.ml | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/rdf_xml.ml b/src/rdf_xml.ml index f87285d..1051a32 100644 --- a/src/rdf_xml.ml +++ b/src/rdf_xml.ml @@ -443,18 +443,28 @@ let output g = in E ((("",Rdf_iri.string pred_iri),atts),children) in - let f_triple acc (sub, pred, obj) = - let atts = - match sub with - Iri iri -> [("", Rdf_iri.string Rdf_rdf.rdf_about), Rdf_iri.string iri] - | Blank_ id -> [("", Rdf_iri.string Rdf_rdf.rdf_nodeID), Rdf_term.string_of_blank_id id] - | Blank -> assert false - | Literal _ -> assert false + let seen_subjects = Hashtbl.create 42 in + let f_triple (sub, pred, obj) = + let (tag, atts, children) = + try Hashtbl.find seen_subjects sub + with Not_found -> + let about_att = + match sub with + Iri iri -> ("", Rdf_iri.string Rdf_rdf.rdf_about), Rdf_iri.string iri + | Blank_ id -> ("", Rdf_iri.string Rdf_rdf.rdf_nodeID), Rdf_term.string_of_blank_id id + | Blank -> assert false + | Literal _ -> assert false + in + (("",Rdf_iri.string Rdf_rdf.rdf_Description), [about_att], []) in - let xml_prop = xml_prop pred obj in - (E ((("",Rdf_iri.string Rdf_rdf.rdf_Description), atts), [xml_prop]) :: acc) + Hashtbl.replace seen_subjects sub (tag, atts, (xml_prop pred obj) :: children) + in + List.iter f_triple (g.find ()); + let xmls = Hashtbl.fold + (fun _ (tag, atts, children) acc -> E ((tag, atts), children) :: acc) + seen_subjects + [] in - let xmls = List.fold_left f_triple [] (g.find ()) in E ((("", Rdf_iri.string Rdf_rdf.rdf_RDF),[]), xmls) -- GitLab