% Set paths
Alex_path=1;

if Alex_path==1,
    % Alex path
    path='/Users/mereacre/Documents/Marco/Heart_Model/Model/';
else
    % Marco path
    path='/users/mardic/Desktop/Latex_Files/Heart_Model/Model/';
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load the PACEMAKER information from file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Aget = 1
% Vget = 2
% AP = 3
% VP = 4
% time = 0
% tau = 5
% AS = 6
% VS = 7
% AR = 8

fprintf(1,'Loading pacemaker transition file...\n');

fid=fopen(strcat(path,'pacemaker_mdp.tra'),'r');


% Read the first line of the file
% n_elements n_transitions n_transitions dummy dummy
var=fscanf(fid,'%d',[1 5]);

% Number of transitions of the PACEMAKER
N_TRAN_PACEMAKER=var(2);

% Number of states of the PACEMAKER
N_STATES_PACEMAKER=var(1);

% Transition matrix for the PACEMAKER
TP=ones(N_TRAN_PACEMAKER,3);

% Index matrix. It contains the index of the first row of each node in TP.
IM=zeros(N_STATES_PACEMAKER,1); 

next=1;

% Read the transitions
for i=1:N_TRAN_PACEMAKER,
    var=[];
    act=[];
   
    
    % Read the first line of the file
    var=fscanf(fid,'%d',[1 4]);
    act=fscanf(fid,'%s',1);
    
    if strcmp(act,'Aget')==0 && strcmp(act,'Vget')==0 && strcmp(act,'AP')==0 && strcmp(act,'VP')==0 && strcmp(act,'time')==0 && strcmp(act,'AS')==0 && strcmp(act,'VS')==0 && strcmp(act,'AR')==0,
        fseek(fid,-size(act,2),'cof');
        
        % Internal action
        act='tau';
    end
    
    % Predecessor state
    TP(i,1)=var(1);
    
    % Successor state
    TP(i,2)=var(3);
    
    % Action
    if strcmp(act,'Aget')==1,
        TP(i,3)=1;
    elseif strcmp(act,'Vget')==1,
        TP(i,3)=2;
    elseif strcmp(act,'AP')==1,
        TP(i,3)=3;
    elseif strcmp(act,'VP')==1,
        TP(i,3)=4;
    elseif strcmp(act,'time')==1,
        TP(i,3)=0;
    elseif strcmp(act,'tau')==1
        TP(i,3)=5;
    elseif strcmp(act,'AS')==1
        TP(i,3)=6;
    elseif strcmp(act,'VS')==1
        TP(i,3)=7;
    elseif strcmp(act,'AR')==1
        TP(i,3)=8;
    else
        TP(i,3)=9;
    end
    
    % Filling IM
    if i==1, 
     IM(next)=1;
     next=next+1;
    elseif TP(i,1)==next-1 && next<=N_STATES_PACEMAKER,
        IM(next)=i;
        next=next+1;
    end
end
fclose(fid);

fprintf(1,'Loading pacemaker transition file: Done.\n');

% Taken directly from pacemaker_mdp.lab
c_state_PACEMAKER=2085135;


% Load the initial states of the PACEMAKER from file
%fid=fopen(strcat(path,'pacemaker_mdp.lab'),'r');

% Read the first line of the file
% state 0="init" 1="deadlock" 2="invariants"
% Read the first dummy line
%tline=fgetl(fid);
%state_string='';
%init_found=0;
%while ischar(tline)
%    state_string='';
%    tline = fgets(fid);
%    for i=1:size(tline,2),
%        if tline(i)~=':',
%            state_string=strcat(state_string,tline(i));
%        elseif tline(i+2)=='0',
%                init_found=1;
%        else
%            break;
%        end
%    end
%    if init_found==1,
%        break;
%    end
%    %disp(state_string);
%end



%% Experimental

% Normal behavior
[N_STATES_N AS_N VS_N PS_N QS_N z_N] = ecggen(110,115);

% Tachycardia behavior
[N_STATES_T AS_T VS_T PS_T QS_T z_T] = ecggen(180,180);

% Bradycardia behavior
[N_STATES_B AS_B VS_B PS_B QS_B z_B] = ecggen(40,40);

% Create the transition matrix for the normal behavior of the HEART
[TH_N, c_state_HEART]=transitions(0, N_STATES_N,AS_N,VS_N);

% Create the transition matrix for the tachycardia
[TH_T, c_state_HEART]=transitions(N_STATES_N,N_STATES_T,AS_T,VS_T);

% Create the transition matrix for the bradycardia
[TH_B, c_state_HEART]=transitions(N_STATES_N+N_STATES_T,N_STATES_B,AS_B,VS_B);

% 
% % Combine the all transition matrices
% [TH, N_STATES ] = combinetransitions( N_STATES_T, TH_T, N_STATES_N, TH_N, N_STATES_B, TH_B);
% 
% c_state_HEART=TH(1,1);
% 
% % Take the composition
% TH_COMPOSITION = composition(N_STATES, TH, TP, IM, c_state_PACEMAKER, c_state_HEART );






