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

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

14
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));
    ]

Bruno Guillaume's avatar
Bruno Guillaume committed
271
  let build_pos_basic ?domain lexicons ?pat_vars basic_ast =
272
    let (graph, pos_table) =
Bruno Guillaume's avatar
Bruno Guillaume committed
273
      P_graph.build ?domain ?pat_vars 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 *)
Bruno Guillaume's avatar
Bruno Guillaume committed
370
  let build_neg_basic ?domain ?pat_vars lexicons pos_table basic_ast =
pj2m's avatar
pj2m committed
371
    let (extension, neg_table) =
Bruno Guillaume's avatar
Bruno Guillaume committed
372
      P_graph.build_extension ?domain ?pat_vars 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
      param: Lex_par.t * string list; (* ([],[]) if None *)
Bruno Guillaume's avatar
Bruno Guillaume committed
398
      lexicons: Lexicons.t;
bguillaum's avatar
bguillaum committed
399
      loc: Loc.t;
pj2m's avatar
pj2m committed
400 401
    }

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

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

406
  let to_json ?domain t =
407
    let param_json = match t.param with
Bruno Guillaume's avatar
Bruno Guillaume committed
408 409 410
    | ([],[]) -> []
    | (lex_par, param_names) -> [
      ("pattern_param", `List (List.map (fun x -> `String x) (param_names)));
411 412 413 414
      ("lex_par", Lex_par.to_json lex_par);
    ] in
    `Assoc
    ([
415 416 417
      ("rule_name", `String t.name);
      ("match", basic_to_json ?domain (fst t.pattern));
      ("without", `List (List.map (basic_to_json ?domain) (snd t.pattern)));
418
      ("commands", `List (List.map (Command.to_json ?domain) t.commands))
419 420
    ] @ param_json
    )
421

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

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

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

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

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

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

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

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

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

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

Bruno Guillaume's avatar
Bruno Guillaume committed
500
  let build_lex loc = function
Bruno Guillaume's avatar
Bruno Guillaume committed
501 502 503 504
  | 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
505
  | Ast.Final (line_list) -> Lexicon.build loc line_list
506 507


508
  (* ====================================================================== *)
509 510 511 512 513
  let build ?domain deprecated_dir rule_ast =

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

Bruno Guillaume's avatar
Bruno Guillaume committed
515 516
    let lexicons =
      List.fold_left (fun acc (name,lex) ->
Bruno Guillaume's avatar
Bruno Guillaume committed
517 518
        try
          let prev = List.assoc name acc in
Bruno Guillaume's avatar
Bruno Guillaume committed
519
          (name, (Lexicon.union prev (build_lex rule_ast.Ast.rule_loc lex))) :: (List.remove_assoc name acc)
Bruno Guillaume's avatar
Bruno Guillaume committed
520 521
        with Not_found -> (name, build_lex rule_ast.Ast.rule_loc lex) :: acc
    ) [] rule_ast.Ast.lexicon_info in
522

523
    let (param, pat_vars) =
bguillaum's avatar
bguillaum committed
524
      match rule_ast.Ast.param with
Bruno Guillaume's avatar
Bruno Guillaume committed
525
      | None -> ([],[])
526
      | Some (files,vars) ->
527
          let nb_var = List.length vars in
bguillaum's avatar
bguillaum committed
528

529
          (* first: load lexical parameters given in the same file at the end of the rule definition *)
bguillaum's avatar
bguillaum committed
530
          let local_param = match rule_ast.Ast.lex_par with
Bruno Guillaume's avatar
Bruno Guillaume committed
531 532
          | None -> []
          | Some lines -> Lex_par.from_lines ~loc:rule_ast.Ast.rule_loc nb_var lines in
bguillaum's avatar
bguillaum committed
533

534
          (* second: load lexical parameters given in external files *)
bguillaum's avatar
bguillaum committed
535
          let full_param = List.fold_left
536
            (fun acc file ->
bguillaum's avatar
bguillaum committed
537
              match acc with
Bruno Guillaume's avatar
Bruno Guillaume committed
538 539
              | [] -> Lex_par.load ~loc:rule_ast.Ast.rule_loc dir nb_var file
              | lp -> Lex_par.append (Lex_par.load ~loc:rule_ast.Ast.rule_loc dir nb_var file) lp
bguillaum's avatar
bguillaum committed
540 541
            ) local_param files in

542
          (full_param, vars) in
543

