/*
    Copyright (C) 2012 Bartosz Broda, Jacek Bochenek, Łukasz Biłeńkij, 
    Damian Jaworski, Wojciech Mazur, Dominik Piasecki, Maciej Piasecki, 
    Radosław Ramocki
    
    This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.

    This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. 

    See the LICENSE and COPYING files for more details.
*/

#include "ROCK.h"
#include "comparator_all.h"
#include "Options.h"

int main(int argc, char* argv[])
{
	using namespace clustering::ROCK;

	Options opt(argc, argv);

	Params & params = opt.GetParams();

	std::cout << "ROCK test...\n";
	std::string dir = params.matrix_dir;
	std::string name = params.matrix_name;
	// load matrix
	Matrices::IO::SuperMatrixReader reader;
	Matrices::SuperMatrixPtr pMatrix = reader.Read( dir, name, false, true);
	// transform matrix
	boost::shared_ptr<smartcomparator::weights::LogEntNormalizeWeight> 
		weight( new smartcomparator::weights::LogEntNormalizeWeight( pMatrix));

	smartcomparator::weights::MatrixTransformer transform( weight );
	transform.Transform();
	
	// create compare function
	smartcomparator::simfunctions::LocalCosineSimilarity *cosinePtr =
				new smartcomparator::simfunctions::LocalCosineSimilarity(true, false);
	boost::shared_ptr<smartcomparator::simfunctions::LocalSimilarityFunction>
				cosine ( (smartcomparator::simfunctions::LocalSimilarityFunction*)cosinePtr	) ;
	// cluster
	clustering::ROCK::ROCK rock(pMatrix, cosine, 0.6);
	rock.StartClustering();
	// save resluts
	std::vector<int> parray;
	ClusteringBase::ClusterSetPtr clusters = rock.GetClusters();
	std::cout << "Number of clusters: " << clusters->size() << std::endl;

//	ClusteringBase::ClusterSet roots;
//	ClusteringBase::GetRoots(clusters, roots);
	
//	for (ClusteringBase::ClusterSet::iterator iter = clusters->begin(); iter != clusters->end(); ++iter)
//	{
//		std::cout << "ID: " << (*iter)->GetID();
//		if ((*iter)->GetParent() != NULL)
//		    std::cout << " parent: " << (*iter)->GetParent()->GetID();
//		else
//		    std::cout << " parent == NULL";
//		std::cout << std::endl;
//		std::cout << "Children: " << (*iter)->GetChildren().size() << std::endl;
//		for (std::vector<boost::shared_ptr<ClusteringBase::Cluster> >::iterator iter2 = (*iter)->GetChildren().begin(); iter2 != (*iter)->GetChildren().end(); ++iter2)
//			std::cout << "\tChild: " << (*iter2)->GetID() << std::endl;
//
//		std::cout << "Members: " << (*iter)->GetPointsID().size() << std::endl;
//		std::set<size_t>::iterator it = (*iter)->GetPointsID().begin();
//		for (; it != (*iter)->GetPointsID().end(); ++it)
//			std::cout << "\tMember: " << (*it) << std::endl;
//	}

	rock.CreateParentsMap();

	rock.SaveClutoTreeFile(params.output_file);

	//ClusteringBase::ToParentArray( clusters , pMatrix, parray );
	//ClusteringBase::CreateParray( clusters, parray );
	//ClusteringBase::SaveParentArray( parray, params.output_file );
	std::cout << "Saved..." << std::endl;
	return 0;
}
