#14535 Orion events: Validate keyword field names

This commit is contained in:
Kristian Bendiksen committed 2026-08-19 10:28:35 +02:00
1 parent a3bd47b365
commit 98147c5c9a
10 files changed
+193 -9

No files matched your search

+1
View File
@@ -442,6 +442,7 @@ set(UNITY_EXCLUDE_FILES
# https://cmake.org/cmake/help/latest/prop_tgt/AUTOGEN_BUILD_DIR.html
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_autogen/mocs_compilation.cpp
ProjectDataModel/RiaOpmParserTools.cpp
ProjectDataModel/RiaOpmKeywordTools.cpp
FileInterface/RifOsduWellPathReader.cpp
FileInterface/RifOsduWellLogReader.cpp
FileInterface/RifByteArrayArrowRandomAccessFile.cpp
@@ -99,6 +99,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RimColorLegend.cpp
${CMAKE_CURRENT_LIST_DIR}/RimColorLegendItem.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaOpmParserTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaOpmKeywordTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RimCaseDisplayNameTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RimCustomObjectiveFunctionCollection.cpp
${CMAKE_CURRENT_LIST_DIR}/RimCustomObjectiveFunction.cpp
@@ -0,0 +1,74 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2026- 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 "RiaOpmKeywordTools.h"
#include "opm/input/eclipse/Parser/Parser.hpp"
#include "opm/input/eclipse/Parser/ParserItem.hpp"
#include "opm/input/eclipse/Parser/ParserKeyword.hpp"
#include "opm/input/eclipse/Parser/ParserRecord.hpp"
#include <iterator>
#include <set>
namespace
{
const Opm::Parser& sharedParser()
{
static const Opm::Parser parser( true );
return parser;
}
} // namespace
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::optional<RiaOpmKeywordInfo> RiaOpmKeywordTools::keywordInfo( const QString& keywordName )
{
const QString keyword = keywordName.toUpper();
const auto& parser = sharedParser();
const auto deckName = keyword.toStdString();
if ( !parser.isRecognizedKeyword( deckName ) ) return std::nullopt;
const auto& parserKeyword = parser.getParserKeywordFromDeckName( deckName );
RiaOpmKeywordInfo info;
info.name = QString::fromStdString( parserKeyword.getName() );
const auto recordCount = static_cast<size_t>( std::distance( parserKeyword.begin(), parserKeyword.end() ) );
if ( recordCount == 1 )
{
const auto& record = parserKeyword.getRecord( 0 );
info.acceptsArbitraryItems = record.size() == 1 && record.get( 0 ).sizeType() == Opm::ParserItem::item_size::ALL;
}
std::set<std::string> seenNames;
for ( const auto& record : parserKeyword )
{
for ( const auto& item : record )
{
if ( seenNames.insert( item.name() ).second )
{
info.itemNames.push_back( QString::fromStdString( item.name() ) );
}
}
}
return info;
}
@@ -0,0 +1,36 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2026- 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.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <QString>
#include <optional>
#include <vector>
struct RiaOpmKeywordInfo
{
QString name;
std::vector<QString> itemNames;
bool acceptsArbitraryItems = false;
};
namespace RiaOpmKeywordTools
{
std::optional<RiaOpmKeywordInfo> keywordInfo( const QString& keywordName );
} // namespace RiaOpmKeywordTools
@@ -19,6 +19,7 @@
#include "RimcWellEventTimeline.h"
#include "CompletionExportCommands/RicScheduleDataGenerator.h"
#include "RiaOpmKeywordTools.h"
#include "RimEclipseCase.h"
#include "RimKeywordEvent.h"
#include "RimWellEventControl.h"
@@ -36,10 +37,45 @@
#include "cafPdmFieldScriptingCapability.h"
#include <QDateTime>
#include <QStringList>
#include <algorithm>
#include <set>
namespace
{
std::expected<void, QString> validateKeywordItems( const QString& keywordName, const std::vector<QString>& itemNames )
{
auto keywordInfo = RiaOpmKeywordTools::keywordInfo( keywordName );
if ( !keywordInfo )
{
return std::unexpected( QString( "Keyword '%1' is not recognized by opm-common." ).arg( keywordName ) );
}
if ( keywordInfo->acceptsArbitraryItems ) return {};
QStringList invalidNames;
for ( const auto& itemName : itemNames )
{
if ( std::find( keywordInfo->itemNames.begin(), keywordInfo->itemNames.end(), itemName ) == keywordInfo->itemNames.end() )
{
invalidNames.push_back( itemName );
}
}
if ( invalidNames.empty() ) return {};
QStringList validNames;
for ( const auto& itemName : keywordInfo->itemNames )
{
validNames.push_back( itemName );
}
return std::unexpected( QString( "Keyword '%1' contains invalid item names: %2. Valid item names are: %3." )
.arg( keywordName.toUpper(), invalidNames.join( ", " ), validNames.join( ", " ) ) );
}
} // namespace
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimWellEventTimeline, RimcWellEventTimeline_addPerfEvent, "AddPerfEvent" );
//--------------------------------------------------------------------------------------------------
@@ -373,6 +409,12 @@ std::expected<caf::PdmObjectHandle*, QString> RimcWellEventTimeline_addWellKeywo
return std::unexpected( QString( "Item arrays must have same length" ) );
}
auto validationResult = validateKeywordItems( m_keywordName(), m_itemNames() );
if ( !validationResult )
{
return std::unexpected( validationResult.error() );
}
// Create event
auto* event = timeline->addWellKeywordEvent( m_wellPath(), date, m_keywordName() );
@@ -469,6 +511,12 @@ std::expected<caf::PdmObjectHandle*, QString> RimcWellEventTimeline_addKeywordEv
return std::unexpected( QString( "Item arrays must have same length" ) );
}
auto validationResult = validateKeywordItems( m_keywordName(), m_itemNames() );
if ( !validationResult )
{
return std::unexpected( validationResult.error() );
}
// Create event (note: no wellPath parameter)
auto* event = timeline->addKeywordEvent( date, m_keywordName() );