
%% This file (switches) contains predicates for 
%% printing out and `flipping' the program `switches'. 
%% 
%% These switches determine the way that solutions to 
%% analysis/generation are printed out by the program, 
%% e.g. how segments are printed, whether partitions 
%% are printed, etc. 
%% 
%% The switches are binary - i.e. either "on" or "off"
%% State of switch stored as a "flag", i.e.: 
%%     flag(Switch, State)
%% If no value flagged, value assumed to be "off". 
%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Initial declaration of switch set
% (order in list also determines order in which switches printed). 

switch_set([ print_partitions,       % Print partitions
	     list_features,          % Print segments as Feat=Val lists
             show_candidates,        % Suggest candidate I-Ds for segs
	     print_seg_as_candidates % Only print candidate I-D for segs
	     ]).                     % (don't print seg itself) 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% switches/0: 
% For changing the state of the program switches.
%
% Prints numbered list of switches and their current state.
% User enters numbers for switches to be flipped. 
% Then, list of switches and their states printed once more. 

switches:-                 
   print_switches,       % Print switch list
   nl, nl,
   write('Enter numbers for switches to be changed, then press return: '), 
   nl, write('|: '), 
   get_alphanum_strings(Input),        % Read selections for changes
   switches(Input).                    % Proceed on input. 

sw:- switches.        % abbreviated command

switches([]):- 
   nl, write('No switches changed.'), nl.  % Null input, no changes.
switches(Input):- 
   digit_strings_to_numbers(Input,Ns), !,  % identify selection numbers  
   switch_set(Switches), 
   change_switches(Ns,Switches),           % change selected switches
   print_switches, nl.                     % Print switch list, again
switches(_):- 
   nl, write(' ** unrecognised input **'), nl.   % Input wrong. 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Change switches selected by number

change_switches([],_).               % finished
change_switches([N|Ns],Switches):-   
   nth(N,Switch,Switches), !,        % identify Nth switch
   flip_switch(Switch),              % flip that switch's state
   change_switches(Ns,Switches).     % recurse
change_switches([N|Ns],Switches):-
   write('Switch number '), write(N),  % otherwise, N out of range
   write(' out of range'), nl,         % for possible switches, warn, 
   change_switches(Ns,Switches).       % then proceed. 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% flip state of switch Switch.

flip_switch(Switch):-
   get_switch_state(Switch,State),     % get current switch state
   opposite_state(State,NewState),     % find opposite state
   set_flag(Switch,NewState).               % set state to be opposite

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Look up current switch state. If no value store, assume off. 

get_switch_state(Switch,State1):-
   flag(Switch,State2), !, 
   State1 = State2.
get_switch_state(_,off).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% opposite states for "on" and "off". 

opposite_state(on,off). 
opposite_state(off,on). 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Print a numbered list of the switches (as defined
% by switch_set), and the switch states. 

print_switches:-  
   switch_set(Switches), 
   max_atom_length(Switches,Max),
   print_switches(1,Switches,Max). 

print_switches(_,[],_Max).
print_switches(N,[Switch|Switches],Max):- 
   name(Switch,Name), 
   length(Name,Len), 
   J is Max - Len, 
   nl, write('   '), write(N), 
   write('. '), write(Switch),
   spaces(J),
   write('   (now '), 
   get_switch_state(Switch,State), 
   write(State), 
   write(')'), 
   N1 is N+1, 
   print_switches(N1,Switches,Max).
