gt: 							String s1=(String) o1;
context: package jach.msthesis.scheduler.regist;
  
  import java.io.BufferedReader;
  import java.io.FileReader;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.Hashtable;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  import java.util.Random;
  import java.util.Vector;
  
  import jach.msthesis.courselector.DefaultSubject;
  import jach.msthesis.courselector.DudaiOffering;
  import jach.msthesis.courselector.IOffering;
  import jach.msthesis.courselector.ISection;
  import jach.msthesis.courselector.ISubject;
  import jach.msthesis.courselector.Section;
  import jach.msthesis.scheduler.SkedNode;
  
  
  /**
   * This class implements the original algorithm in assigning sections
   * to students in Regist
   * @author jach
   *
   */
  public class RegistScheduler {
  	boolean logging=true;
  	int loggingLevel = 2;
  	
  	Map sectionOptionsMap = new Hashtable();
  	
  	//Map: <subject, list of students interested>
  	Map subjectList=new Hashtable();
  	
  	//Map: <subject:section, Classlist>
  	Map classlists=new Hashtable();
  	
  	//Map: <stdnum, Schedule>
  	Map schedules = new Hashtable();
  	
  	//Map: <stdnum, WriteIn>
  	Map writeins = new Hashtable();
  	
  	//Offering data	
  	IOffering offering;
  
  	//filter
  	private String stdNum="2001-56024";
  	
  	public RegistScheduler(){}
  	
  	/**
  	 * 
  	 * @param offering
  	 */
  	public RegistScheduler(IOffering offering){
  		this.offering=offering;
  		Iterator ite=this.offering.getAllSubjects().keySet().iterator();
  		//Initialize sibjectList 
  		while(ite.hasNext()){
  			String subject=(String)ite.next();
  			subjectList.put(subject, new Vector());
  		}		
  	}
  	
  	
  	public void loadWriteIn(String path){
  		BufferedReader in=null;
  		String line;
  		String[] tokens=null;
  						
  		try{
  			in=new BufferedReader(new FileReader(path));
  			line=in.readLine();
  			while(line != null){				
  				if (!line.startsWith("#")){
  					//System.out.println(line);
  					tokens=line.split(",");
  					String stdNum=tokens[0];
  					WriteIn wri=(WriteIn)writeins.get(stdNum);
  					if (wri == null){
  						wri=new WriteIn(tokens[0],Integer.parseInt(tokens[9]),
  								Integer.parseInt(tokens[8]));
  						writeins.put(stdNum,wri);
  					}
  					//Create default subject with term and year to 0 for now.					
  					DefaultSubject ds=new DefaultSubject(tokens[3],0,0,Integer.parseInt(tokens[5]));					
  					wri.addSubject(ds);
  					schedules.put(tokens[0], new Schedule(Integer.parseInt(tokens[8])));
  					//student added to list who wants to take the subject
  					Vector v=(Vector)subjectList.get(tokens[3]);
  					if (v != null){
  						v.add(wri);
  						subjectList.put(tokens[3],v);
  					}
  				}
  				line=in.readLine();
  			}
  			
  		}catch(Exception e){
  			e.printStackTrace();
  		}		
  	}
  	
  	/**
  	 * Performs the assignment of students to section
  	 *
  	 */
  	public void assign(){
  		
  		List studs=new Vector(writeins.keySet());
  		int randStud = new Random().nextInt(studs.size());
  		stdNum=(String)studs.get(randStud);
  		
  		//stdNum="2001-56024";
  		
  		log(2,"Student: "+stdNum);		
  				
  		log(1,"Sorting subjects based on rank.");
  		List sorted=new Vector(offering.getAllSubjects().keySet());
  		//Collections.sort(sorted);
  		Collections.sort(sorted,new Comparator(){
  			public int compare(Object o1, Object o2){
  				String s1=(String) o1;
  				String s2=(String) o2;
  				float t1 = offering.getSubjectRank(s1);
  				float t2 = offering.getSubjectRank(s2);
  				return ((int)(t1-t2));
  			}
  		});
  		
  		//TODO: Select random subject of the same rank
  		
  		//Iterator ite=this.offering.getAllSubjects().keySet().iterator();
  		Iterator ite=sorted.iterator();
  				
  		log(1,"Processing each subject.");
  		//For each subject
  		while(ite.hasNext()){
  			String subject=(String)ite.next();
  			//if (subject.startsWith("CMSC"))
  			//	System.out.println("Processing subject: "+subject);
  			log(3,"Obtaining students interested in subject and sorting based on priority.");
  			Vector students=(Vector)subjectList.get(subject);			
  			
  			//Sort students according to priority
  			Collections.sort(students,new Comparator(){
  				public int compare(Object o1, Object o2){
  					WriteIn s1=(WriteIn) o1;
  					WriteIn s2=(WriteIn) o2;
  					return (s1.getPriority()-s2.getPriority());
  				}
  			});
  			
  			//TODO: Select a random student for those with the same priority
  			
  			Iterator ite2=students.iterator();
  			//For each student
  			while (ite2.hasNext()){
  				
  				//Get the writein information for the student
  				
  				WriteIn wi=(WriteIn)ite2.next();				
  				
  				if (wi.getStudentNumber().equals(stdNum)){
  					log(2,"Processing "+ subject + " Rank: "+offering.getSubjectRank(subject));
  				}
  				
  				
  				//Get all lec sections for the subject
  				Iterator ite3=offering.getLecSections(subject).iterator();
  				//For each lec section
  				while (ite3.hasNext()){
  					//Get the schedule of the student
  					Schedule sked=(Schedule)schedules.get(wi.getStudentNumber());
  					
  					//units must not exceed allowed
  					ISubject temp=wi.getSubjectInfo(subject);					
  					int unt=temp.getUnitCredit()+sked.getUnitsObtained(wi);					
  					if (unt > wi.getUnitsAllowed()){
  						if (wi.getStudentNumber().equals(stdNum)){
  							log(2,"\t\t\tMax units allowed exceeded!");
  						}
  						break;
  					}
  					
  					Section lecture=(Section)ite3.next();
  					if (wi.getStudentNumber().equals(stdNum)){
  						log(2,"\t\tProcessing lec section: "+lecture.getSectionName());
  					}
  					
  										
  					//Create a classlist of the section if it does not exist yet
  					String key=lecture.getSubject().getName()+":"+lecture.getSectionName();
  					Classlist lecCl=(Classlist)classlists.get(key);					
  					if(lecCl==null){
  						classlists.put(key,new Classlist(lecture));
  					}
  					lecCl=(Classlist)classlists.get(key);
  					
  					//proceed to next section if this section full
  					if (lecCl.full()){
  						if (wi.getStudentNumber().equals(stdNum)){
  							log(2,"\t\t\tSection is full");
  						}
  						continue;
  					}					
  					
  					
  					//Create a sked node for it
  					SkedNode lecnode=new SkedNode(lecture);
  					
  					//temporarily add the lecnode
  					sked.addSection(lecnode);
  					
  					if (!sked.passed(lecnode)){
  						//proceed to next lecture section
  						//since this lecture is in conflict already,
  						//there is no need to process its labs
  						if (wi.getStudentNumber().equals(stdNum)){
  							log(2,"\t\t\tSchedule conflict with this lecture!");
  						}
  						sked.removeLastSectionAdded();
  						continue;
  					}
  					
  					//ok the lec node was added, however, it is not
  					//a guaranteed slot unless a lab is found
  				
  					
  					//now we get the lab sections
  					List labs=offering.getLabSections(lecture.getSubject().getName(),lecture.getSectionName());					
  					
  					boolean labFound=false;
  					int labCount=labs.size();
  					Classlist labCl=null;
  					
  					//this lecture got labs!
  					if (labCount > 0){
  						Iterator ite4=labs.iterator();
  						
  						//try to find a lab
  						while (ite4.hasNext()){
  							Section lab=(Section)ite4.next();
  							
  							if (wi.getStudentNumber().equals(stdNum)){
  								log(2,"\t\t\tProcessing lab section: "+lab.getSectionName());
  							}
  							
  //							Create a classlist of the section if it does not exist yet
  							String key2=lab.getSubject().getName()+":"+lab.getSectionName();
  							labCl =(Classlist)classlists.get(key2);					
  							if(labCl==null){
  								classlists.put(key2,new Classlist(lab));								
  							}
  							labCl=(Classlist)classlists.get(key2);
  							
  							//Create a node for the lab
  							SkedNode labNode=new SkedNode(lab);
  							
  							//Add it to the sked
  							sked.addSection(labNode);
  							
  							//Did it satisfy the constraint?
  							if(!sked.passed(labNode)){
  								if (wi.getStudentNumber().equals(stdNum)){
  									log(2,"\t\t\t\tSchedule conflict with this lab!");
  								}
  								//remove this lab section from sked
  								sked.removeLastSectionAdded();								
  							}else{				
  								
  								//Lets remove the lecture from the sked!!!
  								//sked.removeLastSectionAdded();	
  								//sked.removeLastSectionAdded();
  								//sked.addSection(labNode);
  								if (labCl.addStudent(wi.getStudentNumber())) {
  									lecCl.addStudent(wi.getStudentNumber());
  									if (wi.getStudentNumber().equals(stdNum)){
  										log(2,"\t\t\t\tEnlisted!");
  									}
  //									We found a lab! great
  									labFound=true;
  									break;
  								}else{
  									if (wi.getStudentNumber().equals(stdNum)){
  										log(2,"\t\t\t\tSection is full!");
  									}
  									sked.removeLastSectionAdded();	
  									//sked.removeLastSectionAdded();
  								}	
  							}
  						}		
  						
  						//got one
  						if (labFound==true){
  							break;
  						}else{
  							sked.removeLastSectionAdded();
  						}
  					}else{
  						if (lecCl.addStudent(wi.getStudentNumber())){
  							if (wi.getStudentNumber().equals(stdNum)){
  								log(2,"\t\t\t\tEnlisted!");
  							}
  						}else{
  							if (wi.getStudentNumber().equals(stdNum)){
  								log(2,"\t\t\t\tSection is full!");
  							}
  							sked.removeLastSectionAdded();	
  							sked.removeLastSectionAdded();
  						}
  						break;
  					}
  					
  				}
  			}
  		}	
  	}
  	
  	public void assign2(){
  		/**
  		 * Foreach r in R
  		 * 		Let Yr = subset of Vt such that for each element y,
  		 * 			[not conflict(X,y) and not full(y)]
  		 * 		
  		 * 		Order Yr based on desirability
  		 * 
  		 * Foreach c in {1,2,3,4,5,6}
  		 * 		Let q = subset of R belonging to c
  		 * 		Order Q based on |Yq|
  		 * 		Foreach q in Q
  		 * 		If not empty Yq
  		 * 			Let z = Yq[0]
  		 * 			Let wz = wz U q
  		 * 			Let Xq = Xq U z
  		 * 			If (full(z))
  		 * 				Foreach d in Q - {q]
  		 * 					Let Yd = Yd -{z}
  		 */
  		List studs=new Vector(writeins.keySet());
  		int randStud = new Random().nextInt(studs.size());
  		//stdNum=(String)studs.get(randStud);		
  
  		log(2,"Student: "+stdNum);				
  		log(1,"Sorting subjects based on rank.");
  		List sortedSubjects=new Vector(offering.getAllSubjects().keySet());
  		//Collections.sort(sortedSubjects);
  		Collections.sort(sortedSubjects,new Comparator(){
  			public int compare(Object o1, Object o2){
  				String s1=(String) o1;
  				String s2=(String) o2;
  				float t1 = offering.getSubjectRank(s1);
  				float t2 = offering.getSubjectRank(s2);
  				return ((int)(t1-t2));
  			}
  		});
  		
  			
  		Iterator sortedSubjectsIterator=sortedSubjects.iterator();
  		while(sortedSubjectsIterator.hasNext()){
  			//Map: <stdnum, list of section options>
  			sectionOptionsMap = new Hashtable();
  			String subject=(String)sortedSubjectsIterator.next();
  			//System.out.println(subject+":"+offering.getSubjectRank(subject));
  			
  			log(3,"Obtaining students interested in subject");			
  			Vector interestedStudents=(Vector)subjectList.get(subject);
  			Iterator interestedStudentsIterator = interestedStudents.iterator();
  			while(interestedStudentsIterator.hasNext()){
  				//Get the writein information
  				WriteIn wi=(WriteIn)interestedStudentsIterator.next();				
  				String  stdnum = wi.getStudentNumber();
  
  				//Get the schedule
  				Schedule sked=(Schedule)schedules.get(stdnum);
  				List sectionOptions = getSectionOptions(subject,sked);
  				
  				sectionOptionsMap.put(stdnum, sectionOptions );				
  			}
  			
  			Map prioritized = prioritize(subject);
  			
  			//For each priority
  			for (int i = 1; i <= 6;i++){
  				//System.out.println("Priority: "+i);
  				Integer key = new Integer(i);
  				List l = (List)prioritized.get(key);
  				
  				if (l != null){
  					//printList("Unsorted",l);
  					Collections.sort(l,new Comparator(){
  						public int compare(Object o1, Object o2){
  