% File "diagnosis": diagnostic support for the RCS
% First Release date: 04/16/04
%
% $Name:  $


%-----------------------------
%
% Possible Faults
%
%-----------------------------

fault_type(has_leak(ValveType)).
fault_type(stuck(ValveType)).
fault_type(stuck(SwitchType)).
fault_type(bad_circuitry(ValveType)).
#domain fault_type(FaultType;FaultType1;FaultType2).

r(has_leak(ValveType)): has_leak(ValveType) +- .
r(stuck(ValveType)): stuck(ValveType) +-.
r(stuck(SwitchType)): stuck(SwitchType) +-.
r(bad_circuitry(ValveType)): bad_circuitry(ValveType) +-.


% these faults are physically impossible
:- has_leak(V), dummy_valve(V).
:- bad_circuitry(V), dummy_valve(V).



%-----------------------------
%
% Preferences on Faults
%
%-----------------------------

%prefer(r_leak(ValveType),r_stuck(ValveType)).
more_likely(has_leak(ValveType),stuck(ValveType)).

%prefer(r_stuck(ValveType),r_badcircuitry(ValveType)).
%more_likely(stuck(ValveType),badcircuitry(ValveType)).

% with this preference enabled, expect more than 200 secs!!!
%prefer(r_stuck(ValveType),r_stuck(SwitchType)).
%more_likely(stuck(ValveType),stuck(SwitchType)).

% computation of 1 model...
% WITHOUT: d_stuck(fha)      (7 secs)
% WITH:    d_has_leak(ffi12) (11 secs)
%
%prefer(r(stuck(ValveType)),r(stuck(SwitchType))) :- controls(SwitchType,ValveType).
more_likely(stuck(ValveType),stuck(SwitchType)) :- controls(SwitchType,ValveType).

prefer(r(FaultType1),r(FaultType2)) :- more_likely(FaultType1,FaultType2).



%%%% testing
%:- stuck(SwitchType).
%:- has_leak(ValveType).
%:- stuck(ValveType).
%:- bad_circuitry(ValveType).
%:- not d_stuck(ffha).
%%%





%-----------------------------
%
% Domain Declarations
%
%-----------------------------

%
% NOTICE: for some reason, the following
% causes lparse to give nonrestricted rule error
%
% #domain of_type(ValveType,valve).
%
d_valvetype(V) :- of_typev(V,R,valve).
#domain d_valvetype(ValveType).

d_switchtype(Sw) :- of_type(Sw,v_switch).
#domain d_switchtype(SwitchType).



%-----------------------------
%
% Connection with Observations
%
%-----------------------------


%
% Observation Axioms
%
h(F,0) :- fluent(F), obs(F,0).
nh(F,0) :- fluent(F), obs(neg(F),0).

occurs(A,T) :- hpd(A,T).

%
% Reality Checks
%
:- fluent(F), time(T),
%   not nh(F,T), obs(neg(F),T).
   h(F,T), obs(neg(F),T).

:- fluent(F), time(T),
%   not h(F,T), obs(F,T).
   nh(F,T), obs(F,T).





%%%%% For Typing Purposes only

type_cc(closea_fi12).
type_cc(closeb_ffi12).
type_cc(closeb_foi12).

type_cc(closea_li12).
type_cc(closeb_lfi12).
type_cc(closeb_loi12).

type_cc(close_lx12).
type_cc(close_lfx12).
type_cc(close_lox12).

type_cc(closea_ri12).
type_cc(closeb_rfi12).
type_cc(closeb_roi12).

type_cc(close_rx12).
type_cc(close_rfx12).
type_cc(close_rox12).

type_cc(opena_ffm5).
type_cc(openb_ffm5).
type_cc(opena_lfm5).
type_cc(openb_lfm5).
type_cc(opena_rfm5).
type_cc(openb_rfm5).

type_cc(closea_ffm5).
type_cc(closeb_ffm5).
type_cc(closea_lfm5).
type_cc(closeb_lfm5).
type_cc(closea_rfm5).
type_cc(closeb_rfm5).

    

%%%%%%%%%%%%%%             Facts                %%%%%%%%%%%%%%%%%

% of_type(D,R,Dev) is true iff device D from RCS R is of type Dev. 

% Switches

of_type(fha,fwd_rcs,v_switch).       % fha   = forward helium a 
of_type(fhb,fwd_rcs,v_switch).       % fhb   = forward helium b 
of_type(fi12,fwd_rcs,v_switch).      % fi12  = forward isolation 12 
of_type(fi345,fwd_rcs,v_switch).     % fi345 = forward isolation 345 
of_type(fm1,fwd_rcs,v_switch).       % fm1   = forward manifold 1 
of_type(fm2,fwd_rcs,v_switch).       % fm2   = forward manifold 2 
of_type(fm3,fwd_rcs,v_switch).       % fm3   = forward manifold 3 
of_type(fm4,fwd_rcs,v_switch).       % fm4   = forward manifold 4 
of_type(fm5,fwd_rcs,v_switch).       % fm5   = forward manifold 5 

of_type(lha,left_rcs,v_switch).      % lha   = left helium a 
of_type(lhb,left_rcs,v_switch).      % lhb   = left helium b 
of_type(li12,left_rcs,v_switch).     % li12  = left isolation 12 
of_type(li345a,left_rcs,v_switch).   % li345a= left isolation 345a 
of_type(li345b,left_rcs,v_switch).   % li345b= left isolation 345b 
of_type(lm1,left_rcs,v_switch).      % lm1   = left manifold 1 
of_type(lm2,left_rcs,v_switch).      % lm2   = left manifold 2 
of_type(lm3,left_rcs,v_switch).      % lm3   = left manifold 3 
of_type(lm4,left_rcs,v_switch).      % lm4   = left manifold 4 
of_type(lm5,left_rcs,v_switch).      % lm5   = left manifold 5 
of_type(lx12,left_rcs,v_switch).     % lx12  = left cross-feed isolation 12  
of_type(lx345,left_rcs,v_switch).    % lx345 = left cross-feed isolation 345  

