Skip to content

Commit d91c44f

Browse files
committed
Add methods, tests for QN properties
1 parent 3e3ffe0 commit d91c44f

File tree

4 files changed

+103
-12
lines changed

4 files changed

+103
-12
lines changed

Project.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@ version = "0.1.0"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
8+
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
89
DynamicalSystems = "61744808-ddfa-5f27-97ff-6e42cc95d634"
910
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
1011
HerbCore = "2b23ba43-8213-43cb-b5ea-38c12b45bd45"
1112
HerbGrammar = "4ef9e186-2fe5-4b24-8de7-9f7291f24af7"
1213
HerbSearch = "3008d8e8-f9aa-438a-92ed-26e9c7b4829f"
14+
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
1315
MetaGraphsNext = "fa8bd995-216d-47f1-8a91-f3b68fbeb377"
1416
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1517
SoleLogics = "b002da8f-3cb3-4d91-bbe3-2953433912b5"
1618
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1719

1820
[compat]
21+
DocStringExtensions = "0.9.3"
1922
DynamicalSystems = "3"
2023
FileIO = "1"
24+
HerbGrammar = "0.4.0"
25+
MLStyle = "0.4.17"
2126
MetaGraphsNext = "0.7"
2227
SoleLogics = "0.9, 0.10"
2328
Statistics = "1.11.1"

src/GraphDynamicalSystems.jl

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
module GraphDynamicalSystems
22

3-
using SoleLogics
4-
using MetaGraphsNext
3+
using DocStringExtensions
54
using DynamicalSystems
5+
using MetaGraphsNext
6+
using SoleLogics
7+
68

79
include("boolean_networks.jl")
810
export BooleanNetworks
911

1012
include("qualitative_networks.jl")
11-
export QualitativeNetworks
13+
export QualitativeNetwork,
14+
build_qn_grammar,
15+
update_functions_to_network,
16+
sample_qualitative_network,
17+
max_level,
18+
components,
19+
C,
20+
get_state,
21+
S,
22+
target_functions,
23+
T,
24+
set_state!,
25+
interpret,
26+
aqn
1227

1328
# """
1429
# gds(bn::MetaGraph)

src/qualitative_networks.jl

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
module QualitativeNetworks
2-
31
using AbstractTrees: Leaves
42
using HerbCore: AbstractGrammar, AbstractRuleNode, RuleNode, get_rule
53
using HerbGrammar: @csgrammar, add_rule!
64
using HerbSearch
75
using MetaGraphsNext: MetaGraph, SimpleDiGraph, add_edge!
6+
using MLStyle: @match
87

98
const base_qn_grammar = @csgrammar begin
109
ManyVals = Pos() | Neg()
@@ -77,20 +76,70 @@ function sample_qualitative_network(size::Int, max_eq_depth::Int)
7776
sample_qualitative_network(entities, max_eq_depth)
7877
end
7978

80-
mutable struct QualitativeNetwork
79+
struct QualitativeNetwork
8180
graph::MetaGraph
8281
state::AbstractVector{Int}
82+
N::Int
83+
84+
function QualitativeNetwork(g, s, N)
85+
if any(s .> N)
86+
error("All values in state must be <= N (N=$N)")
87+
else
88+
return new(g, s, N)
89+
end
90+
end
8391
end
8492

8593
const QN = QualitativeNetwork
8694

87-
function qn_step!(model::QN) end
95+
function max_level(qn::QN)
96+
return qn.N
97+
end
98+
99+
function _get_component_index(qn::QN, component::Symbol)
100+
return findfirst(isequal(component), components(qn))
101+
end
102+
103+
function components(qn::QN)
104+
return labels(qn.graph)
105+
end
106+
107+
C(qn::QN) = components(qn)
108+
109+
function target_functions(qn::QN)
110+
return qn.graph.vertex_properties
111+
end
112+
113+
T(qn::QN) = target_functions(qn)
114+
115+
get_state(qn::QN) = qn.state
116+
117+
function get_state(qn::QN, component::Symbol)
118+
i = _get_component_index(qn, component)
119+
return qn.state[i]
120+
end
121+
122+
S(qn::QN) = qn.state
123+
S(qn::QN, component::Symbol) = get_state(qn, component)
124+
125+
function _set_state!(qn::QN, component::Symbol, value::Integer)
126+
i = _get_component_index(qn::QN, component::Symbol)
127+
qn.state[i] = value
128+
end
129+
130+
function set_state!(qn::QN, component::Symbol, value::Integer)
131+
if value > max_level(qn)
132+
error(
133+
"Value ($value) cannot be larger than the QN's maximum level (N=$(max_level(qn)))",
134+
)
135+
end
136+
137+
_set_state!(qn, component, value)
138+
end
88139

89140
extract_state(model::QN) = model.state
90141
extract_parameters(model::QN) = model.graph
91142

92143
function reset_model!(model::QN, u, _)
93144
model.state .= u
94145
end
95-
96-
end

test/test-qn.jl

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
using Graphs: ne, nv
2+
using GraphDynamicalSystems
3+
using HerbCore
24

35
@testset "QN Grammar Creation" begin
46
entities = [:a, :b, :c]
57
constants = [i for i = 1:10]
6-
g = QualitativeNetworks.build_qn_grammar(entities, constants)
8+
g = build_qn_grammar(entities, constants)
79

810
@test issubset(Set(entities), Set(g.rules))
911
@test issubset(Set(constants), Set(g.rules))
1012

11-
g2 = QualitativeNetworks.build_qn_grammar(Symbol[], Integer[])
13+
g2 = build_qn_grammar(Symbol[], Integer[])
1214

1315
@test isempty(intersect(Set(g2.rules), Set(entities)))
1416
@test isempty(intersect(Set(g2.rules), Set(constants)))
@@ -17,7 +19,27 @@ end
1719
@testset "QN Sampling" begin
1820
size = 3
1921
max_eq_depth = 3
20-
qn = QualitativeNetworks.sample_qualitative_network(size, max_eq_depth)
22+
qn = sample_qualitative_network(size, max_eq_depth)
2123
@test nv(qn.graph) == size
2224
@test ne(qn.graph) > 0
2325
end
26+
27+
@testset "QN properties, fields" begin
28+
size = 3
29+
max_eq_depth = 3
30+
N = 5
31+
network = sample_qualitative_network(size, max_eq_depth)
32+
initial_state = ones(size)
33+
qn = QualitativeNetwork(network, initial_state, N)
34+
35+
@test length(components(qn)) == size
36+
@test length(C(qn)) == size
37+
38+
@test length(target_functions(qn)) == size
39+
@test length(T(qn)) == size
40+
@test all(depth(T(qn)) .<= max_eq_depth)
41+
42+
@test all(get_state(qn) .<= max_level(qn))
43+
44+
@test_throws r"max" set_state!(qn, Symbol(1), 6)
45+
end

0 commit comments

Comments
 (0)