import javax.swing.JOptionPane;
class SubDeadlock_Holy //to view have thai uses front CordiaUPC 14 
{
	public static String Str_of_DeadLock;
	public static int selectResult=0;
	private String rcvAR[] = new String[20],sndResultStr,beginStr,teabStr;
	private int aa=0,bb,cc,dd,sss,index [] = new int[30],c_index=0;
	private boolean check,T_F;
//****************************************************************************************
	public void rcvARStr(String rcv) // Recive Attribute (data), count data to aa
	{
		aa = aa+1;  //num of count rcvAR[aa]
		rcvAR[aa] = rcv;
		System.out.println("RCV = "+rcvAR[aa]+" aa = "+aa);
		JOptionPane.showMessageDialog(null,"aa = "+aa+"Line = "+rcvAR[aa]);
	}
//********************************************************************************************
private int loop,loop1,loop2; 
private boolean re_findCut, chAR[] = new boolean[20], ch_findLinkCut;
private String copy2_4;

private /*boolean*/ void findCut()
{
			//set check to TRUE
			for (loop=0;loop<=20 ;loop++ )
			{
				if (loop==20)
				{
					break;
				}
				chAR[loop] = true;
			}  //for loop
								cut_P_only_request();
	for (loop1=1;loop1<=aa ;loop1++ )
	{
					if (chAR[loop1] == true)
					{
					
		copy2_4 = rcvAR[loop1].substring(2,4);
		ch_findLinkCut = findLinkCut();
		if (ch_findLinkCut == false)
		{
					if ( (rcvAR[loop1].substring(2,3)).equalsIgnoreCase("r") )
					{  //ตัดเป็นกลุ่มได้ถ้า เป็นแบบ p..r..
//						JOptionPane.showMessageDialog(null,rcvAR[loop1].substring(2,3));
						cutGroup();
					}
					
			chAR[loop1] = false;
			JOptionPane.showMessageDialog(null,"In findCut Check for cut meet and cut : "+rcvAR[loop1]);
			loop1 = 1;
			loop2 = 1;
		}
					}  //if chAR[loop1]
	}  //for loop1
//	return re_findCut;
}  //method
//********************************************************************************************
private boolean re_findLinkCut,chForCut;
// ถ้า method นี้ return TRUE=ไม่มีเส้นออก set เส้นที่เช็คออก
private boolean findLinkCut()
{
	chForCut = false;
	for (loop2=1;loop2<=aa ;loop2++ )
	{
							if (chAR[loop2] == true)
							{
							
// T=มีชี้ ออก	F=ไม่มีชี้ ออก
		if (copy2_4.equalsIgnoreCase(rcvAR[loop2].substring(0,2)) )
		{  //มี ชี้ ออก
			chForCut = true;
			break;
		}		
		if (loop2==aa)
		{
			break;
		}
							}  //if chAR[loop2]
	}  //for loop2
	re_findLinkCut = chForCut;
	return re_findLinkCut;
}  //method
//********************************************************************************************
private int loopG;

private void cutGroup()
{
	for (loopG=1;loopG<=aa ;loopG++ )
	{
		if (rcvAR[loop1].substring(2,4).equalsIgnoreCase(rcvAR[loopG].substring(2,4)) )
		{
		chAR[loopG] = false;
		JOptionPane.showMessageDialog(null,"In cutGroup I Cat : "+rcvAR[loopG]);
		}
	}  //for loopG
}  //method
//********************************************************************************************
private int loopP, loopP1, loopP2;
private String copy0_2;
private boolean ch_p;

//หา P ที่เอาแต่ขออย่างเดียวแต่ไม่ได้ครองอะไรเลย จึงไม่เข้ากฎ Hold and Wait ให้ set = false
private void cut_P_only_request()
{
	for (loopP=1;loopP<=aa ;loopP++ )
	{
		if ( (rcvAR[loopP].substring(0,1)).equalsIgnoreCase("p") )
		{
//			JOptionPane.showMessageDialog(null,"p");
			copy0_2 = rcvAR[loopP].substring(0,2);
				ch_p = false;
				for (loopP1=1;loopP1<=aa ;loopP1++ )
				{  //T = P มีชี้เข้า F = Pไม่มีชี้เข้า
					if ( copy0_2.equalsIgnoreCase(rcvAR[loopP1].substring(2,4)) )
					{  //1
						ch_p = true;
						break;
					}  //1
				} //for loopP1
				//T = ไม่เจอตัวชี้เข้า ให้ลบเส้นของ p นี้ แบบลูปเพราะ p นี้อาจ request หลายตัว F = p ตัวนี้มีตัวชี้เข้า
				if (ch_p == false)
				{  //2
					for (loopP2=1;loopP2<=aa ;loopP2++ )
					{
						if (copy0_2.equalsIgnoreCase(rcvAR[loopP2].substring(0,2)) )
						{  //3
						JOptionPane.showMessageDialog(null,"Set line to F LINE : "+rcvAR[loopP2]);
						chAR[loopP2] = false;
						}  //3
					}  //for loopP3

				}  //2
		}  //if check ว่า ขึ้นต้นด้วย p ป่าว
	}  //for loopP
} //method
//********************************************************************************************
	private boolean findLink()// teabStr = 2ตัวหลังของตัวเริ่ม, uaes to find link String
	{ 
		T_F = false;
		//int sss;
		sss=1;
		for (sss=1;sss<=aa ;sss++ )
		{
					if (chAR[sss] == true)
					{
			
//			JOptionPane.showMessageDialog(null,"Check sss in findLink() sss = "+sss);
			System.out.println("Method findLink : teabStr = "+teabStr);
			if (teabStr.equalsIgnoreCase(rcvAR[sss].substring(0,2)))// T=find link, F=not find link
			{
			//	Str_of_DeadLock = Str_of_DeadLock + "  " + rcvAR[sss];
				T_F = true;
				break;
			}
					}  //if chAR[sss]
			if (sss==aa) //หาไม่เจอเลย ต้อง break ไม่งั้น ค่า sss ที่ไปใช้ที่บรรทัด 51 และหรือ 55 จะ error ได้
			{
				break;
			}
//					}  //if chAR[sss]
		}  // end for sss
		System.out.println("Method findLink : sss = "+sss);
		return T_F;
	}  // end method
//*****************************************************************************************
	public String findDeadlock()  //uses to find deadlock and return result to main
	{
		findCut();
/*		for (int aaa=1;aaa<=aa ;aaa++ )
		{
			if (chAR[aaa] == true)
			{
				JOptionPane.showMessageDialog(null,"For check AR have : "+rcvAR[aaa]);
			}
		}  //for aaa  */
	//	System.out.println(findLink());
	sndResultStr = "Not Have DeadLock";
	for (bb=1;bb<=aa ;bb++ )// select begin
	{	
						if (chAR[bb] == true)
						{
						
	//	Str_of_DeadLock = "";
	//	Str_of_DeadLock = rcvAR[bb];
        c_index = 1;
		index[1] = bb;
		System.out.println("bb = "+bb);
		beginStr = rcvAR[bb].substring(0,2);  // uses to check deadlock
		teabStr = rcvAR[bb].substring(2,4);  //uses to method find link
	for (cc=1;cc<=25000 ;cc++ )
	{
		check=findLink();
				System.out.println("cc = "+cc);		
		if (check)  // check find link
		{  //3
			//Str_of_DeadLock = Str_of_DeadLock + "  " + rcvAR[sss];
			c_index = c_index + 1;
			index[c_index] = sss;
			System.out.println("Find Link and sss = "+sss);
			teabStr = rcvAR[sss].substring(2,4);  // เปลี่ยนตัวเทียบ
			cc=1;
		}  //3
			System.out.println("Before check DeadLock sss = "+sss);
				if (beginStr.equalsIgnoreCase(rcvAR[sss].substring(2,4)) && (check==true))  // check dead lock, 2ตัวหน้าของตัวเริ่ม กับ 2ตัวหลังของตัว find link 
				{  //1
					selectResult = 1;
					System.out.println("Have DeadLock"+beginStr+" "+rcvAR[sss]);
					sndResultStr ="Have DeadLock";

					Str_of_DeadLock = "";
					for (int abc=1;abc<=c_index ;abc++ )
					{
						Str_of_DeadLock = Str_of_DeadLock + "  " + rcvAR[index[abc]];
					}
					bb=10000;
					break;
					
				//	bb=aa+aa;
				//	cc=25077;
				}  //1		
		if (false == check)
		{
			System.out.println("Don't find Link");
			break;
		}
		check = false;
	}  //end for cc
						}  //if chAR[bb] == true
	}  //end for bb
System.out.println(sndResultStr);
return sndResultStr;
}  // end method


//********************************************************************************************************
/* code ของเก่า
if ( (beginStr.substring(0,2)).equalsIgnoreCase(rcvAR[dd+1].substring(2,4)) )//check deadlock
			{//2
				System.out.println("Have DeadLock");
				dd=99;
				bb=99;
				sndResultStr ="Have DeadLock";
			}//2
			}//1
			else
			{
				System.out.println("Don't Find Link and dd = "+dd);
				break;
				//dd=aa;
			}	
        	}//for dd
	}//for bb
*/
	
/*
	public String findDeadlock() //อันเก่า
	{// (AR[j].substring(2,4)).equalsIgnoreCase(AR[k].substring(0,2))

	sndResultStr = "Not Have DeadLock";
	for (bb=1;bb<=aa ;bb++ )// select begin
	{
		beginStr = rcvAR[bb];

		//for (cc=1;cc<=aa ;cc++ )
		//{
			for (dd=1;dd<=aa-1 ;dd++ )
			{

			if ( (rcvAR[dd].substring(2,4)).equalsIgnoreCase(rcvAR[dd+1].substring(0,2)) )
			{//1
				System.out.println("Find Link");
if ( (beginStr.substring(0,2)).equalsIgnoreCase(rcvAR[dd+1].substring(2,4)) )//check deadlock
			{//2
				System.out.println("Have DeadLock");
				dd=99;
				bb=99;
				sndResultStr ="Have DeadLock";
			}//2
			}//1
			else
			{
				System.out.println("Don't Find Link and dd = "+dd);
				break;
				//dd=aa;
			}	
        	}//for dd
		//}//for cc
	}//for bb
System.out.println(sndResultStr);
		return sndResultStr;
	}// end method
	*/
}  //end class
