BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
LookupTable.cpp
Go to the documentation of this file.
1#include "LookupTable.h"
2#include <ranges>
3#include <iostream>
4
5using namespace BPMNOS::Model;
6
7LookupTable::LookupTable(const std::string& name, const std::string& source, const std::vector<std::string>& folders)
8 : name(name)
9{
10 createMap(source,folders);
11}
12
13BPMNOS::CSVReader LookupTable::openCsv(const std::string& filename, const std::vector<std::string>& folders) {
14 // First, try to open the file using the given filename.
15 if (std::filesystem::exists(filename)) {
16 return CSVReader(filename);
17 }
18
19 // If the file is not found with the given filename, try each folder in the list.
20 for (const std::string& folder : folders) {
21 std::filesystem::path fullPath = std::filesystem::path(folder) / filename;
22 if (std::filesystem::exists(fullPath)) {
23 return CSVReader(fullPath.string());
24 }
25 }
26
27 // If the file is not found in any of the folders, throw an exception with all searched locations
28 std::string errorMsg = "LookupTable: CSV file '" + filename + "' not found in:\n" + std::filesystem::current_path().string();
29 for (const std::string& folder : folders) {
30 errorMsg += "\n" + std::filesystem::absolute(folder).string();
31 }
32 throw std::runtime_error(errorMsg);
33}
34
35void LookupTable::createMap(const std::string& source, const std::vector<std::string>& folders) {
36 BPMNOS::CSVReader reader = openCsv(source,folders);
37 CSVReader::Table table = reader.read();
38 if ( table.empty() ) {
39 throw std::runtime_error(std::format("LookupTable: table '{}' with source '{}' is empty", name, source));
40 }
41 // populate lookup map
42 for (size_t j = 1; j < table.size(); j++) { // assume a single header line at index 0
43 auto& row = table[j];
44 std::vector< double > inputs;
45 size_t columns = row.size();
46
47 for ( size_t i = 0; i < columns - 1; i++ ) {
48 auto& cell = row[i];
49 if ( !std::holds_alternative<BPMNOS::number>(cell) ) {
50 throw std::runtime_error(std::format("LookupTable: illegal input in table '{}' at row {}, column {}", name, j, i));
51 }
52//std::cerr << (double)std::get<BPMNOS::number>(cell) << ", ";
53 inputs.push_back( (double)std::get<BPMNOS::number>(cell) );
54 }
55 auto& cell = row[columns - 1];
56 if ( !std::holds_alternative<BPMNOS::number>(cell) ) {
57 std::visit([](auto&& value) { std::cerr << "Value: " << value << " "; }, cell);
58 throw std::runtime_error(std::format("LookupTable: illegal output in table '{}' at row {}, column {}", name, j, columns - 1));
59 }
60//std::cerr << "-> " << (double)std::get<BPMNOS::number>(cell) << std::endl;
61 auto result = (double)std::get<BPMNOS::number>(cell);
62 lookupMap.emplace( std::move(inputs), result );
63 }
64}
65
66double LookupTable::at( const std::vector< double >& keys ) const {
67 auto it = lookupMap.find(keys);
68 if ( it != lookupMap.end() ) {
69 return it->second;
70 }
71 throw std::runtime_error(std::format("LookupTable: keys not found in table '{}'", name));
72}
std::vector< Row > Table
Definition CSVReader.h:17
CSVReader openCsv(const std::string &filename, const std::vector< std::string > &folders)
double at(const std::vector< double > &keys) const
const std::string name
Definition LookupTable.h:44
BPMNOS::vector_map< std::vector< double >, double > lookupMap
Definition LookupTable.h:51
LookupTable(const std::string &name, const std::string &source, const std::vector< std::string > &folders)
Constructs a LookupTable object using data from a CSV file.
void createMap(const std::string &source, const std::vector< std::string > &folders)
iterator find(const Key &key)
Definition vector_map.h:138
iterator end() noexcept
Definition vector_map.h:83
std::pair< iterator, bool > emplace(const Key &key, const Value &value)
Definition vector_map.h:113