aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarie <arie@alleycat.cc>2021-06-22 14:53:14 +0200
committerarie <arie@alleycat.cc>2021-06-28 21:04:42 +0200
commitf85cd7b989699b271201a970756ef343aa5e3131 (patch)
tree2975125f344a9553549c4064f5089d8a5aafc2ab
parent4ba6d6ac2a36c33dc2c2d4f9e0a44551548be5fa (diff)
Some small changes
-rw-r--r--bin/main.ml107
-rw-r--r--lib/turtle/ast.ml2
-rw-r--r--lib/turtle/parser.ml6
-rw-r--r--lib/turtle/rdf_turtle.ml53
-rw-r--r--lib/turtle/rdf_turtle.mli2
5 files changed, 104 insertions, 66 deletions
diff --git a/bin/main.ml b/bin/main.ml
index b7c6260..f87b39b 100644
--- a/bin/main.ml
+++ b/bin/main.ml
@@ -16,41 +16,22 @@ let test (parser_str, parse_str, parser', pp) =
let turtle_list =
[
-(* "<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> _:a ." *)
-(* ; *)
+ "<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> _:a ."
+ ;
(* "(_:a _:b) *)
(* <http://www.perceive.net/schemas/relationship/enemyOf> _:a ." *)
(* ; *)
-(* "(<A-iri> <B-iri>) <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 ."
+ ;
]
-let test_sub =
- "test_sub"
- |> Rdf.Iri.of_string
- |> Rdf.Triple.Subject.of_iri
-
-let test_pred =
- "test_sub"
- |> Rdf.Iri.of_string
- |> Rdf.Triple.Predicate.of_iri
-
-let test_iri =
- "hey-some-iri!"
- |> Rdf.Iri.of_string
-
-let test_iri_2 =
- "http://example.org/#spiderman"
- |> Rdf.Iri.of_string
-
let test_ctx : Rdf_turtle.Ast.parser_state =
{
base_uri = "base" |> Iri.of_string;
namespaces = Rdf_turtle.Ast.SMap.empty;
bnode_labels = Rdf_turtle.Ast.SMap.empty;
- cur_subject = test_sub;
- cur_predicate = test_pred;
}
let empty_graph = Graph.empty
@@ -59,12 +40,13 @@ let () =
List.iter (
fun statement_str ->
statement_str
+ |> (fun str ->
+ Fmt.pr "The statement_str: %s@." str;
+ str)
|> parse Parser.statement
- |> Result.get_ok
- |> (fun statement ->
- Fmt.pr "%a@." Rdf_turtle.Ast.Statement.pp statement;
- 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) ->
@@ -73,28 +55,77 @@ let () =
Fmt.pr "%a@." Triple.pp triple)
) turtle_list ;
(* 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) ; *)
(* test ("language", "@en-fr", Parser.language, Ast.Language.pp) ; *)
- test ("iri", "a:p", Parser.iri, Ast.Iri.pp) ;
+(* test ("iri", "a:p", Parser.iri, Ast.Iri.pp) ; *)
(* test ("prefixed_name", "rdfs:some-example", Parser.prefixed_name, Ast.Prefixed_name.pp) ; *)
(* test ("blank_node", "_:some-example", Parser.blank_node, Ast.Blank_node.pp) ; *)
(* test ("iri" , "<https://someiri.com/blah>", Parser.iri, Ast.Iri.pp) ; *)
(* test ("iri" , "rdfsk:irir", Parser.iri, Ast.Iri.pp) ; *)
(* test ("literal", "\"hia\"", Parser.literal, Ast.Literal.pp) ; *)
(* test ("literal", "\"Cette Série des Années Septantei\"@be", Parser.literal, Ast.Literal.pp) ; *)
- test ("literal", "\"Cette Série des Années Septantei\"^^xsd:string", Parser.literal, Ast.Literal.pp) ;
+(* test ("literal", "\"Cette Série des Années Septantei\"^^xsd:string", Parser.literal, Ast.Literal.pp) ; *)
(* test ("predicate", "b:a", Parser.predicate, Ast.Predicate.pp) ; *)
(* test ("object (iri)", "a:p", Parser.object', Ast.object_pp) ; *)
(* test ("object (bnode)", "_:blabla", Parser.object', Ast.object_pp) ; *)
(* test ("object (literal)", "\"Cette Série des Années Septantei\"^^<iri>", Parser.object', Ast.object_pp) ; *)
(* test ("object (coll)", "(_:a _:b <iriobj> rdfs:o)", Parser.object', Ast.object_pp) ; *)
(* test ("object (bnodps)", "[ <predicate> <object> ]", Parser.object', Ast.object_pp) ; *)
- test ("object (bnodps)", "[ <pred1> <obj1>; <pred2> <obj2>, <obj3> ]", Parser.object', Ast.object_pp) ;
+(* test ("object (bnodps)", "[ <pred1> <obj1>; <pred2> <obj2>, <obj3> ]", Parser.object', Ast.object_pp) ; *)
(* test ("object (bnodps)", "[ a:p a:o1 ]", Parser.object', Ast.object_pp) ; *)
- test ("object (bnodps)",
- "[ rel:enemyOf <#green-goblin> ;
- a foaf:Person ;
- foaf:name \"Spiderman\", \"Человек-паук\"@ru]",
- Parser.object', Ast.object_pp) ;
-
+(* test ("object (bnodps)", *)
+(* "[ rel:enemyOf <#green-goblin>, \"henkie\"; *)
+(* rel:enemyOf <#green-goblin>, _:sjaak; *)
+(* a foaf:Person ; *)
+(* foaf:name \"Spiderman\", \"Человек-паук\"@ru]", *)
+(* Parser.object', Ast.object_pp) ; *)
+(* test ("subject (iri)", "alaaf:potverdorie", Parser.subject, Ast.subject_pp) ; *)
+(* test ("subject (iri)", "<#spiderman>", Parser.subject, Ast.subject_pp) ; *)
+(* test ("subject (blank_node)", "_:potverdorie", Parser.subject, Ast.subject_pp) ; *)
+(* test ("subject (bnodps)", *)
+(* "[ rel:enemyOf <#green-goblin>, \"henkie\"; *)
+(* rel:enemyOf <#green-goblin>, _:sjaak; *)
+(* a foaf:Person ; *)
+(* foaf:name \"Spiderman\", \"Человек-паук\"@ru]", *)
+(* Parser.subject, Ast.subject_pp) ; *)
+(* test ("subject (collection)", "( _:a _:b _:c)", Parser.subject, Ast.subject_pp) ; *)
+(* test ("bnodps", "[ p:a _:b, _:c ]", Parser.bnodps, Ast.bnodps_pp) ; *)
+(* test ("predobjs", " p:a _:b, _:c ", Parser.predobjs, Ast.predobjs_pp) ; *)
+(* test ("predobjs", *)
+(* " rel:enemyOf <#green-goblin> ; *)
+(* 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", *)
+(* " <#spiderman> rel:enemyOf <#green-goblin> ; *)
+(* a foaf:Person ; *)
+(* foaf:name \"Spiderman\", \"Человек-паук\"@ru", *)
+(* Parser.triples, Ast.Triples.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) ; *)
+(* test ("statement", *)
+(* " <#spiderman> rel:enemyOf <#green-goblin> ; *)
+(* a foaf:Person ; *)
+(* foaf:name \"Spiderman\", \"Человек-паук\"@ru . ", *)
+(* Parser.statement, Ast.Statement.pp) ; *)
+(* test ("turtle", *)
+(* " @base <http://example.org/> . *)
+(* @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . *)
+(* @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . *)
+(* @prefix foaf: <http://xmlns.com/foaf/0.1/> . *)
+(* *)
+(* <#green-goblin> *)
+(* rel:enemyOf <#spiderman> ; #LET us put a comment here *)
+(* a foaf:Person ; *)
+(* foaf:name \"Green Goblin\" . *)
+(* *)
+(* @prefix rel: <http://www.perceive.net/schemas/relationship/> . *)
+(* <#spiderman> *)
+(* rel:enemyOf <#green-goblin> ; *)
+(* a foaf:Person ; *)
+(* foaf:name \"Spiderman\", \"Человек-паук\"@ru . ", *)
+(* Parser.turtle, Ast.Turtle.pp) ; *)
diff --git a/lib/turtle/ast.ml b/lib/turtle/ast.ml
index d6dd013..76f6ef5 100644
--- a/lib/turtle/ast.ml
+++ b/lib/turtle/ast.ml
@@ -365,6 +365,4 @@ type parser_state = {
base_uri : Rdf.Iri.t;
namespaces : Rdf.Iri.t SMap.t;
bnode_labels : Blank_node.t SMap.t;
- cur_subject: Rdf.Triple.Subject.t;
- cur_predicate: Rdf.Triple.Predicate.t;
}
diff --git a/lib/turtle/parser.ml b/lib/turtle/parser.ml
index fd6de8a..0f89964 100644
--- a/lib/turtle/parser.ml
+++ b/lib/turtle/parser.ml
@@ -140,7 +140,8 @@ let object_ collection bnodps =
blank_node
);
(lift
- (fun iri -> Ast.Obj_iri iri)
+ (fun iri ->
+ Ast.Obj_iri iri)
iri);
]
@@ -161,7 +162,8 @@ let subject_ bnodps =
let collection = collection_ bnodps in
choice [
(lift
- (fun collection -> Ast.Sub_coll collection)
+ (fun collection ->
+ Ast.Sub_coll collection)
collection
);
(lift
diff --git a/lib/turtle/rdf_turtle.ml b/lib/turtle/rdf_turtle.ml
index 294513e..9581cd8 100644
--- a/lib/turtle/rdf_turtle.ml
+++ b/lib/turtle/rdf_turtle.ml
@@ -6,12 +6,16 @@ open Rdf
type ctxG = Ast.parser_state * Graph.t
-(* TODO use something that creates new blank_nodes (using an i or something?) *)
-let create_blank_node () = Blank_node.of_string "new_blank_node"
+(* 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)
-(* TODO *)
-let iri_of_iriref : Ast.parser_state -> Ast.Iriref.t -> Iri.t =
- fun _ctx iriref ->
+(* TODO do we need to do something with the ctx 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 ->
iriref
|> Ast.Iriref.to_string
|> Iri.of_string
@@ -23,11 +27,12 @@ let iri_of_resource : Ast.parser_state -> Ast.Iri.t -> Iri.t = fun
| Iriref str ->
str |> Iri.of_string
| Prefixed_name (a, b) ->
+
Iri.of_string (a ^ b)
(* TODO what should this be? *)
-let base_predicate =
- "base"
+let base_predicate =
+ "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
|> Iri.of_string
|> Triple.Predicate.of_iri
@@ -47,7 +52,7 @@ let first_pred =
|> Iri.of_string
|> Triple.Predicate.of_iri
-let rest_pred =
+let rest_pred =
"http://www.w3.org/1999/02/22-rdf-syntax-ns#last"
|> Iri.of_string
|> Triple.Predicate.of_iri
@@ -56,15 +61,17 @@ let rest_pred =
(* 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 ->
+ fun bnode (ctx, 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 g = Graph.add g (Triple.make sub_bnode rest_pred nil_obj) in
let new_bnode = create_blank_node () 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
@@ -75,13 +82,14 @@ let rec mk_collection : Blank_node.t -> ctxG -> Ast.object' list -> ctxG * Blank
and mk_collection_sub : ctxG -> Ast.object' list -> Triple.Subject.t * ctxG =
fun (ctx, g) objs ->
+ Fmt.pr "Im in mk_collection_sub; hi!@.";
match objs with
| [] -> nil_sub, (ctx, g)
| objs ->
let bnode = create_blank_node () 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)
+ sub_bnode, (ctx, g)
and mk_collection_obj : Triple.Subject.t -> Triple.Predicate.t -> ctxG -> Ast.object' list -> ctxG =
fun sub pred (ctx, g) objs ->
@@ -105,16 +113,17 @@ and mk_collection_obj : Triple.Subject.t -> Triple.Predicate.t -> ctxG -> Ast.ob
and insert_pred : Triple.Subject.t -> Triple.Predicate.t -> ctxG -> Ast.object' -> ctxG =
fun sub pred (ctx, g) obj ->
match obj with
- | Obj_iri i ->
+ | Obj_iri i ->
+ Fmt.pr "Im in insert_pred; hi!@.";
let rdf_obj = Triple.Object.of_iri (iri_of_resource ctx i) in
let g = Graph.add g (Triple.make sub pred rdf_obj) in
(ctx, g)
- | Obj_blank_node bnode ->
+ | Obj_blank_node bnode ->
let rdf_obj =
bnode
|> Blank_node.of_string
- |> Triple.Object.of_blank_node
- in
+ |> Triple.Object.of_blank_node
+ in
let g = Graph.add g (Triple.make sub pred rdf_obj) in
(ctx, g)
| Obj_literal { value; datatype; language } ->
@@ -129,7 +138,7 @@ and insert_pred : Triple.Subject.t -> Triple.Predicate.t -> ctxG -> Ast.object'
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
- | Obj_bnodps (BnodPs obj_predobjs) ->
+ | Obj_bnodps (BnodPs obj_predobjs) ->
let bnode = create_blank_node () in
let obj_bnode = Triple.Object.of_blank_node @@ bnode in
let sub_bnode = Triple.Subject.of_blank_node @@ bnode in
@@ -146,10 +155,10 @@ and insert_sub_predobj : Triple.Subject.t -> ctxG -> Ast.Predicate.t * (Ast.obje
match pred with
| Pred_iri i -> Triple.Predicate.of_iri @@ (iri_of_resource ctx i)
| Pred_a -> base_predicate
- in
+ in
List.fold_left (insert_pred sub pred) (ctx, g) objs
-let insert_sub_predobjs : ctxG -> Ast.subject -> Ast.predobjs -> ctxG =
+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 *)
@@ -171,17 +180,17 @@ let insert_sub_predobjs : ctxG -> Ast.subject -> Ast.predobjs -> ctxG =
in
List.fold_left (insert_sub_predobj sub) (ctx, g) predobjs
-let apply_statement : ctxG -> Ast.Statement.t -> ctxG =
- fun (ctx, g) s -> match s with
+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 =
+ let iri =
iriref
|> Ast.Iriref.to_string
- |> Iri.of_string
+ |> Iri.of_string
in
let ctx = { ctx with base_uri = iri } in
(ctx, g)
@@ -192,7 +201,7 @@ let apply_statement : ctxG -> Ast.Statement.t -> ctxG =
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
-
+
(* 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 5c75058..1feef57 100644
--- a/lib/turtle/rdf_turtle.mli
+++ b/lib/turtle/rdf_turtle.mli
@@ -230,8 +230,6 @@ module Ast : sig
base_uri : Rdf.Iri.t;
namespaces : Rdf.Iri.t SMap.t;
bnode_labels : Blank_node.t SMap.t;
- cur_subject : Rdf.Triple.Subject.t;
- cur_predicate: Rdf.Triple.Predicate.t;
}
end