grew_rule.ml 53.2 KB
Newer Older
bguillaum's avatar
bguillaum committed
1 2 3 4 5 6 7 8 9 10
(**********************************************************************************)
(*    Libcaml-grew - a Graph Rewriting library dedicated to NLP applications      *)
(*                                                                                *)
(*    Copyright 2011-2013 Inria, Université de Lorraine                           *)
(*                                                                                *)
(*    Webpage: http://grew.loria.fr                                               *)
(*    License: CeCILL (see LICENSE folder or "http://www.cecill.info")            *)
(*    Authors: see AUTHORS file                                                   *)
(**********************************************************************************)

pj2m's avatar
pj2m committed
11
open Log
bguillaum's avatar
bguillaum committed
12
open Printf
pj2m's avatar
pj2m committed
13

14 15
open Dep2pict

16
open Grew_base
17
open Grew_types
bguillaum's avatar
bguillaum committed
18
open Grew_ast
19
open Grew_domain
pj2m's avatar
pj2m committed
20
open Grew_edge
bguillaum's avatar
bguillaum committed
21
open Grew_fs
pj2m's avatar
pj2m committed
22
open Grew_node
bguillaum's avatar
bguillaum committed
23 24
open Grew_command
open Grew_graph
pj2m's avatar
pj2m committed
25

26
(* ================================================================================ *)
pj2m's avatar
pj2m committed
27 28
module Instance = struct
  type t = {
29 30 31
    graph: G_graph.t;
    history: Command.h list;
    rules: string list;
32
    big_step: Libgrew_types.big_step option;
33 34
  }

Bruno Guillaume's avatar
Bruno Guillaume committed
35 36 37 38 39
  let swap t =
    match t.big_step with
    | None -> t
    | Some bs -> {t with big_step = Some (Libgrew_types.swap bs) }

40
  let empty = {graph = G_graph.empty; rules=[]; history=[]; big_step=None; }
41

42
  let from_graph graph = {empty with graph}
pj2m's avatar
pj2m committed
43

bguillaum's avatar
bguillaum committed
44
  let rev_steps t =
pj2m's avatar
pj2m committed
45
    { t with big_step = match t.big_step with
46
      | None -> None
47
      | Some bs -> Some {bs with Libgrew_types.small_step = List.rev bs.Libgrew_types.small_step }
pj2m's avatar
pj2m committed
48 49
    }

50 51
  let refresh t = { empty with graph=t.graph }

52
  (* comparison is done on the list of commands *)
bguillaum's avatar
bguillaum committed
53
  (* only graph rewritten from the same init graph can be "compared" *)
54
  let compare t1 t2 = Pervasives.compare t1.history t2.history
pj2m's avatar
pj2m committed
55

bguillaum's avatar
bguillaum committed
56
  let to_gr ?domain t = G_graph.to_gr ?domain t.graph
57

bguillaum's avatar
bguillaum committed
58
  let to_conll_string ?domain t = G_graph.to_conll_string ?domain t.graph
bguillaum's avatar
bguillaum committed
59

bguillaum's avatar
bguillaum committed
60 61
  let save_dot_png ?domain ?filter ?main_feat base t =
    ignore (Dot.to_png_file (G_graph.to_dot ?domain ?main_feat t.graph) (base^".png"))
62

bguillaum's avatar
bguillaum committed
63 64
  let save_dep_png ?domain ?filter ?main_feat base t =
    let dep = G_graph.to_dep ?domain ?filter ?main_feat t.graph in
65 66 67
    let d2p = Dep2pict.from_dep ~dep in
    let _ = Dep2pict.save_png ~filename: (base^".png") d2p in
    Dep2pict.highlight_shift ()
68

bguillaum's avatar
bguillaum committed
69 70
  let save_dep_svg ?domain ?filter ?main_feat base t =
    let dep = G_graph.to_dep ?domain ?filter ?main_feat t.graph in
71 72 73
    let d2p = Dep2pict.from_dep ~dep in
    let _ = Dep2pict.save_svg ~filename: (base^".png") d2p in
    Dep2pict.highlight_shift ()
74
end (* module Instance *)
pj2m's avatar
pj2m committed
75

76
(* ================================================================================ *)
77
module Instance_set = Set.Make (Instance)
pj2m's avatar
pj2m committed
78

79
(* ================================================================================ *)
bguillaum's avatar
bguillaum committed
80
module Rule = struct
81

bguillaum's avatar
bguillaum committed
82
  (* the rewriting depth is bounded to stop rewriting when the system is not terminating *)
bguillaum's avatar
bguillaum committed
83 84
  let max_depth_det = ref 2000
  let max_depth_non_det = ref 100
bguillaum's avatar
bguillaum committed
85
  let debug_loop = ref false
86 87 88

  let set_max_depth_det value = max_depth_det := value
  let set_max_depth_non_det value = max_depth_non_det := value
bguillaum's avatar
bguillaum committed
89
  let set_debug_loop () = debug_loop := true
pj2m's avatar
pj2m committed
90

91
  type const =
bguillaum's avatar
bguillaum committed
92 93
    | Cst_out of Pid.t * Label_cst.t
    | Cst_in of Pid.t * Label_cst.t
94 95
    | Features_eq of Pid.t * string * Pid.t * string
    | Features_diseq of Pid.t * string * Pid.t * string
96
    (* *)
97
    | Feature_eq_cst of Pid.t * string * string
98
    | Feature_diff_cst of Pid.t * string * string
99
    (* *)
100
    | Feature_eq_float of Pid.t * string * float
101
    | Feature_diff_float of Pid.t * string * float
102
    (* *)
103
    | Feature_eq_regexp of Pid.t * string * string
104
    (* *)
105
    | Features_ineq of Ast.ineq * Pid.t * string * Pid.t * string
106
    | Feature_ineq_cst of Ast.ineq * Pid.t * string * float
107
    (* *)
bguillaum's avatar
bguillaum committed
108
    | Filter of Pid.t * P_fs.t (* used when a without impose a fs on a node defined by the match basic *)
109
    (* *)
110 111
    | Immediate_prec of Pid.t * Pid.t
    | Large_prec of Pid.t * Pid.t
bguillaum's avatar
bguillaum committed
112

113 114 115
  let const_to_json ?domain = function
  | Cst_out (pid, label_cst) -> `Assoc ["cst_out", Label_cst.to_json ?domain label_cst]
  | Cst_in (pid, label_cst) -> `Assoc ["cst_in", Label_cst.to_json ?domain label_cst]
116
  | Features_eq (pid1,fn1,pid2,fn2) ->
117 118 119 120 121 122 123 124
    `Assoc ["features_eq",
      `Assoc [
        ("id1", `String (Pid.to_string pid1));
        ("feature_name_1", `String fn1);
        ("id2", `String (Pid.to_string pid2));
        ("feature_name_2", `String fn2);
      ]
    ]
