/*
    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 "../comparator_stdheaders.h"
#include "SimMatrixTransformation.h"

static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("SuperMatrix.comparator.transformations.SimMatrixTransformation"));

namespace smartcomparator
{
	namespace transformations
	{
		SimMatrixTransformation::SimMatrixTransformation(Matrices::SuperMatrixPtr matrix,
			boost::shared_ptr<compare::CompareFunction>	cmpFun)
			: Transformation(matrix),			
			m_pCompareFunction(cmpFun)
		{
		}

		void SimMatrixTransformation::TransformMatrix()
		{
			LOG4CXX_INFO(logger, "Transforming matrix (SimMatrix)...");
			// create new matrix

			Matrices::CRSMatrixPtr crs = Matrices::CRSMatrixPtr(new Matrices::CRSMatrix(m_pMatrix->GetName(), 
						m_pMatrix->NumberOfRows(),m_pMatrix->NumberOfRows() ) );

			LOG4CXX_INFO(logger,"CRS: " << crs->GetName());

			// for every word

			for( size_t rowNo = 0; rowNo < crs->NumberOfRows(); rowNo++ )
			{
				Matrices::CompressedVectorPtr v = crs->GetCompressedRow(rowNo);
				// for every column
				for( size_t secondRowNo = 0; secondRowNo <crs->NumberOfRows() ; secondRowNo++ )
				{
					double sim = m_pCompareFunction->Compare(rowNo, secondRowNo);

					if(  !(sim <= 0 || sim > 0) )
						sim = 0;

					v->SetElement(secondRowNo, sim);

				}

				LOG4CXX_INFO(logger, "Row " << rowNo << " / " <<crs->NumberOfRows());;
			}

			LOG4CXX_INFO(logger, "Weighting done... coping data to SuperMatrix...");

			// move to m_Matrix, rember about USAGE,

			m_pMatrix->SetCRS(crs);
			LOG4CXX_INFO(logger, "Syncing CCS matrix...");
			m_pMatrix->SyncCRSToCCS();

			m_pMatrix->SetColumnFeatures(m_pMatrix->GetRowFeatures() );

			LOG4CXX_INFO(logger, "SimMatrix transformation finished...\n !!!! Feature set was untouched !!!! ");
		}


	}//namespace transformations
}//namespace smartcomparator
