grew_rule.ml 57.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

bguillaum's avatar
bguillaum committed
28
  (* the rewriting depth is bounded to stop rewriting when the system is not terminating *)
bguillaum's avatar
bguillaum committed
29 30
  let max_depth_det = ref 2000
  let max_depth_non_det = ref 100
bguillaum's avatar
bguillaum committed
31
  let debug_loop = ref false
32 33 34

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

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

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


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

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

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

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

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

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

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

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

222 223 224 225 226 227 228
      | (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)

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

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

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

Bruno Guillaume's avatar
Bruno Guillaume committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
      | (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
258 259


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

265 266 267 268 269 270
  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));
    ]

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

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

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

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

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

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

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

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

332 333 334 335 336 337 338
      | (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)

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

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

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

Bruno Guillaume's avatar
Bruno Guillaume committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
      | (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

367 368


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

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

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

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

391
  let pid_name_list (pos,_) = P_graph.pid_name_list pos.graph
392

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

bguillaum's avatar
bguillaum committed
401 402
  let get_name t = t.name

403
  let get_loc t = t.loc
pj2m's avatar
pj2m committed
404

405
  let to_json ?domain t =
406 407
    `Assoc
    ([
408 409 410
      ("rule_name", `String t.name);
      ("match", basic_to_json ?domain (fst t.pattern));
      ("without", `List (List.map (basic_to_json ?domain) (snd t.pattern)));
411
      ("commands", `List (List.map (Command.to_json ?domain) t.commands))
412
    ]
413
    )
414

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

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

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

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

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

446
    bprintf buff "[EDGES] {\n";
bguillaum's avatar
bguillaum committed
447

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

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

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

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

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


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

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

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

546
  (* ====================================================================== *)
pj2m's avatar
pj2m committed
547
  type matching = {
Bruno Guillaume's avatar
Bruno Guillaume committed
548 549 550 551
    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;                          (* *)
  }
552

553
  let to_python pattern graph m =
Bruno Guillaume's avatar
Bruno Guillaume committed
554 555 556 557 558 559 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 ->
      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
562
    `Assoc (nodes @ edges)
563

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

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

pj2m's avatar
pj2m committed
574 575
  let e_comp (e1,_) (e2,_) = compare e1 e2

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

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

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

  let down_deco (matching,created_nodes) commands =
602 603 604 605 606 607 608 609 610 611
    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
612
    {
613 614 615 616 617 618 619 620 621
      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
622 623 624

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

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

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

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

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

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

pj2m's avatar
pj2m committed
797

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

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

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

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

        let new_partial =