Commit 92889a5e authored by Josh Kunz's avatar Josh Kunz

Fixes minor query bug.

parent bd006382
open Printf
open String
open Hashtbl
open List
......@@ -28,8 +29,10 @@ let madd_fact g f =
let mremove_fact g f =
let rec mremove_edge l e =
match l with
| [] -> []
| {out = o; label = la} :: l when o = e.out && la = e.label -> l
| [] -> print_endline "nothing removed."; []
| {out = o; label = la} :: l when o = e.out && la = e.label ->
print_endline "something removed.";
l
| _e :: l -> _e :: mremove_edge l e
in
let e = {out = f.tail; label = f.rel} in
......
......@@ -2,7 +2,7 @@
OCAMLLEX_FLAGS =
OCAMLYACC_FLAGS =
OCAMLFLAGS = -I ../include/
OCAMLFLAGS = -thread -I ../include/
bin = nagad
......
open Sys
open Printf
open Common
open List
open Fact
open KG
open Query
open HTTP
open Thread
open Mutex
open Mutex;;
open Array
let g = KG.empty () in
KG.madd_fact g {head="a"; rel="b"; tail="c"};
KG.madd_fact g {head="c"; rel="b"; tail="a"};
KG.madd_fact g {head="c"; rel="b"; tail="c"};
KG.madd_fact g {head="c"; rel="b"; tail="a"};
let query = [{head=Variable "A"; rel=Variable "B"; tail=Variable "C"};
{head=Variable "C"; rel=Variable "B"; tail=Value "a"}] in
let result = Query.query_graph g query in
printf "Result count: %d\n" (List.length result);
(*
let main () =
let eval_prog = eval_program handle_query handle_statement in
match Sys.argv with
......@@ -17,3 +26,4 @@ let main () =
| _ -> print_endline "Unrecognized flags.";;
main ();;
*)
open Printf
open List
open KG
......@@ -49,6 +50,8 @@ let field_match (qf, ef) context =
let epairs (q : query_triple) (e : KG.fact) =
[(q.head, e.head); (q.rel, e.rel); (q.tail, e.tail)];;
let fact_as_string (f : KG.fact) = sprintf "fact(%s, %s, %s)" f.head f.rel f.tail;;
(* Checks to see if a given set of pairs 'is a match'. That is to say, that
* all constants are equal, and that all variables can be bound meaningfully.
* This function yeilds true when the pairs all match, and a context that
......@@ -60,11 +63,16 @@ let is_match pairs context =
List.fold_left match_item (true, context) pairs;;
let matches_for graph q context =
printf "Matching with query %s and context: %s\n"
(qtriple_as_string q) (Context.as_string context);
KG.all_facts graph |> List.map fact_as_string |>
String.concat "\n" |> printf "Graph looks like: \n%s\n";
let facts_for_query graph query =
match query.head with
| Value x -> KG.facts_off graph x
| Variable x when Context.mem context x ->
KG.facts_off graph x
printf "Matching bound variable case...\n";
Context.binding context x |> KG.facts_off graph;
(* If the head of the query is an unbound variable, then we have
* to check against the entire graph *)
| Variable x -> KG.all_facts graph
......@@ -72,16 +80,28 @@ let matches_for graph q context =
let rec match_facts = function
| [] -> []
| fact :: facts ->
printf "Matching fact %s with query %s and context: %s\n"
(fact_as_string fact) (qtriple_as_string q) (Context.as_string context);
let (did_match, _context) = is_match (epairs q fact) context in
if did_match then
begin
print_endline "Matched!";
(fact, _context) :: (match_facts facts)
end
else
begin
print_endline "Not matched...";
(match_facts facts)
end
in
facts_for_query graph q |> match_facts;;
let test_facts = facts_for_query graph q in
List.map fact_as_string test_facts |>
String.concat "\n" |> printf "Matching against facts: \n%s\n";
test_facts |> match_facts;;
(* -> (graph, context) *)
let rec rquery_graph graph query context path =
Graph.length graph |> printf "Checking against graph of size: %d\n" ;
match query with
| [] -> [(path, context)]
| q :: qs ->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment