Chris Pollett > Students >
Harini Rao

    ( Print View )

    [Bio]

    [CS297Proposal]

    [Del1]

    [Del2]

    [CS297Report-PDF]

    [CS298Proposal]

    [CS298FinalReport-PDF]

    [CS298Presentation-PDF]

                             

























<>

Harini's Stable Model Computing Program

<>
/**  
*  StableModel3.java - a simple class for generating Stable Models.
*  @author  Harini Rao
*  @version 1.0, 04/12/2002   
*/  

import java.io.*;

import java.util.*;


/**
*  This class contains an application that can be used to generate
*  Stable Models for a given program. It is run from the command line 
as:
*
*   java StableModel.java text1.txt
*
*   Here text1.txt is the name of a file containing the rules of the 
program.
*/

public class StableModel3 

{

/** 
* Calculates StableModels for the rules of a given program which are 
stores in a file.
*
* @param args A variable array of type String.
* @exception IOException.
*
*/
public static void main (String args[]) throws IOException 

{

	FileInputStream fin1;

	try	
				//Check for the existence of the file.
	{
	
		fin1 = new FileInputStream (args[0]);
	}

	catch (FileNotFoundException e)

	{

		System.out.println ("File Not Found");

		return;
	}


	String filename = args[0];

	char a[] = countVariables(filename);

	fin1.close();
}

/**  
*  Calculates the number of the variables.
*
* @param filename A variable of type String.
* @return A char array data type.  
* @exception e If the file is not present.
*
*/  

public static char[] countVariables (String filename)

{
	
	int l = 0;

	int i;

	char a[] = new char[35] ;

	char c;

	char g;

 	int j = 0;

        int k = 0;
	
	FileInputStream fin;

	try
	{
		fin = new FileInputStream (filename) ;

		do

		{
			i = fin.read () ;

			j = fin.read () ;

			if (i != -1)

			{
	    			c = (char) i;

	    			j = i + 1;

	    			g = (char) j;

	    			if (c != g && c != '~' && c != ':' && c != ',')
	    			{
 	       				a[k] = c;

                        		if(a[k] != '\n' && a[k] !=  '\r')
 								
						k++;
	    			 }
			}

		}

		while (i != -1) ;

		int ft = 0;

		for (k = 0; k < a.length; k++)
		
		{
			if (a[k] != '\n')

		        		if (a[k] != '\r')

                                               		if (a[k] != 0)
 
								l++ ;
		}

		j = 0;

		Vector v = new Vector(20);

		fin = new FileInputStream (filename);

		do

		{
			i = fin.read ();

			if (i != -1)

			{
				c = (char) i;

				if ((c >= 48 && c <= 57) || (c >= 97 && c <= 122))

				{
					if(!v.contains(String.valueOf(c))) 

						v.addElement(String.valueOf(c));
					
				}
			}
		}

		while (i!=-1) ;
		
		// Make assumptions for calculating stable model.

		for (int x = 0; x < l * l; x++)

		{
			boolean temp[] = makeAssumption(l,x);
				
			//Store the rules of the program into an array list.

			ArrayList al = storeRules(l,filename);

			//Calculate stable model from the rules.

			calculateStableModel(v, al, temp, l);

		}

		return a;

	}

	catch (Exception e)

	{

	System.out.println (e);

	return null;

	}

}

/**  
*  Makes assumptions to calculate stable models.
*
* @param l A variable of type Integer.  
* @param i A variable of type Integer.  
* @return A Boolean array data type.  
*
*/  

public static boolean[] makeAssumption(int l, int i)

{

	int y = 0;

	boolean temp[] = new boolean[l] ;

	for(int k = 0; k < l; k++)
	
	{
		int g = i%2;

		if(g==1) 

			temp[k] = true;

		else 

			temp[k] = false;

		i = i/2;
	}

	System.out.println();

	System.out.println("Assumption Made:");

	for(int rf = 0; rf < l; rf++)

	System.out.println(temp[rf]);

	return temp;

}

/**  
*  Stores the rules in the file to an ArrayList.
*
* @param l A variable of type Integer.  
* @param filename A variable of type String.
* @return An ArrayList data type.  
* @exception e If the file is not present.
*
*/  

public static ArrayList storeRules(int l, String filename)

{

	int i;

	char c;

	String s = new String ();

	ArrayList al = new ArrayList ();

	FileInputStream fin;

	try

	{
		fin = new FileInputStream (filename) ;

		do

		{
			i = fin.read () ;

			if (i != -1)

			{
				c = (char) i;

				if (c != '\n')

				{
					if (c == ':') 

							s = s+"=" ;

					else 
						if (c ==  '~') 

							s = s+"!" ;

		       	       	       	      	else 
							
							if (c == ',') 
									
								s = s+"&" ;

		              	                   	else 
	
								s = s+c ;

				}

				else

				{
					al.add (s) ;

					s = new String() ;

				}
				
			}

			else 
	
				al.add(s);

		}

		while (i!=-1) ;
		
		return al;

	}

	catch (Exception e)

	{
		System.out.println (e) ;

		return null;

	}
	

}

/**  
*  Calculates StableModels for the given program by applying the rules 
to the assumptions.
* 
* @param al A variable of type ArrayList.  
* @param temp A variable array of type Boolean.
* @param l A variable of type Integer.
*
*/  

public static void calculateStableModel(Vector v, ArrayList al, boolean temp[], int l)

{

	boolean temp1[] = new boolean[l];

	boolean temp2[] = new boolean[l];

	boolean temp3[] = new boolean[l];

	for(int gr = 0;gr < l; gr++)

	temp1[gr] = temp[gr];

	int sl=0;

	ArrayList al1 = new ArrayList ();
	
	do

	{
		if(sl >= al.size())
		
			break;

		if(sl > 0)

		{

			for(int pe = 0; pe < l; pe++)

			temp1[pe] = temp2[pe];

		}

		int yu = 0;
 
		int gk = 0;
	
		String s1 = String.valueOf(al.get(sl));

		int p = s1.length();
								
		char b[] = new char[p] ;

		//Retrieve the contents of the ArrayList
		
		b = s1.toCharArray() ;
				
		boolean t = true;

		boolean rv;

		int gy = 0;

                int ge = 0;
			
		// Apply the truth values in the assumption to the rules and calculate the reduced program.

		for(int k = 0; k < l; k++)

		{	
			if(k>=p) 

				break;
											
			if (b[k] == '=')

			{      
      
				if (b[k+1] != '!')

				{
					if(v.contains(String.valueOf(b[k+1]))) 

					gy = v.indexOf(String.valueOf(b[k+1]));

					t = temp1[gy] ;

					String s2 = b[k-1]+":"+b[k+1];

					if(t) 
						al1.add(s2);

					if(v.contains(String.valueOf(b[k-1])))

						ge = v.indexOf(String.valueOf(b[k-1]));

					temp2[ge] = t;
			
				}

				else

				{	
					if(v.contains(String.valueOf(b[k+2])))

						gy = v.indexOf(String.valueOf(b[k+2]));

					t = temp1[gy];

					String s2 = b[k-1]+":"+".";

					if(!t) 
						al1.add(s2);

					if(v.contains(String.valueOf(b[k-1])))

						ge = v.indexOf(String.valueOf(b[k-1]));

					temp2[ge] = !t;
										
				}

			}
					
			if (b[k] == ',')

			{      
      
				if (b[k+1] != '!')

				{
					if(v.contains(String.valueOf(b[k+1]))) 

					gy = v.indexOf(String.valueOf(b[k+1]));

					t = temp1[gy] ;

					String s2 = b[k-1]+"&"+b[k+1];

					if(t) 
						al1.add(s2);

					if(v.contains(String.valueOf(b[k-1])))

						ge = v.indexOf(String.valueOf(b[k-1]));

					temp2[ge] = t;
			
				}

				else

				{	
					if(v.contains(String.valueOf(b[k+2])))

						gy = v.indexOf(String.valueOf(b[k+2]));

					t = temp1[gy];

					String s2 = b[k-1]+".";

					if(!t) 
						al1.add(s2);

					if(v.contains(String.valueOf(b[k-1])))

						ge = v.indexOf(String.valueOf(b[k-1]));

					temp2[ge] = !t;
										
				}

			}

		}

		for(int rt=0;rt<l;rt++)

		{
			if(rt!=ge)

			 {
							
				temp2[rt]=temp1[rt];

			 }
		}
			
		
		sl++;

	}

	while(sl < l);
	
	
	if(al1.size() == 0)

	{
 		
		System.out.println("This is not a Stable Model");

	
	}

	else
	
	{

		for(int g = 0; g < al1.size();g++)
	
		System.out.println(al1.get(g));
		
		boolean change = false;	

		sl = 0;

		String str = new String();

		String st = new String();
	
		Vector v1 = new Vector();

		for(int gy = 0; gy < l; gy++)

		{
		
			v1.add(String.valueOf(temp2[gy]));

		}
	
		/*System.out.println("Temp[2] Contents:");

		for(int gy = 0; gy < l; gy++)

		{
		
			System.out.println(v1.get(gy));

		}*/	
	
		do

		{

			change = false;

			if(sl>0)

			{

				for(int pe = 0; pe < l; pe++)

				temp2[pe] = temp3[pe];

			}
	
			int yu = 0;
 
			int gk = 0;
		
			String s1 = String.valueOf(al1.get(sl)) ;

			int p = s1.length();
		
			char b[] = new char[p] ;
			
			//Retrieve the contents of the ArrayList
		
			b = s1.toCharArray() ;
				
			boolean t = true;

			boolean rv;

			int gy = 0;

                	int ge = 0;
			
			// Calculate the derived program from the reduced program.
			
		
			for(int sk = 0; sk < p; sk++)

			{
					
				if (change) 
					
						break;
						
				if (b[sk] == ':')

				{      

					if (b[sk+1] == '.')

					{
					
						if(al1.size() > 1)

						{
							st = String.valueOf(b[sk-1]);

							change = true;
						
						}
				
						else

						{
							change = true;

							if(v.contains(String.valueOf(b[sk-1])))

									ge = v.indexOf(String.valueOf(b[sk-1]));

							temp3[ge] = t;
						}

					}

					str = st; 
 
					if(str.equals(String.valueOf(b[sk+1])))

					{
	
						if(v1.contains(String.valueOf(b[sk-1])))

								ge = v1.indexOf(String.valueOf(b[sk-1]));

						temp3[ge] = t;

					}		
								
				}
			
			}

			for(int rt=0;rt<l;rt++)

			{
			
				if(rt!=ge)

			 	{
				
					temp3[rt]=temp2[rt];

			 	}

			}

			/*System.out.println("temp[3] Contents:");

			for(int rt=0;rt<l;rt++)

			{
				System.out.println(temp3[rt]);			
			
			}	*/
		
			sl++;
		}

		while(sl<al1.size());
		
		
		//Check if the assumption made initially is same as the result

		int gu = 0;

		for(int ty = 0,tx = 0; ty < l && tx < l; ty++,tx++)

		{	
			if (temp[ty] == temp3[tx]) 

						gu++;

		}
	
		//Display the Stable models.

		if (gu >=l) 

		{

			System.out.println("This is a StableModel");

			System.out.println("                   ");

			System.out.println("Stable Models:");

			for(int qs = 0; qs < l; qs++)

			{

				System.out.println(v.elementAt(qs)+" is "+temp[qs]);

			}

		}

		else

			System.out.println("This is not a StableModel");
	
	}
		
}
}