% File "cr-plan-unrel-sw": uses computer commands and unreliable
%                          switches ONLY if needed. Also,
%                          minimizes the performed actions with
%                          #minimize.
%
% First Release date: 04/22/05
%
% $Name:  $


% Typical use: (define #const lasttime=5)
%   crmodels -n 1 rcs1 cr-plan-unrel-sw heuristics problem-base tinst-4 hides





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%% ACTIONS %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


switch_action_of(flip(Sw,S),R) :- of_type(Sw,R,v_switch), 
				  state_of(S,v_switch).
#hide switch_action_of(X,Y).


action_of(A,R) :- switch_action_of(A,R).

action_of(CC,R) :- commands(CC,V,S),
                   of_type(V,R,valve).
                  % not bad_circuitry(V).


r1: use_ccs +-.

r3: use_unreliable_sw +-.

:- of_type(Sw,R,v_switch), state_of(S,v_switch),
   time(T),
   occurs(flip(Sw,S),T),
   unreliable(Sw),
   not use_unreliable_sw.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%   END OF ACTIONS
%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% XFEED %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Subsystem R is involved in the maneuver at time T
% if the goal in that subsystem has not yet been achieved.
%
involved(R,T) :- system(R), time(T),
		 not goal(T,R),
		 not done(R).
#hide involved(X,Y).


% aft subsystems
%
aft_system(left_rcs).
aft_system(right_rcs).
#hide aft_system(X).


involved(AFT1,T) :- aft_system(AFT1), time(T),
		    aft_system(AFT2), neq(AFT2,AFT1),
		    not goal(T,AFT2),
		    not done(AFT2),
		    use_xfeed.

r2: use_xfeed +-.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%   END OF XFEED
%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%% GENERATION %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

0{occurs(A,T) :switch_action_of(A,R)}1 :- time(T),
				   T < lasttime,
				   system(R),
				   involved(R,T),
				   not use_ccs.

0{occurs(A,T) :action_of(A,R)}1 :- time(T),
				   T < lasttime,
				   system(R),
				   involved(R,T),
				   use_ccs.


#minimize{occurs(A,T) : action_of(A,R) : time(T)}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%   END OF GENERATION
%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%% 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).

    


%%%%%%%%%%%%%%%%%%%%%%%%     Goal     %%%%%%%%%%%%%%%%%%%%%%%%%% 
 
goal :- time(T1),
        time(T2),
        time(T3), 
        goal(T1,left_rcs),
        goal(T2,right_rcs),
        goal(T3,fwd_rcs).

goal(T,S) :- time(T), done(S).

:- not goal.




%%%%%%%%%%%%%%%%%%%%%%%%   End: Goal   %%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%             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_type(ffha,fwd_rcs,valve). 
of_type(foha,fwd_rcs,valve).
of_type(ffhb,fwd_rcs,valve).
of_type(fohb,fwd_rcs,valve).
of_type(ffi12,fwd_rcs,valve).
of_type(foi12,fwd_rcs,valve).
of_type(ffi345,fwd_rcs,valve).
of_type(foi345,fwd_rcs,valve).
of_type(ffm1,fwd_rcs,valve).
of_type(fom1,fwd_rcs,valve).
of_type(ffm2,fwd_rcs,valve).
of_type(fom2,fwd_rcs,valve).
of_type(ffm3,fwd_rcs,valve).
of_type(fom3,fwd_rcs,valve).
of_type(ffm4,fwd_rcs,valve).
of_type(fom4,fwd_rcs,valve).
of_type(ffm5,fwd_rcs,valve).
of_type(fom5,fwd_rcs,valve).

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

of_type(rfha,right_rcs,valve). 
of_type(roha,right_rcs,valve).
of_type(rfhb,right_rcs,valve).
of_type(rohb,right_rcs,valve).
of_type(rfi12,right_rcs,valve).
of_type(roi12,right_rcs,valve).
of_type(rfi345a,right_rcs,valve).
of_type(roi345a,right_rcs,valve).
of_type(rfi345b,right_rcs,valve).
of_type(roi345b,right_rcs,valve).
of_type(rfm1,right_rcs,valve).
of_type(rom1,right_rcs,valve).
of_type(rfm2,right_rcs,valve).
of_type(rom2,right_rcs,valve).
of_type(rfm3,right_rcs,valve).
of_type(rom3,right_rcs,valve).
of_type(rfm4,right_rcs,valve).
of_type(rom4,right_rcs,valve).
of_type(rfm5,right_rcs,valve).
of_type(rom5,right_rcs,valve).
of_type(rfx12,right_rcs,valve).
of_type(rox12,right_rcs,valve).
of_type(rfx345,right_rcs,valve).
of_type(rox345,right_rcs,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 action_of(X,Y).
hide of_type(X,Y,Z).
hide command_between(X,Y,Z). 
hide issued_commands(X,Y). 
hide prev(X,Y). 
hide open_prev(X,Y). 
hide perf(X,Y).
hide type_cc(X).
hide is_pressurized_by(X,Y). 
