package kappa-library

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file matching.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
(******************************************************************************)
(*  _  __ * The Kappa Language                                                *)
(* | |/ / * Copyright 2010-2020 CNRS - Harvard Medical School - INRIA - IRIF  *)
(* | ' /  *********************************************************************)
(* | . \  * This file is distributed under the terms of the                   *)
(* |_|\_\ * GNU Lesser General Public License Version 3                       *)
(******************************************************************************)

type t = Renaming.t Mods.IntMap.t * Mods.IntSet.t
(* (map,set)
   map: point_i -> (node_j(i) -> id_node_graph_in_current_matching)
   set:codomain of current matching *)

type matching = t

let empty = Mods.IntMap.empty, Mods.IntSet.empty

let add_cc (inj, co) id r =
  let c = Renaming.image r in
  match Mods.IntSet.disjoint_union co c with
  | Some co' -> Some (Mods.IntMap.add id r inj, co')
  | None -> None

let debug_print f (m, _co) =
  Format.fprintf f "@[(%a)@]"
    (Pp.set Mods.IntMap.bindings Pp.comma (fun f (ccid, nm) ->
         Pp.set Renaming.to_list Pp.comma
           (fun f (node, dst) -> Format.fprintf f "%i:%i->%i" ccid node dst)
           f nm))
    m

let reconstruct_renaming ~debug_mode domain graph cc_id root =
  let point = Pattern.Env.get domain cc_id in
  match Pattern.Env.roots point with
  | None -> failwith "Matching.reconstruct cc error"
  (*- rm - add : int -> int -> Renaming.t -> Renaming.t *)
  | Some (rids, rty) ->
    let inj = Renaming.empty () in
    let _, injective =
      match Pattern.reconstruction_navigation (Pattern.Env.content point) with
      | _ :: _ as nav ->
        List.fold_left
          (fun (root, injective) nav ->
            ( None,
              injective
              && Navigation.imperative_edge_is_valid ~debug_mode ?root inj graph
                   nav ))
          (Some (root, rty), true)
          nav
      (*- rm - find_root: cc -> (type, node) option *)
      | [] ->
        ( None,
          (match rids with
          | [ rid ] -> Renaming.imperative_add ~debug_mode rid root inj
          | _ -> false) )
    in
    if injective then
      inj
    else
      failwith
        ("Matching.reconstruct renaming error at root " ^ string_of_int root)

(* reconstruct: Pattern.Env.t -> Edges.t -> t -> int -> Pattern.id ->
   int -> t option*)
let reconstruct ~debug_mode domain graph inj id cc_id root =
  let rename = reconstruct_renaming ~debug_mode domain graph cc_id root in
  match Mods.IntSet.disjoint_union (Renaming.image rename) (snd inj) with
  | None -> None
  | Some co -> Some (Mods.IntMap.add id rename (fst inj), co)

let rec aux_is_root_of ~debug_mode graph root inj = function
  | [] -> true
  | h :: t ->
    Navigation.imperative_edge_is_valid ~debug_mode ?root inj graph h
    && aux_is_root_of ~debug_mode graph None inj t

