<commit_before>package searches;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import recoder.CrossReferenceServiceConfiguration;
import recoder.io.PropertyNames;
import recoder.io.SourceFileRepository;
import recoder.java.CompilationUnit;
import refactorings.Refactoring;
import refactory.Configuration;
import refactory.FitnessFunction;
import refactory.Metrics;

public abstract class Search 
{
	protected CrossReferenceServiceConfiguration sc;
	protected Configuration c;
	protected String resultsPath;
	protected ArrayList<String> refactoringInfo;
	
	public Search(CrossReferenceServiceConfiguration sc, Configuration c) 
	{
		this.sc = sc;
		this.c = c;
		String output = sc.getProjectSettings().getProperty(PropertyNames.OUTPUT_PATH);
		this.resultsPath = "./results/" + output.substring(output.lastIndexOf("/") + 1) + "/";
	}
	
	public Search(CrossReferenceServiceConfiguration sc) 
	{
		this.sc = sc;
		String output = sc.getProjectSettings().getProperty(PropertyNames.OUTPUT_PATH);
		this.resultsPath = "./results/" + output.substring(output.lastIndexOf("/") + 1) + "/";
	}
	
	public abstract void run();
	
	protected void print(SourceFileRepository sfr)
	{
		List<CompilationUnit> list = sfr.getKnownCompilationUnits();

		for (CompilationUnit cu : list)
		{	
			try 
			{
				sfr.print(cu);
			} 
			catch (IOException e) 
			{
				System.out.println("\nEXCEPTION: Cannot print changes to output.");
				System.exit(1);
			}
		}
	}
	
	protected int[] randomElement(Refactoring r)
	{
		int[] position = new int[2];
		position[0] = (int) (Math.random() * r.getServiceConfiguration().getSourceFileRepository().getKnownCompilationUnits().size());
	
		// Only counts the relevant program elements.
		int amount = r.getAmount(position[0]);
		
		// Count the amount of available elements in the chosen class for refactoring.
		// If none are available find the next element up that is available.
		// If there are still no elements found look in the other direction and if there 
		// are no elements available at all return -1 so it can be handled in the search.
		if (amount == 0)
		{
			int[] temp = new int[2];
			position[1] = 1;
			temp = nextElementUp(position[0], position[1], r);
			
			if ((temp[0] == -1) && (temp[1] == -1))
			{
				temp = nextElementDown(position[0], position[1], r);
			}	
			
			position = temp;
		}
		else
			// Random element chosen from the available amount.
			position[1] = (int) (Math.random() * (amount-1)) + 1;
		
		return position;
	}
	
	private int[] nextElementUp(int currentUnit, int currentElement, Refactoring r) 
	{
		int[] position = new int[2];
		
		if  (currentElement >= r.getAmount(currentUnit))
		{
			if (currentUnit == (r.getServiceConfiguration().getSourceFileRepository().getKnownCompilationUnits().size() - 1))
			{
				position[0] = -1;
				position[1] = -1;
			}
			else
			{				
				currentUnit++;

				int amount = 0;<commit_msg>Changed method to be a bit more random.<commit_after>package searches;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import recoder.CrossReferenceServiceConfiguration;
import recoder.io.PropertyNames;
import recoder.io.SourceFileRepository;
import recoder.java.CompilationUnit;
import refactorings.Refactoring;
import refactory.Configuration;
import refactory.FitnessFunction;
import refactory.Metrics;

public abstract class Search 
{
	protected CrossReferenceServiceConfiguration sc;
	protected Configuration c;
	protected String resultsPath;
	protected ArrayList<String> refactoringInfo;
	
	public Search(CrossReferenceServiceConfiguration sc, Configuration c) 
	{
		this.sc = sc;
		this.c = c;
		String output = sc.getProjectSettings().getProperty(PropertyNames.OUTPUT_PATH);
		this.resultsPath = "./results/" + output.substring(output.lastIndexOf("/") + 1) + "/";
	}
	
	public Search(CrossReferenceServiceConfiguration sc) 
	{
		this.sc = sc;
		String output = sc.getProjectSettings().getProperty(PropertyNames.OUTPUT_PATH);
		this.resultsPath = "./results/" + output.substring(output.lastIndexOf("/") + 1) + "/";
	}
	
	public abstract void run();
	
	protected void print(SourceFileRepository sfr)
	{
		List<CompilationUnit> list = sfr.getKnownCompilationUnits();

		for (CompilationUnit cu : list)
		{	
			try 
			{
				sfr.print(cu);
			} 
			catch (IOException e) 
			{
				System.out.println("\nEXCEPTION: Cannot print changes to output.");
				System.exit(1);
			}
		}
	}
	
	protected int[] randomElement(Refactoring r)
	{
		int[] position = new int[2];
		position[0] = (int) (Math.random() * r.getServiceConfiguration().getSourceFileRepository().getKnownCompilationUnits().size());
	
		// Only counts the relevant program elements.
		int amount = r.getAmount(position[0]);
		
		// Count the amount of available elements in the chosen class for refactoring.
		// If none are available find the next element up that is available.
		// If there are still no elements found look in the other direction and if there 
		// are no elements available at all return -1 so it can be handled in the search.
		if (amount == 0)
		{
			int[] temp = new int[2];
			position[1] = 1;
			
			if (Math.random() >= 0.5)
			{
				temp = nextElementUp(position[0], position[1], r);

				if ((temp[0] == -1) && (temp[1] == -1))
					temp = nextElementDown(position[0], position[1], r);
			}
			else
			{
				temp = nextElementDown(position[0], position[1], r);
				
				if ((temp[0] == -1) && (temp[1] == -1))
					temp = nextElementUp(position[0], position[1], r);	
			}
				
			position = temp;
		}
		else
			// Random element chosen from the available amount.
			position[1] = (int) (Math.random() * (amount-1)) + 1;
		
		return position;
	}
	
	private int[] nextElementUp(int currentUnit, int currentElement, Refactoring r) 
	{
		int[] position = new int[2];
		
		if  (currentElement >= r.getAmount(currentUnit))
		{
			if (currentUnit == (r.getServiceConfiguration().getSourceFileRepository().getKnownCompilationUnits().size() - 1))
			{
				position[0] = -1;
				position[1] = -1;
			}
			else
			{				
				currentUnit++;

				int amount = 0;
{'fraction_non_alphanumeric': 0.12507987220447284, 'fraction_numerical': 0.010862619808306708, 'mean_word_length': 3.7684691546077684, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1326475', 'n_tokens_mistral': 2207, 'n_tokens_neox': 1905, 'n_words': 513}