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 or handle the situation as needed.
28 throw std::runtime_error("LookupTable: CSV file '" + filename + "' not found");
29}
30
31void LookupTable::createMap(const std::string& source, const std::vector<std::string>& folders) {
32 BPMNOS::CSVReader reader = openCsv(source,folders);
33 CSVReader::Table table = reader.read();
34 if ( table.empty() ) {
35 throw std::runtime_error("LookupTable: table '" + source + "' is empty");
36 }
37 // populate lookup map
38 for (auto& row : table | std::views::drop(1)) { // assume a single header line
39 std::vector< double > inputs;
40 size_t columns = row.size();
41
42 for ( size_t i = 0; i < columns - 1; i++ ) {
43 auto& cell = row[i];
44 if ( !std::holds_alternative<BPMNOS::number>(cell) ) {
45 throw std::runtime_error("LookupTable: illegal input in table '" + source + "'");
46 }
47//std::cerr << (double)std::get<BPMNOS::number>(cell) << ", ";
48 inputs.push_back( (double)std::get<BPMNOS::number>(cell) );
49 }
50 auto& cell = row[columns - 1];
51 if ( !std::holds_alternative<BPMNOS::number>(cell) ) {
52 std::visit([](auto&& value) { std::cerr << "Value: " << value << " "; }, cell);
53 throw std::runtime_error("LookupTable: illegal output in table '" + source + "'");
54 }
55//std::cerr << "-> " << (double)std::get<BPMNOS::number>(cell) << std::endl;
56 auto result = (double)std::get<BPMNOS::number>(cell);
57 lookupMap.emplace( std::move(inputs), result );
58 }
59}
60
61double LookupTable::at( const std::vector< double >& keys ) const {
62 auto it = lookupMap.find(keys);
63 if ( it != lookupMap.end() ) {
64 return it->second;
65 }
66 throw std::runtime_error("LookupTable: keys not found in table.");
67}
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
BPMNOS::vector_map< std::vector< double >, double > lookupMap
Definition LookupTable.h:49
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:130
iterator end() noexcept
Definition vector_map.h:79
std::pair< iterator, bool > emplace(const Key &key, const Value &value)
Definition vector_map.h:109