SignatureParser.cpp
Go to the documentation of this file.
2 
3 #include <algorithm>
4 #include <iterator>
5 #include <optional>
6 #include <sstream>
7 namespace gtsam {
8 
9 using Row = std::vector<double>;
10 using Table = std::vector<Row>;
11 
12 inline static Row ParseTrueRow() { return {0, 1}; }
13 
14 inline static Row ParseFalseRow() { return {1, 0}; }
15 
16 inline static Table ParseOr() {
18 }
19 
20 inline static Table ParseAnd() {
22 }
23 
24 std::optional<Row> static ParseConditional(const std::string& token) {
25  // Expect something like a/b/c
26  std::istringstream iss2(token);
27  Row row;
28  try {
29  // if the string has no / then return std::nullopt
30  if (std::count(token.begin(), token.end(), '/') == 0) return std::nullopt;
31  // split the word on the '/' character
32  for (std::string s; std::getline(iss2, s, '/');) {
33  // can throw exception
34  row.push_back(std::stod(s));
35  }
36  // if we ended with a '/' then return false
37  if (token.back() == '/') return std::nullopt;
38  } catch (...) {
39  return std::nullopt;
40  }
41  return std::move(row);
42 }
43 
44 std::optional<Table> static ParseConditionalTable(
45  const std::vector<std::string>& tokens) {
46  Table table;
47  // loop over the words
48  // for each word, split it into doubles using a stringstream
49  for (const auto& word : tokens) {
50  // If the string word is F or T then the row is {0,1} or {1,0} respectively
51  if (word == "F") {
52  table.push_back(ParseFalseRow());
53  } else if (word == "T") {
54  table.push_back(ParseTrueRow());
55  } else {
56  // Expect something like a/b/c
57  if (auto row = ParseConditional(word)) {
58  table.push_back(*row);
59  } else {
60  // stop parsing if we encounter an error
61  return std::nullopt;
62  }
63  }
64  }
65  return std::move(table);
66 }
67 
68 std::vector<std::string> static Tokenize(const std::string& str) {
69  std::istringstream iss(str);
70  std::vector<std::string> tokens;
71  for (std::string s; iss >> s;) {
72  tokens.push_back(s);
73  }
74  return tokens;
75 }
76 
77 std::optional<Table> SignatureParser::Parse(const std::string& str) {
78  // check if string is just whitespace
79  if (std::all_of(str.begin(), str.end(), isspace)) {
80  return std::nullopt;
81  }
82 
83  // return std::nullopt if the string is empty
84  if (str.empty()) {
85  return std::nullopt;
86  }
87 
88  // tokenize the str on whitespace
89  std::vector<std::string> tokens = Tokenize(str);
90 
91  // if the first token is "OR", return the OR table
92  if (tokens[0] == "OR") {
93  // if there are more tokens, return std::nullopt
94  if (tokens.size() > 1) {
95  return std::nullopt;
96  }
97  return ParseOr();
98  }
99 
100  // if the first token is "AND", return the AND table
101  if (tokens[0] == "AND") {
102  // if there are more tokens, return std::nullopt
103  if (tokens.size() > 1) {
104  return std::nullopt;
105  }
106  return ParseAnd();
107  }
108 
109  // otherwise then parse the conditional table
110  auto table = ParseConditionalTable(tokens);
111  // return std::nullopt if the table is empty
112  if (!table || table->empty()) {
113  return std::nullopt;
114  }
115  // the boost::phoenix parser did not return an error if we could not fully
116  // parse a string it just returned whatever it could parse
117  return table;
118 }
119 } // namespace gtsam
all_of
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... > > all_of
Definition: wrap/pybind11/include/pybind11/detail/common.h:740
s
RealScalar s
Definition: level1_cplx_impl.h:126
SignatureParser.h
Parser for conditional distribution signatures.
gtsam::Row
std::vector< double > Row
Definition: SignatureParser.cpp:9
gtsam::ParseFalseRow
static Row ParseFalseRow()
Definition: SignatureParser.cpp:14
gtsam::ParseConditional
static std::optional< Row > ParseConditional(const std::string &token)
Definition: SignatureParser.cpp:24
gtsam::ParseTrueRow
static Row ParseTrueRow()
Definition: SignatureParser.cpp:12
table
ArrayXXf table(10, 4)
gtsam::row
const MATRIX::ConstRowXpr row(const MATRIX &A, size_t j)
Definition: base/Matrix.h:221
gtsam::Table
std::vector< Row > Table
Definition: SignatureParser.cpp:10
str
Definition: pytypes.h:1558
gtsam
traits
Definition: SFMdata.h:40
gtsam::ParseAnd
static Table ParseAnd()
Definition: SignatureParser.cpp:20
gtsam::ParseOr
static Table ParseOr()
Definition: SignatureParser.cpp:16
gtsam::ParseConditionalTable
static std::optional< Table > ParseConditionalTable(const std::vector< std::string > &tokens)
Definition: SignatureParser.cpp:44
gtsam::SignatureParser::Parse
static std::optional< Table > Parse(const std::string &str)
Definition: SignatureParser.cpp:77
gtsam::Tokenize
static std::vector< std::string > Tokenize(const std::string &str)
Definition: SignatureParser.cpp:68


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:04:12