<commit_before>package factories;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import command.*;


public class CommandFactory {

	private Map<String, Double> variables;
	private List<Double> falseParams;
	private List<Double> trueParams;
	private List<Double> homeParams;
	private String language;
	
	private Properties translationMap;
	private Properties paramMap;


	private static final double TRUE = 1;
	private static final double FALSE = 0;
	private static final double DIMENSIONS = 2;

	public CommandFactory(String l) throws IOException {
		variables = new HashMap<>();
		language = l;
		
		translationMap = new Properties();
		makeTranslationMap();
		
		paramMap = new Properties();
		makeParamMap();
		
		makeDefaults();
	}

	private void makeDefaults(){
		falseParams = new ArrayList<>();
		falseParams.add(FALSE);

		trueParams = new ArrayList<>();
		trueParams.add(TRUE);

		homeParams = new ArrayList<>();
		while(homeParams.size() <= DIMENSIONS)
			homeParams.add(0.);
	}

	public Command createCommand(List<String> parts){
		List<Double> params = new ArrayList<Double>();
		String command = parts.get(0);
		if(translateCommand(command).equals("MakeVariable")){
			return addVariable(parts.get(1),parts.get(2));
		}
		String cmd = translateCommand(command);
		params = makeParams(parts);
		if (params.contains(null)){
			throw new IllegalArgumentException("The parameters are invalid"); 
		}
		
		Map <String, Command> cmdMap = makeCommandMap(params);
		Command ret = cmdMap.get(cmd);
		if(ret == null)
			throw new IllegalArgumentException("The command is invalid");

		return ret;
	}
	
	private String translateCommand(String s){
		while(translationMap.keys().hasMoreElements()){
			String key = (String) translationMap.keys().nextElement();
			String val = (String) translationMap.get(key);
			for (String sub: val.split(","))
				if(sub.equals(s))
					return key;
				
			for(String sub: val.split(","))
				if(sub.equals(s))
					return key;
		}
		
		throw new IllegalArgumentException("Illegal Command");
	}
	
	public int getNumParameters(String s){
		return Integer.parseInt((String)paramMap.get(translateCommand(s)));
	}
	private void makeParamMap() throws IOException{
		String propFileName = "NumParameters.properties";
		 
		InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
 
		if (inputStream != null) {
			paramMap.load(inputStream);
		} else {
			throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
		}
		
	}
	
	private void makeTranslationMap() throws IOException{
		String propFileName = language + ".properties";
		 
		InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
 
		if (inputStream != null) {
			translationMap.load(inputStream);
		} else {
			throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
		}
		
	}
	
	private Map<String, Command> makeCommandMap(List<Double> params){
		Map<String, Command> paramMap = new HashMap<String, Command>();
		paramMap.putAll(turtleCommands(params));
		paramMap.putAll(queryCommands(params));
		paramMap.putAll(mathCommands(params));
		paramMap.putAll(booleanCommands(params));
		return paramMap;
	}

	private Map<String, Command> booleanCommands(List<Double> params){
		Map<String, Command> commands = new HashMap<>();
		commands.put("LessThan", new LessCommand(params));
		commands.put("GreaterThan", new GreaterCommand(params));
		commands.put("Equal", new EqualsCommand(params));
		commands.put("NotEqual", new NotEqualsCommand(params));<commit_msg>Allowing language to be set dynamically<commit_after>package factories;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import command.*;


public class CommandFactory {

	private Map<String, Double> variables;
	private List<Double> falseParams;
	private List<Double> trueParams;
	private List<Double> homeParams;
	private String language;

	private Properties translationMap;
	private Properties paramMap;


	private static final double TRUE = 1;
	private static final double FALSE = 0;
	private static final double DIMENSIONS = 2;

	public CommandFactory(String l) throws IOException {
		variables = new HashMap<>();
		language = l;

		translationMap = new Properties();
		makeTranslationMap();

		paramMap = new Properties();
		makeParamMap();

		makeDefaults();
	}

	private void makeDefaults(){
		falseParams = new ArrayList<>();
		falseParams.add(FALSE);

		trueParams = new ArrayList<>();
		trueParams.add(TRUE);

		homeParams = new ArrayList<>();
		while(homeParams.size() <= DIMENSIONS)
			homeParams.add(0.);
	}

	public Command createCommand(List<String> parts){
		List<Double> params = new ArrayList<Double>();
		String command = parts.get(0);
		if(translateCommand(command).equals("MakeVariable")){
			return addVariable(parts.get(1),parts.get(2));
		}
		String cmd = translateCommand(command);
		params = makeParams(parts);
		if (params.contains(null)){
			throw new IllegalArgumentException("The parameters are invalid"); 
		}

		Map <String, Command> cmdMap = makeCommandMap(params);
		Command ret = cmdMap.get(cmd);
		if(ret == null)
			throw new IllegalArgumentException("The command is invalid");

		return ret;
	}

	private String translateCommand(String s){
		while(translationMap.keys().hasMoreElements()){
			String key = (String) translationMap.keys().nextElement();
			String val = (String) translationMap.get(key);
			for (String sub: val.split(","))
				if(sub.equals(s))
					return key;

			for(String sub: val.split(","))
				if(sub.equals(s))
					return key;
		}

		throw new IllegalArgumentException("Illegal Command");
	}

	public int getNumParameters(String s){
		return Integer.parseInt((String)paramMap.get(translateCommand(s)));
	}

	private void makeParamMap() throws IOException{
		String propFileName = "NumParameters.properties";

		InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

		if (inputStream != null) {
			paramMap.load(inputStream);
		} else {
			throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
		}

	}

	private void makeTranslationMap() throws IOException{
		String propFileName = language + ".properties";

		InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

		if (inputStream != null) {
			translationMap.load(inputStream);
		} else {
			throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
		}

	}

	public void setLanguage(String l){
		language = l;
		makeTranslationMap();
	}
	private Map<String, Command> makeCommandMap(List<Double> params){
		Map<String, Command> paramMap = new HashMap<String, Command>();
		paramMap.putAll(turtleCommands(params));
		paramMap.putAll(queryCommands(params));
		paramMap.putAll(mathCommands(params));
		paramMap.putAll(booleanCommands(params));
		return paramMap;
	}

	private Map<String, Command> booleanCommands(List<Double> params){
		Map<String, Command> commands = new HashMap<>();
		commands.put("LessThan", new LessCommand(params));
		commands.put("GreaterThan", new GreaterCommand(params));
		commands.put("Equal", new EqualsCommand(params));
		commands.put("NotEqual", new NotEqualsCommand(params));
{'fraction_non_alphanumeric': 0.12426345423595653, 'fraction_numerical': 0.0018331805682859762, 'mean_word_length': 5.125100240577385, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 43, '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': '3377862', 'n_tokens_mistral': 2401, 'n_tokens_neox': 2138, 'n_words': 555}