]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/metadata/metadataConstraints.ml
fixed Makefile
[helm.git] / helm / ocaml / metadata / metadataConstraints.ml
index bedc2db86f20ec1be0301fb96cd75b476f7d2144..da4f271b5ae6be34936d6c14cd02b4b6a21a80ab 100644 (file)
@@ -24,6 +24,7 @@
  *)
 
 open Printf
+open MetadataTypes 
 
 let critical_value = 7
 let just_factor = 4
@@ -36,6 +37,17 @@ type term_signature = (string * string list) option * StringSet.t
 type cardinality_condition =
   | Eq of int
   | Gt of int
+  | Lt of int
+
+type rating_criterion =
+  [ `Hits   (** order by number of hits, most used objects first *)
+  ]
+
+let default_tables =
+   (library_obj_tbl,library_rel_tbl,library_sort_tbl,library_count_tbl)
+
+let current_tables () = 
+  (obj_tbl (),rel_tbl (),sort_tbl (), count_tbl ())
 
 let tbln n = "table" ^ string_of_int n
 
@@ -66,98 +78,125 @@ let mk_positions positions cur_tbl =
       (positions :> MetadataTypes.position list)) ^
   ")"
 
-let add_card_constr tbl (n,from,where) = function
-  | None -> (n,from,where)
-  | Some (Eq card) ->
-      let cur_tbl = tbln n in
-      (n+1,
-       (sprintf "%s as %s" tbl cur_tbl :: from),
-       (sprintf "%s.no=%d" cur_tbl card ::
-        (if n=0 then []
-        else [sprintf "table0.source = %s.source" cur_tbl]) @
-        where))
-  | Some (Gt card) ->
+let explode_card_constr = function
+  | Eq card -> "=", card
+  | Gt card -> ">", card
+  | Lt card -> "<", card
+
+let add_card_constr tbl col where = function
+  | None -> where
+  | Some constr ->
+      let op, card = explode_card_constr constr in
+      (* count(_utente).hypothesis = 3 *)
+      (sprintf "%s.%s %s %d" tbl col op card :: where)
+
+let add_diff_constr tbl where = function
+  | None -> where
+  | Some constr ->
+      let op, card = explode_card_constr constr in
+      (sprintf "%s.hypothesis - %s.conclusion %s %d" tbl tbl op card :: where)
+      
+let add_all_constr ?(tbl=library_count_tbl) (n,from,where) concl full diff =
+  match (concl, full, diff) with
+  | None, None, None -> (n,from,where)
+  | _ -> 
       let cur_tbl = tbln n in
-      (n+1,
-       (sprintf "%s as %s" tbl cur_tbl :: from),
-       (sprintf "%s.no>%d" cur_tbl card ::
-        (if n=0 then []
-        else [sprintf "table0.source = %s.source" cur_tbl]) @
-        where))
-
-let at_least ~(dbd:Mysql.dbd) ?concl_card ?full_card
+      let from = (sprintf "%s as %s" tbl cur_tbl) :: from in
+      let where = add_card_constr cur_tbl "conclusion" where concl in
+      let where = add_card_constr cur_tbl "statement" where full in
+      let where = add_diff_constr cur_tbl where diff in
+      (n+2,from, 
+        (if n > 0 then 
+          sprintf "table0.source = %s.source" cur_tbl :: where 
+        else
+          where))
+      
+
+let add_constraint ?(start=0) ?(tables=default_tables) (n,from,where) metadata =
+  let obj_tbl,rel_tbl,sort_tbl,count_tbl = tables 
+  in
+  let cur_tbl = tbln n in
+  let start_table = tbln start in
+  match metadata with
+  | `Obj (uri, positions) ->
+      let from = (sprintf "%s as %s" obj_tbl cur_tbl) :: from in
+      let where = 
+        (sprintf "(%s.h_occurrence = \"%s\")" cur_tbl uri) ::
+        mk_positions positions cur_tbl ::
+        (if n=start then []
+        else [sprintf "%s.source = %s.source" start_table cur_tbl]) @ 
+        where
+      in
+      ((n+2), from, where)
+  | `Rel positions ->
+      let from = (sprintf "%s as %s" rel_tbl cur_tbl) :: from in
+      let where =
+        mk_positions positions cur_tbl ::
+        (if n=start then []
+        else [sprintf "%s.source = %s.source" start_table cur_tbl]) @ 
+        where
+      in
+      ((n+2), from, where)
+  | `Sort (sort, positions) ->
+      let sort_str = CicPp.ppsort sort in
+      let from = (sprintf "%s as %s" sort_tbl cur_tbl) :: from in
+      let where =
+        (sprintf "%s.h_sort = \"%s\"" cur_tbl sort_str ) ::
+            mk_positions positions cur_tbl ::
+        (if n=start then 
+          []
+        else 
+          [sprintf "%s.source = %s.source" start_table cur_tbl ]) @ where
+      in
+      ((n+2), from, where)
+
+let exec ~(dbd:Mysql.dbd) ?rating (n,from,where) =
+let from = String.concat ", " from in
+  let where = String.concat " and " where in
+  let query =
+    match rating with
+    | None -> sprintf "select table0.source from %s where %s" from where
+    | Some `Hits ->
+        sprintf
+          ("select table0.source from %s, hits where %s
+            and table0.source = hits.source order by hits.no desc")
+          from where 
+  in
+  prerr_endline query; 
+  let result = Mysql.exec dbd query in
+  Mysql.map result
+    (fun row -> match row.(0) with Some s -> s | _ -> assert false)
+
+
+let at_least ~(dbd:Mysql.dbd) ?concl_card ?full_card ?diff ?rating tables
   (metadata: MetadataTypes.constr list)
 =
+  let obj_tbl,rel_tbl,sort_tbl, count_tbl = tables 
+  in
   if (metadata = []) && concl_card = None && full_card = None then
     failwith "MetadataQuery.at_least: no constraints given";
-  let add_constraint (n,from,where) metadata =
-    let cur_tbl = tbln n in
-    let cur_ltbl = tbln (n+1) in
-    match metadata with
-    | `Obj (uri, positions) ->
-        let tbl = MetadataTypes.obj_tbl () in
-        let ltbl = MetadataTypes.library_obj_tbl in
-        let from = 
-          (sprintf "%s AS %s, %s AS %s" tbl cur_tbl ltbl cur_ltbl) :: from 
-        in
-        let where =
-          (sprintf "(%s.h_occurrence = \"%s\" OR %s.h_occurrence = \"%s\")" 
-          cur_tbl uri cur_ltbl uri) ::
-          mk_positions positions cur_tbl ::
-          (if n=0 then []
-          else [sprintf 
-            "(table0.source = %s.source AND table0.source = %s.source)" 
-            cur_tbl cur_ltbl]) @ where
-        in
-        ((n+2), from, where)
-    | `Rel positions ->
-        let tbl = MetadataTypes.rel_tbl () in
-        let ltbl = MetadataTypes.library_rel_tbl in
-        let from = 
-          (sprintf "%s AS %s, %s AS %s" tbl cur_tbl ltbl cur_ltbl) :: from 
-        in
-        let where =
-          mk_positions positions cur_tbl ::
-          (if n=0 then []
-          else [sprintf 
-            "(table0.source = %s.source AND table0.source = %s.source)" 
-             cur_tbl cur_ltbl]) @ where
-        in
-        ((n+2), from, where)
-    | `Sort (sort, positions) ->
-        let tbl = MetadataTypes.sort_tbl () in
-        let ltbl = MetadataTypes.library_sort_tbl in
-        let sort_str = CicPp.ppsort sort in
-        let from = 
-          (sprintf "%s AS %s, %s AS %s" tbl cur_tbl ltbl cur_ltbl) :: from 
-        in
-        let where =
-          (sprintf "(%s.h_sort = \"%s\" OR %s.h_sort = \"%s\")" 
-            cur_tbl sort_str cur_ltbl sort_str) ::
-              mk_positions positions cur_tbl ::
-          (if n=0 then 
-            []
-          else 
-            [sprintf 
-              "(table0.source = %s.source AND table0.source = %s.source)" 
-              cur_tbl cur_ltbl]) @ where
-        in
-        ((n+2), from, where)
-  in
-  let (n,from,where) = List.fold_left add_constraint (0,[],[]) metadata in
   let (n,from,where) =
-    add_card_constr (MetadataTypes.conclno_tbl ()) (n,from,where) concl_card
+    List.fold_left (add_constraint ~tables) (0,[],[]) metadata
   in
   let (n,from,where) =
-    add_card_constr (MetadataTypes.conclno_hyp_tbl ()) (n,from,where) full_card
+    add_all_constr ~tbl:count_tbl (n,from,where) concl_card full_card diff
   in
-  let from = String.concat ", " from in
-  let where = String.concat " and " where in
-  let query = sprintf "select table0.source from %s where %s" from where in
-  let result = Mysql.exec dbd query in
-  Mysql.map result
-    (fun row -> match row.(0) with Some s -> s | _ -> assert false)
-
+  exec ~dbd ?rating (n,from,where)
+    
+let at_least  
+  ~(dbd:Mysql.dbd) ?concl_card ?full_card ?diff ?rating
+      (metadata: MetadataTypes.constr list)
+=
+  if are_tables_ownerized () then
+    (at_least 
+       ~dbd ?concl_card ?full_card ?diff ?rating default_tables metadata) @
+    (at_least 
+       ~dbd ?concl_card ?full_card ?diff ?rating (current_tables ()) metadata)
+  else
+    at_least 
+      ~dbd ?concl_card ?full_card ?diff ?rating default_tables metadata 
+  
+    
   (** Prefix handling *)
 
 let filter_by_card n =
@@ -395,16 +434,20 @@ let get_constants (dbd:Mysql.dbd) ~where uri =
   let uri = escape uri in
   let positions =
     match where with
-    | `Conclusion -> ["\"MainConclusion\""; "\"InConclusion\""]
+    | `Conclusion -> [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos ]
     | `Statement ->
-        ["\"MainConclusion\""; "\"InConclusion\""; "\"InHypothesis\"";
-         "\"MainHypothesis\""]
+        [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos;
+          MetadataTypes.inhyp_pos; MetadataTypes.mainhyp_pos ]
   in
   let query = 
-    sprintf ("SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s) UNION"^^
+    let pos_predicate =
+      String.concat " OR "
+        (List.map (fun pos -> sprintf "(h_position = \"%s\")" pos) positions)
+    in
+    sprintf ("SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s) UNION "^^
              "SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s)")
-      (MetadataTypes.obj_tbl ()) uri (String.concat " OR " positions)
-      MetadataTypes.library_obj_tbl uri (String.concat " OR " positions)
+      (MetadataTypes.obj_tbl ()) uri pos_predicate
+      MetadataTypes.library_obj_tbl uri pos_predicate
       
   in
   let result = Mysql.exec dbd query in
@@ -427,7 +470,7 @@ let myspeciallist_of_facts  =
   [0,"cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)"]
 let myspeciallist =
   [0,"cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
-(*   0,"cic:/Coq/Init/Logic/sym_eq.con"; *)
+   (* 0,"cic:/Coq/Init/Logic/sym_eq.con"; *)
    0,"cic:/Coq/Init/Logic/trans_eq.con";
    0,"cic:/Coq/Init/Logic/f_equal.con";
    0,"cic:/Coq/Init/Logic/f_equal2.con";