grew_rule.ml 59.6 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
  (* a [pattern] is described by the positive basic and a list of negative basics. *)
391
  type pattern = {
392
    global: string list;
393 394 395
    pos: basic;
    negs: basic list;
  }
396

397
  let pid_name_list pattern = P_graph.pid_name_list pattern.pos.graph
398

pj2m's avatar
pj2m committed
399 400
  type t = {
      name: string;
401
      pattern: pattern;
pj2m's avatar
pj2m committed
402
      commands: Command.t list;
Bruno Guillaume's avatar
Bruno Guillaume committed
403
      lexicons: Lexicons.t;
bguillaum's avatar
bguillaum committed
404
      loc: Loc.t;
pj2m's avatar
pj2m committed
405 406
    }

bguillaum's avatar
bguillaum committed
407 408
  let get_name t = t.name

409
  let get_loc t = t.loc
pj2m's avatar
pj2m committed
410

411
  let to_json ?domain t =
412 413
    `Assoc
    ([
414
      ("rule_name", `String t.name);
415 416
      ("pattern", basic_to_json ?domain t.pattern.pos);
      ("without", `List (List.map (basic_to_json ?domain) t.pattern.negs));
417
      ("commands", `List (List.map (Command.to_json ?domain) t.commands))
418
    ]
419
    )
420

421
  (* ====================================================================== *)
bguillaum's avatar
bguillaum committed
422
  let to_dep ?domain t =
423
    let pos_basic = t.pattern.pos in
424 425 426
    let buff = Buffer.create 32 in
    bprintf buff "[GRAPH] { scale = 200; }\n";

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

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

439 440 441 442 443
    bprintf buff "[WORDS] {\n";
    List.iter
      (fun (_, dep_line) -> bprintf buff "%s\n" dep_line
      ) sorted_nodes;

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

452
    bprintf buff "[EDGES] {\n";
bguillaum's avatar
bguillaum committed
453

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

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

480
  (* ====================================================================== *)
481
  let build_commands ?domain lexicons pos pos_table ast_commands =
Bruno Guillaume's avatar
Bruno Guillaume committed
482
    let known_node_ids = Array.to_list pos_table in
483
    let known_edge_ids = get_edge_ids pos in
484

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

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


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

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

Bruno Guillaume's avatar
Bruno Guillaume committed
540
  let build_pattern ?domain ?(lexicons=[]) pattern_ast =
541
    let n_pattern = Ast.normalize_pattern pattern_ast in
542
    let (pos, pos_table) =
Bruno Guillaume's avatar
Bruno Guillaume committed
543
      try build_pos_basic ?domain lexicons n_pattern.Ast.pat_pos
544 545 546 547
      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
548
        (fun basic_ast -> build_neg_basic ?domain lexicons pos_table basic_ast)
549
        n_pattern.Ast.pat_negs in
550
    { pos; negs; global=pattern_ast.pat_glob; }
bguillaum's avatar
bguillaum committed
551

552
  (* ====================================================================== *)
pj2m's avatar
pj2m committed
553
  type matching = {
Bruno Guillaume's avatar
Bruno Guillaume committed
554 555 556 557
    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;                          (* *)
  }
558

559
  let to_python pattern graph m =
Bruno Guillaume's avatar
Bruno Guillaume committed
560 561
    let node_name gid = G_node.get_name gid (G_graph.find gid graph) in
    let nodes = Pid_map.fold (fun pid gid acc ->
562
      let pnode = P_graph.find pid pattern.pos.graph in
Bruno Guillaume's avatar
Bruno Guillaume committed
563 564 565 566 567
        (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
568
    `Assoc (nodes @ edges)
569

570 571 572
  let node_matching pattern graph { n_match } =
    Pid_map.fold
      (fun pid gid acc ->
573
        let pnode = P_graph.find pid pattern.pos.graph in
574
        let gnode = G_graph.find gid graph in
575
        (P_node.get_name pnode, G_node.get_name gid gnode) :: acc
576 577
      ) n_match []

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

pj2m's avatar
pj2m committed
580 581
  let e_comp (e1,_) (e2,_) = compare e1 e2

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

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

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

  let down_deco (matching,created_nodes) commands =
608 609 610 611 612 613 614 615 616 617
    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
618
    {
619 620 621 622 623 624 625 626 627
      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
628 629 630

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

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

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

    (* put all roots in the front of the list to speed up the algo *)
bguillaum's avatar
bguillaum committed
649
    let sorted_node_list =
pj2m's avatar
pj2m committed
650
      List.sort
651 652 653 654
        (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
655 656
    {
      sub = empty_matching ?lexicons ();
pj2m's avatar
pj2m committed
657 658 659
      unmatched_nodes = sorted_node_list;
      unmatched_edges = [];
      already_matched_gids = [];
bguillaum's avatar
bguillaum committed
660
      check = basic.constraints;
pj2m's avatar
pj2m committed
661 662
    }

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

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

pj2m's avatar
pj2m committed
803

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

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

bguillaum's avatar
bguillaum committed
862
  (*  ---------------------------------------------------------------------- *)
bguillaum's avatar
bguillaum committed
863
  and extend_matching_from ?domain (positive,neg) (graph:G_graph.t) pid (gid : Gid.t) partial =
pj2m's avatar
pj2m committed
864 865 866
    if List.mem gid partial.already_matched_gids
    then [] (* the required association pid -> gid is not injective *)
    else
bguillaum's avatar
bguillaum committed
867 868 869 870 871 872 873 874 875 876
      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 *)
877
      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
878

bguillaum's avatar
bguillaum committed
879
      try
Bruno Guillaume's avatar