Skip to content
Snippets Groups Projects
AlgorithmRegistry.cpp 10.1 KiB
Newer Older
/*
 * AlgorithmRegistry.cpp
 *
 *  Created on: 19. 8. 2017
 *	  Author: Jan Travnicek
 */

#include <registry/AlgorithmRegistry.hpp>
#include <registry/CastRegistry.hpp>
#include <alib/foreach>
#include <alib/typeinfo>

namespace abstraction {

ext::map < ext::pair < std::string, ext::vector < std::string > >, ext::list < std::unique_ptr < AlgorithmRegistry::Entry > > > & AlgorithmRegistry::getEntries ( ) {
	static ext::map < ext::pair < std::string, ext::vector < std::string > >, ext::list < std::unique_ptr < Entry > > > algorithmGroups;
bool AlgorithmRegistry::isRegistered ( const std::string & algorithm, const ext::vector < std::string > & templateParams, const AlgorithmBaseInfo & entryInfo ) {
	auto & group = getEntries ( ) [ ext::tie ( algorithm, templateParams ) ];
	for ( const std::unique_ptr < Entry > & entry : group )
		if ( entry->getEntryInfo ( ).getCategory ( ) == entryInfo.getCategory ( ) && entry->getEntryInfo ( ).getParams ( ) == entryInfo.getParams ( ) )
			return true;

	return false;
}

void AlgorithmRegistry::registerInternal ( std::string algorithm, ext::vector < std::string > templateParams, std::unique_ptr < Entry > value ) {
	if ( isRegistered ( algorithm, templateParams, value->getEntryInfo ( ) ) )
		throw std::invalid_argument ( "Callback for " + algorithm + " with params " + ext::to_string ( value->getEntryInfo ( ).getParams ( ) ) + " already registered." );
	auto & group = getEntries ( ) [ ext::make_pair ( std::move ( algorithm ), std::move ( templateParams ) ) ];
	group.insert ( group.end ( ), std::move ( value ) );
void AlgorithmRegistry::unregisterInternal ( const std::string & algorithm, const ext::vector < std::string > & templateParams, const AlgorithmBaseInfo & entryInfo ) {
	auto & group = getEntries ( ) [ ext::tie ( algorithm, templateParams ) ];
	auto iter = find_if ( group.begin ( ), group.end ( ), [ & ] ( const std::unique_ptr < Entry > & entry ) {
				return entry->getEntryInfo ( ).getCategory ( ) == entryInfo.getCategory ( ) && entry->getEntryInfo ( ).getParams ( ) == entryInfo.getParams ( );
	if ( iter == group.end ( ) ) {
		if ( templateParams.empty ( ) )
			throw std::invalid_argument ( "Entry " + algorithm + " with parameters " + ext::to_string ( entryInfo.getParams ( ) ) + " not registered." );
			throw std::invalid_argument ( "Templated entry " + algorithm + " < " + ext::to_string ( templateParams ) + " > with parameters " + ext::to_string ( entryInfo.getParams ( ) ) + " not registered." );
	}
	group.erase ( iter );
	if ( group.empty ( ) )
		getEntries ( ).erase ( ext::tie ( algorithm, templateParams ) );
void AlgorithmRegistry::setDocumentation ( const std::string & algorithm, const ext::vector < std::string > & templateParams, const AlgorithmBaseInfo & entryInfo, std::string documentation ) {
	auto & group = getEntries ( ) [ ext::tie ( algorithm, templateParams ) ];
	auto iter = find_if ( group.begin ( ), group.end ( ), [ & ] ( const std::unique_ptr < Entry > & entry ) {
				return entry->getEntryInfo ( ).getCategory ( ) == entryInfo.getCategory ( ) && entry->getEntryInfo ( ).getParams ( ) == entryInfo.getParams ( );
		if ( templateParams.empty ( ) )
			throw std::invalid_argument ( "Entry " + algorithm + " with parameters " + ext::to_string ( entryInfo.getParams ( ) ) + " not registered." );
			throw std::invalid_argument ( "Templated entry " + algorithm + " < " + ext::to_string ( templateParams ) + " > with parameters " + ext::to_string ( entryInfo.getParams ( ) ) + " not registered." );
	}
	(*iter)->setDocumentation ( std::move ( documentation ) );
}

ext::map < ext::pair < std::string, ext::vector < std::string > >, ext::list < std::unique_ptr < AlgorithmRegistry::Entry > > >::const_iterator AlgorithmRegistry::findAbstractionGroup ( const std::string & name, const ext::vector < std::string > & templateParams ) {
	auto group = getEntries ( ).find ( ext::tie ( name, templateParams ) );
	if ( group == getEntries ( ).end ( ) ) {
		for ( auto iter = getEntries ( ).begin ( ); iter != getEntries ( ).end ( ); ++ iter ) {
			if ( ext::is_same_type ( name, iter->first.first ) && ext::are_same_types ( templateParams, iter->first.second ) ) {
				if ( group == getEntries ( ).end ( ) )
					group = iter;
				else if ( templateParams.empty ( ) )
					throw std::invalid_argument ( "Name " + name + " is ambigous " );
				else
					throw std::invalid_argument ( "Templated name " + name + " < " + ext::to_string ( templateParams ) + " > is ambigous " );
	if ( group == getEntries ( ).end ( ) ) {
		if ( templateParams.empty ( ) )
			throw std::invalid_argument ( "Entry " + name + " not available" );
		else
			throw std::invalid_argument ( "Templated entry " + name + " < " + ext::to_string ( templateParams ) + " > not available" );
	}
std::shared_ptr < abstraction::OperationAbstraction > AlgorithmRegistry::getAbstraction ( const std::string & name, const ext::vector < std::string > & templateParams, const ext::vector < std::string > & paramTypes, const ext::vector < ext::set < abstraction::ParamQualifiers::ParamQualifier > > &, AlgorithmCategories::AlgorithmCategory category ) {
	auto group = findAbstractionGroup ( name, templateParams );
	auto incompatibleLambda = [ ] ( MatchType compatibility ) {
		return compatibility == MatchType::INCOMPATIBLE;
	};

	// determine how one can actually map what we have ( paramTypes ) as params to what is available as overloads ( group->second )
	std::vector < std::pair < ext::vector < MatchType >, Entry * > > compatibilityData;
	for ( const std::unique_ptr < Entry > & entry : group->second ) {
		if ( entry->getEntryInfo ( ).getCategory ( ) != category && AlgorithmCategories::AlgorithmCategory::NONE != category )
			continue;

		if ( entry->getEntryInfo ( ).getParams ( ).size ( ) != paramTypes.size ( ) )
		ext::vector < MatchType > compatibilityVector;
		for ( size_t i = 0; i < paramTypes.size ( ); ++ i ) {
			MatchType matchType;
			if ( std::get < 0 > ( entry->getEntryInfo ( ).getParams ( ) [ i ] ) == paramTypes [ i ] ) {
				matchType = MatchType::EXACT;
			} else if ( abstraction::CastRegistry::castAvailable ( std::get < 0 > ( entry->getEntryInfo ( ).getParams ( ) [ i ] ), paramTypes [ i ], true ) ) {
				matchType = MatchType::CAST;
			} else {
				matchType = MatchType::INCOMPATIBLE;
			}

			compatibilityVector.push_back ( matchType );
		}

		// clear incompatibilities are fitered out
		if ( std::none_of ( compatibilityVector.begin ( ), compatibilityVector.end ( ), incompatibleLambda ) )
Jan Travnicek's avatar
Jan Travnicek committed
			compatibilityData.emplace_back ( std::move ( compatibilityVector ), entry.get ( ) );
	}

	// remaining compatible overloads are examined per parameter and the best option is remembered as overload index that achieved it
	ext::vector < ext::set < unsigned > > winnerList;
	for ( size_t i = 0; i < paramTypes.size ( ); ++ i ) {
		ext::set < unsigned > best;

		unsigned overload = 0;
		for ( const std::pair < ext::vector < MatchType >, Entry * > & data : compatibilityData ) {
			if ( data.first [ i ] == MatchType::EXACT )
				best.insert ( overload );

			++ overload;
		}

		if ( !best.empty ( ) ) {
			winnerList.push_back ( std::move ( best ) );
			continue;
		}

		overload = 0;
		for ( const std::pair < ext::vector < MatchType >, Entry * > & data : compatibilityData ) {
			if ( data.first [ i ] == MatchType::CAST )
				best.insert ( overload );

			++ overload;
		}

		winnerList.push_back ( std::move ( best ) );
	}

	// intersection of everything together finds indexes which are better or of the same quality for all params over all overloads
	ext::set < unsigned > winner { ext::sequence < unsigned > ( 0 ).begin ( ), ext::sequence < unsigned > ( compatibilityData.size ( ) ).end ( ) };
Jan Trávníček's avatar
Jan Trávníček committed
	for ( const ext::set < unsigned > & best : winnerList ) {
		ext::set < unsigned > filtered;
		std::set_intersection ( winner.begin ( ), winner.end ( ), best.begin ( ), best.end ( ), std::inserter ( filtered, filtered.end ( ) ) );
		winner = std::move ( filtered );
	}

	// if there is a sinle winner, return it
	if ( winner.size ( ) == 1 ) {
		return compatibilityData [ * winner.begin ( ) ].second->getAbstraction ( );
	} else if ( winner.size ( ) > 1 ) {
//		throw std::invalid_argument ( "Entry overload " + ext::to_string ( paramTypes ) + " ambiguous." ); FIXME make better overload select algorithm
		return compatibilityData [ * winner.begin ( ) ].second->getAbstraction ( );
/*		if(common::GlobalData::verbose)
			common::Streams::err << "Entry overload " + ss.str ( ) + " ambiguous. First selected." << std::endl;*/
		if ( templateParams.empty ( ) )
			throw std::invalid_argument ( "Entry overload " + name + " " + ext::to_string ( paramTypes ) + " not available" );
		else
			throw std::invalid_argument ( "Templated entry overload " + name + " < " + ext::to_string ( templateParams ) + " > " + ext::to_string ( paramTypes ) + " not available" );
ext::set < ext::pair < std::string, ext::vector < std::string > > > AlgorithmRegistry::listGroup ( const std::string & group ) {
	ext::set < ext::pair < std::string, ext::vector < std::string > > > res;
	for ( const std::pair < const ext::pair < std::string, ext::vector < std::string > >, ext::list < std::unique_ptr < Entry > > > & entry : getEntries ( ) )
		if ( entry.first.first.find ( group ) == 0 ) //found at the begining
			res.insert ( entry.first );

	return res;
ext::list < ext::tuple < AlgorithmFullInfo, std::string > > AlgorithmRegistry::listOverloads ( const std::string & algorithm, const ext::vector < std::string > & templateParams ) {
	auto group = findAbstractionGroup ( algorithm, templateParams );
	ext::list < ext::tuple < AlgorithmFullInfo, std::string > > res;
	for ( const std::unique_ptr < Entry > & overloads : group->second )
		res.push_back ( ext::make_tuple ( overloads->getEntryInfo ( ), overloads->getDocumentation ( ) ) );
ext::set < ext::pair < std::string, ext::vector < std::string > > > AlgorithmRegistry::list ( ) {
	ext::set < ext::pair < std::string, ext::vector < std::string > > > res;
	for ( const std::pair < const ext::pair < std::string, ext::vector < std::string > >, ext::list < std::unique_ptr < Entry > > > & entry : getEntries ( ) )
		res.insert ( entry.first );
}

} /* namespace abstraction */