function main
global hEd1 hEd2 hEd3 hEd4 hEd5 hEd6
global hTex2 hTex3 hTex4  hTex6  hTex8  hTex10 hTex12 
global  hStop hStart hResult hPauseon
global flag
dx=1;
flag=0;

% axis('off')  
% subplot(1,1,1);
% axes('Position',[0 0.2 0.5 0.1],'XLim',[0 0.9],'YLim',[0 0.25],'Visible','off');

deviceNumber=str2num(get(hEd1,'String'));%кол-во устройств
bettaA=str2num(get(hEd2,'String'));%A
bettaS=str2num(get(hEd3,'String'));%S
firstA=str2num(get(hEd4,'String'));%генератор А
firstS=str2num(get(hEd5,'String'));%генератор S
firstPrior=str2num(get(hEd6,'String'));%генератор приоритетов

if gcbo==hStart
   set(hStop,'Enable','off');
   set(hResult,'Enable','off');
   set(hStart,'Enable','off');
   
   set(hEd1,'Enable','off');
   set(hEd2,'Enable','off');
   set(hEd3,'Enable','off');
   set(hEd4,'Enable','off');
   set(hEd5,'Enable','off');
   set(hEd6,'Enable','off');
   

   set(hPauseon,'Enable','on');
   
end;

timeA=Time(bettaA,1000,firstA);
indexA=2;
timeS=Time(bettaS,1200,firstS);
indexS=1;
Prior=prior(1000,firstPrior);
indexPrior=0;
for i=1:deviceNumber
    device(i,1)=0;
    device(i,2)=0;
    device(i,3)=0;
end;

sizeLine=0;
systemTime=0;


avgRequestInLineByTime=0;
avgRequestInLine=0;
avgRequestInSystemByTime=0;
avgRequestInSystem=0;
avgTimeInLineByTime=0;
avgTimeInLine=0;
avgTimeInSystemByTime=0;
avgTimeInSystem=0;
factorUseByTime=0;



iteration=0;    
while(indexA~=1000)
iteration=iteration+1;
%%%%%%%%%%%%%%%%%Определенение действия системы%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    minExe=0;
    isNotNull=0;
    freeNumber=0;
    for i=1:deviceNumber
        if device(i,1)~=0            
            minExe=device(i,1);
            isNotNull=1;
        else
            freeNumber=freeNumber+1;
            freeDevice(freeNumber)=i;            
        end;
    end;
    
    if isNotNull==0
        addTime=timeA(indexA);
        indexA=indexA+1;
        action=3;
    else
        for i=1:deviceNumber
            if device(i,1)~=0 && device(i,1)<=minExe
                minExe=device(i,1);
            end;
        end;
        if minExe>timeA(indexA) && freeNumber==0
            addTime=timeA(indexA);
            indexA=indexA+1;
            action=1;
        elseif minExe>timeA(indexA) && freeNumber~=0
            addTime=timeA(indexA);
            indexA=indexA+1;
            action=3;
        elseif minExe<timeA(indexA) && freeNumber==0
            action=2;
            addTime=minExe;
        elseif minExe<timeA(indexA) && freeNumber~=0
            action=4;
            addTime=minExe;
        elseif minExe==time(indexA) && freeNumber==0
            addTime=timeA(indexA);
            indexA=indexA+1;
            action=1;
        elseif minExe==time(indexA) && freeNumber~=0
            addTime=timeA(indexA);
            indexA=indexA+1;
            action=3;
        end;
    end;

    
    
    
    

    
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Действия системы%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    switch action
        case 1 
            systemTime=systemTime+addTime;
            sizeLine=sizeLine+1;
            procLine(sizeLine,1)=systemTime;
            procLine(sizeLine,2)=Prior(indexPrior);
            indexPrior=indexPrior+1;
            for i=1:deviceNumber
                device(i,1)=device(i,1)-addTime;
            end;
%-------------------------------------------------------------------------------%     
        case 2
            systemTime=systemTime+addTime;
            for i=1:deviceNumber
                device(i,1)=device(i,1)-addTime;
            end;
            timeA(indexA)=timeA(indexA)-addTime;
