Skip to content

Commit ce033de

Browse files
committed
WIP: still need to transform between name forms
1 parent 1049efb commit ce033de

File tree

2 files changed

+12
-76
lines changed

2 files changed

+12
-76
lines changed

src/qualitative_networks.jl

Lines changed: 11 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import StructUtils
55

66
using AbstractTrees: Leaves, PostOrderDFS
77
using 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
99
using HerbConstraints: DomainRuleNode, Forbidden, Ordered, Unique, VarNode, addconstraint!
1010
using HerbCore: AbstractGrammar, RuleNode, get_rule
1111
using HerbGrammar: @csgrammar, add_rule!, rulenode2expr
@@ -232,6 +232,7 @@ struct EntityIdName{S} <: EntityLabel
232232
end
233233
id(e::EntityIdName) = e.id
234234
name(e::EntityIdName) = e.name
235+
combined_name(e::EntityIdName) = Symbol("$(name(e))_$(id(e))")
235236
Base.:(==)(e::EntityIdName, e2::EntityIdName) = id(e) == id(e2) && name(e) == name(e2)
236237

237238
struct Entity{I<:EntityLabel,D}
@@ -251,6 +252,10 @@ domain(e::Entity) = e.domain
251252
range_from(e::Entity) = first(domain(e))
252253
range_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+
254259
function get_used_entities(fn, entities_in_model)
255260
filter(in(name.(entities_in_model)), collect(Leaves(fn)))
256261
end
@@ -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)
744752
end
745753

746754
function 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-
# )
759761
end
760762

761763
"""
@@ -818,58 +820,7 @@ function nested_dicts_keys_to_lowercase(d)
818820
return d
819821
end
820822
end
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+
873824
function 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
941892
end
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

test/qn_test.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ end
192192
qn = QN(model_path)
193193
@test length(labels(get_graph(qn))) > 0
194194
@test length(edge_labels(get_graph(qn))) > 0
195+
195196
output_str = JSON.json(qn)
196197
output_dict = JSON.parse(output_str)
197198
orig_dict = JSON.parse(read(model_path, String))

0 commit comments

Comments
 (0)