ocaml-lib / term.ml

(** Utility code for the representation and managing of taxonomic relations over terms. *)

open Token

(* type of data associated to a term *)
type t = {
    mutable sufs : Syntax.t_list LSet.t;  (* list of sufficient conditions *)
    mutable necs : Syntax.t_list LSet.t  (* list of necessary conditions *)
  }

(* dictionary of terms *)
let tbl : (string,t) Hashtbl.t = Hashtbl.create 1000

(* load and save *)

type data = (string * t) list

let init () =
  Hashtbl.clear tbl

let load (t : data) =
  Hashtbl.clear tbl;
  List.iter (fun (name,x) -> Hashtbl.add tbl name x) t

let save () =
  let t = Hashtbl.fold (fun name x l -> (name,x)::l) tbl [] in
  (t : data)

(* Invariants
   - a term absent from tbl has all fields equal to []
*)

(* get data about a term *)
let get : string -> t =
  fun name ->
    try Hashtbl.find tbl name
    with Not_found ->
      let data = {sufs = LSet.empty (); necs = LSet.empty ()} in
      Hashtbl.add tbl name data; data

let get_sufs : string -> Syntax.t_list list =
  fun name -> (get name).sufs

let get_necs : string -> Syntax.t_list list =
  fun name -> (get name).necs

(* add a sufficient condition *)
let add_suf : string -> Syntax.t_list -> unit =
  fun name suf ->
    let data = get name in
    data.sufs <- LSet.add suf data.sufs;
    match suf with
    | [Token.Term name2] ->
	let data2 = get name2 in
	data2.necs <- LSet.add [Token.Term name] data2.necs
    | _ -> ()

(* add a necessary condition *)
let add_nec : string -> Syntax.t_list -> unit =
  fun name nec ->
    let data = get name in
    data.necs <- LSet.add nec data.necs;
    match nec with
    | [Token.Term name2] ->
	let data2 = get name2 in
	data2.sufs <- LSet.add [Token.Term name] data2.sufs
    | _ -> ()

(*
(* add a relation between 2 terms *)
let add_suf_nec : string -> string -> unit =
  fun suf nec ->
    add_suf nec [Token.Term suf];
    add_nec suf [Token.Term nec]
*)

(* delete a sufficient condition *)
let del_suf : string -> Syntax.t_list -> unit =
  fun name suf ->
    let data = get name in
    data.sufs <- LSet.remove suf data.sufs;
    match suf with
    | [Token.Term name2] ->
	let data2 = get name2 in
	data2.necs <- LSet.remove [Token.Term name] data2.necs
    | _ -> ()

(* delete a necessary condition *)
let del_nec : string -> Syntax.t_list -> unit =
  fun name nec ->
    let data = get name in
    data.necs <- LSet.remove nec data.necs;
    match nec with
    | [Token.Term name2] ->
	let data2 = get name2 in
	data2.sufs <- LSet.remove [Token.Term name] data2.sufs
    | _ -> ()

(*
(* delete a relation between 2 terms *)
let del_suf_nec : string -> string -> unit =
  fun suf nec ->
    del_suf nec [Term suf];
    del_nec suf [Term nec]
*)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.