%-------------------------------------------------------------------------------%
        case 3
            systemTime=systemTime+addTime;
            for i=1:deviceNumber
                if device(i,1)~=0
                    device(i,1)=device(i,1)-addTime;
                end;
            end;
            if sizeLine==0
                device(freeDevice(1),1)=timeS(indexS);
                %device(freeDevice(1),2)=0;
                device(freeDevice(1),3)=systemTime;
                indexS=indexS+1;
                freeNumber=freeNumber-1;
            else
                while freeNumber~=1 && sizeLine~=0
                    minPriorId=1;
                    for i=2:sizeLine
                        if procLine(i,2)<procLine(minPriorId,2)
                            minPriorId=i;
                        end;
                    end;
                    device(freeDevice(freeNumber),1)=timeS(indexS);
                    %device(freeDevice(freeNumber),2)=systemTime-procLine(minPriorId,1);
                    device(freeDevice(freeNumber),3)=systemTime;
                    for i=minPriorId+1:sizeLine
                        procLine(i-1,1)=procLine(i,1);
                        procLine(i-1,2)=procLine(i,2);
                    end;
                    sizeLine=sizeLine-1;
                    indexS=indexS+1;
                    freeNumber=freeNumber-1;
                    
                end;
                if freeNumber>=1 && sizeLine==0   
                    device(freeDevice(freeNumber),1)=timeS(indexS);
                    %device(freeDevice(freeNumber),2)=0;
                    device(freeDevice(freeNumber),3)=systemTime;
                    indexS=indexS+1;
                    freeNumber=freeNumber-1;
                elseif freeNumber>=1 && sizeLine~=0
                    minPriorId=1;
                    for i=2:sizeLine
                        if procLine(i,2)<procLine(minPriorId,2)
                            minPriorId=i;
                        end;
                    end;
                    if Prior(indexPrior)<procLine(minPriorId,2)
                        device(freeDevice(freeNumber),1)=timeS(indexS);
                        %device(freeDevice(freeNumber),2)=0;
                        device(freeDevice(freeNumber),3)=systemTime;
                        indexS=indexS+1;
                        freeNumber=freeNumber-1;
                    else
                        device(freeDevice(freeNumber),1)=timeS(indexS);
                        %device(freeDevice(freeNumber),2)=systemTime-procLine(minPriorId,1);
                        device(freeDevice(freeNumber),3)=systemTime;
                        indexS=indexS+1;
                        freeNumber=freeNumber-1;
                        procLine(minPriorId,1)=systemTime;
                        procLine(minPriorId,2)=Prior(indexPrior);
                    end;
                end;
            end;
            indexPrior=indexPrior+1;
%-------------------------------------------------------------------------------%            
        case 4
            systemTime=systemTime+addTime;
            for i=1:deviceNumber
                if device(i,1)~=0
                    device(i,1)=device(i,1)-addTime;
                end;
            end;
            while freeNumber~=0 && sizeLine~=0
                minPriorId=1;
                for i=2:sizeLine
                    if procLine(i,2)<procLine(minPriorId,2)
                        minPriorId=i;
                    end;
                end;
                device(freeDevice(freeNumber),1)=timeS(indexS);
                %device(freeDevice(freeNumber),2)=systemTime-procLine(minPriorId,1);
                device(freeDevice(freeNumber),3)=systemTime;
                for i=minPriorId+1:sizeLine
                    procLine(i-1,1)=procLine(i,1);
                    procLine(i-1,2)=procLine(i,2);
                end;
                sizeLine=sizeLine-1;
                indexS=indexS+1;
                freeNumber=freeNumber-1;
            end;
            timeA(indexA)=timeA(indexA)-addTime;
    end;
    
%%%%%%%%%%%%%%%%%Вычисление общего времени работы устройств%%%%%%%%%%%%%%%%%%%%%%%%
    for i=1:deviceNumber
        if device(i,1)~=0
            device(i,2)=device(i,2)+addTime;
            %device(1,1)=device(i,1)-addTime;
        end;
    end;