125
  | Features_diseq (pid1,fn1,pid2,fn2) ->
126 127 128 129 130 131 132 133
    `Assoc ["features_diseq",
      `Assoc [
        ("id1", `String (Pid.to_string pid1));
        ("feature_name_1", `String fn1);
        ("id2", `String (Pid.to_string pid2));
        ("feature_name_2", `String fn2);
      ]
    ]
134
  | Feature_eq_cst (pid,fn,value) ->
135 136 137 138 139 140 141 142
    `Assoc ["feature_eq_cst",
      `Assoc [
        ("id", `String (Pid.to_string pid));
        ("feature_name_", `String fn);
        ("value", `String value);
      ]
    ]
  | Feature_diff_cst (pid,fn,value) ->
143
    `Assoc ["feature_diff_cst",
144 145 146 147 148 149
      `Assoc [
        ("id", `String (Pid.to_string pid));
        ("feature_name_", `String fn);
        ("value", `String value);
      ]
    ]
150
  | Feature_eq_float (pid,fn,value) ->
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    `Assoc ["feature_eq_float",
      `Assoc [
        ("id", `String (Pid.to_string pid));
        ("feature_name_", `String fn);
        ("value", `String (string_of_float value));
        ]
    ]
  | Feature_diff_float (pid,fn,value) ->
    `Assoc ["feature_diff_float",
      `Assoc [
        ("id", `String (Pid.to_string pid));
        ("feature_name", `String fn);
        ("value", `String (string_of_float value));
      ]
    ]
166
  | Feature_eq_regexp (pid,fn,regexp) ->
