package net.woodyfolsom.msproj.ann;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.encog.ml.data.MLData;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.networks.PersistBasicNetwork;

public abstract class AbstractNeuralNetFilter implements NeuralNetFilter {
	protected BasicNetwork neuralNetwork;
	protected int actualTrainingEpochs = 0;
	protected int maxTrainingEpochs = 1000;
	
	@Override
	public MLData compute(MLData input) {
		return this.neuralNetwork.compute(input);
	}
	
	public int getActualTrainingEpochs() {
		return actualTrainingEpochs;
	}
	
	public int getMaxTrainingEpochs() {
		return maxTrainingEpochs;
	}
	
	@Override
	public BasicNetwork getNeuralNetwork() {
		return neuralNetwork;
	}
	
	public void load(String filename) throws IOException {
		FileInputStream fis = new FileInputStream(new File(filename));
		neuralNetwork = (BasicNetwork) new PersistBasicNetwork().read(fis);
		fis.close();
	}
	
	@Override
	public void reset() {
		neuralNetwork.reset();
	}
	
	@Override
	public void reset(int seed) {
		neuralNetwork.reset(seed);
	}
	
	public void save(String filename) throws IOException {
		FileOutputStream fos = new FileOutputStream(new File(filename));
		new PersistBasicNetwork().save(fos, getNeuralNetwork());
		fos.close();	
	}
	
	public void setMaxTrainingEpochs(int max) {
		this.maxTrainingEpochs = max;
	}
}
