<commit_before>package recogniser;

import java.util.HashMap;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import coordinates.Coordinate;
import events.event.AcceptingStateEvent;
import events.listeners.AcceptingStateListener;

import gestures.Gesture;
import model.FiniteStateMachine;

/**
 * An object to manage all FiniteStateMachines of the gesture recognizer
 * @author <NAME>
 *
 */
public class FiniteStateMachineManager extends Thread {

	private HashMap<FiniteStateMachine, Gesture> managees = new HashMap<FiniteStateMachine, Gesture>();
	private Queue<Coordinate> coordinateQueue = new ConcurrentLinkedQueue<Coordinate>(); 
	
	/**
	 * Create an instance of a FiniteStateMachineManager
	 */
	public FiniteStateMachineManager() {
		
	}
	
	/**
	 * Add a FiniteStateMachine to the manager, with its corresponding Gesture
	 * @param fsm The FiniteStateMachine
	 * @param gesture The corresponding Gesture
	 */
	public void add(FiniteStateMachine fsm, Gesture gesture) {
		fsm.addEventListener(new AcceptingStateListener() {
			@Override
			public void handleAcceptingState(AcceptingStateEvent e) {
				processAcceptingStateEvent(e);
			}
		});
		managees.put(fsm, gesture);
	}
	
	/**
	 * Add a coordinate to the manager to be processed by the FiniteStateMachines
	 * @param coordinate The coordinate to be added
	 */
	public synchronized void input(Coordinate coordinate) {
		coordinateQueue.add(coordinate);
		run();
	}
	
	/**
	 * Method to clear the Coordinate queue of the manager
	 */
	public void clear() {
		coordinateQueue.clear();
	}
	
	/**
	 * Method to reset all managed FiniteStateMachinesto their base states, 
	 * and clear the coordinates queue of the manager
	 */
	public void reset() {
		interrupt();
		clear();
		for(FiniteStateMachine fsm : managees.keySet()) {
			fsm.reset();
		}
	}
	
	/**
	 * Add an AcceptinStateListener to all FiniteStateMachines
	 * @param listener The AcceptinStateListener to add to the FSMs.
	 */
	public void addAcceptingStateListener(AcceptingStateListener listener) {
		for(FiniteStateMachine fsm : managees.keySet()) {
			fsm.addEventListener(listener);
		}
	}
	
	@Override
	public void run() {
		Coordinate coordinate = coordinateQueue.poll();
		for(FiniteStateMachine fsm : managees.keySet()) {
			fsm.input(coordinate);
		}
	}
	
	/**
	 * Method to handle an AcceptingStateEvent
	 * @param e The AcceptingStateEvent to be handled
	 */
	private void processAcceptingStateEvent(AcceptingStateEvent e) {
		Gesture g = managees.get((FiniteStateMachine) e.getSource());
		reset();<commit_msg>FiniteStateMachineManager: Modifying execution of the new coordinate process to be within a new Thread<commit_after>package recogniser;

import java.awt.EventQueue;
import java.util.HashMap;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import coordinates.Coordinate;
import events.event.AcceptingStateEvent;
import events.listeners.AcceptingStateListener;

import gestures.Gesture;
import model.FiniteStateMachine;

/**
 * An object to manage all FiniteStateMachines of the gesture recognizer
 * @author <NAME>
 *
 */
public class FiniteStateMachineManager {

	private HashMap<FiniteStateMachine, Gesture> managees = new HashMap<FiniteStateMachine, Gesture>();
	private Queue<Coordinate> coordinateQueue = new ConcurrentLinkedQueue<Coordinate>(); 
	
	/**
	 * Create an instance of a FiniteStateMachineManager
	 */
	public FiniteStateMachineManager() {
		
	}
	
	/**
	 * Add a FiniteStateMachine to the manager, with its corresponding Gesture
	 * @param fsm The FiniteStateMachine
	 * @param gesture The corresponding Gesture
	 */
	public void add(FiniteStateMachine fsm, Gesture gesture) {
		fsm.addEventListener(new AcceptingStateListener() {
			@Override
			public void handleAcceptingState(AcceptingStateEvent e) {
				processAcceptingStateEvent(e);
			}
		});
		managees.put(fsm, gesture);
	}
	
	/**
	 * Add a coordinate to the manager to be processed by the FiniteStateMachines
	 * @param coordinate The coordinate to be added
	 */
	public synchronized void input(Coordinate coordinate) {
		coordinateQueue.add(coordinate);
		(new Thread(new Runnable(){
			@Override
			public void run() {
				Coordinate coordinate = getFirstCoordinate();
				for(FiniteStateMachine fsm : managees.keySet()) {
					fsm.input(coordinate);
				}
			}
		})).start();
	}
	
	/**
	 * Retrieve the first element in the coordinate queue
	 * @return The first Coordinate
	 */
	private synchronized Coordinate getFirstCoordinate() {
		return coordinateQueue.poll();
	}
	
	/**
	 * Method to clear the Coordinate queue of the manager
	 */
	public void clear() {
		coordinateQueue.clear();
	}
	
	/**
	 * Method to reset all managed FiniteStateMachinesto their base states, 
	 * and clear the coordinates queue of the manager
	 */
	public void reset() {
		clear();
		for(FiniteStateMachine fsm : managees.keySet()) {
			fsm.reset();
		}
	}
	
	/**
	 * Add an AcceptinStateListener to all FiniteStateMachines
	 * @param listener The AcceptinStateListener to add to the FSMs.
	 */
	public void addAcceptingStateListener(AcceptingStateListener listener) {
		for(FiniteStateMachine fsm : managees.keySet()) {
			fsm.addEventListener(listener);
		}
	}
	
	/**
	 * Method to handle an AcceptingStateEvent
	 * @param e The AcceptingStateEvent to be handled
	 */
	private void processAcceptingStateEvent(AcceptingStateEvent e) {
		Gesture g = managees.get((FiniteStateMachine) e.getSource());
		reset();
{'fraction_non_alphanumeric': 0.09120818863096326, 'fraction_numerical': 0.0, 'mean_word_length': 4.428571428571429, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '2275306', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1531, 'n_words': 452}