# EYE Components for NBBN -- Jos De Roo

@prefix math: <http://www.w3.org/2000/10/swap/math#>.
@prefix log: <http://www.w3.org/2000/10/swap/log#>.
@prefix list: <http://www.w3.org/2000/10/swap/list#>.
@prefix e: <http://eulersharp.sourceforge.net/2003/03swap/log-rules#>.
@prefix bbn: <http://eulersharp.sourceforge.net/2006/02swap/nbbn-plugin#>.

# unplug the Full Bayes Belief Network builtin
true <= {
    ("abolish" (| "/" e:biconditional 2 |)) log:pro true.
}.

# plugin the Naive Bayes Belief Network builtin
{?L e:biconditional ?D} <= {
    ?SCOPE e:call {
        ?L e:firstRest ({?A e:boolean ?B} ?C).
        ("if_then_else"
            {("nb_getval" "bnet" "done") log:pro true}
            true
            {   () bbn:bnet true.
                ("nb_setval" "bnet" "done") log:pro true.
            }
        ) log:pro true.
        (?A) bbn:bvar true.
        (?B) bbn:bval true.
        ({?A e:boolean ?B} ?C ?D) bbn:nb true.
    }
}.

{() bbn:bnet true} <= {
    (";"
        {   (";"
                {   ?L1 e:firstRest (?I ?J).
                    ?L1 e:conditional ?O.
                    ?J e:sort ?K.
                    ("findall"
                        ?Y
                        {   ?L2 e:firstRest (?I ?X).
                            ?L2 e:conditional ?Y.
                            ?X e:sort ?K.
                        }
                        ?L
                    ) log:pro true.
                    ?L math:sum ?S.
                    ?L math:memberCount ?N.
                    (?S ?N) math:quotient ?Z.
                    ("call" {?L1 bbn:bcnd ?P}) log:pro false.
                    ("assertz" {?L1 bbn:bcnd ?Z}) log:pro true.
                    ?I bbn:inverse ?M.
                    ?L3 e:firstRest (?M ?J).
                    ("call" {?L3 bbn:bcnd ?Q}) log:pro false.
                    (1 ?Z) math:difference ?E.
                    ("assertz" {?L3 bbn:bcnd ?E}) log:pro true.
                    ("fail") log:pro true.
                }
                {   ?L4 e:firstRest ({?I e:boolean ?R} ?J).
                    ?L4 bbn:bcnd ?T.
                    (";"
                        {   
                            ("call" {(?I) bbn:bvar true}) log:pro false.
                            ("assertz" {(?I) bbn:bvar true}) log:pro true.
                        }
                        true
                    ) log:pro true.
                    {?K e:boolean ?U} list:in ?J.
                    ("call" {(?K) bbn:bvar true}) log:pro false.
                    ("assertz" {(?K) bbn:bvar true}) log:pro true.
                    ("fail") log:pro true.
                }
            ) log:pro true.
        }
        true
    ) log:pro true.
}.

{{?A e:boolean e:T} bbn:inverse {?A e:boolean e:F}} <= true.
{{?A e:boolean e:F} bbn:inverse {?A e:boolean e:T}} <= true.

(e:T) bbn:bval true.
(e:F) bbn:bval true.

{(?A ?B 1.0) bbn:nb true} <= {
    ("memberchk" ?A ?B) log:pro true.
    true log:callWithCut true.
}.

{({?A e:boolean e:T} ?B 0.0) bbn:nb true} <= {
    ("memberchk" {?A e:boolean e:F} ?B) log:pro true.
    true log:callWithCut true.
}.

{({?A e:boolean e:F} ?B ?C) bbn:nb true} <= {
    ("if_then_else"
        {   ("memberchk" {?A e:boolean e:T} ?B) log:pro true.
            ?C log:equalTo 0.0.
        }
        true
        {   ({?A e:boolean e:T} ?B ?D) bbn:nb true.
            ("1-_D" ?D) e:calculate ?C.
        }
    ) log:pro true.
}.

{(?A ?B ?C) bbn:nb true} <= {
    ?L e:firstRest (?A ?B).
    ?L bbn:bcnd ?C.
    true log:callWithCut true.
}.

{(?A ?B ?C) bbn:nb true} <= {
    ("if_then_else"
        {   (?A ?D) bbn:bcnd ?E.
            (?D ?B ?F) bbn:nb true.
            ?D bbn:inverse ?G.
            (?A ?G) bbn:bcnd ?H.
            ("_F*_E+(1-_F)*_H" ?F ?E ?H) e:calculate ?C.
        }
        true
        {   (?A ?B ?I) bbn:nc true.
            ?A bbn:inverse ?J.
            (?J ?B ?K) bbn:nc true.
            ("1/(1+2**(_K-_I))" ?K ?I) e:calculate ?C.
        }
    ) log:pro true.
}.

{(?A () 0.0) bbn:nc true} <= {
    true log:callWithCut true.
}.

{(?A ?L ?D) bbn:nc true} <= {
    ?L e:firstRest (?B ?C).
    (";"
        {   (";"
                {(?B ?A) bbn:bcnd ?E}
                {   ?B bbn:inverse ?F.
                    (?F ?A) bbn:bcnd ?G.
                    (1 ?G) math:difference ?E.
                }
            ) log:pro true.
        }
        {?E log:equalTo 1.0}
    ) log:pro true.
    true log:callWithCut true.
    ("if_then_else"
        {?E math:equalTo 0}
        {("epsilon") e:calculate ?I}
        {?I log:equalTo ?E}
    ) log:pro true.
    (?A ?C ?H) bbn:nc true.
    ("log(_I)/log(2)+_H" ?I ?H) e:calculate ?D.
}.
