ResInsight/ApplicationLibCode/Application/Tools/RiaEnsembleNameTools.cpp

162 lines
5.3 KiB
C++
Raw Normal View History

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2021 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight 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 GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaEnsembleNameTools.h"
#include "RiaFilePathTools.h"
#include "RimCaseDisplayNameTools.h"
#include <QFileInfo>
#include <QRegularExpression>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaEnsembleNameTools::findSuitableEnsembleName( const QStringList& fileNames )
{
std::vector<QStringList> componentsForAllFilePaths;
for ( const auto& filePath : fileNames )
{
QStringList components = RiaFilePathTools::splitPathIntoComponents( filePath );
componentsForAllFilePaths.push_back( components );
}
// Find list of all folders inside a folder matching realization-*
QRegularExpression realizationRe( "realization\\-\\d+" );
QStringList iterations;
for ( const auto& fileComponents : componentsForAllFilePaths )
{
QString lastComponent = "";
for ( auto it = fileComponents.rbegin(); it != fileComponents.rend(); ++it )
{
if ( realizationRe.match( *it ).hasMatch() )
{
iterations.push_back( lastComponent );
}
lastComponent = *it;
}
}
iterations.removeDuplicates();
if ( iterations.size() == 1u )
{
return iterations.front();
}
if ( !iterations.empty() )
{
return QString( "Multiple iterations: %1" ).arg( iterations.join( ", " ) );
}
QString root = RiaFilePathTools::commonRootOfFileNames( fileNames );
QRegularExpression trimRe( "[^a-zA-Z0-9]+$" );
QString trimmedRoot = root.replace( trimRe, "" );
if ( trimmedRoot.length() >= 4 )
{
return trimmedRoot;
}
return "Ensemble";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaEnsembleNameTools::findCommonBaseName( const QStringList& fileNames )
{
QStringList baseNames;
for ( const auto& f : fileNames )
{
QFileInfo fi( f );
baseNames.push_back( fi.baseName() );
}
if ( baseNames.isEmpty() ) return "Empty";
auto firstName = baseNames.front();
for ( int i = 0; i < firstName.size(); i++ )
{
auto candidate = firstName.left( firstName.size() - i );
bool identicalNames = true;
for ( const auto& baseName : baseNames )
{
auto str = baseName.left( firstName.size() - i );
if ( candidate != str ) identicalNames = false;
}
if ( identicalNames )
{
return candidate;
}
}
return "Mixed Items";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaEnsembleNameTools::uniqueShortName( const QString& sourceFileName,
const QStringList& allFileNames,
const QString& ensembleCaseName )
{
QRegularExpression trimRe( "^[^a-zA-Z0-9]+" );
std::map<QString, QStringList> keyFileComponentsForAllFiles =
RiaFilePathTools::keyPathComponentsForEachFilePath( allFileNames );
QStringList keyFileComponents = keyFileComponentsForAllFiles[sourceFileName];
if ( keyFileComponents.empty() ) return "Unnamed";
if ( !ensembleCaseName.isEmpty() )
{
for ( auto& component : keyFileComponents )
{
component = component.replace( ensembleCaseName, "" );
component = component.replace( trimRe, "" );
}
}
QStringList shortNameComponents;
QRegularExpression numberRe( "[0-9]+" );
for ( auto keyComponent : keyFileComponents )
{
QStringList subComponents;
QString numberGroup = numberRe.match( keyComponent ).captured();
if ( !numberGroup.isEmpty() )
{
keyComponent = keyComponent.replace( numberGroup, "" );
QString stem = keyComponent.left( RimCaseDisplayNameTools::CASE_SHORT_NAME_LENGTH );
if ( !stem.isEmpty() ) subComponents.push_back( stem );
subComponents.push_back( numberGroup );
}
else
{
subComponents.push_back( keyComponent.left( RimCaseDisplayNameTools::CASE_SHORT_NAME_LENGTH ) );
}
shortNameComponents.push_back( subComponents.join( "-" ) );
}
return shortNameComponents.join( "," );
}