grew_node.ml 2.99 KB
Newer Older
1 2
open Printf

bguillaum's avatar
bguillaum committed
3 4
open Grew_utils
open Grew_ast
pj2m's avatar
pj2m committed
5 6 7
open Grew_edge
open Grew_fs

8 9
(* ================================================================================ *)
module G_node = struct
pj2m's avatar
pj2m committed
10
  type t = {
11
      fs: G_fs.t;
12 13
      pos: int option;
      next: G_edge.t Massoc.t;
pj2m's avatar
pj2m committed
14 15
    }

16 17 18 19 20
  let get_fs t = t.fs
  let get_next t = t.next

  let set_fs t fs = {t with fs = fs}

21
  let empty = { fs = G_fs.empty; pos = None; next = Massoc.empty }
22 23

  let to_string t = 
bguillaum's avatar
bguillaum committed
24
    Printf.sprintf "  fs=[%s]\n  next=%s\n" 
25
      (G_fs.to_string t.fs)
26 27 28 29 30
      (Massoc.to_string G_edge.to_string t.next)

  let to_gr t = 
    sprintf "%s [%s] " 
      (match t.pos with Some i -> sprintf "(%d)" i | None -> "")
31
      (G_fs.to_gr t.fs)
32 33 34 35 36

  let add_edge g_edge gid_tar t =
    match Massoc.add gid_tar g_edge t.next with
    | Some l -> Some {t with next = l}
    | None -> None
pj2m's avatar
pj2m committed
37

38
  let build (ast_node, loc) =
pj2m's avatar
pj2m committed
39
    (ast_node.Ast.node_id, 
40
     { fs = G_fs.build ast_node.Ast.fs;
pj2m's avatar
pj2m committed
41 42 43
       pos = ast_node.Ast.position;
       next = Massoc.empty;
     } )
44

bguillaum's avatar
bguillaum committed
45
  let of_conll line = {
46
      fs = G_fs.of_conll line;
bguillaum's avatar
bguillaum committed
47 48 49 50 51
      pos = Some line.Conll.num;
      next = Massoc.empty;
    }
    
    
52 53 54 55 56
  let remove id_tar label t = {t with next = Massoc.remove id_tar label t.next}
      
  let remove_key node_id t = 
    try {t with next = Massoc.remove_key node_id t.next} with Not_found -> t

bguillaum's avatar
bguillaum committed
57
  let merge_key ?(strict=false) src_id tar_id t = 
58
    try Some {t with next = Massoc.merge_key src_id tar_id t.next}
bguillaum's avatar
bguillaum committed
59
    with Massoc.Duplicate -> if strict then None else Some t
60

bguillaum's avatar
bguillaum committed
61
  let shift_out ?(strict=false) src_t tar_t = 
62
    try Some {tar_t with next = Massoc.disjoint_union src_t.next tar_t.next}
bguillaum's avatar
bguillaum committed
63
    with Massoc.Not_disjoint -> if strict then None else Some tar_t
64 65 66 67 68 69 70 71 72 73 74 75 76

  let rm_out_edges t = {t with next = Massoc.empty}


  let build_neighbour t = {empty with pos = match t.pos with Some x -> Some (x+1) | None -> None}

  let pos_comp n1 n2 = Pervasives.compare n1.pos n2.pos
end
(* ================================================================================ *)

(* ================================================================================ *)
module P_node = struct
  type t = {
bguillaum's avatar
bguillaum committed
77
      name: Id.name;
78
      fs: P_fs.t;
79
      next: P_edge.t Massoc.t;
bguillaum's avatar
bguillaum committed
80
      loc: Loc.t option;
81 82
    }

bguillaum's avatar
bguillaum committed
83
  let get_name t = t.name
84 85 86
  let get_fs t = t.fs
  let get_next t = t.next

bguillaum's avatar
bguillaum committed
87 88
  let unif_fs fs t = { t with fs = P_fs.unif fs t.fs }

bguillaum's avatar
bguillaum committed
89
  let empty = { fs = P_fs.empty; next = Massoc.empty; name = ""; loc=None   }
90
        
91
  let build ?pat_vars (ast_node, loc) =
92
    (ast_node.Ast.node_id, 
bguillaum's avatar
bguillaum committed
93 94 95
     { 
       name = ast_node.Ast.node_id;
       fs = P_fs.build ?pat_vars ast_node.Ast.fs;
96
       next = Massoc.empty;
bguillaum's avatar
bguillaum committed
97
       loc = Some loc;
98 99 100 101 102 103
     } )

  let add_edge p_edge pid_tar t =
    match Massoc.add pid_tar p_edge t.next with
    | Some l -> Some {t with next = l}
    | None -> None
pj2m's avatar
pj2m committed
104

105
  let match_ ?param p_node g_node = P_fs.match_ ?param p_node.fs (G_node.get_fs g_node)
pj2m's avatar
pj2m committed
106

bguillaum's avatar
bguillaum committed
107
  let compare_pos t1 t2 = Pervasives.compare t1.loc t2.loc
pj2m's avatar
pj2m committed
108
end
109 110 111 112
(* ================================================================================ *)