@@ -5,7 +5,7 @@ import StructUtils
55
66using AbstractTrees: Leaves, PostOrderDFS
77using DynamicalSystemsBase: ArbitrarySteppable, current_parameters, initial_state
8- using Graphs: AbstractGraph, SimpleDiGraph, add_edge!, add_vertex!
8+ using Graphs: AbstractGraph, SimpleDiGraph, add_edge!, add_vertex!, ne
99using HerbConstraints: DomainRuleNode, Forbidden, Ordered, Unique, VarNode, addconstraint!
1010using HerbCore: AbstractGrammar, RuleNode, get_rule
1111using HerbGrammar: @csgrammar , add_rule!, rulenode2expr
@@ -232,6 +232,7 @@ struct EntityIdName{S} <: EntityLabel
232232end
233233id (e:: EntityIdName ) = e. id
234234name (e:: EntityIdName ) = e. name
235+ combined_name (e:: EntityIdName ) = Symbol (" $(name (e)) _$(id (e)) " )
235236Base.:(== )(e:: EntityIdName , e2:: EntityIdName ) = id (e) == id (e2) && name (e) == name (e2)
236237
237238struct Entity{I<: EntityLabel ,D}
@@ -251,6 +252,10 @@ domain(e::Entity) = e.domain
251252range_from (e:: Entity ) = first (domain (e))
252253range_to (e:: Entity ) = last (domain (e))
253254
255+ function get_used_entities (fn, entities_in_model:: Vector{<:Entity{<:EntityIdName}} )
256+ filter (in (combined_name .(label .(entities_in_model))), collect (Leaves (fn)))
257+ end
258+
254259function get_used_entities (fn, entities_in_model)
255260 filter (in (name .(entities_in_model)), collect (Leaves (fn)))
256261end
@@ -341,50 +346,20 @@ Systems that include the model semantics wrap around this struct with an
341346from [`DynamicalSystems`](https://juliadynamics.github.io/DynamicalSystems.jl/stable/). See
342347[`create_qn_system`](@ref) for an example.
343348"""
344- struct QualitativeNetwork{
345- N,
346- Schedule,
347- M<: MetaGraph ,
348- # EntityLabelType,
349- # EntityData{EntityLabelType},
350- # EdgeDataType,
351- } <: GraphDynamicalSystem{N,Schedule}
349+ struct QualitativeNetwork{N,Schedule,M<: MetaGraph } <: GraphDynamicalSystem{N,Schedule}
352350 " Graph containing the topology and target functions of the network"
353- graph:: M # MetaGraph{
354- # Int,
355- # SimpleDiGraph{Int},
356- # EntityLabelType,
357- # EntityData{EntityLabelType},
358- # EdgeDataType,
359- # } # {Code, Graph type, vert. label, vert data, edge data}
351+ graph:: M
360352 " State of the network"
361353 state:: MVector{N,Int}
362354
363- function QualitativeNetwork (
364- graph, # ::MetaGraph{
365- # Int,
366- # SimpleDiGraph{Int},
367- # EntityLabelType,
368- # EntityDataType{EntityLabelType},
369- # EdgeDataType,
370- # },
371- state;
372- schedule = Synchronous,
373- ) # where {EntityLabelType,EntityDataType,EdgeDataType}
355+ function QualitativeNetwork (graph, state; schedule = Synchronous)
374356 N = nv (graph)
375357 if N != length (state)
376358 error (""" The number of entities in the model ($N ) must match the \
377359 length of the provided state vector ($(length (state)) ).""" )
378360 end
379361
380- return new{
381- N,
382- schedule (),
383- typeof (graph),
384- # EntityLabelType,
385- # EntityDataType{EntityLabelType},
386- # EdgeDataType,
387- }(graph, state)
362+ return new {N,schedule(),typeof(graph)} (graph, state)
388363 end
389364end
390365
@@ -683,10 +658,13 @@ function bma_dict_to_qn(bma_model::JSONModel)
683658 end
684659
685660 foreach (bma_relationships) do r
661+ if from (r) ∉ labels (mg) || to (r) ∉ labels (mg)
662+ error (" Either the source or destination of the edge is not in the graph." )
663+ end
686664 added = add_edge! (mg, from (r), to (r), type (r))
687665 if ! added
688666 @warn """
689- Encountered a duplicate relationship between entities (from: \
667+ Could not create an edge between entities (from: \
690668 #$(from (r)) ; to: #$(to (r)) ) while constructing \
691669 the QN.
692670 """
@@ -744,18 +722,12 @@ function classify_activators_inhibitors(d::AbstractDict)
744722end
745723
746724function remove_ids_from_entities_in_target_fn (ex)
747- # Main.@infiltrate
748725 @match ex begin
749726 :: Symbol => Symbol (first (rsplit (string (ex), " _" ; limit = 2 )))
750727 Expr (:call , op, children... ) =>
751728 Expr (:call , op, remove_ids_from_entities_in_target_fn .(children)... )
752729 _ => ex
753730 end
754-
755- # postwalk.(
756- # x -> @capture(x, e_Symbol) ? :($(Symbol(first(split(string(e), "_"))))) : x,
757- # functions,
758- # )
759731end
760732
761733"""
@@ -818,58 +790,7 @@ function nested_dicts_keys_to_lowercase(d)
818790 return d
819791 end
820792end
821- #
822- # function bma_dict_to_qn(bma_dict::AbstractDict)
823- # bma_dict = nested_dicts_keys_to_lowercase(bma_dict)
824- # model = bma_dict["model"]
825- # variables = model["variables"]
826- # relationships = model["relationships"]
827- #
828- # id_to_name = Dict([v["id"] => v["name"] for v in variables])
829- # names = [Symbol("$(v["name"])_$(v["id"])") for v in variables]
830- # mg = MetaGraph(SimpleDiGraph(), Int, Union{Expr,Integer,Symbol}, String)
831- #
832- # foreach(variables) do v
833- # id = v["id"]
834- # name = v["name"]
835- # # adding an empty expression: :()
836- # # because we need to construct the interaction graph
837- # # first before parsing the functions correctly
838- # added = add_vertex!(mg, id, :())
839- # if !added
840- # error(
841- # """
842- # Failed to add the entity (\"$name\", id: #$id) from the input file while \
843- # constructing the QN. Check that there is only one entity in the model with \
844- # the id #$id.
845- # """,
846- # )
847- # end
848- # end
849- #
850- # foreach(relationships) do r
851- # from = to_from_variable_id(r, "from")
852- # to = to_from_variable_id(r, "to")
853- # type_of_edge = r["type"]
854- # added = add_edge!(mg, from, to, type_of_edge)
855- # if !added
856- # @warn """
857- # Encountered a duplicate relationship between entities (from: \
858- # $(id_to_name[from]), #$from; to: $(id_to_name[to]), #$to) while constructing \
859- # the QN.
860- # """
861- # end
862- # end
863- #
864- # formulas = Union{Expr,Integer,Symbol}[
865- # create_target_function(v, collect(inneighbor_labels(mg, v["id"])), id_to_name, mg) for v in variables
866- # ]
867- #
868- # domains = [v["rangefrom"]:v["rangeto"] for v in variables]
869- #
870- # return QualitativeNetwork(names, formulas, domains; schedule = Asynchronous)
871- # end
872- #
793+
873794function sanitize_formula (f)
874795 # surround variable names with quotes
875796 return replace (f, r" var\( ([^\) ]+)\) " => s " var(\"\1\" )" )
@@ -939,19 +860,3 @@ function create_target_function(
939860 )
940861 end
941862end
942- #
943- # function to_from_variable_id(r, from_to)
944- # k = "$(from_to)variable"
945- # k_w_id = k * "id"
946- #
947- # if haskey(r, k)
948- # return r[k]
949- # elseif haskey(r, k_w_id)
950- # return r[k_w_id]
951- # else
952- # error("""
953- # Neither alternative key was found to retrieve the edge variable id. The \
954- # model file is not using the expected structure for BMA models.
955- # """)
956- # end
957- # end
0 commit comments