Blame view

code/biflang/draft_biflang.jl 3.52 KB
3e0f9b8a   Francisco Coelho   back to work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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))))
=#