#include "test.h"

int main(int argc, char* argv[]) 
{
	char IDesc[] = "matrix D, matrix Dm, matrix Dp, matrix L";
	char ODesc[] = "matrix Df, matrix Dfm, matrix Dfp";
	int* IFilled;
	int OFilled[3];
	supervis_r result;
	FILE* input;
	MemoryManager mem;
	
	matrix D, Dm, Dp, L, Df;
		
	/*Get the input file.*/
	if (! (input = ParseCmdLine(argc,argv)))
	{
		return 0;
	}
	
	mem = CreateMemoryManager(1, 5, 0, 0);
	
	while (ParseStructure(input, IDesc, &IFilled, &mem, &D, &Dm, &Dp, &L))
	{
		/*Show the problem.*/
		DisplayStructure(IDesc, IFilled, &D, &Dm, &Dp, &L);
		
		/*If just an incidence matrix was provided, fill the i/o matrices.*/
		FillDmDp(IFilled, &D, &Dm, &Dp, &mem);
		
		/*Solve the problem.*/	
		result = supervis(IFilled[2] ? &Dp : 0, IFilled[1] ? &Dm : 0, IFilled[3] ? &L : 0);
	
		/*Make sure a result was provided.*/
		if(NumberOfRows(result.Dfm) && NumberOfRows(result.Dfp))
		{
			/*If there are no self-loops, display an incidence matrix only.*/
			if (! HasSelfLoops(&result.Dfm, &result.Dfp))
			{
				Df = SubtractMatrix(&result.Dfp, &result.Dfm, (matrix*) 2);
				ManageMatrix(&mem, &Df);
				OFilled[0] = 1;
				OFilled[1] = 0;
				OFilled[2] = 0;
			}
			else
			{
				OFilled[0] = 0;
				OFilled[1] = 1;
				OFilled[2] = 1;
			}
			/*Show the result.*/
			printf("Supervised Net:\n");
			DisplayStructure(ODesc, OFilled, &Df, &result.Dfm, &result.Dfp);
		}
		else
		{
			printf("WARNING: supervis call returned empty matrices!\n");
		}
		printf("---------------------------------------------------------------\n");
		FreeMemory(&mem);
		DeallocateSupervis(&result);
		mem = CreateMemoryManager(1, 5, 0, 0);
	}
	FreeMemory(&mem);
	return 0;
}
