% widget example from "The Independent Choice Logic for modelling
% multiple agents under uncertainty".
% based on the example of Draper, Hanks and Weld 94
% Copyright 1997, David Poole, all rights reserved.

% NATURE MODULE
utility(1) <- do(notify,T) & painted(T) & processed(T).

painted(T+1) <- do(paint,T) & painting_works & ~ done(T). 
      % done prevents us from shipping then painting!
painted(T+1) <- painted(T).
      % painted persists

random([painting_works:0.95,painting_doesnt_work:0.05]).

done(T) <- shipped(T).
done(T) <- rejected(T).

blemished(T) <- flawed(T) & ~ painted(T).
flawed(T+1) <- flawed(T).
      % flawed persists

processed(T) <- rejected(T) & flawed(T).
processed(T) <- shipped(T) & ~ flawed(T).

shipped(T) <- do(ship,T).
shipped(T+1) <- shipped(T).

rejected(T) <- do(reject,T).
rejected(T+1) <- rejected(T).

sense(blemish,bad,T+1) <- do(inspect,T) & blemished(T) & ~ falsepos(T).
sense(blemish,ok,T+1) <- do(inspect,T) & blemished(T) & falsepos(T).
sense(blemish,ok,T+1) <- do(inspect,T) & ~ blemished(T).
sense(blemish,none,T+1) <- ~ do(inspect,T).

random([falsepos(T):0.1,notfalsepos(T):0.9]). % this assumes that tests are
          % independent.
random([flawed(0):0.3,unflawed(0):0.7]).

% AGENT MODULE
% perceptible: sense(blemished,Val,T)
% recallable: bel(ok,T), bel(bad,T)
% actions: do(reject,T), do(ship,T), do(notify,T), do(paint,T), do(inspect,T)

bel(bad,T) <- sense(blemish,bad,T).
bel(bad,T+1) <- bel(bad,T) & ~ sense(blemish,bad,T+1).
bel(ok,T) <- sense(blemish,ok,T) & ~ bel(bad,T).
bel(ok,T+1) <- bel(ok,T) & ~ sense(blemish,ok,T+1) & ~ sense(blemish,bad,T+1).
% bel(ok,0) <- true.
% bel(ok,T+1) <- bel(ok,T) & ~ sense(blemish,bad,T).

  % this represents badpersists (if you ever belive it is bad, no sensor
  % readings change your mind).
  % challenge: represent controllable([bel_latest,badpersists,okpersists])

do(reject,T) <- rejectORship(T) & rejectifOK(T) & bel(ok,T).
do(reject,T) <- rejectORship(T) & rejectifBAD(T) & ~ bel(ok,T).
do(ship,T) <- rejectORship(T) & shipifOK(T) & bel(ok,T).
do(ship,T) <- rejectORship(T) & shipifBAD(T) & ~ bel(ok,T).

controllable([do(notify,T), do(paint,T), do(inspect,T), do(nothing,T), rejectORship(T)]).
controllable([rejectifOK(T),shipifOK(T)]).
controllable([rejectifBAD(T),shipifBAD(T)]).

% TRY THE FOLLOWING QUERIES:
% explain(utility(1), [do(inspect,0), do(paint,0+1), rejectORship(0+1+1), shipifOK(0+1+1), rejectifBAD(0+1+1), do(notify,0+1+1+1)], []).
% explain(utility(1), [do(inspect,0), do(inspect,0+1), do(paint,0+1+1), rejectORship(0+1+1+1), shipifOK(0+1+1+1), rejectifBAD(0+1+1+1), do(notify,0+1+1+1+1)], []).
% explain(utility(1), [do(nothing,0), do(paint,0+1), rejectORship(0+1+1), shipifOK(0+1+1), rejectifBAD(0+1+1), do(notify,0+1+1+1)], []).
% explain(utility(1), [do(nothing,0), do(paint,0+1), rejectORship(0+1+1), shipifOK(0+1+1), shipifBAD(0+1+1), do(notify,0+1+1+1)], []).
