function [TH_COMPOSITION TH_STATES] = composition(N_STATES, TH, TP, IM, c_state_PACEMAKER, c_state_HEART )
% Take the composition between the PACEMAKER and the HEART

% Taken directly from pacemaker_mdp.lab
c_state_PACEMAKER=2085135;

% Set the initial state of the HEART to 0
c_state_HEART=0;

% Save to file
%fid=fopen(strcat(path,'temp.tra'),'w+');

next_state_HEART=0;
next_state_PACEMAKER=0;

% Counter for the transition matrix of the HEART
cnt_tp=1;

act_HEART=0;
imm_flag=0;

% Screw up the HEART
%TH(:,3)=zeros(N_STATES,1);


% Form the composition transition matrix
TH_COMPOSITION=zeros(N_STATES,3);

next_state_PACEMAKER_AP=0;
next_state_PACEMAKER_VP=0;
imm_flag_AP=0;
imm_flag_VP=0;

% Counter for the composition
cnt_tp_comp=1;

% First and second state of the composition
c_state_HEART_comp=0;
next_state_HEART_comp=c_state_HEART_comp+1;

% Safety counter
cnt_safety=1;

% Save the history of the composition
cnt_states=1;
TH_STATES=zeros(N_STATES*2,2);


% Cycle over the transitions of the HEART
while cnt_tp_comp<=N_STATES,
    
    % Retrieve the action of the HEART and the next state
    next_state_HEART = TH(cnt_tp,2);
    act_HEART=TH(cnt_tp,3);
    
    
    % Check for Aget
    if act_HEART==1,
        [next_state_PACEMAKER imm_flag]=getpacemakerstate(TP,IM,c_state_PACEMAKER,act_HEART);   
    
        % Log the current state of the HEART and the PACEMAKER
        %fprintf(fid,'%d %d %d\n',c_state_HEART, c_state_PACEMAKER, act_HEART);

        if next_state_PACEMAKER==-1,
            fprintf(1,'Found deadlock [Aget] at %d %d\n',c_state_HEART,c_state_PACEMAKER);
            break;
        elseif imm_flag==2 && next_state_PACEMAKER~=-1,
        
            % Fill in the transition matrix
            TH_COMPOSITION(cnt_tp_comp,1)=c_state_HEART_comp;
            TH_COMPOSITION(cnt_tp_comp,2)=next_state_HEART_comp;
            TH_COMPOSITION(cnt_tp_comp,3)=act_HEART;

        
            % Get the next state of the composition
            c_state_HEART_comp=next_state_HEART_comp;
            next_state_HEART_comp=next_state_HEART_comp+1;
        
            % Proceed to the next entry of the transition matrix for the
            % compisition
            cnt_tp_comp=cnt_tp_comp+1;
        
            c_state_PACEMAKER=next_state_PACEMAKER;
            
            % Assign the current state of the heart
            cnt_tp=cnt_tp+1;
            if cnt_tp>N_STATES,
                cnt_tp=1;
            end

            c_state_HEART=TH(cnt_tp,1);
        
            continue;
        end
    end
    
    % Check for Vget
    if act_HEART==2,
        [next_state_PACEMAKER imm_flag]=getpacemakerstate(TP,IM,c_state_PACEMAKER,act_HEART);
    
        % Log the current state of the HEART and the PACEMAKER
        %fprintf(fid,'%d %d %d\n',c_state_HEART, c_state_PACEMAKER, act_HEART);

        if next_state_PACEMAKER==-1,
            fprintf(1,'Found deadlock[Vget] at %d %d\n',c_state_HEART,c_state_PACEMAKER);
            break;
        elseif imm_flag==2 && next_state_PACEMAKER~=-1,
        
            % Fill in the transition matrix
            TH_COMPOSITION(cnt_tp_comp,1)=c_state_HEART_comp;
            TH_COMPOSITION(cnt_tp_comp,2)=next_state_HEART_comp;
            TH_COMPOSITION(cnt_tp_comp,3)=act_HEART;

        
            % Get the next state of the composition
            c_state_HEART_comp=next_state_HEART_comp;
            next_state_HEART_comp=next_state_HEART_comp+1;
        
            % Proceed to the next entry of the transition matrix for the
            % compisition
            cnt_tp_comp=cnt_tp_comp+1;
        
            c_state_PACEMAKER=next_state_PACEMAKER;
            
            % Assign the current state of the heart
            cnt_tp=cnt_tp+1;
            if cnt_tp>N_STATES,
                cnt_tp=1;
            end

            c_state_HEART=TH(cnt_tp,1);
            
            continue;
        end
    end
    
    % Check for the AP action act_HEART=3
    [next_state_PACEMAKER_AP imm_flag_AP]=getpacemakerstate(TP,IM,c_state_PACEMAKER,3);
    
    if imm_flag_AP==0 && next_state_PACEMAKER_AP~=-1,
        % Fill in the transition matrix
        TH_COMPOSITION(cnt_tp_comp,1)=c_state_HEART_comp;
        TH_COMPOSITION(cnt_tp_comp,2)=next_state_HEART_comp;
        TH_COMPOSITION(cnt_tp_comp,3)=3;
        
        % Get the next state of the heart
        c_state_HEART_comp=next_state_HEART_comp;
        next_state_HEART_comp=next_state_HEART_comp+1;
        
        act_HEART=3;
        
        %fprintf(1,'PACEMAKER atrium: %d %d %d\n',c_state_HEART,c_state_PACEMAKER, c_state_HEART_comp);
        
        % Log the current state of the HEART and the PACEMAKER
        %fprintf(fid,'%d %d AP\n',c_state_HEART, c_state_PACEMAKER);
        
        % Proceed to the next entry of the transition matrix for the
        % compisition
        cnt_tp_comp=cnt_tp_comp+1;

        c_state_PACEMAKER=next_state_PACEMAKER_AP;
        
        % Move the next state of the HEART
        cnt_safety=1;
        
        % Check for Aget
        while TH(cnt_tp,3)~=1,
            cnt_tp=cnt_tp+1;
            if cnt_tp>N_STATES,
                cnt_tp=1;
            end
            cnt_safety=cnt_safety+1;
            % If no Aget found then error
            if cnt_safety>N_STATES,
                error('Error: [composition.m] safety counter on from Aget');
            end
        end
        
        % Assign the current state of the heart
        cnt_tp=cnt_tp+1;
        if cnt_tp>N_STATES,
            cnt_tp=1;
        end

        c_state_HEART=TH(cnt_tp,1);
        
        continue;
        
    end


    % Check for the VP action act_HEART=4
    [next_state_PACEMAKER_VP imm_flag_VP]=getpacemakerstate(TP,IM,c_state_PACEMAKER,4);
    
    if c_state_HEART==430,
        cnt_safety=1;
    end
    
    if imm_flag_VP==0 && next_state_PACEMAKER_VP~=-1,
        
        % Fill in the transition matrix
        TH_COMPOSITION(cnt_tp_comp,1)=c_state_HEART_comp;
        TH_COMPOSITION(cnt_tp_comp,2)=next_state_HEART_comp;
        TH_COMPOSITION(cnt_tp_comp,3)=4;
        
        % Get the next state of the heart
        c_state_HEART_comp=next_state_HEART_comp;
        next_state_HEART_comp=next_state_HEART_comp+1;
        
        act_HEART=4;
        
        %fprintf(1,'PACEMAKER ventricle: %d %d %d\n',c_state_HEART,c_state_PACEMAKER, c_state_HEART_comp);
        
        % Log the current state of the HEART and the PACEMAKER
        %fprintf(fid,'%d %d VP\n',c_state_HEART, c_state_PACEMAKER);

  
        % Proceed to the next entry of the transition matrix for the
        % compisition
        cnt_tp_comp=cnt_tp_comp+1;
        
        
        c_state_PACEMAKER=next_state_PACEMAKER_VP;
        
        % Move the next state of the HEART
        cnt_safety=1;
        
        % Check for Vget
        while TH(cnt_tp,3)~=2,
            cnt_tp=cnt_tp+1;
            if cnt_tp>N_STATES,
                cnt_tp=1;
            end
            cnt_safety=cnt_safety+1;
            % If no Vget found then error
            if cnt_safety>N_STATES,
                error('Error: [composition.m] safety counter on from Vget');
            end
        end
        
        % Assign the current state of the heart
        cnt_tp=cnt_tp+1;
        if cnt_tp>N_STATES,
            cnt_tp=1;
        end

        c_state_HEART=TH(cnt_tp,1);

        continue;
        
    end
    
    % Check for time
    if act_HEART==0, 
        [next_state_PACEMAKER imm_flag]=getpacemakerstate(TP,IM,c_state_PACEMAKER,act_HEART);
    
        % Log the current state of the HEART and the PACEMAKER
        %fprintf(fid,'%d %d %d\n',c_state_HEART, c_state_PACEMAKER, act_HEART);

        if next_state_PACEMAKER==-1,
            fprintf(1,'Found deadlock [time] at %d %d\n',c_state_HEART,c_state_PACEMAKER);
            break;
        elseif imm_flag==2 && next_state_PACEMAKER~=-1,
        
            % Fill in the transition matrix
            TH_COMPOSITION(cnt_tp_comp,1)=c_state_HEART_comp;
            TH_COMPOSITION(cnt_tp_comp,2)=next_state_HEART_comp;
            TH_COMPOSITION(cnt_tp_comp,3)=act_HEART;

        
            % Get the next state of the composition
            c_state_HEART_comp=next_state_HEART_comp;
            next_state_HEART_comp=next_state_HEART_comp+1;
        
            % Proceed to the next entry of the transition matrix for the
            % compisition
            cnt_tp_comp=cnt_tp_comp+1;
        
        
            % Assign the current state of the heart
            cnt_tp=cnt_tp+1;
            if cnt_tp>N_STATES,
                cnt_tp=1;
            end

            c_state_HEART=TH(cnt_tp,1);

        end
    end
    
    
    % Save the history of the composition
    TH_STATES(cnt_states,1)=c_state_HEART;
    TH_STATES(cnt_states,2)=c_state_PACEMAKER;
    cnt_states=cnt_states+1;

    
    % Assign the next state of the PACEMAKER to the current state of the
    % PACEMAKER
    c_state_PACEMAKER=next_state_PACEMAKER;
end


%fclose(fid);
end