167 168 169 170 171 172 173
    `Assoc ["feature_eq_regexp",
      `Assoc [
        ("id", `String (Pid.to_string pid));
        ("feature_name", `String fn);
        ("regexp", `String regexp);
      ]
    ]
174
  | Features_ineq (ineq,pid1,fn1,pid2,fn2) ->
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    `Assoc ["features_ineq",
      `Assoc [
        ("ineq", `String (Ast.string_of_ineq ineq));
        ("id1", `String (Pid.to_string pid1));
        ("feature_name_1", `String fn1);
        ("id2", `String (Pid.to_string pid2));
        ("feature_name_2", `String fn2);
      ]
    ]
  | Feature_ineq_cst (ineq,pid,fn,value) ->
    `Assoc ["feature_ineq_cst",
      `Assoc [
        ("ineq", `String (Ast.string_of_ineq ineq));
        ("id", `String (Pid.to_string pid));
        ("feature_name", `String fn);
        ("value", `String (string_of_float value));
      ]
    ]
  | Filter (pid, p_fs) ->
    `Assoc ["filter",
      `Assoc [
        ("id", `String (Pid.to_string pid));
        ("fs", P_fs.to_json ?domain p_fs);
      ]
    ]
200
  | Immediate_prec (pid1, pid2) ->
201 202 203 204 205 206
    `Assoc ["immediate_prec",
      `Assoc [
        ("id1", `String (Pid.to_string pid1));
        ("id2", `String (Pid.to_string pid2));
      ]
    ]
207
  | Large_prec (pid1, pid2) ->
208 209 210 211 212 213 214
    `Assoc ["large_prec",
      `Assoc [
        ("id1", `String (Pid.to_string pid1));
        ("id2", `String (Pid.to_string pid2));
      ]
    ]

bguillaum's avatar
bguillaum committed
215
  let build_pos_constraint ?domain pos_table const =
bguillaum's avatar
bguillaum committed
216 217
    let pid_of_name loc node_name = Pid.Pos (Id.build ~loc node_name pos_table) in
    match const with
218
      | (Ast.Cst_out (id,label_cst), loc) ->
bguillaum's avatar
bguillaum committed
219
        Cst_out (pid_of_name loc id, Label_cst.build ~loc ?domain label_cst)
220
      | (Ast.Cst_in (id,label_cst), loc) ->
bguillaum's avatar
bguillaum committed
221
        Cst_in (pid_of_name loc id, Label_cst.build ~loc ?domain label_cst)
222

223
      | (Ast.Features_eq ((node_name1, feat_name1), (node_name2, feat_name2)), loc) ->
bguillaum's avatar
bguillaum committed
224 225
        Domain.check_feature_name ?domain ~loc feat_name1;
        Domain.check_feature_name ?domain ~loc feat_name2;
226
        Features_eq (pid_of_name loc node_name1, feat_name1, pid_of_name loc node_name2, feat_name2)
227

228
      | (Ast.Features_diseq ((node_name1, feat_name1), (node_name2, feat_name2)), loc) ->
bguillaum's avatar
bguillaum committed
229 230
        Domain.check_feature_name ?domain ~loc feat_name1;
        Domain.check_feature_name ?domain ~loc feat_name2;
231
        Features_diseq (pid_of_name loc node_name1, feat_name1, pid_of_name loc node_name2, feat_name2)
232

233
      | (Ast.Features_ineq (ineq, (node_name1, feat_name1), (node_name2, feat_name2)), loc) ->
bguillaum's avatar
bguillaum committed
234 235
        Domain.check_feature_name ?domain ~loc feat_name1;
        Domain.check_feature_name ?domain ~loc feat_name2;
236
        Features_ineq (ineq, pid_of_name loc node_name1, feat_name1, pid_of_name loc node_name2, feat_name2)
237

238
      | (Ast.Feature_ineq_cst (ineq, (node_name1, feat_name1), constant), loc) ->
bguillaum's avatar
bguillaum committed
239
        Domain.check_feature_name ?domain ~loc feat_name1;
240 241
        Feature_ineq_cst (ineq, pid_of_name loc node_name1, feat_name1, constant)

242
      | (Ast.Feature_eq_regexp ((node_name, feat_name), regexp), loc) ->
bguillaum's avatar
bguillaum committed
243
        Domain.check_feature_name ?domain ~loc feat_name;
244
        Feature_eq_regexp (pid_of_name loc node_name, feat_name, regexp)
bguillaum's avatar
bguillaum committed
245

246
      | (Ast.Feature_eq_cst ((node_name, feat_name), string), loc) ->
247
        Domain.check_feature_name ?domain ~loc feat_name;
248
        Feature_eq_cst (pid_of_name loc node_name, feat_name, string)
249 250 251 252
      | (Ast.Feature_diff_cst ((node_name, feat_name), string), loc) ->
        Domain.check_feature_name ?domain ~loc feat_name;
        Feature_diff_cst (pid_of_name loc node_name, feat_name, string)

253
      | (Ast.Feature_eq_float ((node_name, feat_name), float), loc) ->
254
        Domain.check_feature_name ?domain ~loc feat_name;
255
        Feature_eq_float (pid_of_name loc node_name, feat_name, float)
256 257 258 259
      | (Ast.Feature_diff_float ((node_name, feat_name), float), loc) ->
        Domain.check_feature_name ?domain ~loc feat_name;
        Feature_diff_float (pid_of_name loc node_name, feat_name, float)

260 261
      | (Ast.Immediate_prec (id1, id2), loc) ->
        Immediate_prec (pid_of_name loc id1, pid_of_name loc id2)
bguillaum's avatar
bguillaum committed
262

263 264
      | (Ast.Large_prec (id1, id2), loc) ->
        Large_prec (pid_of_name loc id1, pid_of_name loc id2)
bguillaum's avatar
bguillaum committed
265

bguillaum's avatar
bguillaum committed
266
  type basic = {
267 268 269
    graph: P_graph.t;
    constraints: const list;
  }
pj2m's avatar
pj2m committed
270

271 272 273 274 275 276
  let basic_to_json ?domain basic =
    `Assoc [
      ("graph", P_graph.to_json ?domain basic.graph);
      ("constraints", `List (List.map (const_to_json ?domain) basic.constraints));
    ]

Bruno Guillaume's avatar
Bruno Guillaume committed
277
  let build_pos_basic ?domain ?pat_vars basic_ast =
278
    let (graph, pos_table) =
bguillaum's avatar
bguillaum committed
279
      P_graph.build ?domain ?pat_vars basic_ast.Ast.pat_nodes basic_ast.Ast.pat_edges in
pj2m's avatar
pj2m committed
280
    (
bguillaum's avatar
bguillaum committed
281 282
      {
        graph = graph;
bguillaum's avatar
bguillaum committed
283
        constraints = List.map (build_pos_constraint ?domain pos_table) basic_ast.Ast.pat_const
bguillaum's avatar
bguillaum committed
284 285
      },
      pos_table
pj2m's avatar
pj2m committed
286 287
    )

bguillaum's avatar
bguillaum committed
288
  (* the neg part *)
bguillaum's avatar
bguillaum committed
289
  let build_neg_constraint ?domain pos_table neg_table const =
bguillaum's avatar
bguillaum committed
290 291 292 293
    let pid_of_name loc node_name =
      match Id.build_opt node_name pos_table with
        | Some i -> Pid.Pos i
        | None -> Pid.Neg (Id.build ~loc node_name neg_table) in
pj2m's avatar
pj2m committed
294
    match const with
295
      | (Ast.Cst_out (id,label_cst), loc) ->
bguillaum's avatar
bguillaum committed
296
        Cst_out (pid_of_name loc id, Label_cst.build ~loc ?domain label_cst)
297
      | (Ast.Cst_in (id,label_cst), loc) ->
bguillaum's avatar
bguillaum committed
298
        Cst_in (pid_of_name loc id, Label_cst.build ~loc ?domain label_cst)
299

300
      | (Ast.Features_eq (feat_id1, feat_id2), loc) ->
301 302
        let (node_name1, feat_name1) = feat_id1
        and (node_name2, feat_name2) = feat_id2 in
bguillaum's avatar
bguillaum committed
303 304
        Domain.check_feature_name ?domain ~loc feat_name1;
        Domain.check_feature_name ?domain ~loc feat_name2;
305
        Features_eq (pid_of_name loc node_name1, feat_name1, pid_of_name loc node_name2, feat_name2)
306

307
      | (Ast.Features_diseq (feat_id1, feat_id2), loc) ->
308 309
        let (node_name1, feat_name1) = feat_id1
        and (node_name2, feat_name2) = feat_id2 in
bguillaum's avatar
bguillaum committed
310 311
        Domain.check_feature_name ?domain ~loc feat_name1;
        Domain.check_feature_name ?domain ~loc feat_name2;
312
        Features_diseq (pid_of_name loc node_name1, feat_name1, pid_of_name loc node_name2, feat_name2)
313

314
      | (Ast.Features_ineq (ineq, feat_id1, feat_id2), loc) ->
315 316
        let (node_name1, feat_name1) = feat_id1
        and (node_name2, feat_name2) = feat_id2 in
bguillaum's avatar
bguillaum committed
317 318
        Domain.check_feature_name ?domain ~loc feat_name1;
        Domain.check_feature_name ?domain ~loc feat_name2;
319
        Features_ineq (ineq, pid_of_name loc node_name1, feat_name1, pid_of_name loc node_name2, feat_name2)
320

321 322
      | (Ast.Feature_ineq_cst (ineq, feat_id1, constant), loc) ->
        let (node_name1, feat_name1) = feat_id1 in
bguillaum's avatar
bguillaum committed
323
        Domain.check_feature_name ?domain ~loc feat_name1;
324
        Feature_ineq_cst (ineq, pid_of_name loc node_name1, feat_name1, constant)
pj2m's avatar
pj2m committed
325

326
      | (Ast.Feature_eq_regexp (feat_id, regexp), loc) ->
bguillaum's avatar
bguillaum committed
327
        let (node_name, feat_name) = feat_id in
bguillaum's avatar
bguillaum committed
328
        Domain.check_feature_name ?domain ~loc feat_name;
329
        Feature_eq_regexp (pid_of_name loc node_name, feat_name, regexp)
bguillaum's avatar
bguillaum committed
330

331
      | (Ast.Feature_eq_cst ((node_name, feat_name), string), loc) ->
332
        Domain.check_feature_name ?domain ~loc feat_name;
333
        Feature_eq_cst (pid_of_name loc node_name, feat_name, string)
334 335 336 337
      | (Ast.Feature_diff_cst ((node_name, feat_name), string), loc) ->
        Domain.check_feature_name ?domain ~loc feat_name;
        Feature_diff_cst (pid_of_name loc node_name, feat_name, string)

338
      | (Ast.Feature_eq_float ((node_name, feat_name), float), loc) ->
339
        Domain.check_feature_name ?domain ~loc feat_name;
340
        Feature_eq_float (pid_of_name loc node_name, feat_name, float)
341 342 343 344 345
      | (Ast.Feature_diff_float ((node_name, feat_name), float), loc) ->
        Domain.check_feature_name ?domain ~loc feat_name;
        Feature_diff_float (pid_of_name loc node_name, feat_name, float)


346 347
      | (Ast.Immediate_prec (id1, id2), loc) ->
        Immediate_prec (pid_of_name loc id1, pid_of_name loc id2)
bguillaum's avatar
bguillaum committed
348

349 350
      | (Ast.Large_prec (id1, id2), loc) ->
        Large_prec (pid_of_name loc id1, pid_of_name loc id2)
bguillaum's avatar
bguillaum committed
351

bguillaum's avatar
bguillaum committed
352
  (* It may raise [P_fs.Fail_unif] in case of contradiction on constraints *)
Bruno Guillaume's avatar
Bruno Guillaume committed
353
  let build_neg_basic ?domain ?pat_vars pos_table basic_ast =
pj2m's avatar
pj2m committed
354
    let (extension, neg_table) =
bguillaum's avatar
bguillaum committed
355
      P_graph.build_extension ?domain ?pat_vars pos_table basic_ast.Ast.pat_nodes basic_ast.Ast.pat_edges in
bguillaum's avatar
bguillaum committed
356

357
    let filters = Pid_map.fold (fun id node acc -> Filter (id, P_node.get_fs node) :: acc) extension.P_graph.old_map [] in
pj2m's avatar
pj2m committed
358
    {
bguillaum's avatar
bguillaum committed
359
      graph = extension.P_graph.ext_map;
bguillaum's avatar
bguillaum committed
360
      constraints = filters @ List.map (build_neg_constraint ?domain pos_table neg_table) basic_ast.Ast.pat_const ;
bguillaum's avatar
bguillaum committed
361
    }
pj2m's avatar
pj2m committed
362

bguillaum's avatar
bguillaum committed
363
  let get_edge_ids basic =
364
    Pid_map.fold
bguillaum's avatar
bguillaum committed
365 366
      (fun _ node acc ->
        Massoc_pid.fold
367
          (fun acc2 _ edge -> (P_edge.get_id edge)::acc2)
368
          acc (P_node.get_next node)
bguillaum's avatar
bguillaum committed
369
      ) basic.graph []
bguillaum's avatar
bguillaum committed
370

371 372 373
  (* a [pattern] is described by the positive basic and a list of negative basics. *)
  type pattern = basic * basic list

374
  let pid_name_list (pos,_) = P_graph.pid_name_list pos.graph
375

pj2m's avatar
pj2m committed
376 377
  type t = {
      name: string;
378
      pattern: pattern;
pj2m's avatar
pj2m committed
379
      commands: Command.t list;
380
      param: Lex_par.t option;
381
      param_names: (string list * string list);
bguillaum's avatar
bguillaum committed
382
      loc: Loc.t;
pj2m's avatar
pj2m committed
383 384
    }

bguillaum's avatar
bguillaum committed
385 386
  let get_name t = t.name

387
  let get_loc t = t.loc
pj2m's avatar
pj2m committed
388

389
  let to_json ?domain t =
390 391 392 393 394 395 396 397 398
    let param_json = match t.param with
    | None -> []
    | Some lex_par -> [
      ("pattern_param", `List (List.map (fun x -> `String x) (fst t.param_names)));
      ("command_param", `List (List.map (fun x -> `String x) (snd t.param_names)));
      ("lex_par", Lex_par.to_json lex_par);
    ] in
    `Assoc
    ([
399 400 401
      ("rule_name", `String t.name);
      ("match", basic_to_json ?domain (fst t.pattern));
      ("without", `List (List.map (basic_to_json ?domain) (snd t.pattern)));
402
      ("commands", `List (List.map (Command.to_json ?domain) t.commands))
403 404
    ] @ param_json
    )
405

406
  (* ====================================================================== *)
bguillaum's avatar
bguillaum committed
407
  let to_dep ?domain t =
408
    let pos_basic = fst t.pattern in
409 410 411
    let buff = Buffer.create 32 in
    bprintf buff "[GRAPH] { scale = 200; }\n";

bguillaum's avatar
bguillaum committed
412 413
    let nodes =
      Pid_map.fold
414
        (fun id node acc ->
bguillaum's avatar
bguillaum committed
415 416
          (node, sprintf "  N_%s { word=\"%s\"; subword=\"%s\"}"
            (Pid.to_id id) (P_node.get_name node) (P_fs.to_dep t.param_names (P_node.get_fs node))
417
          )
418
          :: acc
419
        ) pos_basic.graph [] in
420

421
    (* nodes are sorted to appear in the same order in dep picture and in input file *)
422
    let sorted_nodes = List.sort (fun (n1,_) (n2,_) -> P_node.compare_pos n1 n2) nodes in
bguillaum's avatar
bguillaum committed
423

424 425 426 427 428
    bprintf buff "[WORDS] {\n";
    List.iter
      (fun (_, dep_line) -> bprintf buff "%s\n" dep_line
      ) sorted_nodes;

429
    List.iteri
430 431
      (fun i cst ->
        match cst with
bguillaum's avatar
bguillaum committed
432
          | Cst_out _ | Cst_in _ -> bprintf buff "  C_%d { word=\"*\"}\n" i
433
          | _ -> ()
434
      ) pos_basic.constraints;
435
    bprintf buff "}\n";
bguillaum's avatar
bguillaum committed
436

437
    bprintf buff "[EDGES] {\n";
bguillaum's avatar
bguillaum committed
438

439 440
    Pid_map.iter
      (fun id_src node ->
bguillaum's avatar
bguillaum committed
441
        Massoc_pid.iter
442
          (fun id_tar edge ->
bguillaum's avatar
bguillaum committed
443 444 445
            bprintf buff "  N_%s -> N_%s { label=\"%s\"}\n"
              (Pid.to_id id_src)
              (Pid.to_id id_tar)
bguillaum's avatar
bguillaum committed
446
              (P_edge.to_string ?domain edge)
447 448
          )
          (P_node.get_next node)
449
      ) pos_basic.graph;
450

451
    List.iteri
452 453
      (fun i cst ->
        match cst with
bguillaum's avatar
bguillaum committed
454
          | Cst_out (pid, label_cst) ->
bguillaum's avatar
bguillaum committed
455
            bprintf buff "  N_%s -> C_%d {label = \"%s\"; style=dot; bottom; color=green;}\n"
bguillaum's avatar
bguillaum committed
456
              (Pid.to_id pid) i (Label_cst.to_string ?domain label_cst)
bguillaum's avatar
bguillaum committed
457
          | Cst_in (pid, label_cst) ->
bguillaum's avatar
bguillaum committed
458
            bprintf buff "  C_%d -> N_%s {label = \"%s\"; style=dot; bottom; color=green;}\n"
bguillaum's avatar
bguillaum committed
459
              i (Pid.to_id pid) (Label_cst.to_string ?domain label_cst)
460
          | _ -> ()
461
      ) pos_basic.constraints;
462 463
    bprintf buff "}\n";
    Buffer.contents buff
bguillaum's avatar
bguillaum committed
464

465
  (* ====================================================================== *)
Bruno Guillaume's avatar
Bruno Guillaume committed
466
  let build_commands ?domain ?param pos pos_table ast_commands =
bguillaum's avatar
bguillaum committed
467
    let known_act_ids = Array.to_list pos_table in
468
    let known_edge_ids = get_edge_ids pos in
469

470
    let rec loop (kai,kei) = function
471 472
      | [] -> []
      | ast_command :: tail ->
473
          let (command, (new_kai, new_kei)) =
474
            Command.build
bguillaum's avatar
bguillaum committed
475
              ?domain
476
              ?param
477
              (kai,kei)
478 479
              pos_table
              ast_command in
480 481
          command :: (loop (new_kai,new_kei) tail) in
    loop (known_act_ids, known_edge_ids) ast_commands
482 483

  (* ====================================================================== *)
bguillaum's avatar
bguillaum committed
484 485 486 487 488 489 490 491 492 493 494 495
  let parse_vars loc vars =
    let rec parse_cmd_vars = function
      | [] -> []
      | x::t when x.[0] = '@' -> x :: parse_cmd_vars t
      | x::t -> Error.bug ~loc "Illegal feature definition '%s' in the lexical rule" x in
    let rec parse_pat_vars = function
      | [] -> ([],[])
      | x::t when x.[0] = '@' -> ([],parse_cmd_vars (x::t))
      | x::t when x.[0] = '$' -> let (pv,cv) = parse_pat_vars t in (x::pv, cv)
      | x::t -> Error.bug ~loc "Illegal feature definition '%s' in the lexical rule" x in
    parse_pat_vars vars

496
  (* ====================================================================== *)
497 498 499 500 501
  let build ?domain deprecated_dir rule_ast =

    let dir = match rule_ast.Ast.rule_dir with
    | Some d -> d
    | None -> deprecated_dir in
bguillaum's avatar
bguillaum committed
502 503

    let (param, pat_vars, cmd_vars) =
bguillaum's avatar
bguillaum committed
504 505
      match rule_ast.Ast.param with
      | None -> (None,[],[])
506
      | Some (files,vars) ->
bguillaum's avatar
bguillaum committed
507 508 509
          let (pat_vars, cmd_vars) = parse_vars rule_ast.Ast.rule_loc vars in
          let nb_pv = List.length pat_vars in
          let nb_cv = List.length cmd_vars in
bguillaum's avatar
bguillaum committed
510

511
          (* first: load lexical parameters given in the same file at the end of the rule definition *)
bguillaum's avatar
bguillaum committed
512 513 514 515
          let local_param = match rule_ast.Ast.lex_par with
          | None -> None
          | Some lines -> Some (Lex_par.from_lines ~loc:rule_ast.Ast.rule_loc nb_pv nb_cv lines) in

516
          (* second: load lexical parameters given in external files *)
bguillaum's avatar
bguillaum committed
517
          let full_param = List.fold_left
518
            (fun acc file ->
bguillaum's avatar
bguillaum committed
519 520 521 522 523 524
              match acc with
              | None -> Some (Lex_par.load ~loc:rule_ast.Ast.rule_loc dir nb_pv nb_cv file)
              | Some lp -> Some (Lex_par.append (Lex_par.load ~loc:rule_ast.Ast.rule_loc dir nb_pv nb_cv file) lp)
            ) local_param files in

          (full_param, pat_vars, cmd_vars) in
525

526
    (match (param, pat_vars) with
527
      | (None, _::_) -> Error.build ~loc:rule_ast.Ast.rule_loc "[Rule.build] Missing lexical parameters in rule \"%s\"" rule_ast.Ast.rule_id
528 529 530
      | _ -> ()
    );

531
    let pattern = Ast.normalize_pattern rule_ast.Ast.pattern in
532
    let (pos, pos_table) =
533
      try build_pos_basic ?domain ~pat_vars pattern.Ast.pat_pos
534 535 536 537
      with P_fs.Fail_unif ->
        Error.build ~loc:rule_ast.Ast.rule_loc
          "[Rule.build] in rule \"%s\": feature structures declared in the \"match\" clause are inconsistent"
          rule_ast.Ast.rule_id in
bguillaum's avatar
bguillaum committed
538 539 540
    let (negs,_) =
      List.fold_left
      (fun (acc,pos) basic_ast ->
bguillaum's avatar
bguillaum committed
541
        try ((build_neg_basic ?domain ~pat_vars pos_table basic_ast) :: acc, pos+1)
bguillaum's avatar
bguillaum committed
542 543 544 545
        with P_fs.Fail_unif ->
          Log.fwarning "In rule \"%s\" [%s], the wihtout number %d cannot be satisfied, it is skipped"
            rule_ast.Ast.rule_id (Loc.to_string rule_ast.Ast.rule_loc) pos;
          (acc, pos+1)
546
      ) ([],1) pattern.Ast.pat_negs in
pj2m's avatar
pj2m committed
547
    {
bguillaum's avatar
bguillaum committed
548
      name = rule_ast.Ast.rule_id;
549
      pattern = (pos, negs);
bguillaum's avatar
bguillaum committed
550
      commands = build_commands ?domain ~param:(pat_vars,cmd_vars) pos pos_table rule_ast.Ast.commands;
bguillaum's avatar
bguillaum committed
551 552 553 554 555
      loc = rule_ast.Ast.rule_loc;
      param = param;
      param_names = (pat_vars,cmd_vars)
    }

bguillaum's avatar
bguillaum committed
556
  let build_pattern ?domain pattern_ast =
557
    let n_pattern = Ast.normalize_pattern pattern_ast in
558
    let (pos, pos_table) =
559
      try build_pos_basic ?domain n_pattern.Ast.pat_pos
560 561 562 563
      with P_fs.Fail_unif -> Error.build "feature structures declared in the \"match\" clause are inconsistent " in
    let negs =
      List_.try_map
        P_fs.Fail_unif (* Skip the without parts that are incompatible with the match part *)
bguillaum's avatar
bguillaum committed
564
        (fun basic_ast -> build_neg_basic ?domain pos_table basic_ast)
565
        n_pattern.Ast.pat_negs in
bguillaum's avatar
bguillaum committed
566 567
    (pos, negs)

568
  (* ====================================================================== *)
pj2m's avatar
pj2m committed
569
  type matching = {
570
      n_match: Gid.t Pid_map.t;                     (* partial fct: pattern nodes |--> graph nodes *)
571
      e_match: (string*(Gid.t*Label.t*Gid.t)) list; (* edge matching: edge ident  |--> (src,label,tar) *)
572
      m_param: Lex_par.t option;
pj2m's avatar
pj2m committed
573
    }
574

575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
  let to_python pattern graph m =

    let node_name gid =
      let gnode = G_graph.find gid graph in
      G_node.get_name gid gnode in
    let buff = Buffer.create 32 in
    bprintf buff "%s" "{ ";

    Pid_map.iter
      (fun pid gid ->
        let pnode = P_graph.find pid (fst pattern).graph in
        bprintf buff "\"%s\":\"%s\", " (P_node.get_name pnode) (node_name gid)
      ) m.n_match;

    List.iter
      (fun (id, (src,lab,tar)) ->
591
        bprintf buff "\"%s\":\"%s/%s/%s\", " id (node_name src) (Label.to_string lab) (node_name tar)
592 593 594 595 596
      ) m.e_match;

    bprintf buff "%s" "}";
    Buffer.contents buff

597 598 599 600 601
  let node_matching pattern graph { n_match } =
    Pid_map.fold
      (fun pid gid acc ->
        let pnode = P_graph.find pid (fst pattern).graph in
        let gnode = G_graph.find gid graph in
602
        (P_node.get_name pnode, G_node.get_float gnode) :: acc
603 604
      ) n_match []

605
  let empty_matching param = { n_match = Pid_map.empty; e_match = []; m_param = param;}
606

pj2m's avatar
pj2m committed
607 608
  let e_comp (e1,_) (e2,_) = compare e1 e2

bguillaum's avatar
bguillaum committed
609
  let e_match_add ?pos edge_id matching =
pj2m's avatar
pj2m committed
610 611
    match List_.usort_insert ~compare:e_comp edge_id matching.e_match with
    | Some new_e_match -> { matching with e_match = new_e_match }
bguillaum's avatar
bguillaum committed
612
    | None -> Error.bug "The edge identifier '%s' is binded twice in the same pattern" (fst edge_id)
bguillaum's avatar
bguillaum committed
613

614
  let match_deco pattern matching =
615 616 617
    { G_deco.nodes =
        Pid_map.fold
          (fun pid gid acc ->
618
            let pnode = P_graph.find pid (fst pattern).graph in
619 620
            (gid, (P_node.get_name pnode, P_fs.feat_list (P_node.get_fs pnode))) ::acc
          ) matching.n_match [];
621
      G_deco.edges = List.fold_left (fun acc (_,edge) -> edge::acc) [] matching.e_match;
pj2m's avatar
pj2m committed
622 623
    }

bguillaum's avatar
bguillaum committed
624
  let find cnode ?loc (matching, created_nodes) =
pj2m's avatar
pj2m committed
625
    match cnode with
626
    | Command.Pat pid ->
627
        (try Pid_map.find pid matching.n_match
bguillaum's avatar
bguillaum committed
628 629
        with Not_found -> Error.bug ?loc "Inconsistent matching pid '%s' not found" (Pid.to_string pid))
    | Command.New name ->
bguillaum's avatar
bguillaum committed
630 631
        (try List.assoc name created_nodes
        with Not_found -> Error.run ?loc "Identifier '%s' not found" name)
pj2m's avatar
pj2m committed
632 633

  let down_deco (matching,created_nodes) commands =
634 635 636 637 638 639 640 641 642 643
    let feat_to_highlight = List.fold_left
      (fun acc -> function
        | (Command.UPDATE_FEAT (tar_cn,feat_name,_),loc) ->
          (* | (Command.SHIFT_EDGE (_,tar_cn),loc) *)
          let gid = find tar_cn (matching, created_nodes) in
          let old_feat_list = try Gid_map.find gid acc with Not_found -> [] in
          Gid_map.add gid (feat_name :: old_feat_list) acc
        | _ -> acc
      ) Gid_map.empty commands in

pj2m's avatar
pj2m committed
644
    {
645
     G_deco.nodes = List.map (fun (gid,feat_list) -> (gid, ("",feat_list))) (Gid_map.bindings feat_to_highlight);
646
     G_deco.edges = List.fold_left
bguillaum's avatar
bguillaum committed
647 648
       (fun acc -> function
         | (Command.ADD_EDGE (src_cn,tar_cn,edge),loc) ->
649
             (find src_cn (matching, created_nodes), edge, find tar_cn (matching, created_nodes)) :: acc
650
         | _ -> acc
pj2m's avatar
pj2m committed
651 652 653 654 655
       ) [] commands
   }

  exception Fail
  type partial = {
bguillaum's avatar
bguillaum committed
656
      sub: matching;
657 658 659
      unmatched_nodes: Pid.t list;
      unmatched_edges: (Pid.t * P_edge.t * Pid.t) list;
      already_matched_gids: Gid.t list; (* to ensure injectivity *)
pj2m's avatar
pj2m committed
660
      check: const list (* constraints to verify at the end of the matching *)
bguillaum's avatar
bguillaum committed
661 662 663
    }

        (* PREREQUISITES:
bguillaum's avatar
bguillaum committed
664 665
           - all partial matching have the same ?domain
           - the ?domain of the pattern P is the disjoint union of ?domain([sub]) and [unmatched_nodes]
666
         *)
bguillaum's avatar
bguillaum committed
667
  (*  ---------------------------------------------------------------------- *)
bguillaum's avatar
bguillaum committed
668 669
  let init param basic =
    let roots = P_graph.roots basic.graph in
pj2m's avatar
pj2m committed
670

bguillaum's avatar
bguillaum committed
671
    let node_list = Pid_map.fold (fun pid _ acc -> pid::acc) basic.graph [] in
pj2m's avatar
pj2m committed
672 673

    (* put all roots in the front of the list to speed up the algo *)
bguillaum's avatar
bguillaum committed
674
    let sorted_node_list =
pj2m's avatar
pj2m committed
675
      List.sort
676 677 678 679
        (fun n1 n2 -> match (List.mem n1 roots, List.mem n2 roots) with
        | true, false -> -1
        | false, true -> 1
        | _ -> 0) node_list in
bguillaum's avatar
bguillaum committed
680

bguillaum's avatar
bguillaum committed
681
    { sub = empty_matching param;
pj2m's avatar
pj2m committed
682 683 684
      unmatched_nodes = sorted_node_list;
      unmatched_edges = [];
      already_matched_gids = [];
bguillaum's avatar
bguillaum committed
685
      check = basic.constraints;
pj2m's avatar
pj2m committed
686 687
    }

bguillaum's avatar
bguillaum committed
688
  (*  ---------------------------------------------------------------------- *)
bguillaum's avatar
bguillaum committed
689
  let apply_cst ?domain graph matching cst =
bguillaum's avatar
bguillaum committed
690
    let get_node pid = G_graph.find (Pid_map.find pid matching.n_match) graph in
691
    let get_string_feat pid = function
692 693 694 695 696 697
      | "position" ->
        begin
          match G_node.get_position (get_node pid) with
          | G_node.Ordered f -> Some (sprintf "%g" f)
          | _ -> Error.run "Cannot read position of an unordered node"
        end
698 699
      | feat_name -> G_fs.get_string_atom feat_name (G_node.get_fs (get_node pid)) in
    let get_float_feat pid = function
700 701 702 703 704 705
      | "position" ->
        begin
          match G_node.get_position (get_node pid) with
          | G_node.Ordered f -> Some f
          | _ -> Error.run "Cannot read position of an unordered node"
        end
706
      | feat_name -> G_fs.get_float_feat feat_name (G_node.get_fs (get_node pid)) in
bguillaum's avatar
bguillaum committed
707 708

    match cst with
bguillaum's avatar
bguillaum committed
709
      | Cst_out (pid,label_cst) ->
710
        let gid = Pid_map.find pid matching.n_match in
bguillaum's avatar
bguillaum committed
711
        if G_graph.edge_out ?domain graph gid label_cst
bguillaum's avatar
bguillaum committed
712 713
        then matching
        else raise Fail
bguillaum's avatar
bguillaum committed
714
      | Cst_in (pid,label_cst) ->
715
        let gid = Pid_map.find pid matching.n_match in
bguillaum's avatar
bguillaum committed
716
        if G_graph.node_exists
717
          (fun node ->
bguillaum's avatar
bguillaum committed
718
            List.exists (fun e -> Label_cst.match_ ?domain label_cst e) (Massoc_gid.assoc gid (G_node.get_next node))
719
          ) graph
bguillaum's avatar
bguillaum committed
720 721
        then matching
        else raise Fail
bguillaum's avatar
bguillaum committed
722
      | Filter (pid, fs) ->
bguillaum's avatar
bguillaum committed
723 724 725 726 727 728 729 730
        begin
          try
            let gid = Pid_map.find pid matching.n_match in
            let gnode = G_graph.find gid graph in
            let new_param = P_fs.match_ ?param:matching.m_param fs (G_node.get_fs gnode) in
            {matching with m_param = new_param }
          with P_fs.Fail -> raise Fail
        end
731
      | Features_eq (pid1, feat_name1, pid2, feat_name2) ->
bguillaum's avatar
bguillaum committed
732 733
        begin
          match (get_string_feat pid1 feat_name1, get_string_feat pid2 feat_name2) with
bguillaum's avatar
bguillaum committed
734 735
            | Some fv1, Some fv2 when fv1 = fv2 -> matching
            | _ -> raise Fail
bguillaum's avatar
bguillaum committed
736
        end
737
      | Feature_eq_cst (pid1, feat_name1, value) ->
738 739 740 741 742 743 744 745 746 747 748
        begin
          match get_string_feat pid1 feat_name1 with
            | Some fv1 when fv1 = value -> matching
            | _ -> raise Fail
        end
      | Feature_diff_cst (pid1, feat_name1, value) ->
        begin
          match get_string_feat pid1 feat_name1 with
            | Some fv1 when fv1 <> value -> matching
            | _ -> raise Fail
        end
749
      | Feature_eq_float (pid1, feat_name1, float) ->
750 751 752 753 754 755 756 757 758 759 760
        begin
          match get_float_feat pid1 feat_name1 with
            | Some fv1 when fv1 = float -> matching
            | _ -> raise Fail
        end
      | Feature_diff_float (pid1, feat_name1, float) ->
        begin
          match get_float_feat pid1 feat_name1 with
            | Some fv1 when fv1 <> float -> matching
            | _ -> raise Fail
        end
761
      | Features_diseq (pid1, feat_name1, pid2, feat_name2) ->
bguillaum's avatar
bguillaum committed
762 763
        begin
          match (get_string_feat pid1 feat_name1, get_string_feat pid2 feat_name2) with
bguillaum's avatar
bguillaum committed
764 765
            | Some fv1, Some fv2 when fv1 <> fv2 -> matching
            | _ -> raise Fail
bguillaum's avatar
bguillaum committed
766
        end
767
      | Features_ineq (ineq, pid1, feat_name1, pid2, feat_name2) ->
768 769
        begin
          match (ineq, get_float_feat pid1 feat_name1, get_float_feat pid2 feat_name2) with
bguillaum's avatar
bguillaum committed
770 771 772 773 774
            | (Ast.Lt, Some fv1, Some fv2) when fv1 < fv2 -> matching
            | (Ast.Gt, Some fv1, Some fv2) when fv1 > fv2 -> matching
            | (Ast.Le, Some fv1, Some fv2) when fv1 <= fv2 -> matching
            | (Ast.Ge, Some fv1, Some fv2) when fv1 >= fv2 -> matching
            | _ -> raise Fail
775 776 777 778 779 780 781 782 783 784
          end
      | Feature_ineq_cst (ineq, pid1, feat_name1, constant) ->
        begin
          match (ineq, get_float_feat pid1 feat_name1) with
            | (Ast.Lt, Some fv1) when fv1 < constant -> matching
            | (Ast.Gt, Some fv1) when fv1 > constant -> matching
            | (Ast.Le, Some fv1) when fv1 <= constant -> matching
            | (Ast.Ge, Some fv1) when fv1 >= constant -> matching
            | _ -> raise Fail
          end
785
      | Feature_eq_regexp (pid, feat_name, regexp) ->
bguillaum's avatar
bguillaum committed
786 787 788 789 790
        begin
          match get_string_feat pid feat_name with
          | None -> raise Fail
          | Some string_feat ->
            let re = Str.regexp regexp in
791
            if String_.re_match re string_feat then matching else raise Fail
bguillaum's avatar
bguillaum committed
792
        end
793
      | Immediate_prec (pid1, pid2) ->
bguillaum's avatar
bguillaum committed
794 795
          let gid1 = Pid_map.find pid1 matching.n_match in
          let gid2 = Pid_map.find pid2 matching.n_match in
796
          let gnode1 = G_graph.find gid1 graph in
797
          if G_node.get_succ gnode1 = Some gid2
798 799
          then matching
          else  raise Fail
800
      | Large_prec (pid1, pid2) ->
801 802 803 804 805
          let gnode1 = G_graph.find (Pid_map.find pid1 matching.n_match) graph in
          let gnode2 = G_graph.find (Pid_map.find pid2 matching.n_match) graph in
          if G_node.get_position gnode1 < G_node.get_position gnode2
          then matching
          else raise Fail
pj2m's avatar
pj2m committed
806

bguillaum's avatar
bguillaum committed
807
  (*  ---------------------------------------------------------------------- *)
bguillaum's avatar
bguillaum committed
808
  (* returns all extension of the partial input matching *)
bguillaum's avatar
bguillaum committed
809
  let rec extend_matching ?domain (positive,neg) (graph:G_graph.t) (partial:partial) =
pj2m's avatar
pj2m committed
810
    match (partial.unmatched_edges, partial.unmatched_nodes) with
bguillaum's avatar
bguillaum committed
811
    | [], [] ->
bguillaum's avatar
bguillaum committed
812 813 814 815 816
      begin
        try
          let new_matching =
            List.fold_left
              (fun acc const ->
bguillaum's avatar
bguillaum committed
817
                apply_cst ?domain graph acc const
bguillaum's avatar
bguillaum committed
818 819 820 821
              ) partial.sub partial.check in
          [new_matching, partial.already_matched_gids]
        with Fail -> []
      end
pj2m's avatar
pj2m committed
822
    | (src_pid, p_edge, tar_pid)::tail_ue, _ ->
823 824
        begin
          try (* is the tar already found in the matching ? *)
bguillaum's avatar
bguillaum committed
825
            let new_partials =
826 827 828
              let src_gid = Pid_map.find src_pid partial.sub.n_match in
              let tar_gid = Pid_map.find tar_pid partial.sub.n_match in
              let src_gnode = G_graph.find src_gid graph in
829
              let g_edges = Massoc_gid.assoc tar_gid (G_node.get_next src_gnode) in
bguillaum's avatar
bguillaum committed
830

bguillaum's avatar
bguillaum committed
831
              match P_edge.match_list ?domain p_edge g_edges with
832
              | P_edge.Fail -> (* no good edge in graph for this pattern edge -> stop here *)
833
                  []
834
              | P_edge.Binds (id,labels) -> (* n edges in the graph match the identified p_edge -> make copies of the [k] matchings (and returns n*k matchings) *)
bguillaum's avatar
bguillaum committed
835
                  List.map
836 837
                    (fun label ->
                      {partial with sub = e_match_add (id,(src_gid,label,tar_gid)) partial.sub; unmatched_edges = tail_ue }
bguillaum's avatar
bguillaum committed
838
                    ) labels
bguillaum's avatar
bguillaum committed
839
            in List_.flat_map (extend_matching ?domain (positive,neg) graph) new_partials
840 841
          with Not_found -> (* p_edge goes to an unmatched node *)
            let candidates = (* candidates (of type (gid, matching)) for m(tar_pid) = gid) with new partial matching m *)
bguillaum's avatar
bguillaum committed
842
              let (src_gid : Gid.t) = Pid_map.find src_pid partial.sub.n_match in
843
              let src_gnode = G_graph.find src_gid graph in
844
              Massoc_gid.fold
845
                (fun acc gid_next g_edge ->
bguillaum's avatar
bguillaum committed
846
                  match P_edge.match_ ?domain p_edge g_edge with
847
                  | P_edge.Fail -> (* g_edge does not fit, no new candidate *)
848
                      acc
849
                  | P_edge.Binds (id,[label]) -> (* g_edge fits with an extended matching *)
850
                      (gid_next, e_match_add (id, (src_gid, label, gid_next)) partial.sub) :: acc
851 852
                  | _ -> Error.bug "P_edge.match_ must return exactly one label"
                ) [] (G_node.get_next src_gnode) in
853
            List_.flat_map
bguillaum's avatar
bguillaum committed
854