#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <windows.h>
#include "mbd.h"

void print_info()
{
	cout << "command line: mbd [options]"<< endl;
	cout << "Options:"<< endl;
	cout << "-i filename \t input pla file."<< endl;
	cout << "-o filename \t output file name."<< endl;
	cout << "-ot type \t output file type: dot or pla (default: pla)"<< endl;
	cout << "-so type \t static ordering - none random stat force (default: none)"<< endl;
	cout << "-do flag \t use dynamic ordering - yes no (default: yes)"<< endl;
	cout << "-xm method \t don't care minimization - none brother random (def: brother)"<< endl;
	cout << "-min flag \t minimize function - yes no (default: no)"<< endl;
	cout << "-help \t\t prints this info"<< endl << endl;

	cout << "Example - minimize function and write result to dot file:"<< endl;
	cout << "mbd -i infile.pla -o outfile.dot -ot dot -min yes -so stat -do no"<< endl << endl;
}

int main(int argc, char* argv[])
{
	bool dyn=true; 
	bool dot=false; 
	bool m=false;
	char* outfile=NULL;
	char* infile=NULL;
	uint so = MBD_STATIC_ORDERING_METHOD___NONE;
	uint xm = MBD_MIN_X_SUBST_METHOD___BROTHER;

	cout << "MBD application - Jan Bilek, 2006-2007" << endl << endl;

	if (argc < 3)
	{
		cout << "Error: not enough parameters."<<endl<<endl;
		print_info();
		return -1;
	}

	for (int i=1; i<argc; i++)
	{
		if (!strcmp("-i",argv[i]))
		{
			infile = argv[++i];
		}
		else if (!strcmp("-o",argv[i]))
		{
			outfile = argv[++i];
		}
		else if (!strcmp("-ot", argv[i]))
		{
			i++;
			if (!strcmp("dot", argv[i]))
			{
				dot = true;
			}
		}
		else if (!strcmp("-so", argv[i]))
		{
			i++;
			if (!strcmp("random", argv[i]))
			{
				so = MBD_STATIC_ORDERING_METHOD___RANDOM;
			}
			else if (!strcmp("force", argv[i]))
			{
				so = MBD_STATIC_ORDERING_METHOD___FORCE;
			}
			else if (!strcmp("stat", argv[i]))
			{
				so = MBD_STATIC_ORDERING_METHOD___STAT;
			}
		}
		else if (!strcmp("-do", argv[i]))
		{
			i++;
			if (!strcmp("no", argv[i]))
			{
				dyn = false;
			}
		}
		else if (!strcmp("-xm", argv[i]))
		{
			i++;
			if (!strcmp("none", argv[i]))
			{
				xm = MBD_MIN_X_SUBST_METHOD___NONE;
			}
			else if (!strcmp("random", argv[i]))
			{
				xm = MBD_MIN_X_SUBST_METHOD___RAND_NODE;
			}
		}
		else if (!strcmp("-min", argv[i]))
		{
			i++;
			if (!strcmp("yes", argv[i]))
			{
				m = true;
			}
		}
		else
		{
			cout << "Error: Invalid parameter "  << argv[i] << endl;
			return -1;
		}
	}

	if (infile==NULL)
	{
		cout << "Error: No input file specified."<<endl;
		return -1;
	}

	MBD *mbd=NULL;
	PLA *pla=NULL;
	MBDLIB* mbdlib = new MBDLIB();

	mbdlib->set_static_ordering_method(so);
	mbdlib->set_x_subst_method(xm);

	cout << "Loading file...";
	if (mbdlib->load_from_pla(infile, &mbd, &pla))
	{
		cout << "Error: Cannot load file "  << infile << endl;
		return -1;
	}

	cout << "Done ";
	cout << "MBD has " << mbd->get_node_count() << " nodes." << endl;

	cout << "Substituing X...";
	mbdlib->minimize_X(&mbd);
	cout << "Done ";
	cout << "MBD has " << mbd->get_node_count() << " nodes." << endl;
	
	cout << "Dynamically ordering...";
	mbdlib->order_vars_dynamic(mbd);
	cout << "Done ";
	cout << "MBD has " << mbd->get_node_count() << " nodes." << endl;

	if (dot && (NULL != outfile))
	{
		cout << "Saving to DOT...";
		if (m) cout << "Cannot minimize when output is DOT...";
		mbd->save_to_dot(outfile);
		cout << "Done ";
	}
	else if (NULL != outfile)
	{
		cout << "Saving to PLA...";
		PLA pla2(mbd->in_num,1);
		mbd->get_pla(pla2);
		pla2.save(outfile);
		cout << "Done ";
	}

	cout << endl;


	return 0;
}

































