@@ -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
@@ -683,10 +688,13 @@ function bma_dict_to_qn(bma_model::JSONModel)
683688 end
684689
685690 foreach (bma_relationships) do r
691+ if from (r) ∉ labels (mg) || to (r) ∉ labels (mg)
692+ error (" Either the source or destination of the edge is not in the graph." )
693+ end
686694 added = add_edge! (mg, from (r), to (r), type (r))
687695 if ! added
688696 @warn """
689- Encountered a duplicate relationship between entities (from: \
697+ Could not create an edge between entities (from: \
690698 #$(from (r)) ; to: #$(to (r)) ) while constructing \
691699 the QN.
692700 """
@@ -744,18 +752,12 @@ function classify_activators_inhibitors(d::AbstractDict)
744752end
745753
746754function remove_ids_from_entities_in_target_fn (ex)
747- # Main.@infiltrate
748755 @match ex begin
749756 :: Symbol => Symbol (first (rsplit (string (ex), " _" ; limit = 2 )))
750757 Expr (:call , op, children... ) =>
751758 Expr (:call , op, remove_ids_from_entities_in_target_fn .(children)... )
752759 _ => ex
753760 end
754-
755- # postwalk.(
756- # x -> @capture(x, e_Symbol) ? :($(Symbol(first(split(string(e), "_"))))) : x,
757- # functions,
758- # )
759761end
760762
761763"""
@@ -818,58 +820,7 @@ function nested_dicts_keys_to_lowercase(d)
818820 return d
819821 end
820822end
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- #
823+
873824function sanitize_formula (f)
874825 # surround variable names with quotes
875826 return replace (f, r" var\( ([^\) ]+)\) " => s " var(\"\1\" )" )
@@ -939,19 +890,3 @@ function create_target_function(
939890 )
940891 end
941892end
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