544
    (match (param, pat_vars) with
Bruno Guillaume's avatar
Bruno Guillaume committed
545
      | ([], _::_) -> Error.build ~loc:rule_ast.Ast.rule_loc "[Rule.build] Missing lexical parameters in rule \"%s\"" rule_ast.Ast.rule_id
546 547 548
      | _ -> ()
    );

549
    let pattern = Ast.normalize_pattern rule_ast.Ast.pattern in
550
    let (pos, pos_table) =
Bruno Guillaume's avatar
Bruno Guillaume committed
551
      try build_pos_basic ?domain lexicons ~pat_vars pattern.Ast.pat_pos
552 553 554 555
      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
556 557 558
    let (negs,_) =
      List.fold_left
      (fun (acc,pos) basic_ast ->
Bruno Guillaume's avatar
Bruno Guillaume committed
559
        try ((build_neg_basic ?domain ~pat_vars lexicons pos_table basic_ast) :: acc, pos+1)
bguillaum's avatar
bguillaum committed
560 561 562 563
        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)
564
      ) ([],1) pattern.Ast.pat_negs in
pj2m's avatar
pj2m committed
565
    {
bguillaum's avatar
bguillaum committed
566
      name = rule_ast.Ast.rule_id;
567
      pattern = (pos, negs);
Bruno Guillaume's avatar
Bruno Guillaume committed
568
      commands = build_commands ?domain ~param:pat_vars lexicons pos pos_table rule_ast.Ast.commands;
bguillaum's avatar
bguillaum committed
569
      loc = rule_ast.Ast.rule_loc;
570
      lexicons;
Bruno Guillaume's avatar
Bruno Guillaume committed
571
      param = (param, pat_vars);
bguillaum's avatar
bguillaum committed
572 573
    }

Bruno Guillaume's avatar
Bruno Guillaume committed
574
  let build_pattern ?domain ?(lexicons=[]) pattern_ast =
575
    let n_pattern = Ast.normalize_pattern pattern_ast in
576
    let (pos, pos_table) =
Bruno Guillaume's avatar
Bruno Guillaume committed
577
      try build_pos_basic ?domain lexicons n_pattern.Ast.pat_pos
578 579 580 581
      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
582
        (fun basic_ast -> build_neg_basic ?domain lexicons pos_table basic_ast)
583
        n_pattern.Ast.pat_negs in
bguillaum's avatar
bguillaum committed
584 585
    (pos, negs)

586
  (* ====================================================================== *)
pj2m's avatar
pj2m committed
587
  type matching = {
588
      n_match: Gid.t Pid_map.t;                     (* partial fct: pattern nodes |--> graph nodes *)
589
      e_match: (string*(Gid.t*Label.t*Gid.t)) list; (* edge matching: edge ident  |--> (src,label,tar) *)
590
      m_param: Lex_par.t option;
Bruno Guillaume's avatar
Bruno Guillaume committed
591
      l_param: Lexicons.t;
592

pj2m's avatar
pj2m committed
593
    }
594

595
  let to_python pattern graph m =
