X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Focaml%2Fmathql_interpreter%2Fintersect.ml;h=73bebaa507c2300f5bd90dcc4c5001d887744eae;hb=89262281b6e83bd2321150f81f1a0583645eb0c8;hp=e346101cf9152a19599ba0241b853801ebf67d8b;hpb=2ef44e8d1a908a08d31e6114c15898ae7dc8109e;p=helm.git diff --git a/helm/ocaml/mathql_interpreter/intersect.ml b/helm/ocaml/mathql_interpreter/intersect.ml index e346101cf..73bebaa50 100644 --- a/helm/ocaml/mathql_interpreter/intersect.ml +++ b/helm/ocaml/mathql_interpreter/intersect.ml @@ -23,118 +23,53 @@ * http://cs.unibo.it/helm/. *) -(* - * implementazione del comando INTERSECT - *) -(* - * eccezione sollevata quando il join dei contesti - * deve essere vuoto - *) -exception Join_must_be_empty;; +(* Catenates two lists preserving order and getting rid of duplicates *) +let rec append (l1,l2) = + match l1,l2 with + [],_ -> l2 + | _,[] -> l1 + | s1::tl1, s2::_ when s1 < s2 -> s1::append (tl1,l2) + | s1::_, s2::tl2 when s2 < s1 -> s2::append (l1,tl2) + | s1::tl1, s2::tl2 -> s1::append (tl1,tl2) -(* - * join fra due contesti - *) -let xres_join_context h1 l1 h2 l2 = - match (l1, l2) with - ([], _) -> l2 - | (_, []) -> l1 - | (_, _) -> - let hh = h2 @ (List.find_all (function t -> not (List.mem t h2)) h1) - and m1 = List.combine h1 l1 - and m2 = List.combine h2 l2 - in - List.map - (fun elem -> - let value1 = try (List.assoc elem m1) with Not_found -> List.assoc elem m2 - and value2 = try (List.assoc elem m2) with Not_found -> List.assoc elem m1 - in - if value1 = value2 then value1 else raise Join_must_be_empty - ) - hh ;; -(* - * - *) -let intersect_tails h1 t1 h2 t2 = - let rec aux t1 t2 = - match (t1, t2) with - ([], _) - | (_, []) -> [] - | ((l1::tl1)::tll1, (l2::tl2)::tll2) -> - if l1 = l2 then - try - (*match xres_join_context h1 tl1 h2 tl2 with - [] -> aux tll1 tll2 - | t -> (l1::(xres_join_context h1 tl1 h2 tl2))::(aux tll1 tll2)*) - (l1::(tl1 @ tl2))::(aux tll1 tll2) - with - Join_must_be_empty -> aux tll1 tll2 - else - if l1 < l2 then - aux tll1 t2 - else - aux t1 tll2 - | _ -> assert false - in - aux t1 t2 + +(* Sums two attribute groups preserving order *) +let rec sum_groups(gr1, gr2) = + match gr1, gr2 with + [],_ -> gr2 + | _,[] -> gr1 + | gr1, gr2 when gr1 = gr2 -> gr1 + | (key1,l1)::tl1, (key2,l2)::_ when key1 < key2 -> (key1,l1)::(sum_groups (tl1,gr2)) + | (key1,l1)::_, (key2,l2)::tl2 when key2 < key1 -> (key2,l2)::(sum_groups (gr1,tl2)) + | (key1,l1)::tl1, (key2,l2)::tl2 -> (key1,(append (l1,l2)))::(sum_groups (tl1,tl2)) + ;; -(* - * implementazione del comando INTERSECT - *) -let intersect_ex l1 l2 = - let _ = print_string ("INTERSECT ") - and t = Unix.time () in - let result = - match (l1, l2) with - ((head1::tail1), (head2::tail2)) -> - (match (head1, head2) with - ([], _) -> assert false (* gli header non devono mai essere vuoti *) - | (_, []) -> assert false (* devono contenere almeno [retVal] *) - | (_, _) -> - (match (tail1, tail2) with - ([], _) -> [["retVal"]] (* se una delle due code e' vuota... *) - | (_, []) -> [["retVal"]] (* ... l'intersezione e' vuota *) - | (_, _) -> - [head2 @ - (List.find_all - (function t -> not (List.mem t head2)) - head1 - ) - ] (* header del risultato finale *) - @ - intersect_tails (List.tl head1) tail1 (List.tl head2) tail2 - (* - List.fold_left - (fun par1 elem1 -> par1 @ - List.map - (fun elem2 -> - [(List.hd elem1)] @ - (xres_join_context (List.tl head1) (List.tl elem1) - (List.tl head2) (List.tl elem2) - ) - ) - (List.find_all (* *) - (fun elem2 -> (* trova tutti gli elementi della lista tail2 *) - ((List.hd elem1) = (List.hd elem2)) && (* che stanno in tail1 *) - not ((xres_join_context (List.tl head1) (List.tl elem1) - (List.tl head2) (List.tl elem2)) = []) - (* e per i quali la xres_join_context non sia vuota *) - ) - tail2 (* List.find_all *) - ) - ) - [] - tail1 (* per ogni elemento di tail1 applica la List.fold_left *) - *) - ) (* match *) - ) - | _ -> [] - in - let _ = print_endline (string_of_float (Unix.time () -. t)); flush stdout in - result +(* Product between an attribute set and a group of attributes *) +let rec sub_prod (aset, gr) = (*prende un aset e un gr, fa la somma tra tutti i gruppi di aset e gr*) + match aset with + [] -> [] + | gr1::tl1 -> sum_groups (gr1, gr)::(sub_prod(tl1, gr)) +;; + + +(* Cartesian product between two attribute sets*) +let rec prod (as1, as2) = + match as1, as2 with + [],_ -> [] + | _,[] -> [] + | gr1::tl1, _ -> append((sub_prod (as2, gr1)), (prod (tl1, as2))) (* chiamo la sub_prod con un el. as1 e as2 *) ;; +(* Intersection between two resource sets, preserves order and gets rid of duplicates *) +let rec intersect_ex rs1 rs2 = + match (rs1, rs2) with + [],_ + | _,[] -> [] + | (uri1,_)::tl1, (uri2,_)::_ when uri1 < uri2 -> intersect_ex tl1 rs2 + | (uri1,_)::_, (uri2,_)::tl2 when uri2 < uri1 -> intersect_ex rs1 tl2 + | (uri1,as1)::tl1, (uri2,as2)::tl2 -> (uri1, prod(as1,as2))::intersect_ex tl1 tl2 +;;