aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2021-06-22 15:05:19 +0200
committerarie <arie@alleycat.cc>2021-06-28 21:04:42 +0200
commitb3a7faa0624dd8890ed3ba0e34eef3a0f8080323 (patch)
tree7fafaf92895fc242258f6eb012f8962e744de397
parentf85cd7b989699b271201a970756ef343aa5e3131 (diff)
Rdf_turtle: Use a bnode_counter to create new blank nodes while parsing
-rw-r--r--bin/main.ml1
-rw-r--r--lib/turtle/ast.ml1
-rw-r--r--lib/turtle/rdf_turtle.ml74
-rw-r--r--lib/turtle/rdf_turtle.mli1
4 files changed, 40 insertions, 37 deletions
diff --git a/bin/main.ml b/bin/main.ml
index f87b39b..a470aeb 100644
--- a/bin/main.ml
+++ b/bin/main.ml
@@ -31,6 +31,7 @@ let test_ctx : Rdf_turtle.Ast.parser_state =
{
base_uri = "base" |> Iri.of_string;
namespaces = Rdf_turtle.Ast.SMap.empty;
+ bnode_counter = 0;
bnode_labels = Rdf_turtle.Ast.SMap.empty;
}
diff --git a/lib/turtle/ast.ml b/lib/turtle/ast.ml
index 76f6ef5..1caa478 100644
--- a/lib/turtle/ast.ml
+++ b/lib/turtle/ast.ml
@@ -364,5 +364,6 @@ end
type parser_state = {
base_uri : Rdf.Iri.t;
namespaces : Rdf.Iri.t SMap.t;
+ bnode_counter : int;
bnode_labels : Blank_node.t SMap.t;
}
diff --git a/lib/turtle/rdf_turtle.ml b/lib/turtle/rdf_turtle.ml
index 9581cd8..e7c5152 100644
--- a/lib/turtle/rdf_turtle.ml
+++ b/lib/turtle/rdf_turtle.ml
@@ -6,11 +6,11 @@ open Rdf
type ctxG = Ast.parser_state * Graph.t
-(* TODO is this a good solution? *)
-let blank_node_counter = ref 0
-let create_blank_node () =
- blank_node_counter := !blank_node_counter + 1;
- Blank_node.of_string ("bNode" ^ string_of_int !blank_node_counter)
+(* Creates a new blank node and incrementes the bnode_counter in the context *)
+let create_blank_node (ctx:Ast.parser_state) =
+ let i = ctx.bnode_counter in
+ {ctx with bnode_counter = ctx.bnode_counter + 1},
+ Blank_node.of_string @@ string_of_int i
(* TODO do we need to do something with the ctx here? Can prefixes depend on previous prefixes?
* In that case it would be necessary *)
@@ -68,13 +68,13 @@ let rec mk_collection : Blank_node.t -> ctxG -> Ast.object' list -> ctxG * Blank
let sub_bnode = Triple.Subject.of_blank_node @@ bnode in
let (ctx, g) = insert_pred sub_bnode first_pred (ctx, g) obj in
let g = Graph.add g (Triple.make sub_bnode rest_pred nil_obj) in
- let new_bnode = create_blank_node () in
+ let (ctx, new_bnode) = create_blank_node ctx in
(ctx, g), new_bnode
| head :: tail ->
Fmt.pr "Im in mk_collection head :: tail: hi!@.";
let sub_bnode = Triple.Subject.of_blank_node @@ bnode in
let (ctx, g) = insert_pred sub_bnode first_pred (ctx, g) head in
- let new_bnode = create_blank_node () in
+ let (ctx, new_bnode) = create_blank_node ctx in
let obj_bnode = Triple.Object.of_blank_node @@ new_bnode in
let g = Graph.add g (Triple.make sub_bnode rest_pred obj_bnode) in
mk_collection new_bnode (ctx, g) tail
@@ -86,7 +86,7 @@ and mk_collection_sub : ctxG -> Ast.object' list -> Triple.Subject.t * ctxG =
match objs with
| [] -> nil_sub, (ctx, g)
| objs ->
- let bnode = create_blank_node () in
+ let (ctx, bnode) = create_blank_node ctx in
let (ctx, g), _bnode = mk_collection bnode (ctx, g) objs in
let sub_bnode = Triple.Subject.of_blank_node @@ bnode in
sub_bnode, (ctx, g)
@@ -98,18 +98,18 @@ and mk_collection_obj : Triple.Subject.t -> Triple.Predicate.t -> ctxG -> Ast.ob
let g = Graph.add g (Triple.make sub pred nil_obj) in
(ctx, g)
| objs ->
- let bnode = create_blank_node () in
+ let (ctx, bnode) = create_blank_node ctx in
let (ctx, g), _bnode = mk_collection bnode (ctx, g) objs in
let obj_bnode = Triple.Object.of_blank_node @@ bnode in
let g = Graph.add g (Triple.make sub pred obj_bnode) in
(ctx, g)
- (* We match on objects *)
- (* If the object is an Iri, check whether it is a prefixed name and apply the namespaces *)
- (* Add a triple to the graph. *)
- (* If the object is a blank_node, transform to an rdf_blank_node and add to graph. *)
- (* If the object is a literal, transform to an rdf_literal and add to graph *)
- (* If the object is a collection, perform mk_collection with sub, pred, ctxG. *)
+(* We match on objects *)
+(* If the object is an Iri, check whether it is a prefixed name and apply the namespaces *)
+(* Add a triple to the graph. *)
+(* If the object is a blank_node, transform to an rdf_blank_node and add to graph. *)
+(* If the object is a literal, transform to an rdf_literal and add to graph *)
+(* If the object is a collection, perform mk_collection with sub, pred, ctxG. *)
and insert_pred : Triple.Subject.t -> Triple.Predicate.t -> ctxG -> Ast.object' -> ctxG =
fun sub pred (ctx, g) obj ->
match obj with
@@ -139,7 +139,7 @@ and insert_pred : Triple.Subject.t -> Triple.Predicate.t -> ctxG -> Ast.object'
(ctx, g)
| Obj_coll (Collection objs) -> mk_collection_obj sub pred (ctx, g) objs
| Obj_bnodps (BnodPs obj_predobjs) ->
- let bnode = create_blank_node () in
+ let (ctx, bnode) = create_blank_node ctx in
let obj_bnode = Triple.Object.of_blank_node @@ bnode in
let sub_bnode = Triple.Subject.of_blank_node @@ bnode in
let g = Graph.add g (Triple.make sub pred obj_bnode) in
@@ -164,7 +164,7 @@ let insert_sub_predobjs : ctxG -> Ast.subject -> Ast.predobjs -> ctxG =
(* If the subject is an Iri, check whether it is a prefixed name and apply the namespaces *)
(* If the subject is a blank_node, create a subject with that blank node. *)
(* If the subject is a collection, apply the mk_collection_sub function (is there a difference between subject and object?) *)
-(* If the *)
+ (* If the *)
(* Then we fold over the list of pred * object list. *)
fun (ctx, g) sub predobjs ->
let sub, (ctx, g) =
@@ -173,7 +173,7 @@ let insert_sub_predobjs : ctxG -> Ast.subject -> Ast.predobjs -> ctxG =
| Sub_blank_node b -> (Triple.Subject.of_blank_node @@ Blank_node.of_string b), (ctx, g)
| Sub_coll (Collection objs) -> mk_collection_sub (ctx, g) objs
| Sub_bnodps (BnodPs sub_predobjs) ->
- let bnode = create_blank_node () in
+ let (ctx, bnode) = create_blank_node ctx in
let sub_bnode = Triple.Subject.of_blank_node @@ bnode in
let (ctx, g) = List.fold_left (insert_sub_predobj sub_bnode) (ctx, g) sub_predobjs in
sub_bnode, (ctx, g)
@@ -182,25 +182,25 @@ let insert_sub_predobjs : ctxG -> Ast.subject -> Ast.predobjs -> ctxG =
let apply_statement : ctxG -> Ast.Statement.t -> ctxG =
fun (ctx, g) statement -> match statement with
- | Directive (PrefixID (s, iriref)) ->
- let iri = iri_of_iriref ctx iriref in
- let ctx = { ctx with namespaces = Ast.SMap.add s iri ctx.namespaces } in
- (ctx, g)
- | Directive (Base iriref) ->
- let iri =
- iriref
- |> Ast.Iriref.to_string
- |> Iri.of_string
- in
- let ctx = { ctx with base_uri = iri } in
- (ctx, g)
- | Triples (SubjPredObjs (sub, predobjs)) ->
- insert_sub_predobjs (ctx, g) sub predobjs
-(* TODO, double BnodPs is weird *)
- | Triples (BnodPs (BnodPs predobjs)) ->
- let bnode = create_blank_node () in
- let sub_bnode = Triple.Subject.of_blank_node @@ bnode in
- List.fold_left (insert_sub_predobj sub_bnode) (ctx, g) predobjs
+ | Directive (PrefixID (s, iriref)) ->
+ let iri = iri_of_iriref iriref in
+ let ctx = { ctx with namespaces = Ast.SMap.add s iri ctx.namespaces } in
+ (ctx, g)
+ | Directive (Base iriref) ->
+ let iri =
+ iriref
+ |> Ast.Iriref.to_string
+ |> Iri.of_string
+ in
+ let ctx = { ctx with base_uri = iri } in
+ (ctx, g)
+ | Triples (SubjPredObjs (sub, predobjs)) ->
+ insert_sub_predobjs (ctx, g) sub predobjs
+ (* TODO, double BnodPs is weird *)
+ | Triples (BnodPs (BnodPs predobjs)) ->
+ let (ctx, bnode) = create_blank_node ctx in
+ let sub_bnode = Triple.Subject.of_blank_node @@ bnode in
+ List.fold_left (insert_sub_predobj sub_bnode) (ctx, g) predobjs
(* SUBJECT COLLECTIONS *)
(* <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> <http://a.example/p> <http://a.example/o> . *)
diff --git a/lib/turtle/rdf_turtle.mli b/lib/turtle/rdf_turtle.mli
index 1feef57..03f754d 100644
--- a/lib/turtle/rdf_turtle.mli
+++ b/lib/turtle/rdf_turtle.mli
@@ -229,6 +229,7 @@ module Ast : sig
type parser_state = {
base_uri : Rdf.Iri.t;
namespaces : Rdf.Iri.t SMap.t;
+ bnode_counter: int;
bnode_labels : Blank_node.t SMap.t;
}