%%%%%%%%%%%%%%%%%Вычисление средних параметров%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    


    %--------------------Среднее время задержки в очереди-------------------------%
        
    avgTimeInLine=mean(timeA)*avgRequestInLine/((systemTime));
    avgTimeInLineByTime(1,iteration)=avgTimeInLine;
    avgTimeInLineByTime(2,iteration)=systemTime;
        
    %--------------------Среднее время задержки в системе-------------------------%

    for i=1:deviceNumber
        if device(i,1)~=0
            avgTimeInSystem=avgTimeInSystem+((systemTime-device(i,3))/deviceNumber);
        end;
    end;
    %!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!%
    curAvgTimeInSystem=(avgTimeInSystem/indexA)+mean(timeA)*avgRequestInLine/((systemTime));
    avgTimeInSystemByTime(1,iteration)=curAvgTimeInSystem;
    avgTimeInSystemByTime(2,iteration)=systemTime;
    %!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!%
   %--------------------Среднее количество запросов в очереди--------------------%
    avgRequestInLine=avgRequestInLine+sizeLine*addTime;
    avgRequestInLineByTime(1,iteration)=avgRequestInLine/(systemTime);
    avgRequestInLineByTime(2,iteration)=systemTime;   
    %--------------------Среднее количество запросов в системе--------------------%
    avgRequestInSystem=(curAvgTimeInSystem)/mean(timeA);
    avgRequestInSystemByTime(1,iteration)=avgRequestInSystem;
    avgRequestInSystemByTime(2,iteration)=systemTime;
    
    %--------------------Коэффициент использования системы------------------------%
    %!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!%
    commonUseTime=0;
    for i=1:deviceNumber
        commonUseTime=commonUseTime+device(i,2);
    end;
    factorUse=commonUseTime/(deviceNumber*(systemTime));
    factorUseByTime(1,iteration)=factorUse;
    factorUseByTime(2,iteration)=systemTime;
    %!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!%
    
%%%%%%%%%%%%%%%%%Вывод вычисленных средних значений%%%%%%%%%%%%%%%%%%%%%%%%
    set(hTex4,'String',factorUse);
    set(hTex6,'String',avgTimeInLine);
    set(hTex8,'String',curAvgTimeInSystem);
    set(hTex10,'String',avgRequestInLine/systemTime);
    set(hTex12,'String',avgRequestInSystem);
       axis('off')

%%%%%%%%%%%%%%%%%Приостановка системы%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    pause(0.00001)

    while flag
        pause(1);
        if flag==1
            set(hStop,'Enable','on');
        end;
        if flag==0
            set(hStop,'Enable','off');
        end;
    end;
 

    

end;%while
axis('off')
figure('Name','Коэффициент использования системы')
plot(factorUseByTime(2,1:iteration),factorUseByTime(1,1:iteration));axis([0 systemTime+20 0 1]);

%figure('Name','Среднее время задержки в системе')
%plot(avgTimeInSystemByTime(2,1:iteration),avgTimeInSystemByTime(1,1:iteration));

%figure('Name','Среднее время задержки в очереди')
%plot(avgTimeInLineByTime(2,1:iteration),avgTimeInLineByTime(1,1:iteration));

figure('Name','Среднее количество запросов в системе')
plot(avgRequestInSystemByTime(2,1:iteration),avgRequestInSystemByTime(1,1:iteration));

figure('Name','Среднее количество запросов в очереди')
plot(avgRequestInLineByTime(2,1:iteration),avgRequestInLineByTime(1,1:iteration));


set(hPauseon,'Enable','off');
set(hStop,'Enable','on');
set(hResult,'Enable','on');
set(hStart,'Enable','on');

   set(hEd1,'Enable','on');
   set(hEd2,'Enable','on');
   set(hEd3,'Enable','on');
   set(hEd4,'Enable','on');
   set(hEd5,'Enable','on');
   set(hEd6,'Enable','on');