include("BIFLang.jl") using .BIFLang src = read("asia2.bif", String) # src = "network unknown { } # variable asia { # type discrete [ 2 ] { yes, no }; # } # probability ( either | lung, tub ) { # (yes, yes) 1.0, 0.0; # (no, yes) 1.0, 0.0; # (yes, no) 1.0, 0.0; # (no, no) 0.0, 1.0; # }" expr = parse_bif(src) # println(expr) children(s::Any) = [] function children(node::BIFLang.Node) node.args end function descendants(node::BIFLang.Node) unvisited = filter(n -> isa(n, BIFLang.Node), children(node)) visited = [] while length(unvisited) > 0 h = popfirst!(unvisited) push!(visited, h) fresh = filter(n -> isa(n, BIFLang.Node), children(h)) union!(unvisited, setdiff(fresh, visited)) end return visited end is_leaf(x::Any) = true is_leaf(x::BIFLang.Node) = false is_leaf(x::BIFLang.Property) = true is_leaf(x::BIFLang.Variable) = true is_leaf(x::BIFLang.Values) = true is_leaf(x::BIFLang.Key) = true is_leaf(x::BIFLang.Distribution) = true branches(x::Any) = [] function branches(node::BIFLang.Node) if is_leaf(node) return [[node.args]] else cs = children(node) return [ [ci, bi...] for ci in cs for bi in branches(ci) ] end end # println.(filter(n -> isa(n, Variable), children(expr))) # println.(filter(n -> isa(n, Values), descendants(expr))) # println.(filter(n -> 2 ∈ n.args, descendants(expr))) for bs in branches(expr) println.(bs) println() end #= Unit( Network(asia2,Properties( Property(what: ever), Property(goes: well), Property(with_me))), VariableDeclaration(Variable(asia), Discrete(2,Values(yes,no))), VariableDeclaration(Variable(tub), Discrete(2,Values(yes,no))), VariableDeclaration(Variable(smoke), Discrete(2,Values(yes,no))), VariableDeclaration(Variable(lung), Discrete(2,Values(yes,no))), VariableDeclaration(Variable(bronc), Discrete(2,Values(yes,no))), VariableDeclaration(Variable(either), Discrete(2,Values(yes,no))), VariableDeclaration(Variable(xray), Discrete(2,Values(yes,no))), VariableDeclaration(Variable(dysp), Discrete(2,Values(yes,no))), ProbabilityDeclaration(Variable(asia), Distribution(0.01,0.99)), ProbabilityDeclaration(Variable(tub),Parents(Variable(asia)), Entry(Key(yes),Distribution(0.05,0.95)), Entry(Key(no),Distribution(0.01,0.99))), ProbabilityDeclaration(Variable(smoke), Distribution(0.5,0.5)), ProbabilityDeclaration(Variable(lung),Parents(Variable(smoke)), Entry(Key(yes),Distribution(0.1,0.9)), Entry(Key(no),Distribution(0.01,0.99))), ProbabilityDeclaration(Variable(bronc),Parents(Variable(smoke)), Entry(Key(yes),Distribution(0.6,0.4)), Entry(Key(no),Distribution(0.3,0.7))), ProbabilityDeclaration(Variable(either),Parents(Variable(lung),Variable(tub)), Entry(Key(yes,yes),Distribution(1.0,0.0)), Entry(Key(no,yes),Distribution(1.0,0.0)), Entry(Key(yes,no),Distribution(1.0,0.0)), Entry(Key(no,no),Distribution(0.0,1.0))), ProbabilityDeclaration(Variable(xray),Parents(Variable(either)), Entry(Key(yes),Distribution(0.98,0.02)), Entry(Key(no),Distribution(0.05,0.95))), ProbabilityDeclaration(Variable(dysp),Parents(Variable(bronc),Variable(either)), Entry(Key(yes,yes),Distribution(0.9,0.1)), Entry(Key(no,yes),Distribution(0.7,0.3)), Entry(Key(yes,no),Distribution(0.8,0.2)), Entry(Key(no,no),Distribution(0.1,0.9)))) =#