@@ -15,8 +15,8 @@ import Compat: @compat
1515A graph dynamical system with a finite domain. State values of each entity are
1616limited to change by at most 1 per time step.
1717"""
18- struct QualitativeNetwork{N_Entities,Schedule,Graph<: MG.MetaGraph } < :
19- GraphDynamicalSystem{N_Entities,Schedule}
18+ struct QualitativeNetwork{N_Entities, Schedule, Graph <: MG.MetaGraph } < :
19+ GraphDynamicalSystem{N_Entities, Schedule}
2020 graph:: Graph
2121end
2222
@@ -34,7 +34,7 @@ DSB.current_time(::QualitativeNetwork) = 0
3434DSB. reinit! (qn:: QN , state:: AbstractVector ) = DSB. set_state! (qn, Int .(state))
3535
3636
37- mutable struct QNEntity{F,S, D} <: AbstractEntity
37+ mutable struct QNEntity{F, S, D} <: AbstractEntity
3838 fn:: F
3939 state:: S
4040 domain:: D
@@ -49,16 +49,16 @@ function DSB.set_state!(qne::QNEntity, s)
4949 " New state value for entity must be within its domain (domain: $domain , new state: $s )" ,
5050 )
5151 end
52- qne. state = s
52+ return qne. state = s
5353end
5454get_domain (qne:: QNEntity ) = qne. domain
5555
56- function QualitativeNetwork (graph:: G ) where {G<: Graphs.AbstractGraph }
57- QualitativeNetwork {Graphs.nv(graph),Synchronous(),G} (graph)
56+ function QualitativeNetwork (graph:: G ) where {G <: Graphs.AbstractGraph }
57+ return QualitativeNetwork {Graphs.nv(graph), Synchronous(), G} (graph)
5858end
5959
6060function QualitativeNetwork (update_functions:: AbstractDict , domains)
61- QualitativeNetwork {Graphs.SimpleDiGraph} (update_functions, domains)
61+ return QualitativeNetwork {Graphs.SimpleDiGraph} (update_functions, domains)
6262end
6363
6464"""
@@ -72,49 +72,49 @@ The entity names (`E` in the signature) can be anything, while the functions (`F
7272- implement the `TermInterface.jl` interface. Any terminal nodes in the functions must be numerical constants or reference an entity.
7373"""
7474function QualitativeNetwork {GraphType} (
75- update_functions:: AbstractDict{E,F} ,
76- domains,
77- ):: QualitativeNetwork where {E,F, GraphType<: Graphs.AbstractGraph }
75+ update_functions:: AbstractDict{E, F} ,
76+ domains,
77+ ):: QualitativeNetwork where {E, F, GraphType <: Graphs.AbstractGraph }
7878 entity_keys = collect (keys (update_functions))
7979 entity_fns = getindex .((update_functions,), entity_keys)
8080 entity_domains = getindex .((domains,), entity_keys)
8181 get_arguments_or_empty = x -> TI. isexpr (x) ? (x, TI. arguments (x)) : (x, ())
8282 collect_arguments =
8383 x ->
84- AT. treemap (get_arguments_or_empty, x) |>
85- AT. Leaves .| >
86- AT. nodevalue |>
87- filter (in (entity_keys))
84+ AT. treemap (get_arguments_or_empty, x) |>
85+ AT. Leaves .| >
86+ AT. nodevalue |>
87+ filter (in (entity_keys))
8888
8989 referenced_entities = union .(collect_arguments .(entity_fns))
9090 referenced_indices =
9191 map (ref_for_e -> findfirst .(.== (ref_for_e), (entity_keys,)), referenced_entities)
9292 edges =
9393 Iterators. flatten (
94- map (((j, idxs),) -> tuple .(idxs, (j,)), enumerate (referenced_indices)),
95- ) |> collect
94+ map (((j, idxs),) -> tuple .(idxs, (j,)), enumerate (referenced_indices)),
95+ ) |> collect
9696 graph = GraphType ()
9797 Graphs. add_vertices! (graph, length (entity_keys))
9898 Graphs. add_edge! .((graph,), Graphs. Edge .(edges))
99- vertices_description = Pair{E,QNEntity}[
99+ vertices_description = Pair{E, QNEntity}[
100100 (e => QNEntity (fn, 0 , d)) for
101- (e, fn, d) in zip (entity_keys, entity_fns, entity_domains)
101+ (e, fn, d) in zip (entity_keys, entity_fns, entity_domains)
102102 ]
103- edges_description = Pair{Tuple{E,E},Nothing}[
103+ edges_description = Pair{Tuple{E, E}, Nothing}[
104104 (entity_keys[s], entity_keys[d]) => nothing for (s, d) in edges
105105 ]
106106 return QualitativeNetwork (
107107 MG. MetaGraph (graph, vertices_description, edges_description, nothing ),
108108 )
109109end
110110
111- function SciMLBase. step! (qn:: QualitativeNetwork{N,S} ) where {N,S}
112- SciMLBase. step! (S, qn)
111+ function SciMLBase. step! (qn:: QualitativeNetwork{N, S} ) where {N, S}
112+ return SciMLBase. step! (S, qn)
113113end
114114SciMLBase. step! (qn:: QN , n:: Int , _... ) = foreach (_ -> SciMLBase. step! (qn), 1 : n)
115115
116116function limit_change (next, prev, lower, upper)
117- if next > prev
117+ return if next > prev
118118 min (upper, prev + 1 )
119119 elseif next < prev
120120 max (lower, prev - 1 )
@@ -124,18 +124,18 @@ function limit_change(next, prev, lower, upper)
124124end
125125
126126function DSB. set_state! (
127- qn:: QN{N,S, M} ,
128- new_state:: Int ,
129- entity:: L ,
130- ) where {N,S,I,G,L,M <: MG.MetaGraph{I,G, L} }
127+ qn:: QN{N, S, M} ,
128+ new_state:: Int ,
129+ entity:: L ,
130+ ) where {N, S, I, G, L, M <: MG.MetaGraph{I, G, L} }
131131 g = get_graph (qn)
132- DSB. set_state! (g[entity], new_state)
132+ return DSB. set_state! (g[entity], new_state)
133133end
134134
135135function DSB. set_state! (qn:: QN , new_state:: AbstractVector )
136136 g = get_graph (qn)
137137
138- DSB. set_state! .((qn,), new_state, MG. labels (g))
138+ return DSB. set_state! .((qn,), new_state, MG. labels (g))
139139end
140140
141141function SciMLBase. step! (:: Synchronous , qn:: QualitativeNetwork )
@@ -155,7 +155,7 @@ function SciMLBase.step!(::Synchronous, qn::QualitativeNetwork)
155155end
156156
157157function interpret (fn, state)
158- MLStyle. @match fn begin
158+ return MLStyle. @match fn begin
159159 :: Int => fn
160160 :: Symbol => state[fn]
161161 :($ a + $ b) => interpret (a, state) + interpret (b, state)
0 commit comments