let is_root_of ~debug_mode domain graph ((_, rty) as root) cc_id =
  let point = Pattern.Env.get domain cc_id in
  match Pattern.reconstruction_navigation (Pattern.Env.content point) with
  | [] ->
    (match Pattern.Env.roots point with
    | Some (_, rty') -> rty = rty'
    | None -> false)
  | nav -> aux_is_root_of ~debug_mode graph (Some root) (Renaming.empty ()) nav

let roots_of ~debug_mode domain graph cc =
  Edges.all_agents_where
    (fun x -> is_root_of ~debug_mode domain graph x cc)
    graph

(* get : (ContentAgent.t * int) -> t -> int *)
let get ~debug_mode ((node, _), id) (t, _) =
  Renaming.apply ~debug_mode (Mods.IntMap.find_default Renaming.dummy id t) node

let elements_with_types domain ccs (t, _) =
  let out = Array.make (Mods.IntMap.size t) [] in
  let () =
    Mods.IntMap.iter
      (fun id map ->
        out.(id) <-
          Renaming.fold
            (fun i out acc ->
              ( out,
                Pattern.find_ty
                  (Pattern.Env.content (Pattern.Env.get domain ccs.(id)))
                  i )
              :: acc)
            map [])
      t
  in
  out

module Cache = struct
  type t = Pattern.id * (int * int) option

  let compare (a, a') (b, b') =
    let c = Pattern.compare_canonicals a b in
    if c = 0 then (
      match a', b' with
      | None, None -> 0
      | None, Some _ -> 1
      | Some _, None -> -1
      | Some x, Some y -> Mods.int_pair_compare x y
    ) else
      c

  let print f (a, a') =
    Format.fprintf f "%a%a"
      (Pattern.print ~noCounters:true ?domain:None ~with_id:true)
      a
      (Pp.option (Pp.pair Format.pp_print_int Format.pp_print_int))
      a'
end

module CacheSetMap = SetMap.Make (Cache)

type cache = CacheSetMap.Set.t

let empty_cache = CacheSetMap.Set.empty

let survive_nav ~debug_mode inj graph =
  List.fold_left
    (fun inj step ->
      match inj with
      | None -> inj
      | Some inj ->
        Navigation.injection_for_one_more_edge ~debug_mode inj graph step)
    (Some inj)

(*edges: list of concrete edges,
    returns the roots of observables that are above in the domain*)
let from_edge ~debug_mode domain graph ((out, cache) as acc) node site arrow =
  let rec aux_from_edges cache ((obs, rev_deps) as acc) = function
    | [] -> acc, cache
    | (pid, point, inj_point2graph) :: remains ->
      let acc' =
        match Pattern.Env.roots point with
        | None -> acc
        | Some (ids, ty) ->
          ( List.fold_left
              (fun acc id ->
                (pid, (Renaming.apply ~debug_mode inj_point2graph id, ty))
                :: acc)
              obs ids,
            Operator.DepSet.union rev_deps (Pattern.Env.deps point) )
      in
      let remains', cache' =
        List.fold_left
          (fun ((re, ca) as pair) son ->
            match
              survive_nav ~debug_mode inj_point2graph graph son.Pattern.Env.next
            with
            | None -> pair
            | Some inj' ->
              let rename =
                Renaming.compose ~debug_mode false son.Pattern.Env.inj inj'
              in
              let ca' =
                CacheSetMap.Set.add
                  (son.Pattern.Env.dst, Renaming.min_elt rename)
                  ca
              in
              if ca == ca' then
                pair
              else (
                let p' = Pattern.Env.get domain son.Pattern.Env.dst in
                let next = son.Pattern.Env.dst, p', rename in
                next :: re, ca'
              ))
          (remains, cache) (Pattern.Env.sons point)
      in
      aux_from_edges cache' acc' remains'
  in
  match Pattern.Env.get_elementary ~debug_mode domain node site arrow with
  | None -> acc
  | Some x ->
    aux_from_edges
      (*(*unnecessary*)CacheSetMap.Set.add (cc_id,Renaming.min_elt inj')*)
      cache out [ x ]

let observables_from_agent domain graph (((obs, rdeps), cache) as acc)
    ((_, ty) as node) =
  if Edges.is_agent node graph then (
    match Pattern.Env.get_single_agent ty domain with
    | Some (cc, deps) ->
      ((cc, node) :: obs, Operator.DepSet.union rdeps deps), cache
    | None -> acc
  ) else
    acc

let observables_from_free ~debug_mode domain graph acc node site =
  from_edge ~debug_mode domain graph acc node site Navigation.ToNothing

let observables_from_internal ~debug_mode domain graph acc node site id =
  from_edge ~debug_mode domain graph acc node site (Navigation.ToInternal id)

let observables_from_link ~debug_mode domain graph acc n site n' site' =
  from_edge domain ~debug_mode graph acc n site
    (Navigation.ToNode (Navigation.Fresh n', site'))

module Agent = struct
  type t = Existing of Agent.t * int | Fresh of int * int (* type, id *)

  let rename ~debug_mode id inj = function
    | Existing (n, id') as x ->
      if id <> id' then
        x
      else (
        let n' = Agent.rename ~debug_mode inj n in
        if n == n' then
          x
        else
          Existing (n', id')
      )
    | Fresh _ as x -> x

  let print ?sigs f = function
    | Existing (n, id) ->
      Format.fprintf f "%a/*%i*/" (Agent.print ?sigs ~with_id:true) n id
    | Fresh (ty, i) ->
      Format.fprintf f "%a/*%t %i*/"
        (match sigs with
        | None -> Format.pp_print_int
        | Some sigs -> Signature.print_agent sigs)
        ty Pp.nu i

  let print_site ?sigs place f site =
    match place with
    | Existing (n, _) -> Agent.print_site ?sigs n f site
    | Fresh (ty, _) ->
      (match sigs with
      | None -> Format.pp_print_int f site
      | Some sigs -> Signature.print_site sigs ty f site)

  let print_internal ?sigs place site f id =
    match place with
    | Existing (n, _) -> Agent.print_internal ?sigs n site f id
    | Fresh (ty, _) ->
      (match sigs with
      | None -> Format.fprintf f "%i~%i" site id
      | Some sigs ->
        Signature.print_site_internal_state sigs ty site f (Some id))

  let get_type = function
    | Existing (n, _) -> Agent.sort n
    | Fresh (i, _) -> i

  let get_id = function
    | Existing (n, _) -> Agent.id n
    | Fresh (_, i) -> i

  let is_fresh = function
    | Existing _ -> false
    | Fresh _ -> true

  let concretize ~debug_mode (inj_nodes, inj_fresh) = function
    | Existing (n, id) -> get ~debug_mode (n, id) inj_nodes, Agent.sort n
    | Fresh (ty, id) ->
      (match Mods.IntMap.find_option id inj_fresh with
      | Some x -> x, ty
      | None -> raise Not_found)

  let to_yojson = function
    | Existing (n, ty) ->
      (`Assoc
         [
           ( "Existing",
             `List
               [
                 `Assoc [ "agent", Agent.to_json n ]; `Assoc [ "type", `Int ty ];
               ] );
         ]
        : Yojson.Basic.t)
    | Fresh (id, ty) ->
      `Assoc [ "Fresh", `Assoc [ "id", `Int id; "type", `Int ty ] ]

  let of_yojson = function
    | `Assoc [ ("Existing", `List list) ] ->
      (match list with
      | [ `Assoc [ ("agent", a) ]; `Assoc [ ("type", `Int ty) ] ] ->
        Existing (Agent.of_json a, ty)
      | x :: _ -> raise (Yojson.Basic.Util.Type_error ("Invalid agent", x))
      | [] -> raise (Yojson.Basic.Util.Type_error ("Invalid agent", `Null)))
    | `Assoc [ ("Fresh", a) ] ->
      (match a with
      | `Assoc [ ("id", `Int id); ("type", `Int ty) ] -> Fresh (id, ty)
      | x -> raise (Yojson.Basic.Util.Type_error ("Invalid agent", x)))
    | x -> raise (Yojson.Basic.Util.Type_error ("Invalid agent", x))
end
OCaml

Innovation. Community. Security.