of_type(rha,right_rcs,v_switch).     % rha   = right helium a 
of_type(rhb,right_rcs,v_switch).     % rhb   = right helium b 
of_type(ri12,right_rcs,v_switch).    % ri12  = right isolation 12 
of_type(ri345a,right_rcs,v_switch).  % ri345a= right isolation 345a 
of_type(ri345b,right_rcs,v_switch).  % ri345b= right isolation 345b 
of_type(rm1,right_rcs,v_switch).     % rm1   = right manifold 1 
of_type(rm2,right_rcs,v_switch).     % rm2   = right manifold 2 
of_type(rm3,right_rcs,v_switch).     % rm3   = right manifold 3 
of_type(rm4,right_rcs,v_switch).     % rm4   = right manifold 4 
of_type(rm5,right_rcs,v_switch).     % rm5   = right manifold 5 
of_type(rx12,right_rcs,v_switch).    % rx12  = right cross-feed isolation 12  
of_type(rx345,right_rcs,v_switch).   % rx345 = right cross-feed isolation 345 
 
% Valves
 
of_typev(ffha,fwd_rcs,valve). 
of_typev(foha,fwd_rcs,valve).
of_typev(ffhb,fwd_rcs,valve).
of_typev(fohb,fwd_rcs,valve).
of_typev(ffi12,fwd_rcs,valve).
of_typev(foi12,fwd_rcs,valve).
of_typev(ffi345,fwd_rcs,valve).
of_typev(foi345,fwd_rcs,valve).
of_typev(ffm1,fwd_rcs,valve).
of_typev(fom1,fwd_rcs,valve).
of_typev(ffm2,fwd_rcs,valve).
of_typev(fom2,fwd_rcs,valve).
of_typev(ffm3,fwd_rcs,valve).
of_typev(fom3,fwd_rcs,valve).
of_typev(ffm4,fwd_rcs,valve).
of_typev(fom4,fwd_rcs,valve).
of_typev(ffm5,fwd_rcs,valve).
of_typev(fom5,fwd_rcs,valve).

of_typev(lfha,left_rcs,valve). 
of_typev(loha,left_rcs,valve).
of_typev(lfhb,left_rcs,valve).
of_typev(lohb,left_rcs,valve).
of_typev(lfi12,left_rcs,valve).
of_typev(loi12,left_rcs,valve).
of_typev(lfi345a,left_rcs,valve).
of_typev(loi345a,left_rcs,valve).
of_typev(lfi345b,left_rcs,valve).
of_typev(loi345b,left_rcs,valve).
of_typev(lfm1,left_rcs,valve).
of_typev(lom1,left_rcs,valve).
of_typev(lfm2,left_rcs,valve).
of_typev(lom2,left_rcs,valve).
of_typev(lfm3,left_rcs,valve).
of_typev(lom3,left_rcs,valve).
of_typev(lfm4,left_rcs,valve).
of_typev(lom4,left_rcs,valve).
of_typev(lfm5,left_rcs,valve).
of_typev(lom5,left_rcs,valve).
of_typev(lfx12,left_rcs,valve).
of_typev(lox12,left_rcs,valve).
of_typev(lfx345,left_rcs,valve).
of_typev(lox345,left_rcs,valve).

of_typev(rfha,right_rcs,valve). 
of_typev(roha,right_rcs,valve).
of_typev(rfhb,right_rcs,valve).
of_typev(rohb,right_rcs,valve).
of_typev(rfi12,right_rcs,valve).
of_typev(roi12,right_rcs,valve).
of_typev(rfi345a,right_rcs,valve).
of_typev(roi345a,right_rcs,valve).
of_typev(rfi345b,right_rcs,valve).
of_typev(roi345b,right_rcs,valve).
of_typev(rfm1,right_rcs,valve).
of_typev(rom1,right_rcs,valve).
of_typev(rfm2,right_rcs,valve).
of_typev(rom2,right_rcs,valve).
of_typev(rfm3,right_rcs,valve).
of_typev(rom3,right_rcs,valve).
of_typev(rfm4,right_rcs,valve).
of_typev(rom4,right_rcs,valve).
of_typev(rfm5,right_rcs,valve).
of_typev(rom5,right_rcs,valve).
of_typev(rfx12,right_rcs,valve).
of_typev(rox12,right_rcs,valve).
of_typev(rfx345,right_rcs,valve).
of_typev(rox345,right_rcs,valve).

of_type(V,R,valve) :- of_typev(V,R,valve).

of_type(V,R,valve) :- dummy_valve(V,R).


% Dummy valves have been added between fuel/oxidizer tanks
% and the following junction node in order to correctly
% determine when the junction node is leaking.
 
dummy_valve(ffdummy,fwd_rcs).
dummy_valve(fodummy,fwd_rcs).
dummy_valve(lfdummy,left_rcs).
dummy_valve(lodummy,left_rcs).
dummy_valve(rfdummy,right_rcs).
dummy_valve(rodummy,right_rcs).



 
%%%%%%%%%%%%%%%%%%%%%%%%   End: Control Module   %%%%%%%%%%%%%%%%%%%%%%%%% 

hide dummy_valve(X,Y).
hide of_type(X,Y,Z).
hide of_typev(X,Y,Z).
hide type_cc(X).

