Clean up code and docs
parent
deafbdb3a7
commit
d957d5e8f6
|
|
@ -18,7 +18,8 @@ inline static SignatureParser::Table ParseAnd() {
|
||||||
return {ParseFalseRow(), ParseFalseRow(), ParseFalseRow(), ParseTrueRow()};
|
return {ParseFalseRow(), ParseFalseRow(), ParseFalseRow(), ParseTrueRow()};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool static ParseConditional(const std::string& token, std::vector<double>& row) {
|
bool static ParseConditional(const std::string& token,
|
||||||
|
std::vector<double>& row) {
|
||||||
// Expect something like a/b/c
|
// Expect something like a/b/c
|
||||||
std::istringstream iss2(token);
|
std::istringstream iss2(token);
|
||||||
try {
|
try {
|
||||||
|
|
@ -35,7 +36,8 @@ bool static ParseConditional(const std::string& token, std::vector<double>& row)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void static ParseConditionalTable(const std::vector<std::string>& tokens, SignatureParser::Table& table) {
|
void static ParseConditionalTable(const std::vector<std::string>& tokens,
|
||||||
|
SignatureParser::Table& table) {
|
||||||
// loop over the words
|
// loop over the words
|
||||||
// for each word, split it into doubles using a stringstream
|
// for each word, split it into doubles using a stringstream
|
||||||
for (const auto& word : tokens) {
|
for (const auto& word : tokens) {
|
||||||
|
|
@ -105,8 +107,8 @@ bool SignatureParser::parse(const std::string& str, Table& table) {
|
||||||
if (table.empty()) {
|
if (table.empty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// the boost::phoenix parser did not return an error if we could not fully parse a string
|
// the boost::phoenix parser did not return an error if we could not fully
|
||||||
// it just returned whatever it could parse
|
// parse a string it just returned whatever it could parse
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} // namespace gtsam
|
} // namespace gtsam
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,19 @@
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
|
||||||
|
* Atlanta, Georgia 30332-0415
|
||||||
|
* All Rights Reserved
|
||||||
|
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
|
||||||
|
|
||||||
|
* See LICENSE for the license information
|
||||||
|
|
||||||
|
* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a simple parser that replaces the boost spirit parser. It is
|
* @file SignatureParser.h
|
||||||
* meant to parse strings like "1/1 2/3 1/4". Every word of the form "a/b/c/..."
|
* @brief Parser for conditional distribution signatures.
|
||||||
* should be parsed as a row, and the rows should be stored in a table.
|
* @author Kartik Arcot
|
||||||
* The elements of the row will be doubles.
|
* @date January 2023
|
||||||
* The table is a vector of rows. The row is a vector of doubles.
|
|
||||||
* The parser should be able to parse the following strings:
|
|
||||||
* "1/1 2/3 1/4": {{1,1},{2,3},{1,4}}
|
|
||||||
* "1/1 2/3 1/4 1/1 2/3 1/4" : {{1,1},{2,3},{1,4},{1,1},{2,3},{1,4}}
|
|
||||||
* "1/2/3 2/3/4 1/2/3 2/3/4 1/2/3 2/3/4 1/2/3 2/3/4 1/2/3" : {{1,2,3},{2,3,4},{1,2,3},{2,3,4},{1,2,3},{2,3,4},{1,2,3},{2,3,4},{1,2,3}}
|
|
||||||
* If the string has unparseable elements, the parser should parse whatever it can
|
|
||||||
* "1/2 sdf" : {{1,2}}
|
|
||||||
* It should return false if the string is empty.
|
|
||||||
* "": false
|
|
||||||
* We should return false if the rows are not of the same size.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
@ -21,6 +22,30 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
/**
|
||||||
|
* @brief A simple parser that replaces the boost spirit parser.
|
||||||
|
*
|
||||||
|
* It is meant to parse strings like "1/1 2/3 1/4". Every word of the form
|
||||||
|
* "a/b/c/..." is parsed as a row, and the rows are stored in a table.
|
||||||
|
*
|
||||||
|
* A `Row` is a vector of doubles, and a `Table` is a vector of rows.
|
||||||
|
*
|
||||||
|
* Examples: the parser is able to parse the following strings:
|
||||||
|
* "1/1 2/3 1/4":
|
||||||
|
* {{1,1},{2,3},{1,4}}
|
||||||
|
* "1/1 2/3 1/4 1/1 2/3 1/4" :
|
||||||
|
* {{1,1},{2,3},{1,4},{1,1},{2,3},{1,4}}
|
||||||
|
* "1/2/3 2/3/4 1/2/3 2/3/4 1/2/3 2/3/4 1/2/3 2/3/4 1/2/3" :
|
||||||
|
* {{1,2,3},{2,3,4},{1,2,3},{2,3,4},{1,2,3},{2,3,4},{1,2,3},{2,3,4},{1,2,3}}
|
||||||
|
*
|
||||||
|
* If the string has un-parsable elements, should parse whatever it can:
|
||||||
|
* "1/2 sdf" : {{1,2}}
|
||||||
|
*
|
||||||
|
* It should return false if the string is empty:
|
||||||
|
* "": false
|
||||||
|
*
|
||||||
|
* We should return false if the rows are not of the same size.
|
||||||
|
*/
|
||||||
namespace SignatureParser {
|
namespace SignatureParser {
|
||||||
typedef std::vector<double> Row;
|
typedef std::vector<double> Row;
|
||||||
typedef std::vector<Row> Table;
|
typedef std::vector<Row> Table;
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,16 @@
|
||||||
* @file testSimpleParser.cpp
|
* @file testSimpleParser.cpp
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <CppUnitLite/TestHarness.h>
|
||||||
#include <gtsam/base/TestableAssertions.h>
|
#include <gtsam/base/TestableAssertions.h>
|
||||||
#include <gtsam/discrete/SignatureParser.h>
|
#include <gtsam/discrete/SignatureParser.h>
|
||||||
#include <CppUnitLite/TestHarness.h>
|
|
||||||
|
|
||||||
bool compare_tables(const gtsam::SignatureParser::Table& table1,
|
using namespace gtsam;
|
||||||
const gtsam::SignatureParser::Table& table2) {
|
|
||||||
|
/* ************************************************************************* */
|
||||||
|
// Simple test case
|
||||||
|
bool compareTables(const SignatureParser::Table& table1,
|
||||||
|
const SignatureParser::Table& table2) {
|
||||||
if (table1.size() != table2.size()) {
|
if (table1.size() != table2.size()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -25,72 +29,79 @@ bool compare_tables(const gtsam::SignatureParser::Table& table1,
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************* */
|
||||||
// Simple test case
|
// Simple test case
|
||||||
TEST(SimpleParser, simple) {
|
TEST(SimpleParser, Simple) {
|
||||||
gtsam::SignatureParser::Table table, expectedTable;
|
SignatureParser::Table table, expectedTable;
|
||||||
expectedTable = {{1, 1}, {2, 3}, {1, 4}};
|
expectedTable = {{1, 1}, {2, 3}, {1, 4}};
|
||||||
bool ret = gtsam::SignatureParser::parse("1/1 2/3 1/4", table);
|
bool ret = SignatureParser::parse("1/1 2/3 1/4", table);
|
||||||
EXPECT(ret);
|
EXPECT(ret);
|
||||||
// compare the tables
|
// compare the tables
|
||||||
EXPECT(compare_tables(table, expectedTable));
|
EXPECT(compareTables(table, expectedTable));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************* */
|
||||||
// Test case with each row having 3 elements
|
// Test case with each row having 3 elements
|
||||||
TEST(SimpleParser, three_elements) {
|
TEST(SimpleParser, ThreeElements) {
|
||||||
gtsam::SignatureParser::Table table, expectedTable;
|
SignatureParser::Table table, expectedTable;
|
||||||
expectedTable = {{1, 1, 1}, {2, 3, 2}, {1, 4, 3}};
|
expectedTable = {{1, 1, 1}, {2, 3, 2}, {1, 4, 3}};
|
||||||
bool ret = gtsam::SignatureParser::parse("1/1/1 2/3/2 1/4/3", table);
|
bool ret = SignatureParser::parse("1/1/1 2/3/2 1/4/3", table);
|
||||||
EXPECT(ret);
|
EXPECT(ret);
|
||||||
// compare the tables
|
// compare the tables
|
||||||
EXPECT(compare_tables(table, expectedTable));
|
EXPECT(compareTables(table, expectedTable));
|
||||||
}
|
}
|
||||||
|
|
||||||
// A test case to check if we can parse a signarue with 'T' and 'F'
|
/* ************************************************************************* */
|
||||||
TEST(SimpleParser, TandF) {
|
// A test case to check if we can parse a signature with 'T' and 'F'
|
||||||
gtsam::SignatureParser::Table table, expectedTable;
|
TEST(SimpleParser, TAndF) {
|
||||||
|
SignatureParser::Table table, expectedTable;
|
||||||
expectedTable = {{1, 0}, {1, 0}, {1, 0}, {0, 1}};
|
expectedTable = {{1, 0}, {1, 0}, {1, 0}, {0, 1}};
|
||||||
bool ret = gtsam::SignatureParser::parse("F F F T", table);
|
bool ret = SignatureParser::parse("F F F T", table);
|
||||||
EXPECT(ret);
|
EXPECT(ret);
|
||||||
// compare the tables
|
// compare the tables
|
||||||
EXPECT(compare_tables(table, expectedTable));
|
EXPECT(compareTables(table, expectedTable));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************* */
|
||||||
// A test to parse {F F F 1}
|
// A test to parse {F F F 1}
|
||||||
TEST(SimpleParser, FFF1) {
|
TEST(SimpleParser, FFF1) {
|
||||||
gtsam::SignatureParser::Table table, expectedTable;
|
SignatureParser::Table table, expectedTable;
|
||||||
expectedTable = {{1, 0}, {1, 0}, {1, 0}};
|
expectedTable = {{1, 0}, {1, 0}, {1, 0}};
|
||||||
// should ignore the last 1
|
// should ignore the last 1
|
||||||
bool ret = gtsam::SignatureParser::parse("F F F 1", table);
|
bool ret = SignatureParser::parse("F F F 1", table);
|
||||||
EXPECT(ret);
|
EXPECT(ret);
|
||||||
// compare the tables
|
// compare the tables
|
||||||
EXPECT(compare_tables(table, expectedTable));
|
EXPECT(compareTables(table, expectedTable));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************* */
|
||||||
// Expect false if the string is empty
|
// Expect false if the string is empty
|
||||||
TEST(SimpleParser, empty_string) {
|
TEST(SimpleParser, emptyString) {
|
||||||
gtsam::SignatureParser::Table table;
|
SignatureParser::Table table;
|
||||||
bool ret = gtsam::SignatureParser::parse("", table);
|
bool ret = SignatureParser::parse("", table);
|
||||||
EXPECT(!ret);
|
EXPECT(!ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************* */
|
||||||
// Expect false if jibberish
|
// Expect false if gibberish
|
||||||
TEST(SimpleParser, jibberish) {
|
TEST(SimpleParser, Gibberish) {
|
||||||
gtsam::SignatureParser::Table table;
|
SignatureParser::Table table;
|
||||||
bool ret = gtsam::SignatureParser::parse("sdf 22/3", table);
|
bool ret = SignatureParser::parse("sdf 22/3", table);
|
||||||
EXPECT(!ret);
|
EXPECT(!ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If jibberish is in the middle, it should still parse the rest
|
// If Gibberish is in the middle, it should still parse the rest
|
||||||
TEST(SimpleParser, jibberish_in_middle) {
|
TEST(SimpleParser, GibberishInMiddle) {
|
||||||
gtsam::SignatureParser::Table table, expectedTable;
|
SignatureParser::Table table, expectedTable;
|
||||||
expectedTable = {{1, 1}, {2, 3}};
|
expectedTable = {{1, 1}, {2, 3}};
|
||||||
bool ret = gtsam::SignatureParser::parse("1/1 2/3 sdf 1/4", table);
|
bool ret = SignatureParser::parse("1/1 2/3 sdf 1/4", table);
|
||||||
EXPECT(ret);
|
EXPECT(ret);
|
||||||
// compare the tables
|
// compare the tables
|
||||||
EXPECT(compare_tables(table, expectedTable));
|
EXPECT(compareTables(table, expectedTable));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************* */
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
TestResult tr;
|
TestResult tr;
|
||||||
return TestRegistry::runAllTests(tr);
|
return TestRegistry::runAllTests(tr);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue