grew_rule.ml 58.8 KB
Newer Older
bguillaum's avatar
bguillaum committed
1 2 3
(**********************************************************************************)
(*    Libcaml-grew - a Graph Rewriting library dedicated to NLP applications      *)
(*                                                                                *)
Bruno Guillaume's avatar
Bruno Guillaume committed
4
(*    Copyright 2011-2018 Inria, Université de Lorraine                           *)
bguillaum's avatar
bguillaum committed
5
(*                                                                                *)
Bruno Guillaume's avatar
Bruno Guillaume committed
6
(*    Webpage: http://grew.fr                                                     *)
bguillaum's avatar
bguillaum committed
7 8 9 10
(*    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
open Grew_base
15
open Grew_types
bguillaum's avatar
bguillaum committed
16
open Grew_ast
17
open Grew_domain
pj2m's avatar
pj2m committed
18
open Grew_edge
bguillaum's avatar
bguillaum committed
19
open Grew_fs
pj2m's avatar
pj2m committed
20
open Grew_node
bguillaum's avatar
bguillaum committed
21 22
open Grew_command
open Grew_graph
pj2m's avatar
pj2m committed
23 24


25
(* ================================================================================ *)
bguillaum's avatar
bguillaum committed
26
module Rule = struct
27

28 29 30 31 32 33 34 35 36 37
  (* the number of rewriting steps is bounded to stop rewriting when the system is not terminating *)
  let max_rules = ref 10000
  let current_rules = ref 0

  let set_max_rules n = max_rules := n
  let reset_rules () = current_rules := 0
  let incr_rules () =
    incr current_rules;
    if !current_rules > !max_rules
    then Error.run "More than %d rewriting steps: ckeck for loops or increase max_rules value" !max_rules
38

39
  type const =
bguillaum's avatar
bguillaum committed
40 41
    | Cst_out of Pid.t * Label_cst.t
    | Cst_in of Pid.t * Label_cst.t
42 43
    | Features_eq of Pid.t * string * Pid.t * string
    | Features_diseq of Pid.t * string * Pid.t * string
44
    (* *)
45
    | Feature_eq_cst of Pid.t * string * string
46
    | Feature_diff_cst of Pid.t * string * string
47
    (* *)
48 49 50
    | Feature_eq_lex of Pid.t * string * (string * string)
    | Feature_diff_lex of Pid.t * string * (string * string)
    (* *)
51
    | Feature_eq_float of Pid.t * string * float
52
    | Feature_diff_float of Pid.t * string * float
53
    (* *)
54
    | Feature_eq_regexp of Pid.t * string * string
55
    (* *)
56
    | Features_ineq of Ast.ineq * Pid.t * string * Pid.t * string
57
    | Feature_ineq_cst of Ast.ineq * Pid.t * string * float
58
    (* *)
bguillaum's avatar
bguillaum committed
59
    | Filter of Pid.t * P_fs.t (* used when a without impose a fs on a node defined by the match basic *)
60
    (* *)
61 62
    | Immediate_prec of Pid.t * Pid.t
    | Large_prec of Pid.t * Pid.t
bguillaum's avatar
bguillaum committed
63

64 65 66
  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]
67
  | Features_eq (pid1,fn1,pid2,fn2) ->
68 69 70 71 72 73 74 75
    `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);
      ]
    ]
76
  | Features_diseq (pid1,fn1,pid2,fn2) ->
77 78 79 80 81 82 83 84
    `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);
      ]
    ]
85
  | Feature_eq_cst (pid,fn,value) ->
86 87 88 89 90 91 92 93
    `Assoc ["feature_eq_cst",
      `Assoc [
        ("id", `String (Pid.to_string pid));
        ("feature_name_", `String fn);
        ("value", `String value);
      ]
    ]
  | Feature_diff_cst (pid,fn,value) ->
94
    `Assoc ["feature_diff_cst",
95 96 97 98 99 100
      `Assoc [
        ("id", `String (Pid.to_string pid));
        ("feature_name_", `String fn);
        ("value", `String value);
      ]
    ]
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  | Feature_eq_lex (pid,fn,(lex,field)) ->
    `Assoc ["feature_eq_lex",
      `Assoc [
        ("id", `String (Pid.to_string pid));
        ("feature_name_", `String fn);
        ("lexicon", `String lex);
        ("field", `String field);
      ]
    ]
  | Feature_diff_lex (pid,fn,(lex,field)) ->
    `Assoc ["feature_diff_lex",
      `Assoc [
        ("id", `String (Pid.to_string pid));
        ("feature_name_", `String fn);
        ("lexicon", `String lex);
        ("field", `String field);
      ]
    ]


121
  | Feature_eq_float (pid,fn,value) ->
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    `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));
      ]
    ]
137
  | Feature_eq_regexp (pid,fn,regexp) ->
138 139 140 141 142 143 144
    `Assoc ["feature_eq_regexp",
      `Assoc [
        ("id", `String (Pid.to_string pid));
        ("feature_name", `String fn);
        ("regexp", `String regexp);
      ]
    ]