Bruno Guillaume's avatar
Bruno Guillaume committed
596 597 598 599 600 601 602 603 604
    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
    `Assoc ( nodes @ edges)
605

606 607 608 609 610
  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
611
        (P_node.get_name pnode, G_node.get_float gnode) :: acc
612 613
      ) n_match []

Bruno Guillaume's avatar
Bruno Guillaume committed
614
  let empty_matching ?(lexicons=[]) param = { n_match = Pid_map.empty; e_match = []; m_param = param; l_param = lexicons;}
615

pj2m's avatar
pj2m committed
616 617
  let e_comp (e1,_) (e2,_) = compare e1 e2

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

623
  let match_deco pattern matching =
624 625 626
    { G_deco.nodes =
        Pid_map.fold
          (fun pid gid acc ->
627
            let pnode = P_graph.find pid (fst pattern).graph in
628 629
            let pattern_feat_list = P_fs.feat_list (P_node.get_fs pnode) in
            (gid, (P_node.get_name pnode, pattern_feat_list)) ::acc
630
          ) matching.n_match [];
631
      G_deco.edges = List.fold_left (fun acc (_,edge) -> edge::acc) [] matching.e_match;
pj2m's avatar
pj2m committed
632 633
    }

bguillaum's avatar
bguillaum committed
634
  let find cnode ?loc (matching, created_nodes) =
pj2m's avatar
pj2m committed
635
    match cnode with
636
    | Command.Pat pid ->
637
        (try Pid_map.find pid matching.n_match
bguillaum's avatar
bguillaum committed
638 639
        with Not_found -> Error.bug ?loc "Inconsistent matching pid '%s' not found" (Pid.to_string pid))
    | Command.New name ->
bguillaum's avatar
bguillaum committed
640 641
        (try List.assoc name created_nodes
        with Not_found -> Error.run ?loc "Identifier '%s' not found" name)
pj2m's avatar
pj2m committed
642 643

  let down_deco (matching,created_nodes) commands =
644 645 646 647 648 649 650 651 652 653
    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
654
    {
655 656 657 658 659 660 661 662 663
      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
664 665 666

  exception Fail
  type partial = {
bguillaum's avatar
bguillaum committed
667
      sub: matching;
668 669 670
      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
671
      check: const list (* constraints to verify at the end of the matching *)
bguillaum's avatar
bguillaum committed
672 673 674
    }

        (* PREREQUISITES:
bguillaum's avatar
bguillaum committed
675 676
           - all partial matching have the same ?domain
           - the ?domain of the pattern P is the disjoint union of ?domain([sub]) and [unmatched_nodes]
677
         *)
bguillaum's avatar
bguillaum committed
678
  (*  ---------------------------------------------------------------------- *)
Bruno Guillaume's avatar
Bruno Guillaume committed
679
  let init ?lexicons param basic =
bguillaum's avatar
bguillaum committed
680
    let roots = P_graph.roots basic.graph in
pj2m's avatar
pj2m committed
681

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

    (* put all roots in the front of the list to speed up the algo *)
bguillaum's avatar
bguillaum committed
685
    let sorted_node_list =
pj2m's avatar
pj2m committed
686
      List.sort
687 688 689 690
        (fun n1 n2 -> match (List.mem n1 roots, List.mem n2 roots) with
        | true, false -> -1
        | false, true -> 1
        | _ -> 0) node_list in
bguillaum's avatar
bguillaum committed
691

Bruno Guillaume's avatar
Bruno Guillaume committed
692
    { sub = empty_matching ?lexicons param;
pj2m's avatar
pj2m committed
693 694 695
      unmatched_nodes = sorted_node_list;
      unmatched_edges = [];
      already_matched_gids = [];
bguillaum's avatar
bguillaum committed
696
      check = basic.constraints;
pj2m's avatar
pj2m committed
697 698
    }

bguillaum's avatar
bguillaum committed
699
  (*  ---------------------------------------------------------------------- *)
bguillaum's avatar
bguillaum committed
700
  let apply_cst ?domain graph matching cst =
bguillaum's avatar
bguillaum committed
701
    let get_node pid = G_graph.find (Pid_map.find pid matching.n_match) graph in
702
    let get_string_feat pid = function
703 704 705 706 707 708
      | "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
709 710
      | feat_name -> G_fs.get_string_atom feat_name (G_node.get_fs (get_node pid)) in
    let get_float_feat pid = function
711 712 713 714 715 716
      | "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
717
      | feat_name -> G_fs.get_float_feat feat_name (G_node.get_fs (get_node pid)) in
bguillaum's avatar
bguillaum committed
718 719

    match cst with
bguillaum's avatar
bguillaum committed
720
      | Cst_out (pid,label_cst) ->
721
        let gid = Pid_map.find pid matching.n_match in
722
        if G_graph.edge_out graph gid label_cst
bguillaum's avatar
bguillaum committed
723 724
        then matching
        else raise Fail
bguillaum's avatar
bguillaum committed
725
      | Cst_in (pid,label_cst) ->
726
        let gid = Pid_map.find pid matching.n_match in
bguillaum's avatar
bguillaum committed
727
        if G_graph.node_exists
728
          (fun node ->
bguillaum's avatar
bguillaum committed
729
            List.exists (fun e -> Label_cst.match_ ?domain label_cst e) (Massoc_gid.assoc gid (G_node.get_next node))
730
          ) graph
bguillaum's avatar
bguillaum committed
731 732
        then matching
        else raise Fail
bguillaum's avatar
bguillaum committed
733
      | Filter (pid, fs) ->
bguillaum's avatar
bguillaum committed
734 735 736 737
        begin
          try
            let gid = Pid_map.find pid matching.n_match in
            let gnode = G_graph.find gid graph in
738 739
            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
740 741
          with P_fs.Fail -> raise Fail
        end
742
      | Features_eq (pid1, feat_name1, pid2, feat_name2) ->
bguillaum's avatar
bguillaum committed
743 744
        begin
          match (get_string_feat pid1 feat_name1, get_string_feat pid2 feat_name2) with
bguillaum's avatar
bguillaum committed
745 746
            | Some fv1, Some fv2 when fv1 = fv2 -> matching
            | _ -> raise Fail
bguillaum's avatar
bguillaum committed
747
        end
748
      | Feature_eq_cst (pid1, feat_name1, value) ->
749 750 751 752 753 754 755 756 757 758 759
        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
760
      | Feature_eq_float (pid1, feat_name1, float) ->
761 762 763 764 765 766 767 768 769 770 771
        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
772
      | Features_diseq (pid1, feat_name1, pid2, feat_name2) ->
bguillaum's avatar
bguillaum committed
773 774
        begin
          match (get_string_feat pid1 feat_name1, get_string_feat pid2 feat_name2) with
bguillaum's avatar
bguillaum committed
775 776
            | Some fv1, Some fv2 when fv1 <> fv2 -> matching
            | _ -> raise Fail
bguillaum's avatar
bguillaum committed
777
        end
778
      | Features_ineq (ineq, pid1, feat_name1, pid2, feat_name2) ->
779 780
        begin
          match (ineq, get_float_feat pid1 feat_name1, get_float_feat pid2 feat_name2) with
bguillaum's avatar
bguillaum committed
781 782 783 784 785
            | (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
786 787 788 789 790 791 792 793 794 795
          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
796
      | Feature_eq_regexp (pid, feat_name, regexp) ->
bguillaum's avatar
bguillaum committed
797 798 799 800 801
        begin
          match get_string_feat pid feat_name with
          | None -> raise Fail
          | Some string_feat ->
            let re = Str.regexp regexp in
802
            if String_.re_match re string_feat then matching else raise Fail
bguillaum's avatar
bguillaum committed
803
        end
804
      | Immediate_prec (pid1, pid2) ->
bguillaum's avatar
bguillaum committed
805 806
          let gid1 = Pid_map.find pid1 matching.n_match in
          let gid2 = Pid_map.find pid2 matching.n_match in
807
          let gnode1 = G_graph.find gid1 graph in
808
          if G_node.get_succ gnode1 = Some gid2
809 810
          then matching
          else  raise Fail
811
      | Large_prec (pid1, pid2) ->
812 813 814 815 816
          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
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
      | Feature_eq_lex (pid, feature_name, (lexicon,field)) ->
        begin
          Printf.printf "### Feature_eq_lex\n%!";
          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
839

pj2m's avatar
pj2m committed
840

bguillaum's avatar
bguillaum committed
841
  (*  ---------------------------------------------------------------------- *)
bguillaum's avatar
bguillaum committed
842
  (* returns all extension of the partial input matching *)
bguillaum's avatar
bguillaum committed
843
  let rec extend_matching ?domain (positive,neg) (graph:G_graph.t) (partial:partial) =
pj2m's avatar
pj2m committed
844
    match (partial.unmatched_edges, partial.unmatched_nodes) with
bguillaum's avatar
bguillaum committed
845
    | [], [] ->
bguillaum's avatar
bguillaum committed
846 847 848 849 850
      begin
        try
          let new_matching =
            List.fold_left
              (fun acc const ->
bguillaum's avatar
bguillaum committed
851
                apply_cst ?domain graph acc const
bguillaum's avatar
bguillaum committed
852 853 854 855
              ) partial.sub partial.check in
          [new_matching, partial.already_matched_gids]
        with Fail -> []
      end
pj2m's avatar
pj2m committed
856
    | (src_pid, p_edge, tar_pid)::tail_ue, _ ->
857 858
        begin
          try (* is the tar already found in the matching ? *)
bguillaum's avatar
bguillaum committed
859
            let new_partials =
860 861 862
              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
863
              let g_edges = Massoc_gid.assoc tar_gid (G_node.get_next src_gnode) in
bguillaum's avatar
bguillaum committed
864

bguillaum's avatar
bguillaum committed
865
              match P_edge.match_list ?domain p_edge g_edges with
866
              | P_edge.Fail -> (* no good edge in graph for this pattern edge -> stop here *)
867
                  []
868
              | 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
869
                  List.map
870 871
                    (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
872
                    ) labels
bguillaum's avatar
bguillaum committed
873
            in List_.flat_map (extend_matching ?domain (positive,neg) graph) new_partials
874 875
          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
876
              let (src_gid : Gid.t) = Pid_map.find src_pid partial.sub.n_match in
877
              let src_gnode = G_graph.find src_gid graph in
878
              Massoc_gid.fold
879
                (fun acc gid_next g_edge ->
bguillaum's avatar
bguillaum committed
880
                  match P_edge.match_ ?domain p_edge g_edge with