<commit_before>package structure.impl;

import structure.intf.QEA;

/**
 * This class represents a simple Quantified Event Automaton (QEA) with the
 * following characteristics:
 * <ul>
 * <li>There is at most one quantified variable
 * <li>The transitions in the function delta consist of a start state, an event
 * and an end state, no guards or assigns are considered
 * <li>The QEA can is non-deterministic
 * </ul>
 * 
 * @author <NAME>
 * @author <NAME>
 */
public class SimpleNonDeterministicQEA implements QEA {

	private int[] finalStates;

	private int[][][] delta;

	protected final int initialState;

	private boolean quantificationUniversal;

	public SimpleNonDeterministicQEA(int numStates, int numEvents,
			int initialState, boolean quantificationUniversal) {
		delta = new int[numStates + 1][numEvents + 1][numStates + 1];
		finalStates = new int[numStates + 1];
		this.initialState = initialState;
		this.quantificationUniversal = quantificationUniversal;
	}

	/**
	 * Adds a transition for the transition function delta of this QEA
	 * 
	 * @param startState
	 *            Start state for this transition
	 * @param event
	 *            Name of the event
	 * @param endStates
	 *            Array of end states for this transition. The size of the array
	 *            is the number of states for this QEA + 1. The array can
	 *            contain values 0 and 1; 1 indicates that the current position
	 *            of the array is an end state for this transition, while 0
	 *            indicates it's not
	 */
	public void addTransition(int startState, int event, int[] endStates) {
		System.arraycopy(endStates, 0, delta[startState][event], 0,
				endStates.length);
	}

	/**
	 * Retrieves an array of end states for a given start state and an event,
	 * according to the transition function delta of this QEA
	 * 
	 * @param previousState
	 *            Start state
	 * @param event
	 *            Name of the event
	 * @return Array of end states for this transition. The size of the array is
	 *         the number of states for this QEA + 1. The array can contain
	 *         values 0 and 1; 1 indicates that the current position of the
	 *         array is an end state for this transition, while 0 indicates it's
	 *         not
	 */
	public int[] getNextStates(int previousState, int event) {
		return delta[previousState][event];
	}

	/**
	 * Adds the specified state to the set of final states
	 * 
	 * @param state
	 *            State name
	 */
	public void setStateAsFinal(int state) {
		finalStates[state] = 1;
	}

	/**
	 * Adds the specified states to the set of final statess
	 * 
	 * @param states
	 *            Names of states to add
	 */
	public void setStatesAsFinal(int... states) {
		for (int state : states)
			finalStates[state] = 1;
	}

	@Override
	public int[] getStates() {
		int[] q = new int[delta.length];
		for (int i = 0; i < q.length; i++) {
			q[i] = i + 1;
		}
		return q;
	}

	@Override
	public int[] getEventsAlphabet() {
		int[] a = new int[delta[0].length];
		for (int i = 0; i < a.length; i++) {
			a[i] = i + 1;
		}
		return a;
	}

	@Override
	public int[] getLambda() {
		if (quantificationUniversal) {
			return new int[] { 1 };
		}
		return new int[] { -1 };
	}

	@Override
	public boolean isDeterministic() {
		return false;
	}

	@Override
	public boolean usesFreeVariables() {
		return false;
	}

	@Override
	public boolean isStateFinal(int state) {
		if (finalStates[state] == 1) {
			return true;
		}
		return false;
	}

	public boolean isQuantificationUniversal() {
		return quantificationUniversal;
	}

}
<commit_msg>*Method addTransition modified to remove array copy *Method getNextStates modified to receive as parameter and return a NonDetConfig instead of an array of integer *Method containsFinalState added<commit_after>package structure.impl;

import monitoring.impl.NonDetConfig;
import structure.intf.QEA;

/**
 * This class represents a simple Quantified Event Automaton (QEA) with the
 * following characteristics:
 * <ul>
 * <li>There is at most one quantified variable
 * <li>The transitions in the function delta consist of a start state, an event
 * and a set of end states, no guards or assigns are considered
 * <li>The QEA can is non-deterministic
 * </ul>
 * 
 * @author <NAME>
 * @author <NAME>
 */
public class SimpleNonDeterministicQEA implements QEA {

	private int[] finalStates;

	private int[][][] delta;

	protected final int initialState;

	private boolean quantificationUniversal;

	public SimpleNonDeterministicQEA(int numStates, int numEvents,
			int initialState, boolean quantificationUniversal) {
		delta = new int[numStates + 1][numEvents + 1][numStates + 1];
		finalStates = new int[numStates + 1];
		this.initialState = initialState;
		this.quantificationUniversal = quantificationUniversal;
	}

