-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathJSONCSetTransformations.jl
More file actions
114 lines (93 loc) · 3.51 KB
/
Copy pathJSONCSetTransformations.jl
File metadata and controls
114 lines (93 loc) · 3.51 KB
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
""" JSON serialization of acset transformations.
"""
module JSONCSetTransformations
export generate_json_fin_function, parse_json_fin_function,
read_json_fin_function, write_json_fin_function,
generate_json_acset_transformation, parse_json_acset_transformation,
read_json_acset_transformation, write_json_acset_transformation
import JSON
using DataStructures: OrderedDict
using ..FinSets, ..CSets
# ACSetTransformation serialization
#####################
""" Generate JSON-able object representing a FinFunction.
Inverse to [`parse_json_fin_function`](@ref).
"""
function generate_json_fin_function(F::FinFunction)
OrderedDict{Symbol,Any}(
:dom => dom(F),
:codom => codom(F),
:map => collect(F))
end
""" Serialize a FinFunction object to a JSON file.
Inverse to [`read_json_fin_function`](@ref).
"""
function write_json_fin_function(x::FinFunction, fname::AbstractString)
open(fname, "w") do f
write(f, JSON.json(generate_json_fin_function(x)))
end
end
function parse_json_fin_function(input::AbstractDict)
FinFunction(
input["dom"]["n"] != 0 ? Int.(input["map"]) : 1:0,
input["dom"]["n"],
input["codom"]["n"])
end
""" Deserialize a FinFunction object from a JSON file.
Inverse to [`write_json_fin_function`](@ref).
"""
function read_json_fin_function(fname::AbstractString)
parse_json_fin_function(JSON.parsefile(fname))
end
""" Generate JSON-able object representing an ACSetTransformation.
Inverse to [`parse_json_acset_transformation`](@ref).
"""
function generate_json_acset_transformation(X::ACSetTransformation)
OrderedDict{Symbol,Any}(
:dom => (generate_json_acset ∘ dom)(X),
:codom => (generate_json_acset ∘ codom)(X),
:components => OrderedDict{Symbol,Any}(
Iterators.map((keys ∘ components)(X), (values ∘ components)(X)) do k,v
k , k ∈ (attrtypes ∘ acset_schema ∘ dom)(X) ?
# TODO: Support VarFunctions that are not empty.
"TODO: VarFunctions are current not supported." :
generate_json_fin_function(v)
end))
end
""" Serialize an ACSetTransformation object to a JSON file.
Inverse to [`read_json_acset_transformation`](@ref).
"""
function write_json_acset_transformation(x::ACSetTransformation, fname::AbstractString)
open(fname, "w") do f
write(f, JSON.json(generate_json_acset_transformation(x)))
end
end
""" Parse JSON-able object or JSON string representing an ACSetTransformation.
Inverse to [`generate_json_acset_transformation`](@ref).
"""
parse_json_acset_transformation(cons, input::AbstractString) =
parse_json_acset_transformation(cons, JSON.parse(input))
parse_json_acset_transformation(acs::ACSet, input::AbstractDict) =
parse_json_acset_transformation(constructor(acs), input)
function parse_json_acset_transformation(cons, input::AbstractDict)
domain = parse_json_acset(cons(), input["dom"])
codomain = parse_json_acset(cons(), input["codom"])
hom_keys = filter(keys(input["components"])) do k
Symbol(k) ∉ (attrtypes ∘ acset_schema)(domain)
end
# TODO: Support VarFunctions that are not empty.
ACSetTransformation(
NamedTuple{Tuple(Symbol.(hom_keys))}(
Iterators.map(hom_keys) do k
parse_json_fin_function(input["components"][k])
end),
domain,
codomain)
end
""" Deserialize an ACSetTransformation object from a JSON file.
Inverse to [`write_json_acset_transformation`](@ref).
"""
function read_json_acset_transformation(ty, fname::AbstractString)
parse_json_acset_transformation(ty, JSON.parsefile(fname))
end
end # module