aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarie <arie@alleycat.cc>2021-06-23 10:34:31 +0200
committerarie <arie@alleycat.cc>2021-06-28 21:08:41 +0200
commitcb1aaf6416a155390b85eae5493793fa12f4c788 (patch)
treefbbb36b5cc6b463758ff6064a02639df1ebaaea5
parent184396b507e4bff859ffb5212caccfecf341d2ed (diff)
Cleaned up some code, changed a few names. Added a test function in
bin/main for turtle strings.
-rw-r--r--bin/main.ml67
-rw-r--r--lib/turtle/ast.ml19
-rw-r--r--lib/turtle/parser.ml42
-rw-r--r--lib/turtle/rdf_turtle.ml266
-rw-r--r--lib/turtle/rdf_turtle.mli15
-rw-r--r--test/alcotest/rdf_alcotest.ml2
-rw-r--r--test/alcotest/rdf_alcotest.mli2
-rw-r--r--test/turtle/main.ml20
8 files changed, 213 insertions, 220 deletions
diff --git a/bin/main.ml b/bin/main.ml
index a470aeb..bb097d0 100644
--- a/bin/main.ml
+++ b/bin/main.ml
@@ -16,45 +16,56 @@ let test (parser_str, parse_str, parser', pp) =
let turtle_list =
[
- "<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> _:a ."
+ "() <http://a.example/p> <http://a.example/o> ."
;
-(* "(_:a _:b) *)
-(* <http://www.perceive.net/schemas/relationship/enemyOf> _:a ." *)
-(* ; *)
-(* "(<A-iri>) <http://www.perceive.net/schemas/relationship/enemyOf> _:a ." *)
-(* ; *)
- "(<A-iri> <B-iri>) <http://www.perceive.net/schemas/relationship/enemyOf> _:a ."
+ "('hello') <http://a.example/p> <http://a.example/o> ."
;
+ "('1' '2') <http://a.example/p> <http://a.example/o> ."
+(* ; *)
]
-let test_ctx : Rdf_turtle.Ast.parser_state =
+let test_ctx : Rdf_turtle.Ast.state =
{
base_uri = "base" |> Iri.of_string;
namespaces = Rdf_turtle.Ast.SMap.empty;
bnode_counter = 0;
- bnode_labels = Rdf_turtle.Ast.SMap.empty;
}
let empty_graph = Graph.empty
let () =
List.iter (
- fun statement_str ->
- statement_str
- |> (fun str ->
- Fmt.pr "The statement_str: %s@." str;
- str)
- |> parse Parser.statement
- |> (function
- | Ok x -> Fmt.pr "... ok ... pp_output is:@. %a @." Ast.Statement.pp x; x
- | Error e -> raise (Failure (Fmt.str "error: %s@." e)))
- |> (fun statement ->
- Rdf_turtle.apply_statement (test_ctx, empty_graph) statement)
- |> (fun (_ctx, g) ->
- Graph.to_list @@ g)
- |> List.iter (fun triple ->
- Fmt.pr "%a@." Triple.pp triple)
- ) turtle_list ;
+ fun turtle_str ->
+ turtle_str
+ |> (fun turtle ->
+ Fmt.pr "@.THE TURTLE STRING: %s@." turtle;
+ turtle)
+ |> Rdf_turtle.decode
+ |> Rdf.Graph.to_triples
+ |> Seq.iter (fun triple ->
+ Fmt.pr "@.%a" Rdf.Triple.pp triple;
+ )
+ ) turtle_list
+
+(* List.iter ( *)
+(* fun statement_str -> *)
+(* statement_str *)
+(* |> (fun str -> *)
+(* Fmt.pr "The statement_str: %s@." str; *)
+(* str) *)
+(* |> parse Parser.statement *)
+(* |> (function *)
+(* | Ok x -> Fmt.pr "... ok ... pp_output is:@. %a @." Ast.Statement.pp x; x *)
+(* | Error e -> raise (Failure (Fmt.str "error: %s@." e))) *)
+(* |> (fun statement -> *)
+(* Rdf_turtle.apply_statement (test_ctx, empty_graph) statement) *)
+(* |> (fun (_ctx, g) -> *)
+(* List.of_seq @@ Graph.to_triples @@ g) *)
+(* |> List.iter (fun triple -> *)
+(* Fmt.pr "%a@." Triple.pp triple) *)
+(* ) turtle_list ; *)
+
+>>>>>>> a64d084 (Cleaned up some code, changed a few names. Added a test function in)
(* test ("literal", "\"Cette Série des Années Septantei\"^^<iri>@lang", Parser.literal, Ast.Literal.pp) ; *)
(* *)
(* test ("iriref", "<http://example.org/#spidermanhhhhhhhhhhhhhh>", Parser.iriref, Ast.Iriref.pp) ; *)
@@ -98,13 +109,13 @@ let () =
(* a foaf:Person ; *)
(* foaf:name \"Spiderman\", \"Человек-паук\"@ru ", *)
(* Parser.predobjs, Ast.predobjs_pp) ; *)
-(* test ("triples", "[ p:a _:b, _:c ] ", Parser.triples, Ast.Triples.pp) ; *)
-(* test ("triples", "<#spiderman> <what> <#green-goblin> ", Parser.triples, Ast.Triples.pp) ; *)
+(* test ("triples", "[ p:a _:b, _:c ] ", Parser.triples, Ast.Triple.pp) ; *)
+(* test ("triples", "<#spiderman> <what> <#green-goblin> ", Parser.triples, Ast.Triple.pp) ; *)
(* test ("triples", *)
(* " <#spiderman> rel:enemyOf <#green-goblin> ; *)
(* a foaf:Person ; *)
(* foaf:name \"Spiderman\", \"Человек-паук\"@ru", *)
-(* Parser.triples, Ast.Triples.pp) ; *)
+(* Parser.triples, Ast.Triple.pp) ; *)
(* test ("directive", "@base <http://one.example/>", Parser.directive, Ast.Directive.pp) ; *)
(* test ("directive", "@prefix foaf: <http://xmlns.com/foaf/0.1/>", Parser.directive, Ast.Directive.pp) ; *)
(* test ("statement", "@base <http://one.example/> . ", Parser.statement, Ast.Statement.pp) ; *)
diff --git a/lib/turtle/ast.ml b/lib/turtle/ast.ml
index 1caa478..95c2276 100644
--- a/lib/turtle/ast.ml
+++ b/lib/turtle/ast.ml
@@ -243,7 +243,7 @@ and predobjs_pp ppf predobjs =
(Fmt.list ~sep:fmt_break_and_semi_colon (Fmt.pair ~sep:fmt_space Predicate.pp objlist_pp))
predobjs
-module Triples = struct
+module Triple = struct
type t = SubjPredObjs of subject * predobjs
| BnodPs of bnodps
@@ -302,25 +302,25 @@ end
module Statement = struct
- type t = Directive of Directive.t | Triples of Triples.t
+ type t = Directive of Directive.t | Triple of Triple.t
let of_directive d =
Directive d
- let of_triples t =
- Triples t
+ let of_triple t =
+ Triple t
let equal a b =
match a, b with
| Directive a, Directive b -> Directive.equal a b
- | Triples a, Triples b -> Triples.equal a b
+ | Triple a, Triple b -> Triple.equal a b
| _, _ -> false
let pp ppf = function
| Directive d ->
Fmt.pf ppf "@[<hov 1>%a@]" Directive.pp d
- | Triples t ->
- Fmt.pf ppf "@[<hov 1>%a@]" Triples.pp t
+ | Triple t ->
+ Fmt.pf ppf "@[<hov 1>%a@]" Triple.pp t
end
@@ -335,7 +335,7 @@ module Turtle = struct
List.fold_right
(fun x (dirs, triples) ->
match x with
- | Statement.Triples y -> (dirs, Statement.of_triples y :: triples)
+ | Statement.Triple y -> (dirs, Statement.of_triple y :: triples)
| Statement.Directive y -> (Statement.of_directive y :: dirs, triples)
)
lst
@@ -361,9 +361,8 @@ module Turtle = struct
end
-type parser_state = {
+type 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/parser.ml b/lib/turtle/parser.ml
index 0f89964..7ff1dba 100644
--- a/lib/turtle/parser.ml
+++ b/lib/turtle/parser.ml
@@ -81,25 +81,43 @@ let literal =
lift2
(fun value lang ->
Ast.Literal.make value ~language:lang datatype_str)
- (char '"'
- *> take_while (char_is_not_equal_to (['"']))
- <* char '"')
+ (choice [
+ (char '"'
+ *> take_while (char_is_not_equal_to (['"']))
+ <* char '"')
+ ;
+ (char '\''
+ *> take_while (char_is_not_equal_to (['\'']))
+ <* char '\'')
+ ])
(language)
;
lift2
(fun value datatype ->
Ast.Literal.make value datatype)
- (char '"'
- *> take_while (char_is_not_equal_to ([':'; '"']))
- <* char '"')
+ (choice [
+ (char '"'
+ *> take_while (char_is_not_equal_to (['"']))
+ <* char '"')
+ ;
+ (char '\''
+ *> take_while (char_is_not_equal_to (['\'']))
+ <* char '\'')
+ ])
(string "^^" *> iri)
;
lift
(fun value ->
Ast.Literal.make value datatype_str)
- (char '"'
- *> take_while (char_is_not_equal_to (['"']))
- <* char '"')
+ (choice [
+ (char '"'
+ *> take_while (char_is_not_equal_to (['"']))
+ <* char '"')
+ ;
+ (char '\''
+ *> take_while (char_is_not_equal_to (['\'']))
+ <* char '\'')
+ ])
;
]
@@ -220,12 +238,12 @@ let object' = object_ collection bnodps
let triples =
choice [
(lift2
- Ast.Triples.of_subject_and_predobjs
+ Ast.Triple.of_subject_and_predobjs
(whitespace_or_comment *> subject <* whitespace_or_comment)
predobjs)
;
(lift
- Ast.Triples.of_bnodps
+ Ast.Triple.of_bnodps
bnodps)
;
]
@@ -264,7 +282,7 @@ let statement =
<* whitespace_or_comment)
;
lift
- Ast.Statement.of_triples
+ Ast.Statement.of_triple
(triples
<* whitespace_or_comment
<* char '.'
diff --git a/lib/turtle/rdf_turtle.ml b/lib/turtle/rdf_turtle.ml
index ef64ae3..36702c7 100644
--- a/lib/turtle/rdf_turtle.ml
+++ b/lib/turtle/rdf_turtle.ml
@@ -4,15 +4,13 @@ module Parser = Parser
open Rdf
-type ctxG = Ast.parser_state * Graph.t
-
(* 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
+let create_blank_node (state:Ast.state) =
+ let i = state.bnode_counter in
+ {state with bnode_counter = state.bnode_counter + 1},
+ Blank_node.of_string @@ ("bnode" ^ string_of_int i)
-(* TODO do we need to do something with the ctx here? Can prefixes depend on previous prefixes?
+(* TODO do we need to do something with the state here? Can prefixes depend on previous prefixes?
* In that case it would be necessary *)
let iri_of_iriref : Ast.Iriref.t -> Iri.t =
fun iriref ->
@@ -20,22 +18,39 @@ let iri_of_iriref : Ast.Iriref.t -> Iri.t =
|> Ast.Iriref.to_string
|> Iri.of_string
-(* TODO *)
-let iri_of_resource : Ast.parser_state -> Ast.Iri.t -> Iri.t = fun
- _ctx ast_iri ->
+(* This function computes an iri:Rdf.Iri.t, given an iri:Ast.Iri.t and the state.
+ * In case the iri is an Iriref, it will just be the corresponding iri.
+ * In the case the iri is a Prefixed_name, it looks for the prefix_label in the state.
+ * If it finds this label, the resulting iri is the concatenation of this label and the
+ * local part. Otherwise it raises Not_found. *)
+(* TODO It's kind of weird to store the Iri's in the state, then
+ * to take out the iri, turn it into a string, take iriref, and the string
+ * from that, glue them together and make an iri out of it. We can also store the
+ * string's themselves. *)
+let iri_of_resource : Ast.state -> Ast.Iri.t -> Iri.t = fun
+ state ast_iri ->
+ Fmt.pr "Im in iri_of_resource; hi!@.";
match ast_iri with
| Iriref str ->
str |> Iri.of_string
| Prefixed_name (a, b) ->
- Iri.of_string (a ^ b)
+ let iri = Ast.SMap.find a state.namespaces in
+ let lst = b |> Ast.Iriref.to_string in
+ let fst = iri |> Iri.to_string in
+ Iri.of_string (fst ^ lst)
+
+(* TODO is this a natural choice? Do we expect a base in all the turtle strings we'll receive? *)
+let base_iri =
+ "<http://example.org/>"
+ |> Iri.of_string
-(* TODO what should this be? *)
+(* TODO should it be this? *)
let base_predicate =
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
|> Iri.of_string
|> Triple.Predicate.of_iri
-(* TODO should it be like this? *)
+(* TODO should it be this? *)
let nil_obj =
"http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"
|> Iri.of_string
@@ -59,64 +74,64 @@ let rest_pred =
(* We separate the cases where the collection is an object, and where it is a subject. *)
(* They have in common that, for the list of objects, a list of bnodes is generated. *)
(* And a list of triples is emitted. This happens in mk_collection. *)
-let rec mk_collection : Blank_node.t -> ctxG -> Ast.object' list -> ctxG * Blank_node.t =
- fun bnode (ctx, g) objs ->
+let rec mk_collection : Blank_node.t -> (Ast.state * Graph.t) -> Ast.object' list -> (Ast.state * Graph.t) * Blank_node.t =
+ fun bnode (state, g) objs ->
match objs with
| [obj] ->
Fmt.pr "Im in mk_collection [x] : hi!@.";
let sub_bnode = Triple.Subject.of_blank_node @@ bnode in
- let (ctx, g) = insert_pred sub_bnode first_pred (ctx, g) obj in
+ let (state, g) = insert_pred sub_bnode first_pred (state, g) obj in
let g = Graph.add g (Triple.make sub_bnode rest_pred nil_obj) in
- let (ctx, new_bnode) = create_blank_node ctx in
- (ctx, g), new_bnode
+ let (state, new_bnode) = create_blank_node state in
+ (state, 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 (ctx, new_bnode) = create_blank_node ctx in
+ let (state, g) = insert_pred sub_bnode first_pred (state, g) head in
+ let (state, new_bnode) = create_blank_node state 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
+ mk_collection new_bnode (state, g) tail
| _ -> raise @@ Invalid_argument "This list cannot be empty."
-and mk_collection_sub : ctxG -> Ast.object' list -> Triple.Subject.t * ctxG =
- fun (ctx, g) objs ->
+and mk_collection_sub : Ast.state * Graph.t -> Ast.object' list -> Triple.Subject.t * (Ast.state * Graph.t) =
+ fun (state, g) objs ->
Fmt.pr "Im in mk_collection_sub; hi!@.";
match objs with
- | [] -> nil_sub, (ctx, g)
+ | [] -> nil_sub, (state, g)
| objs ->
- let (ctx, bnode) = create_blank_node ctx in
- let (ctx, g), _bnode = mk_collection bnode (ctx, g) objs in
+ let (state, bnode) = create_blank_node state in
+ let (state, g), _bnode = mk_collection bnode (state, g) objs in
let sub_bnode = Triple.Subject.of_blank_node @@ bnode in
- sub_bnode, (ctx, g)
+ sub_bnode, (state, g)
-and mk_collection_obj : Triple.Subject.t -> Triple.Predicate.t -> ctxG -> Ast.object' list -> ctxG =
- fun sub pred (ctx, g) objs ->
+and mk_collection_obj : Triple.Subject.t -> Triple.Predicate.t -> Ast.state * Graph.t -> Ast.object' list -> Ast.state * Graph.t =
+ fun sub pred (state, g) objs ->
match objs with
| [] ->
let g = Graph.add g (Triple.make sub pred nil_obj) in
- (ctx, g)
+ (state, g)
| objs ->
- let (ctx, bnode) = create_blank_node ctx in
- let (ctx, g), _bnode = mk_collection bnode (ctx, g) objs in
+ let (state, bnode) = create_blank_node state in
+ let (state, g), _bnode = mk_collection bnode (state, 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)
+ (state, 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. *)
-and insert_pred : Triple.Subject.t -> Triple.Predicate.t -> ctxG -> Ast.object' -> ctxG =
- fun sub pred (ctx, g) obj ->
+(* If the object is a collection, perform mk_collection with sub, pred, Ast.state * Graph.t. *)
+and insert_pred : Triple.Subject.t -> Triple.Predicate.t -> Ast.state * Graph.t -> Ast.object' -> Ast.state * Graph.t =
+ fun sub pred (state, g) obj ->
match obj with
| Obj_iri i ->
Fmt.pr "Im in insert_pred; hi!@.";
- let rdf_obj = Triple.Object.of_iri (iri_of_resource ctx i) in
+ let rdf_obj = Triple.Object.of_iri (iri_of_resource state i) in
let g = Graph.add g (Triple.make sub pred rdf_obj) in
- (ctx, g)
+ (state, g)
| Obj_blank_node bnode ->
let rdf_obj =
bnode
@@ -124,9 +139,10 @@ and insert_pred : Triple.Subject.t -> Triple.Predicate.t -> ctxG -> Ast.object'
|> Triple.Object.of_blank_node
in
let g = Graph.add g (Triple.make sub pred rdf_obj) in
- (ctx, g)
+ (state, g)
| Obj_literal { value; datatype; language } ->
- let rdf_iri_datatype = iri_of_resource ctx datatype in
+ Fmt.pr "Im in insert_pred literal; hi!@.";
+ let rdf_iri_datatype = iri_of_resource state datatype in
let rdf_obj =
Triple.Object.of_literal
(Literal.make
@@ -135,95 +151,105 @@ and insert_pred : Triple.Subject.t -> Triple.Predicate.t -> ctxG -> Ast.object'
?language)
in
let g = Graph.add g (Triple.make sub pred rdf_obj) in
- (ctx, g)
- | Obj_coll (Collection objs) -> mk_collection_obj sub pred (ctx, g) objs
+ (state, g)
+ | Obj_coll (Collection objs) -> mk_collection_obj sub pred (state, g) objs
| Obj_bnodps (BnodPs obj_predobjs) ->
- let (ctx, bnode) = create_blank_node ctx in
+ let (state, bnode) = create_blank_node state 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
- let (ctx, g) = List.fold_left (insert_sub_predobj sub_bnode) (ctx, g) obj_predobjs in
- (ctx, g)
+ let (state, g) = List.fold_left (insert_sub_predobj sub_bnode) (state, g) obj_predobjs in
+ (state, g)
-and insert_sub_predobj : Triple.Subject.t -> ctxG -> Ast.Predicate.t * (Ast.object' list) -> ctxG =
- (* We match on predicates. *)
- (* We transform the Ast Predicate into an Rdf Predicate *)
- (* Then we fold over the obect' list *)
- fun sub (ctx, g) (pred, objs) ->
+and insert_sub_predobj : Triple.Subject.t -> Ast.state * Graph.t -> Ast.Predicate.t * (Ast.object' list) -> Ast.state * Graph.t =
+ fun sub (state, g) (pred, objs) ->
let pred =
match pred with
- | Pred_iri i -> Triple.Predicate.of_iri @@ (iri_of_resource ctx i)
+ | Pred_iri i -> Triple.Predicate.of_iri @@ (iri_of_resource state i)
| Pred_a -> base_predicate
in
- List.fold_left (insert_pred sub pred) (ctx, g) objs
+ List.fold_left (insert_pred sub pred) (state, g) objs
-let insert_sub_predobjs : ctxG -> Ast.subject -> Ast.predobjs -> ctxG =
- (* We match on subject. *)
- (* There is already a subject (this is also available in the ctx, so maybe we can take it out of the ctx). *)
- (* 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 *)
- (* Then we fold over the list of pred * object list. *)
- fun (ctx, g) sub predobjs ->
- let sub, (ctx, g) =
+(* We match on subject.
+ * If the subject is an Iri, we apply the function iri_of_resource, which computes a new Iri, using the namespaces.
+ * If the subject is a blank_node, we create a subject with that blank node.
+ * If the subject is a collection, we apply the mk_collection_sub function.
+ * If the subject is a BnodPs of sub_predobjs, we create a fresh blank_node,
+ * an corresponding subject sub_bnode, and fold with insert_sub_predobjs sub_bnode sub_predobjs.
+ * After this match, we fold over preobjs *)
+let insert_sub_predobjs : Ast.state * Graph.t -> Ast.subject -> Ast.predobjs -> Ast.state * Graph.t =
+ fun (state, g) sub predobjs ->
+ let sub, (state, g) =
match sub with
- | Sub_iri i -> Triple.Subject.of_iri (iri_of_resource ctx i), (ctx, g)
- | 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_iri i -> Triple.Subject.of_iri (iri_of_resource state i), (state, g)
+ | Sub_blank_node b -> (Triple.Subject.of_blank_node @@ Blank_node.of_string b), (state, g)
+ | Sub_coll (Collection objs) -> mk_collection_sub (state, g) objs
| Sub_bnodps (BnodPs sub_predobjs) ->
- let (ctx, bnode) = create_blank_node ctx in
+ let (state, bnode) = create_blank_node state 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)
+ let (state, g) = List.fold_left (insert_sub_predobj sub_bnode) (state, g) sub_predobjs in
+ sub_bnode, (state, g)
in
- List.fold_left (insert_sub_predobj sub) (ctx, g) predobjs
+ List.fold_left (insert_sub_predobj sub) (state, g) predobjs
-let apply_statement : ctxG -> Ast.Statement.t -> ctxG =
- fun (ctx, g) statement -> match statement with
+(* Statements can be either a Directive or a Triple.
+ * If it is a Directive, the prefix or base is included in the state.
+ * If its a Triple of SubjPredObjs, the function insert_sub_predobjs is applied
+ * If it is a Triple of BnodPs of predobjs, a fresh blank_node is created, and from that a subject sub_bnode,
+ * and then we fold over the list predobjs, with the function insert_sub_predobj and accumulator sub_bnode.*)
+let apply_statement : Ast.state * Graph.t -> Ast.Statement.t -> Ast.state * Graph.t =
+ fun (state, g) statement -> match statement with
| 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)
+ let state = { state with namespaces = Ast.SMap.add s iri state.namespaces } in
+ (state, 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
+ let state = { state with base_uri = iri } in
+ (state, g)
+ | Triple (SubjPredObjs (sub, predobjs)) ->
+ insert_sub_predobjs (state, g) sub predobjs
(* TODO, double BnodPs is weird *)
- | Triples (BnodPs (BnodPs predobjs)) ->
- let (ctx, bnode) = create_blank_node ctx in
+ | Triple (BnodPs (BnodPs predobjs)) ->
+ let (state, bnode) = create_blank_node state in
let sub_bnode = Triple.Subject.of_blank_node @@ bnode in
- List.fold_left (insert_sub_predobj sub_bnode) (ctx, g) predobjs
+ List.fold_left (insert_sub_predobj sub_bnode) (state, g) predobjs
-let apply_statements : ctxG -> Ast.Turtle.t -> ctxG =
- fun (ctx, g) statements ->
- List.fold_left apply_statement (ctx, g) statements
+(* A turtle element is in fact a list of statements. Here, we fold over this list, with the function apply_statements *)
+let apply_statements : Ast.state * Graph.t -> Ast.Turtle.t -> Ast.state * Graph.t =
+ fun (state, g) statements ->
+ List.fold_left apply_statement (state, g) statements
+(* This function is in quite some places. *)
let parse p =
Angstrom.parse_string
~consume:Angstrom.Consume.All
p
+(* This function accepts a string, tries to parse it as a turtle string, and, if this succeeds, it transforms
+ * the resulting instance of a turtle type into an Rdf graph. *)
let decode str =
- let ctx : Ast.parser_state = {
- base_uri = "base" |> Iri.of_string;
- namespaces = Ast.SMap.empty;
+(* This 'string -> iri' map is needed because of the default datatype 'xsd:string' in literals *)
+ let xsd_iri =
+ "<http://www.w3.org/2001/XMLSchema#"
+ |> Rdf.Iri.of_string in
+ let namespaces = Ast.SMap.add "xsd" xsd_iri Ast.SMap.empty in
+ let state : Ast.state = {
+ base_uri = base_iri;
+ namespaces = namespaces;
bnode_counter = 0;
- bnode_labels = Ast.SMap.empty;
} in
let g = Graph.empty in
str
|> parse Parser.turtle
|> (function
| Ok x ->
- apply_statements (ctx, g) x
- |> (fun (_ctx, g) -> g)
+ apply_statements (state, g) x
+ |> (fun (_state, g) -> g)
| Error e -> raise (Failure (Fmt.str "eror: %s@." e)))
@@ -262,67 +288,9 @@ let encode graph =
in
Graph.to_nested_seq graph
|> Seq.map (fun (subject, pos_seq) ->
- Ast.Triples.of_subject_and_predobjs
+ Ast.Triple .of_subject_and_predobjs
(encode_subject subject)
(encode_pos pos_seq)
- |> Ast.Statement.of_triples)
+ |> Ast.Statement.of_triple)
|> 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> . *)
-(* *)
-(* _:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "2"^^<http://www.w3.org/2001/XMLSchema#integer> . *)
-(* _:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> . *)
-(* _:genid1 <http://a.example/p> <http://a.example/o> . *)
-(* *)
-(* _:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "2"^^<http://www.w3.org/2001/XMLSchema#integer> . *)
-(* _:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:genid2 . *)
-(* _:genid1 <http://a.example/p> <http://a.example/o> . *)
-(* _:genid2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "3"^^<http://www.w3.org/2001/XMLSchema#integer> . *)
-(* _:genid2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> . *)
-(* *)
-(* _:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "2"^^<http://www.w3.org/2001/XMLSchema#integer> . *)
-(* _:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:genid2 . *)
-(* _:genid1 <http://a.example/p> <http://a.example/o> . *)
-(* _:genid2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "3"^^<http://www.w3.org/2001/XMLSchema#integer> . *)
-(* _:genid2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:genid3 . *)
-(* _:genid3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "4"^^<http://www.w3.org/2001/XMLSchema#integer> . *)
-(* _:genid3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> . *)
-
-
-(* OBJECT COLLECTIONS *)
-(* <http://a.example/s> <http://a.example/p> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> . *)
-(* *)
-(* _:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "1"^^<http://www.w3.org/2001/XMLSchema#integer> . *)
-(* _:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> . *)
-(* <http://a.example/s> <http://a.example/p> _:genid1 . *)
-(* *)
-(* _:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "1"^^<http://www.w3.org/2001/XMLSchema#integer> . *)
-(* _:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:genid2 . *)
-(* _:genid2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "2"^^<http://www.w3.org/2001/XMLSchema#integer> . *)
-(* _:genid2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> . *)
-(* <http://a.example/s> <http://a.example/p> _:genid1 . *)
-(* *)
-(* _:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "1"^^<http://www.w3.org/2001/XMLSchema#integer> . *)
-(* _:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:genid2 . *)
-(* _:genid2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "2"^^<http://www.w3.org/2001/XMLSchema#integer> . *)
-(* _:genid2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:genid3 . *)
-(* _:genid3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "3"^^<http://www.w3.org/2001/XMLSchema#integer> . *)
-(* _:genid3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> . *)
-(* <http://a.example/s> <http://a.example/p> _:genid1 . *)
-(* *)
-
-(* @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . *)
-(* @prefix ns0: <http://a.example/> . *)
-(* *)
-(* [] *)
-(* rdf:first 2 ; *)
-(* rdf:rest ( *)
-(* 3 *)
-(* 4 *)
-(* ) ; *)
-(* ns0:p ns0:o . *)
diff --git a/lib/turtle/rdf_turtle.mli b/lib/turtle/rdf_turtle.mli
index b013be1..342f131 100644
--- a/lib/turtle/rdf_turtle.mli
+++ b/lib/turtle/rdf_turtle.mli
@@ -165,7 +165,7 @@ module Ast : sig
val predobjs_pp : predobjs Fmt.t
[@@ocaml.toplevel_printer]
- module Triples : sig
+ module Triple : sig
type t = SubjPredObjs of subject * predobjs
| BnodPs of bnodps
@@ -198,11 +198,11 @@ module Ast : sig
module Statement : sig
- type t = Directive of Directive.t | Triples of Triples.t
+ type t = Directive of Directive.t | Triple of Triple.t
val of_directive : Directive.t -> t
- val of_triples : Triples.t -> t
+ val of_triple : Triple.t -> t
val equal : t -> t -> bool
@@ -226,11 +226,10 @@ module Ast : sig
end
- type parser_state = {
+ type state = {
base_uri : Rdf.Iri.t;
namespaces : Rdf.Iri.t SMap.t;
bnode_counter: int;
- bnode_labels : Blank_node.t SMap.t;
}
end
@@ -265,7 +264,7 @@ module Parser : sig
val object' : Ast.object' Angstrom.t
- val triples : Ast.Triples.t Angstrom.t
+ val triples : Ast.Triple.t Angstrom.t
val directive : Ast.Directive.t Angstrom.t
@@ -275,9 +274,7 @@ module Parser : sig
end
-type ctxG = Ast.parser_state * Rdf.Graph.t
-
-val apply_statement : ctxG -> Ast.Statement.t -> ctxG
+val apply_statement : Ast.state * Rdf.Graph.t -> Ast.Statement.t -> Ast.state * Rdf.Graph.t
val decode : string -> Rdf.Graph.t
diff --git a/test/alcotest/rdf_alcotest.ml b/test/alcotest/rdf_alcotest.ml
index b281027..63a4adb 100644
--- a/test/alcotest/rdf_alcotest.ml
+++ b/test/alcotest/rdf_alcotest.ml
@@ -62,7 +62,7 @@ let ast_predobjs =
Alcotest.testable Rdf_turtle.Ast.predobjs_pp Rdf_turtle.Ast.predobjs_equal
let ast_triples =
- Alcotest.testable Rdf_turtle.Ast.Triples.pp Rdf_turtle.Ast.Triples.equal
+ Alcotest.testable Rdf_turtle.Ast.Triple.pp Rdf_turtle.Ast.Triple.equal
let ast_directive =
Alcotest.testable Rdf_turtle.Ast.Directive.pp Rdf_turtle.Ast.Directive.equal
diff --git a/test/alcotest/rdf_alcotest.mli b/test/alcotest/rdf_alcotest.mli
index 0e6bb33..4fa3d4f 100644
--- a/test/alcotest/rdf_alcotest.mli
+++ b/test/alcotest/rdf_alcotest.mli
@@ -42,7 +42,7 @@ val ast_bnodps : Rdf_turtle.Ast.bnodps Alcotest.testable
val ast_predobjs : Rdf_turtle.Ast.predobjs Alcotest.testable
-val ast_triples : Rdf_turtle.Ast.Triples.t Alcotest.testable
+val ast_triples : Rdf_turtle.Ast.Triple.t Alcotest.testable
val ast_directive : Rdf_turtle.Ast.Directive.t Alcotest.testable
diff --git a/test/turtle/main.ml b/test/turtle/main.ml
index 5b95da4..839a7f7 100644
--- a/test/turtle/main.ml
+++ b/test/turtle/main.ml
@@ -302,14 +302,14 @@ let subject_test_case =
let triples_test_case =
let cases = [
"<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green-goblin>",
- Triples.of_subject_and_predobjs
+ Triple.of_subject_and_predobjs
(Sub_iri (Iri.of_iriref (Iriref.of_string @@ "http://example.org/#spiderman")))
([ Predicate.of_iri @@ Iri.of_iriref "http://www.perceive.net/schemas/relationship/enemyOf",
[ Obj_iri (Iri.of_iriref "http://example.org/#green-goblin"); ] ; ])
;
(* TODO write test with proper predobjs *)
"[ foaf:name \"Bob\" ]",
- Triples.of_bnodps (
+ Triple.of_bnodps (
BnodPs ([ Predicate.of_iri @@ Iri.of_prefixed_name @@ Prefixed_name.of_strings "foaf" "name",
[ Obj_literal ( Literal.make
("Bob")
@@ -359,8 +359,8 @@ let statement_test_case =
;
"<http://example.org/#spiderman>
<http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green-goblin> .",
- Statement.of_triples (
- Triples.of_subject_and_predobjs
+ Statement.of_triple (
+ Triple.of_subject_and_predobjs
(Sub_iri (Iri.of_iriref (Iriref.of_string @@ "http://example.org/#spiderman")))
([ Predicate.of_iri @@ Iri.of_iriref "http://www.perceive.net/schemas/relationship/enemyOf",
[ Obj_iri (Iri.of_iriref "http://example.org/#green-goblin"); ] ; ]))
@@ -368,15 +368,15 @@ let statement_test_case =
(* "<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> \"literal\" .", *)
"<http://example.org/#spiderman>
<http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green-goblin> .",
- Statement.of_triples (
- Triples.of_subject_and_predobjs
+ Statement.of_triple (
+ Triple.of_subject_and_predobjs
(Sub_iri (Iri.of_iriref (Iriref.of_string @@ "http://example.org/#spiderman")))
([ Predicate.of_iri @@ Iri.of_iriref "http://www.perceive.net/schemas/relationship/enemyOf",
[ Obj_iri (Iri.of_iriref "http://example.org/#green-goblin"); ] ; ]))
;
"<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> \"literal\" .",
- Statement.of_triples (
- Triples.of_subject_and_predobjs
+ Statement.of_triple (
+ Triple.of_subject_and_predobjs
(Sub_iri (Iri.of_iriref (Iriref.of_string @@ "http://example.org/#spiderman")))
([ Predicate.of_iri @@ Iri.of_iriref "http://www.perceive.net/schemas/relationship/enemyOf",
[ Obj_literal ( Literal.make
@@ -406,8 +406,8 @@ let turtle_test_case =
;
"<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green-goblin> .",
Turtle.of_statement_lst (
- [Statement.of_triples (
- Triples.of_subject_and_predobjs
+ [Statement.of_triple (
+ Triple.of_subject_and_predobjs
(Sub_iri (Iri.of_iriref (Iriref.of_string @@ "http://example.org/#spiderman")))
([ Predicate.of_iri @@ Iri.of_iriref "http://www.perceive.net/schemas/relationship/enemyOf",
[ Obj_iri (Iri.of_iriref "http://example.org/#green-goblin"); ] ; ]))])