145
  | Features_ineq (ineq,pid1,fn1,pid2,fn2) ->
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    `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);
      ]
    ]
171
  | Immediate_prec (pid1, pid2) ->
172 173 174 175 176 177
    `Assoc ["immediate_prec",
      `Assoc [
        ("id1", `String (Pid.to_string pid1));
        ("id2", `String (Pid.to_string pid2));
      ]
    ]
178
  | Large_prec (pid1, pid2) ->
179 180 181 182 183 184 185
    `Assoc ["large_prec",
      `Assoc [
        ("id1", `String (Pid.to_string pid1));
        ("id2", `String (Pid.to_string pid2));
      ]
    ]

Bruno Guillaume's avatar
Bruno Guillaume committed
186
  let build_pos_constraint ?domain lexicons pos_table const =
bguillaum's avatar
bguillaum committed
187 188
    let pid_of_name loc node_name = Pid.Pos (Id.build ~loc node_name pos_table) in
    match const with
189
      | (Ast.Cst_out (id,label_cst), loc) ->
bguillaum's avatar
bguillaum committed
190
        Cst_out (pid_of_name loc id, Label_cst.build ~loc ?domain label_cst)
191
      | (Ast.Cst_in (id,label_cst), loc) ->
bguillaum's avatar
bguillaum committed
192
        Cst_in (pid_of_name loc id, Label_cst.build ~loc ?domain label_cst)
193

194
      | (Ast.Features_eq ((node_name1, feat_name1), (node_name2, feat_name2)), loc) ->
bguillaum's avatar
bguillaum committed
195 196
        Domain.check_feature_name ?domain ~loc feat_name1;
        Domain.check_feature_name ?domain ~loc feat_name2;
197
        Features_eq (pid_of_name loc node_name1, feat_name1, pid_of_name loc node_name2, feat_name2)
198

199
      | (Ast.Features_diseq ((node_name1, feat_name1), (node_name2, feat_name2)), loc) ->
bguillaum's avatar
bguillaum committed
200 201
        Domain.check_feature_name ?domain ~loc feat_name1;
        Domain.check_feature_name ?domain ~loc feat_name2;
202
        Features_diseq (pid_of_name loc node_name1, feat_name1, pid_of_name loc node_name2, feat_name2)
203

204
      | (Ast.Features_ineq (ineq, (node_name1, feat_name1), (node_name2, feat_name2)), loc) ->
bguillaum's avatar
bguillaum committed
205 206
        Domain.check_feature_name ?domain ~loc feat_name1;
        Domain.check_feature_name ?domain ~loc feat_name2;
207
        Features_ineq (ineq, pid_of_name loc node_name1, feat_name1, pid_of_name loc node_name2, feat_name2)
208

209
      | (Ast.Feature_ineq_cst (ineq, (node_name1, feat_name1), constant), loc) ->
bguillaum's avatar
bguillaum committed
210
        Domain.check_feature_name ?domain ~loc feat_name1;
211 212
        Feature_ineq_cst (ineq, pid_of_name loc node_name1, feat_name1, constant)

213
      | (Ast.Feature_eq_regexp ((node_name, feat_name), regexp), loc) ->
bguillaum's avatar
bguillaum committed
214
        Domain.check_feature_name ?domain ~loc feat_name;
215
        Feature_eq_regexp (pid_of_name loc node_name, feat_name, regexp)
bguillaum's avatar
bguillaum committed
216

217
      | (Ast.Feature_eq_cst ((node_name, feat_name), string), loc) ->
218
        Domain.check_feature_name ?domain ~loc feat_name;
219
        Feature_eq_cst (pid_of_name loc node_name, feat_name, string)
220 221 222 223
      | (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)

224 225 226 227 228 229 230
      | (Ast.Feature_eq_lex ((node_name, feat_name), lf), loc) ->
        Domain.check_feature_name ?domain ~loc feat_name;
        Feature_eq_lex (pid_of_name loc node_name, feat_name, lf)
      | (Ast.Feature_diff_lex ((node_name, feat_name), lf), loc) ->
        Domain.check_feature_name ?domain ~loc feat_name;
        Feature_diff_lex (pid_of_name loc node_name, feat_name, lf)

231
      | (Ast.Feature_eq_float ((node_name, feat_name), float), loc) ->
232
        Domain.check_feature_name ?domain ~loc feat_name;
233
        Feature_eq_float (pid_of_name loc node_name, feat_name, float)
234 235 236 237
      | (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)

238 239
      | (Ast.Immediate_prec (id1, id2), loc) ->
        Immediate_prec (pid_of_name loc id1, pid_of_name loc id2)
bguillaum's avatar
bguillaum committed
240

241 242
      | (Ast.Large_prec (id1, id2), loc) ->
        Large_prec (pid_of_name loc id1, pid_of_name loc id2)
bguillaum's avatar
bguillaum committed
243

Bruno Guillaume's avatar
Bruno Guillaume committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
      | (Ast.Feature_eq_lex_or_fs ((node_name, feat_name),(node_or_lex, fn_or_field)), loc) ->
          begin
            match Id.build_opt node_or_lex pos_table with
            | None ->
              Lexicons.check ~loc node_or_lex fn_or_field lexicons;
              Feature_eq_lex (pid_of_name loc node_name, feat_name, (node_or_lex, fn_or_field))
            | _ ->  Features_eq (pid_of_name loc node_name, feat_name, pid_of_name loc node_or_lex, fn_or_field)
          end
      | (Ast.Feature_diff_lex_or_fs ((node_name, feat_name),(node_or_lex, fn_or_field)), loc) ->
          begin
            match Id.build_opt node_or_lex pos_table with
            | None ->
              Lexicons.check ~loc node_or_lex fn_or_field lexicons;
              Feature_diff_lex (pid_of_name loc node_name, feat_name, (node_or_lex, fn_or_field))
            | _ ->  Features_diseq (pid_of_name loc node_name, feat_name, pid_of_name loc node_or_lex, fn_or_field)
          end
260 261


bguillaum's avatar
bguillaum committed
262
  type basic = {
263 264 265
    graph: P_graph.t;
    constraints: const list;
  }
pj2m's avatar
pj2m committed
266

267 268 269 270 271 272
  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));
    ]

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

bguillaum's avatar
bguillaum committed
284
  (* the neg part *)
Bruno Guillaume's avatar
Bruno Guillaume committed
285
  let build_neg_constraint ?domain lexicons pos_table neg_table const =
bguillaum's avatar
bguillaum committed
286 287 288 289
    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
290
    match const with
291
      | (Ast.Cst_out (id,label_cst), loc) ->
bguillaum's avatar
bguillaum committed
292
        Cst_out (pid_of_name loc id, Label_cst.build ~loc ?domain label_cst)
293
      | (Ast.Cst_in (id,label_cst), loc) ->
bguillaum's avatar
bguillaum committed
294
        Cst_in (pid_of_name loc id, Label_cst.build ~loc ?domain label_cst)
295

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

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

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

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

322
      | (Ast.Feature_eq_regexp (feat_id, regexp), loc) ->
bguillaum's avatar
bguillaum committed
323
        let (node_name, feat_name) = feat_id in
bguillaum's avatar
bguillaum committed
324
        Domain.check_feature_name ?domain ~loc feat_name;
325
        Feature_eq_regexp (pid_of_name loc node_name, feat_name, regexp)
bguillaum's avatar
bguillaum committed
326

327
      | (Ast.Feature_eq_cst ((node_name, feat_name), string), loc) ->
328
        Domain.check_feature_name ?domain ~loc feat_name;
329
        Feature_eq_cst (pid_of_name loc node_name, feat_name, string)
330 331 332 333
      | (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)

334 335 336 337 338 339 340
      | (Ast.Feature_eq_lex ((node_name, feat_name), lf), loc) ->
        Domain.check_feature_name ?domain ~loc feat_name;
        Feature_eq_lex (pid_of_name loc node_name, feat_name, lf)
      | (Ast.Feature_diff_lex ((node_name, feat_name), lf), loc) ->
        Domain.check_feature_name ?domain ~loc feat_name;
        Feature_diff_lex (pid_of_name loc node_name, feat_name, lf)

341
      | (Ast.Feature_eq_float ((node_name, feat_name), float), loc) ->
342
        Domain.check_feature_name ?domain ~loc feat_name;
343
        Feature_eq_float (pid_of_name loc node_name, feat_name, float)
344 345 346 347
      | (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)

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

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

Bruno Guillaume's avatar
Bruno Guillaume committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
      | (Ast.Feature_eq_lex_or_fs ((node_name, feat_name),(node_or_lex, fn_or_field)), loc) ->
          begin
            match (Id.build_opt node_or_lex pos_table, Id.build_opt node_or_lex neg_table) with
            | (None, None) ->
              Lexicons.check ~loc node_or_lex fn_or_field lexicons;
              Feature_eq_lex (pid_of_name loc node_name, feat_name, (node_or_lex, fn_or_field))
            | _ ->  Features_eq (pid_of_name loc node_name, feat_name, pid_of_name loc node_or_lex, fn_or_field)
          end
      | (Ast.Feature_diff_lex_or_fs ((node_name, feat_name),(node_or_lex, fn_or_field)), loc) ->
          begin
            match (Id.build_opt node_or_lex pos_table, Id.build_opt node_or_lex neg_table) with
            | (None, None) -> Feature_diff_lex (pid_of_name loc node_name, feat_name, (node_or_lex, fn_or_field))
            | _ ->  Features_diseq (pid_of_name loc node_name, feat_name, pid_of_name loc node_or_lex, fn_or_field)
          end

369 370


bguillaum's avatar
bguillaum committed
371
  (* It may raise [P_fs.Fail_unif] in case of contradiction on constraints *)
372
  let build_neg_basic ?domain lexicons pos_table basic_ast =
pj2m's avatar
pj2m committed
373
    let (extension, neg_table) =
374
      P_graph.build_extension ?domain lexicons pos_table basic_ast.Ast.pat_nodes basic_ast.Ast.pat_edges in
bguillaum's avatar
bguillaum committed
375

376
    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
377
    {
bguillaum's avatar
bguillaum committed
378
      graph = extension.P_graph.ext_map;
Bruno Guillaume's avatar
Bruno Guillaume committed
379
      constraints = filters @ List.map (build_neg_constraint ?domain lexicons pos_table neg_table) basic_ast.Ast.pat_const ;
bguillaum's avatar
bguillaum committed
380
    }
pj2m's avatar
pj2m committed
381

bguillaum's avatar
bguillaum committed
382
  let get_edge_ids basic =
383
    Pid_map.fold
bguillaum's avatar
bguillaum committed
384 385
      (fun _ node acc ->
        Massoc_pid.fold
386
          (fun acc2 _ edge -> (P_edge.get_id edge)::acc2)
387
          acc (P_node.get_next node)
bguillaum's avatar
bguillaum committed
388
      ) basic.graph []
bguillaum's avatar
bguillaum committed
389

390 391 392
  (* a [pattern] is described by the positive basic and a list of negative basics. *)
  type pattern = basic * basic list

393
  let pid_name_list (pos,_) = P_graph.pid_name_list pos.graph
394

pj2m's avatar
pj2m committed
395 396
  type t = {
      name: string;
397
      pattern: pattern;
pj2m's avatar
pj2m committed
398
      commands: Command.t list;
Bruno Guillaume's avatar
Bruno Guillaume committed
399
      lexicons: Lexicons.t;
bguillaum's avatar
bguillaum committed
400
      loc: Loc.t;
pj2m's avatar
pj2m committed
401 402
    }

bguillaum's avatar
bguillaum committed
403 404
  let get_name t = t.name

405
  let get_loc t = t.loc
pj2m's avatar
pj2m committed
406

407
  let to_json ?domain t =
408 409
    `Assoc
    ([
410
      ("rule_name", `String t.name);
Bruno Guillaume's avatar
typo  
Bruno Guillaume committed
411
      ("pattern", basic_to_json ?domain (fst t.pattern));
412
      ("without", `List (List.map (basic_to_json ?domain) (snd t.pattern)));
413
      ("commands", `List (List.map (Command.to_json ?domain) t.commands))
414
    ]
415
    )
416

417
  (* ====================================================================== *)
bguillaum's avatar
bguillaum committed
418
  let to_dep ?domain t =
419
    let pos_basic = fst t.pattern in
420 421 422
    let buff = Buffer.create 32 in
    bprintf buff "[GRAPH] { scale = 200; }\n";

bguillaum's avatar
bguillaum committed
423 424
    let nodes =
      Pid_map.fold
425
        (fun id node acc ->
bguillaum's avatar
bguillaum committed
426
          (node, sprintf "  N_%s { word=\"%s\"; subword=\"%s\"}"
427
            (Pid.to_id id) (P_node.get_name node) (P_fs.to_dep (P_node.get_fs node))
428
          )
429
          :: acc
430
        ) pos_basic.graph [] in
431

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

435 436 437 438 439
    bprintf buff "[WORDS] {\n";
    List.iter
      (fun (_, dep_line) -> bprintf buff "%s\n" dep_line
      ) sorted_nodes;

440
    List.iteri
441 442
      (fun i cst ->
        match cst with
bguillaum's avatar
bguillaum committed
443
          | Cst_out _ | Cst_in _ -> bprintf buff "  C_%d { word=\"*\"}\n" i
444
          | _ -> ()
445
      ) pos_basic.constraints;
446
    bprintf buff "}\n";
bguillaum's avatar
bguillaum committed
447

448
    bprintf buff "[EDGES] {\n";
bguillaum's avatar
bguillaum committed
449

450 451
    Pid_map.iter
      (fun id_src node ->
bguillaum's avatar
bguillaum committed
452
        Massoc_pid.iter
453
          (fun id_tar edge ->
bguillaum's avatar
bguillaum committed
454 455 456
            bprintf buff "  N_%s -> N_%s { label=\"%s\"}\n"
              (Pid.to_id id_src)
              (Pid.to_id id_tar)
bguillaum's avatar
bguillaum committed
457
              (P_edge.to_string ?domain edge)
458 459
          )
          (P_node.get_next node)
460
      ) pos_basic.graph;
461

462
    List.iteri
463 464
      (fun i cst ->
        match cst with
bguillaum's avatar
bguillaum committed
465
          | Cst_out (pid, label_cst) ->
bguillaum's avatar
bguillaum committed
466
            bprintf buff "  N_%s -> C_%d {label = \"%s\"; style=dot; bottom; color=green;}\n"
bguillaum's avatar
bguillaum committed
467
              (Pid.to_id pid) i (Label_cst.to_string ?domain label_cst)
bguillaum's avatar
bguillaum committed
468
          | Cst_in (pid, label_cst) ->
bguillaum's avatar
bguillaum committed
469
            bprintf buff "  C_%d -> N_%s {label = \"%s\"; style=dot; bottom; color=green;}\n"
bguillaum's avatar
bguillaum committed
470
              i (Pid.to_id pid) (Label_cst.to_string ?domain label_cst)
471
          | _ -> ()
472
      ) pos_basic.constraints;
473 474
    bprintf buff "}\n";
    Buffer.contents buff
bguillaum's avatar
bguillaum committed
475

476
  (* ====================================================================== *)
477
  let build_commands ?domain lexicons pos pos_table ast_commands =
Bruno Guillaume's avatar
Bruno Guillaume committed
478
    let known_node_ids = Array.to_list pos_table in
479
    let known_edge_ids = get_edge_ids pos in
480

Bruno Guillaume's avatar
Bruno Guillaume committed
481
    let rec loop (kni,kei) = function
482 483
      | [] -> []
      | ast_command :: tail ->
Bruno Guillaume's avatar
Bruno Guillaume committed
484
          let (command, (new_kni, new_kei)) =
485
            Command.build
bguillaum's avatar
bguillaum committed
486
              ?domain
Bruno Guillaume's avatar
Bruno Guillaume committed
487
              lexicons
Bruno Guillaume's avatar
Bruno Guillaume committed
488
              (kni,kei)
489 490
              pos_table
              ast_command in
Bruno Guillaume's avatar
Bruno Guillaume committed
491 492
          command :: (loop (new_kni,new_kei) tail) in
    loop (known_node_ids, known_edge_ids) ast_commands
493

Bruno Guillaume's avatar
Bruno Guillaume committed
494
  let build_lex loc = function
Bruno Guillaume's avatar
Bruno Guillaume committed
495 496
  | Ast.File filename ->
      if Filename.is_relative filename
497 498
      then Lexicon.load loc (Filename.concat (Global.get_dir ()) filename)
      else Lexicon.load loc filename
Bruno Guillaume's avatar
Bruno Guillaume committed
499
  | Ast.Final (line_list) -> Lexicon.build loc line_list
500 501


502
  (* ====================================================================== *)
Bruno Guillaume's avatar
Bruno Guillaume committed
503
  let build ?domain rule_ast =
Bruno Guillaume's avatar
Bruno Guillaume committed
504 505
    let lexicons =
      List.fold_left (fun acc (name,lex) ->
Bruno Guillaume's avatar
Bruno Guillaume committed
506 507
        try
          let prev = List.assoc name acc in
Bruno Guillaume's avatar
Bruno Guillaume committed
508
          (name, (Lexicon.union prev (build_lex rule_ast.Ast.rule_loc lex))) :: (List.remove_assoc name acc)
Bruno Guillaume's avatar
Bruno Guillaume committed
509 510
        with Not_found -> (name, build_lex rule_ast.Ast.rule_loc lex) :: acc
    ) [] rule_ast.Ast.lexicon_info in
511

512
    let pattern = Ast.normalize_pattern rule_ast.Ast.pattern in
513
    let (pos, pos_table) =
514
      try build_pos_basic ?domain lexicons pattern.Ast.pat_pos
515 516 517 518
      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
519 520 521
    let (negs,_) =
      List.fold_left
      (fun (acc,pos) basic_ast ->
522
        try ((build_neg_basic ?domain lexicons pos_table basic_ast) :: acc, pos+1)
bguillaum's avatar
bguillaum committed
523 524 525 526
        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)
527
      ) ([],1) pattern.Ast.pat_negs in
pj2m's avatar
pj2m committed
528
    {
bguillaum's avatar
bguillaum committed
529
      name = rule_ast.Ast.rule_id;
530
      pattern = (pos, negs);
531
      commands = build_commands ?domain lexicons pos pos_table rule_ast.Ast.commands;
bguillaum's avatar
bguillaum committed
532
      loc = rule_ast.Ast.rule_loc;
533
      lexicons;
bguillaum's avatar
bguillaum committed
534 535
    }

Bruno Guillaume's avatar
Bruno Guillaume committed
536
  let build_pattern ?domain ?(lexicons=[]) pattern_ast =
537
    let n_pattern = Ast.normalize_pattern pattern_ast in
538
    let (pos, pos_table) =
Bruno Guillaume's avatar
Bruno Guillaume committed
539
      try build_pos_basic ?domain lexicons n_pattern.Ast.pat_pos
540 541 542 543
      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 *)
Bruno Guillaume's avatar
Bruno Guillaume committed
544
        (fun basic_ast -> build_neg_basic ?domain lexicons pos_table basic_ast)
545
        n_pattern.Ast.pat_negs in
bguillaum's avatar
bguillaum committed
546 547
    (pos, negs)

548
  (* ====================================================================== *)
pj2m's avatar
pj2m committed
549
  type matching = {
Bruno Guillaume's avatar
Bruno Guillaume committed
550 551 552 553
    n_match: Gid.t Pid_map.t;                     (* partial fct: pattern nodes |--> graph nodes *)
    e_match: (string*(Gid.t*Label.t*Gid.t)) list; (* edge matching: edge ident  |--> (src,label,tar) *)
    l_param: Lexicons.t;                          (* *)
  }
554

555
  let to_python pattern graph m =
Bruno Guillaume's avatar
Bruno Guillaume committed
556 557 558 559 560 561 562 563
    let node_name gid = G_node.get_name gid (G_graph.find gid graph) in
    let nodes = Pid_map.fold (fun pid gid acc ->
      let pnode = P_graph.find pid (fst pattern).graph in
        (P_node.get_name pnode, `String (node_name gid))::acc
      ) m.n_match [] in
    let edges = List.map (fun (id, (src,lab,tar)) ->
      (id, `String (sprintf "%s/%s/%s" (node_name src) (Label.to_string lab) (node_name tar)))
      ) m.e_match in
Bruno Guillaume's avatar
Bruno Guillaume committed
564
    `Assoc (nodes @ edges)
565

566 567 568 569 570
  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
571
        (P_node.get_name pnode, G_node.get_name gid gnode) :: acc
572 573
      ) n_match []

574
  let empty_matching ?(lexicons=[]) () = { n_match = Pid_map.empty; e_match = []; l_param = lexicons;}
575

pj2m's avatar
pj2m committed
576 577
  let e_comp (e1,_) (e2,_) = compare e1 e2

bguillaum's avatar
bguillaum committed
578
  let e_match_add ?pos edge_id matching =
pj2m's avatar
pj2m committed
579 580
    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
581
    | None -> Error.bug "The edge identifier '%s' is binded twice in the same pattern" (fst edge_id)
bguillaum's avatar
bguillaum committed
582

583
  let match_deco pattern matching =
584 585 586
    { G_deco.nodes =
        Pid_map.fold
          (fun pid gid acc ->
587
            let pnode = P_graph.find pid (fst pattern).graph in
588 589
            let pattern_feat_list = P_fs.feat_list (P_node.get_fs pnode) in
            (gid, (P_node.get_name pnode, pattern_feat_list)) ::acc
590
          ) matching.n_match [];
591
      G_deco.edges = List.fold_left (fun acc (_,edge) -> edge::acc) [] matching.e_match;
pj2m's avatar
pj2m committed
592 593
    }

bguillaum's avatar
bguillaum committed
594
  let find cnode ?loc (matching, created_nodes) =
pj2m's avatar
pj2m committed
595
    match cnode with
596
    | Command.Pat pid ->
597
        (try Pid_map.find pid matching.n_match
bguillaum's avatar
bguillaum committed
598 599
        with Not_found -> Error.bug ?loc "Inconsistent matching pid '%s' not found" (Pid.to_string pid))
    | Command.New name ->
bguillaum's avatar
bguillaum committed
600 601
        (try List.assoc name created_nodes
        with Not_found -> Error.run ?loc "Identifier '%s' not found" name)
pj2m's avatar
pj2m committed
602 603

  let down_deco (matching,created_nodes) commands =
604 605 606 607 608 609 610 611 612 613
    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
614
    {
615 616 617 618 619 620 621 622 623
      G_deco.nodes = List.map (fun (gid,feat_list) ->
        (gid, ("", (List.map (fun x -> (x,None)) feat_list)))
      ) (Gid_map.bindings feat_to_highlight);
      G_deco.edges = List.fold_left (fun acc -> function
        | (Command.ADD_EDGE (src_cn,tar_cn,edge),loc) ->
            (find src_cn (matching, created_nodes), edge, find tar_cn (matching, created_nodes)) :: acc
        | _ -> acc
      ) [] commands;
    }
pj2m's avatar
pj2m committed
624 625 626

  exception Fail
  type partial = {
bguillaum's avatar
bguillaum committed
627
      sub: matching;
628 629 630
      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
631
      check: const list (* constraints to verify at the end of the matching *)
bguillaum's avatar
bguillaum committed
632 633 634
    }

        (* PREREQUISITES:
bguillaum's avatar
bguillaum committed
635 636
           - all partial matching have the same ?domain
           - the ?domain of the pattern P is the disjoint union of ?domain([sub]) and [unmatched_nodes]
637
         *)
bguillaum's avatar
bguillaum committed
638
  (*  ---------------------------------------------------------------------- *)
639
  let init ?lexicons basic =
bguillaum's avatar
bguillaum committed
640
    let roots = P_graph.roots basic.graph in
pj2m's avatar
pj2m committed
641

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

    (* put all roots in the front of the list to speed up the algo *)
bguillaum's avatar
bguillaum committed
645
    let sorted_node_list =
pj2m's avatar
pj2m committed
646
      List.sort
647 648 649 650
        (fun n1 n2 -> match (List.mem n1 roots, List.mem n2 roots) with
        | true, false -> -1
        | false, true -> 1
        | _ -> 0) node_list in
Bruno Guillaume's avatar
Bruno Guillaume committed
651 652
    {
      sub = empty_matching ?lexicons ();
pj2m's avatar
pj2m committed
653 654 655
      unmatched_nodes = sorted_node_list;
      unmatched_edges = [];
      already_matched_gids = [];
bguillaum's avatar
bguillaum committed
656
      check = basic.constraints;
pj2m's avatar
pj2m committed
657 658
    }

bguillaum's avatar
bguillaum committed
659
  (*  ---------------------------------------------------------------------- *)
bguillaum's avatar
bguillaum committed
660
  let apply_cst ?domain graph matching cst =
bguillaum's avatar
bguillaum committed
661
    let get_node pid = G_graph.find (Pid_map.find pid matching.n_match) graph in
662
    let get_string_feat pid = function
663 664 665 666 667 668
      | "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
669 670
      | feat_name -> G_fs.get_string_atom feat_name (G_node.get_fs (get_node pid)) in
    let get_float_feat pid = function
671 672 673 674 675 676
      | "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
677
      | feat_name -> G_fs.get_float_feat feat_name (G_node.get_fs (get_node pid)) in
bguillaum's avatar
bguillaum committed
678 679

    match cst with
bguillaum's avatar
bguillaum committed
680
      | Cst_out (pid,label_cst) ->
681
        let gid = Pid_map.find pid matching.n_match in
682
        if G_graph.edge_out graph gid label_cst
bguillaum's avatar
bguillaum committed
683 684
        then matching
        else raise Fail
bguillaum's avatar
bguillaum committed
685
      | Cst_in (pid,label_cst) ->
686
        let gid = Pid_map.find pid matching.n_match in
bguillaum's avatar
bguillaum committed
687
        if G_graph.node_exists
688
          (fun node ->
bguillaum's avatar
bguillaum committed
689
            List.exists (fun e -> Label_cst.match_ ?domain label_cst e) (Massoc_gid.assoc gid (G_node.get_next node))
690
          ) graph
bguillaum's avatar
bguillaum committed
691 692
        then matching
        else raise Fail
bguillaum's avatar
bguillaum committed
693
      | Filter (pid, fs) ->
bguillaum's avatar
bguillaum committed
694 695 696 697
        begin
          try
            let gid = Pid_map.find pid matching.n_match in
            let gnode = G_graph.find gid graph in
698 699
            let new_param = P_fs.match_ ~lexicons:(matching.l_param) fs (G_node.get_fs gnode) in
            {matching with l_param = new_param }
bguillaum's avatar
bguillaum committed
700 701
          with P_fs.Fail -> raise Fail
        end
702
      | Features_eq (pid1, feat_name1, pid2, feat_name2) ->
bguillaum's avatar
bguillaum committed
703 704
        begin
          match (get_string_feat pid1 feat_name1, get_string_feat pid2 feat_name2) with
bguillaum's avatar
bguillaum committed
705 706
            | Some fv1, Some fv2 when fv1 = fv2 -> matching
            | _ -> raise Fail
bguillaum's avatar
bguillaum committed
707
        end
708
      | Feature_eq_cst (pid1, feat_name1, value) ->
709 710 711 712 713 714 715 716 717 718 719
        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
720
      | Feature_eq_float (pid1, feat_name1, float) ->
721 722 723 724 725 726 727 728 729 730 731
        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
732
      | Features_diseq (pid1, feat_name1, pid2, feat_name2) ->
bguillaum's avatar
bguillaum committed
733 734
        begin
          match (get_string_feat pid1 feat_name1, get_string_feat pid2 feat_name2) with
bguillaum's avatar
bguillaum committed
735 736
            | Some fv1, Some fv2 when fv1 <> fv2 -> matching
            | _ -> raise Fail
bguillaum's avatar
bguillaum committed
737
        end
738
      | Features_ineq (ineq, pid1, feat_name1, pid2, feat_name2) ->
739 740
        begin
          match (ineq, get_float_feat pid1 feat_name1, get_float_feat pid2 feat_name2) with
bguillaum's avatar
bguillaum committed
741 742 743 744 745
            | (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
746 747 748 749 750 751 752 753 754 755
          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
756
      | Feature_eq_regexp (pid, feat_name, regexp) ->
bguillaum's avatar
bguillaum committed
757 758 759 760 761
        begin
          match get_string_feat pid feat_name with
          | None -> raise Fail
          | Some string_feat ->
            let re = Str.regexp regexp in
762
            if String_.re_match re string_feat then matching else raise Fail
bguillaum's avatar
bguillaum committed
763
        end
764
      | Immediate_prec (pid1, pid2) ->
bguillaum's avatar
bguillaum committed
765 766
          let gid1 = Pid_map.find pid1 matching.n_match in
          let gid2 = Pid_map.find pid2 matching.n_match in
767
          let gnode1 = G_graph.find gid1 graph in
768
          if G_node.get_succ gnode1 = Some gid2
769 770
          then matching
          else  raise Fail
771
      | Large_prec (pid1, pid2) ->
772 773 774 775 776
          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
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
      | Feature_eq_lex (pid, feature_name, (lexicon,field)) ->
        begin
          match get_string_feat pid feature_name with
          | None -> raise Fail
          | Some v ->
              let old_lex = List.assoc lexicon matching.l_param in
              match Lexicon.select field v old_lex with
              | None -> raise Fail
              | Some new_lex -> {matching with l_param = (lexicon, new_lex) :: (List.remove_assoc lexicon matching.l_param) }
        end

      | Feature_diff_lex (pid, feature_name, (lexicon,field)) ->
        begin
          match get_string_feat pid feature_name with
          | None -> raise Fail
          | Some v ->
              let old_lex = List.assoc lexicon matching.l_param in
              match Lexicon.unselect field v old_lex with
              | None -> raise Fail
              | Some new_lex -> {matching with l_param = (lexicon, new_lex) :: (List.remove_assoc lexicon matching.l_param) }
        end
798

pj2m's avatar
pj2m committed
799

bguillaum's avatar
bguillaum committed
800
  (*  ---------------------------------------------------------------------- *)
bguillaum's avatar
bguillaum committed
801
  (* returns all extension of the partial input matching *)
bguillaum's avatar
bguillaum committed
802
  let rec extend_matching ?domain (positive,neg) (graph:G_graph.t) (partial:partial) =
pj2m's avatar
pj2m committed
803
    match (partial.unmatched_edges, partial.unmatched_nodes) with
bguillaum's avatar
bguillaum committed
804
    | [], [] ->
bguillaum's avatar
bguillaum committed
805 806 807 808 809
      begin
        try
          let new_matching =
            List.fold_left
              (fun acc const ->
bguillaum's avatar
bguillaum committed
810
                apply_cst ?domain graph acc const
bguillaum's avatar
bguillaum committed
811 812 813 814
              ) partial.sub partial.check in
          [new_matching, partial.already_matched_gids]
        with Fail -> []
      end
pj2m's avatar
pj2m committed
815
    | (src_pid, p_edge, tar_pid)::tail_ue, _ ->
816 817
        begin
          try (* is the tar already found in the matching ? *)
bguillaum's avatar
bguillaum committed
818
            let new_partials =
819 820 821
              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
822
              let g_edges = Massoc_gid.assoc tar_gid (G_node.get_next src_gnode) in
bguillaum's avatar
bguillaum committed
823

bguillaum's avatar
bguillaum committed
824
              match P_edge.match_list ?domain p_edge g_edges with
825
              | P_edge.Fail -> (* no good edge in graph for this pattern edge -> stop here *)
826
                  []
827
              | 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
828
                  List.map
829 830
                    (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
831
                    ) labels
bguillaum's avatar
bguillaum committed
832
            in List_.flat_map (extend_matching ?domain (positive,neg) graph) new_partials
833 834
          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
835
              let (src_gid : Gid.t) = Pid_map.find src_pid partial.sub.n_match in
836
              let src_gnode = G_graph.find src_gid graph in
837
              Massoc_gid.fold
838
                (fun acc gid_next g_edge ->
bguillaum's avatar
bguillaum committed
839
                  match P_edge.match_ ?domain p_edge g_edge with
840
                  | P_edge.Fail -> (* g_edge does not fit, no new candidate *)
841
                      acc
842
                  | P_edge.Binds (id,[label]) -> (* g_edge fits with an extended matching *)
843
                      (gid_next, e_match_add (id, (src_gid, label, gid_next)) partial.sub) :: acc
844 845
                  | _ -> Error.bug "P_edge.match_ must return exactly one label"
                ) [] (G_node.get_next src_gnode) in
846
            List_.flat_map
bguillaum's avatar
bguillaum committed
847
              (fun (gid_next, matching) ->
bguillaum's avatar
bguillaum committed
848
                extend_matching_from ?domain (positive,neg) graph tar_pid gid_next
849 850 851
                  {partial with sub=matching; unmatched_edges = tail_ue}
              ) candidates
        end
bguillaum's avatar
bguillaum committed
852
    | [], pid :: _ ->
853 854
        G_graph.fold_gid
          (fun gid acc ->
bguillaum's avatar
bguillaum committed
855
            (extend_matching_from ?domain (positive,neg) graph pid gid partial) @ acc
856
          ) graph []
bguillaum's avatar
bguillaum committed
857

bguillaum's avatar
bguillaum committed
858
  (*  ---------------------------------------------------------------------- *)
bguillaum's avatar
bguillaum committed
859
  and extend_matching_from ?domain (positive,neg) (graph:G_graph.t) pid (gid : Gid.t) partial =
pj2m's avatar
pj2m committed
860 861 862
    if List.mem gid partial.already_matched_gids
    then [] (* the required association pid -> gid is not injective *)
    else
bguillaum's avatar
bguillaum committed
863 864 865 866 867 868 869 870 871 872
      let p_node =
        try P_graph.find pid positive
        with Not_found ->
          try P_graph.find pid neg
          with Not_found -> Error.bug "[Grew_rule.extend_matching_from] cannot find node" in

      (* let p_node =  *)
      (*   if pid >= 0  *)
      (*   then try P_graph.find pid positive with Not_found -> failwith "POS" *)
      (*   else try P_graph.find pid neg with Not_found -> failwith "NEG" in *)
873
      let g_node = try G_graph.find gid graph with Not_found -> Error.bug "[extend_matching_from] cannot find gid in graph" in
bguillaum's avatar
bguillaum committed
874

bguillaum's avatar
bguillaum committed
875
      try
876
        let new_lex_set = P_node.match_ ~lexicons:partial.sub.l_param p_node g_node in
877
        (* add all out-edges from pid in pattern *)
bguillaum's avatar
bguillaum committed
878 879
        let new_unmatched_edges =
          Massoc_pid.fold
880
            (fun acc pid_next p_edge -> (pid, p_edge, pid_next) :: acc
881
            ) partial.unmatched_edges (P_node.get_next p_node) in
bguillaum's avatar
bguillaum committed
882 883

        let new_partial =
bguillaum's avatar
bguillaum committed