package learner;

import de.ls5.jlearn.abstractclasses.LearningException;
import de.ls5.jlearn.interfaces.Oracle;
import de.ls5.jlearn.interfaces.Symbol;
import de.ls5.jlearn.interfaces.Word;
import de.ls5.jlearn.shared.SymbolImpl;
import de.ls5.jlearn.shared.WordImpl;

public class MembershipOracle implements Oracle {
	private static final long serialVersionUID = -1374892499287788040L;
	private SutWrapper sutWrapper;
	private int numMembQueries = 0;
	
	public MembershipOracle(Mapper mapper) {
		sutWrapper = new SutWrapper();
	}

	public Word processQuery(Word query) throws LearningException {
		Word result = new WordImpl();
		System.out.println("LearnLib Query: " + query);

		sutWrapper.sendReset();
		/*try {
			Thread.sleep(20000);
			System.out.println("sleep");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		//sutWrapper.sendConnect();
		String input;
		String output;
		
		numMembQueries++;
		System.out.println("Member query number: " + numMembQueries);

		for (Symbol currentSymbol : query.getSymbolList()) {
			input = currentSymbol.toString();
				
			System.out.println("Sending: " + input);
			output = sutWrapper.sendInput(input);
			System.out.println("Received: " + output.toString());
	
			result.addSymbol(new SymbolImpl(output));
		}

		System.out.println("Returning to LearnLib: " + result);
		return result;
	}

	public int getNumMembQueries() {
		int result = numMembQueries;
		numMembQueries = 0;
		return result;
	}
}
