// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   NeuroNet.java

package lt.ktu.gmj.tasks;

import drm.signal.neuro.*;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;
import javax.swing.JApplet;
import lt.ktu.HostNameRetriever;
import lt.ktu.gmj.*;
import lt.ktu.gmj.propertySheet.*;

// Referenced classes of package lt.ktu.gmj.tasks:
//			NeuroNetDomain, NeuroNetChoiceProvider

public class NeuroNet extends AbstractTask
{

	private NeuroNetDomain domain;
	private TestObject testObject;
	public int nnTypeChoice;
	public int neuronTypeChoice;
	public String fileName;

	public NeuroNet()
	{
		domain = new NeuroNetDomain();
		testObject = new TestObject();
		nnTypeChoice = 0;
		neuronTypeChoice = 0;
		fileName = "";
		setDefaults();
	}

	public void init()
	{
		try
		{
			URL tmyurl = new URL(fileName);
			System.out.println("Reading data from: " + tmyurl.getHost() + tmyurl.getFile());
			URLConnection tmycon = tmyurl.openConnection();
			System.out.println("Data type: " + tmycon.getContentType() + " with size: " + tmycon.getContentLength());
			java.io.InputStream tis = tmycon.getInputStream();
			Vector testVector = SignalStream.loadLearnFile(new InputStreamReader(tis));
			testObject.setTestVector(testVector);
			Vector first[] = (Vector[])testVector.get(0);
			int inputs = first[0].size();
			int outputs = first[1].size();
			System.out.println("Learn file loaded (inputs=" + String.valueOf(inputs) + "; outputs=" + String.valueOf(outputs) + ")");
			int wc = 0;
			Class clas = drm.signal.neuro.SimpleParceptron.class;
			switch(neuronTypeChoice)
			{
			case 0: // '\0'
				clas = drm.signal.neuro.SimpleParceptron.class;
				break;

			case 1: // '\001'
				clas = drm.signal.neuro.ExponentialParceptron.class;
				break;

			case 2: // '\002'
				clas = drm.signal.neuro.HTangentParceptron.class;
				break;

			case 3: // '\003'
				clas = drm.signal.neuro.QuadFunctionParceptron.class;
				break;
			}
			switch(nnTypeChoice)
			{
			case 0: // '\0'
				testObject.testObject = new NeuroNetSingleLayer(inputs, outputs, clas);
				wc = inputs * outputs;
				System.out.println("Single Layer Neural Network loaded (" + String.valueOf(wc) + " connections)");
				break;

			case 1: // '\001'
				testObject.testObject = new NeuroNetTwoLayers(inputs, outputs, outputs, clas);
				wc = inputs * (inputs + outputs);
				System.out.println("Two Layer Neural Network loaded (" + String.valueOf(wc) + " connections)");
				break;
			}
			domain.update(wc, 0.0D, -1D, 1.0D);
		}
		catch(Exception ex)
		{
			System.err.println("Failed to init NeuroNet");
			ex.printStackTrace();
		}
	}

	public Domain domain()
	{
		return domain;
	}

	public IResult f(Point p)
	{
		if(testObject.getTestVector() == null || testObject.getTestVector().size() == 0)
			init();
		double rez = testObject.test(p.x);
		return new SimpleResult(rez);
	}

	public void setDefaults()
	{
		switch(nnTypeChoice)
		{
		case 0: // '\0'
			fileName = HostNameRetriever.getApplet().getCodeBase().toString() + "/neuro1.txt";
			break;

		case 1: // '\001'
			fileName = HostNameRetriever.getApplet().getCodeBase().toString() + "/neuro2.txt";
			break;
		}
	}

	public void customize(PropertyManager manager)
	{
		manager.add(new ChoiceProperty("NN type:", new NeuroNetChoiceProvider(this, "nnTypeChoice"), new String[] {
			"Single layer network", "Two layers network"
		}));
		manager.add(new ChoiceProperty("Neuron type:", new NeuroNetChoiceProvider(this, "neuronTypeChoice"), new String[] {
			"Simple parceptron", "Exponential function parceptron", "HyperbTangent function parceptron", "Quad function parceptron"
		}));
		manager.add(new UrlProperty("Learning file:", new FieldProvider(this, "fileName")));
	}
}
