(* Copyright (C) 2000, HELM Team. * * This file is part of HELM, an Hypertextual, Electronic * Library of Mathematics, developed at the Computer Science * Department, University of Bologna, Italy. * * HELM is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * HELM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HELM; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * * For details, see the HELM World-Wide-Web page, * http://cs.unibo.it/helm/. *) (* AUTOR: Ferruccio Guidi *) exception Found module M = MathQL module P = MQueryUtil module C = MQIConn module U = MQIUtil module L = MQILib module F = MQueryIO (* contexts *****************************************************************) type svar_context = (M.svar * M.resource_set) list type avar_context = (M.avar * M.resource) list type group_context = (M.avar * M.attribute_group) list type context = {svars: svar_context; avars: avar_context; groups: group_context (* auxiliary context *) } (* execute ******************************************************************) let execute h x = let warn q = if C.set h C.Warn then begin C.log h "MQIExecute: waring: reference to undefined variables: "; F.text_of_query (C.log h) "\n" q end in let proj v = List.map fst v in let rec eval_query c = function | M.Const r -> r | M.Dot i p -> begin try U.mql_subj (List.assoc p (List.assoc i c.groups)) with Not_found -> warn (M.Dot i p); [] end | M.Ex l y -> let rec ex_aux h = function | [] -> let d = {c with groups = h} in if eval_query d y = U.mql_false then () else raise Found | i :: tail -> begin try let (_, a) = List.assoc i c.avars in let rec add_group = function | [] -> () | g :: t -> ex_aux ((i, g) :: h) tail; add_group t in add_group a with Not_found -> () end in (try ex_aux [] l; U.mql_false with Found -> U.mql_true) | M.SVar i -> begin try List.assoc i c.svars with Not_found -> warn (M.SVar i); [] end | M.AVar i -> begin try [List.assoc i c.avars] with Not_found -> warn (M.AVar i); [] end | M.Let i x1 x2 -> let d = {c with svars = P.add_assoc (i, eval_query c x1) c.svars} in eval_query d x2 | M.For k i x1 x2 -> let f = match k with | M.GenFJoin -> U.mql_union | M.GenFMeet -> U.mql_intersect in let rec for_aux = function | [] -> [] | h :: t -> let d = {c with avars = P.add_assoc (i, h) c.avars} in f (eval_query d x2) (for_aux t) in for_aux (eval_query c x1) | M.Add b z x -> let f = if b then U.mql_prod else U.set_union in let g a s = (fst a, f (snd a) (eval_grp c z)) :: s in List.fold_right g (eval_query c x) [] | M.Property q0 q1 q2 mc ct cfl el pat y -> let subj, mct = if q0 then [], (pat, q2 @ mc, eval_query c y) else (q2 @ mc), (pat, [], eval_query c y) in let eval_cons (pat, p, y) = (pat, q2 @ p, eval_query c y) in let cons_true = mct :: List.map eval_cons ct in let cons_false = List.map (List.map eval_cons) cfl in let eval_exp (p, po) = (q2 @ p, po) in let exp = List.map eval_exp el in let t = P.start_time () in let r = MQIProperty.exec h q1 subj cons_true cons_false exp in let s = P.stop_time t in if C.set h C.Stat then C.log h (Printf.sprintf "Property: %s,%i\n" s (List.length r)); r | M.Select i x y -> let rec select_aux = function | [] -> [] | h :: t -> let d = {c with avars = P.add_assoc (i, h) c.avars} in if eval_query d y = U.mql_false then select_aux t else h :: select_aux t in select_aux (eval_query c x) | M.Fun p pl xl -> let e = {L.eval = eval_query c; L.conn = h} in L.eval e (F.text_out_spec (C.log h) "\n") p pl xl and eval_grp c = function | M.Attr gs -> let attr_aux g (p, y) = U.mql_union g [p, proj (eval_query c y)] in let attr_auxs s l = U.set_union s [List.fold_left attr_aux [] l] in List.fold_left attr_auxs [] gs | M.From i -> try snd (List.assoc i c.avars) with Not_found -> warn (M.AVar i); [] in let c = {svars = []; avars = []; groups = []} in let t = P.start_time () in let r = eval_query c x in let s = P.stop_time t in if C.set h C.Stat then C.log h (Printf.sprintf "MQIExecute: %s,%s\n" s (C.string_of_flags (C.flags h))); r