	/**
	 * Adds a transition for the transition function delta of this QEA
	 * 
	 * @param startState
	 *            Start state for this transition
	 * @param event
	 *            Name of the event
	 * @param endStates
	 *            Array of end states for this transition
	 */
	public void addTransition(int startState, int event, int[] endStates) {
		delta[startState][event] = endStates;
	}

	/**
	 * Retrieves the final configuration for a given start configuration and an
	 * event, according to the transition function delta of this QEA
	 * 
	 * @param config
	 *            Start configuration containing the set of start states
	 * @param event
	 *            Name of the event
	 * @return End configuration containing the set of end states
	 */
	public NonDetConfig getNextStates(NonDetConfig config, int event) {

		if (config.getStates().length == 1) { // Only one state in the start
												// configuration
			config.setStates(delta[config.getStates()[0]][event]);

		} else { // More than one state in the start configuration

			// Get a reference to the start states
			int[] startStates = config.getStates();

			// Create a boolean array of size equalto the number of states
			boolean[] endStatesBool = new boolean[delta.length];

			// Initialise end states count
			int endStatesCount = 0;

			// Iterate over the multiple arrays of end states
			for (int i = 0; i < startStates.length; i++) {

				int[] intermEndStates = delta[startStates[i]][event];

				// Iterate over the intermediate arrays of end states
				for (int j = 0; j < intermEndStates.length; j++) {
					if (!endStatesBool[j]) {
						endStatesBool[j] = true;
						endStatesCount++;
					}
				}
			}

			// Remove state 0 if there are other end states
			if (endStatesBool[0] && endStatesCount > 1) {
				endStatesBool[0] = false;
				endStatesCount--;
			}

			int[] endStates;

			// Check if the number of end states is the same as start states
			if (endStatesCount == startStates.length) { // Same size
				// Use the same array
				endStates = startStates;
			} else { // Different number of start states and end states
				// Create a new array with the right size
				endStates = new int[endStatesCount];
			}

			// Populate array of end states
			int j = 0;
			for (int i = 0; i < endStatesBool.length; i++) {
				if (endStatesBool[i]) {
					endStates[j] = i;
					j++;
				}
			}

			config.setStates(endStates);
		}

		return config;
	}

	/**
	 * Determines if the set of states in the specified configuration contains
	 * at least one final state
	 * 
	 * @param config
	 *            Configuration encapsulating the set of states to be checked
	 * @return <code>true</code> if the set of states in the specified
	 *         configuration contains at least one final state;
	 *         <code>false</code> otherwise
	 */
	public boolean containsFinalState(NonDetConfig config) {
		for (int i = 0; i < config.getStates().length; i++) {
			if (isStateFinal(config.getStates()[i])) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Adds the specified state to the set of final states
	 * 
	 * @param state
	 *            State name
	 */
	public void setStateAsFinal(int state) {
		finalStates[state] = 1;
	}

	/**
	 * Adds the specified states to the set of final statess
	 * 
	 * @param states
	 *            Names of states to add
	 */
	public void setStatesAsFinal(int... states) {
		for (int state : states)
			finalStates[state] = 1;
	}

	@Override
	public int[] getStates() {
		int[] q = new int[delta.length];
		for (int i = 0; i < q.length; i++) {
			q[i] = i + 1; // TODO Is this method returning one more state?
		}
		return q;
	}

	@Override
	public int[] getEventsAlphabet() {
		int[] a = new int[delta[0].length];
		for (int i = 0; i < a.length; i++) {
			a[i] = i + 1;
		}
		return a;
	}

	@Override
	public int[] getLambda() {
		if (quantificationUniversal) {
			return new int[] { 1 };
		}
		return new int[] { -1 };
	}

	@Override
	public boolean isDeterministic() {
		return false;
	}

	@Override
	public boolean usesFreeVariables() {
		return false;
	}

	@Override
	public boolean isStateFinal(int state) {
		if (finalStates[state] == 1) {
			return true;
		}
		return false;
	}

	public boolean isQuantificationUniversal() {
		return quantificationUniversal;
	}

}

{'fraction_non_alphanumeric': 0.09365750528541227, 'fraction_numerical': 0.0054968287526427064, 'mean_word_length': 2.55276004506196, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 22, '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': '5560770', 'n_tokens_mistral': 3186, 'n_tokens_neox': 2643, 'n_words': 1010}