Simplified parsing as we moved on from this boost version
parent
f59342882a
commit
fd7640b1b7
|
@ -38,19 +38,7 @@ namespace gtsam {
|
||||||
using boost::phoenix::push_back;
|
using boost::phoenix::push_back;
|
||||||
|
|
||||||
// Special rows, true and false
|
// Special rows, true and false
|
||||||
Signature::Row createF() {
|
Signature::Row F{1, 0}, T{0, 1};
|
||||||
Signature::Row r(2);
|
|
||||||
r[0] = 1;
|
|
||||||
r[1] = 0;
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
Signature::Row createT() {
|
|
||||||
Signature::Row r(2);
|
|
||||||
r[0] = 0;
|
|
||||||
r[1] = 1;
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
Signature::Row T = createT(), F = createF();
|
|
||||||
|
|
||||||
// Special tables (inefficient, but do we care for user input?)
|
// Special tables (inefficient, but do we care for user input?)
|
||||||
Signature::Table logic(bool ff, bool ft, bool tf, bool tt) {
|
Signature::Table logic(bool ff, bool ft, bool tf, bool tt) {
|
||||||
|
@ -69,40 +57,13 @@ namespace gtsam {
|
||||||
table = or_ | and_ | rows;
|
table = or_ | and_ | rows;
|
||||||
or_ = qi::lit("OR")[qi::_val = logic(false, true, true, true)];
|
or_ = qi::lit("OR")[qi::_val = logic(false, true, true, true)];
|
||||||
and_ = qi::lit("AND")[qi::_val = logic(false, false, false, true)];
|
and_ = qi::lit("AND")[qi::_val = logic(false, false, false, true)];
|
||||||
rows = +(row | true_ | false_); // only loads first of the rows under boost 1.42
|
rows = +(row | true_ | false_);
|
||||||
row = qi::double_ >> +("/" >> qi::double_);
|
row = qi::double_ >> +("/" >> qi::double_);
|
||||||
true_ = qi::lit("T")[qi::_val = T];
|
true_ = qi::lit("T")[qi::_val = T];
|
||||||
false_ = qi::lit("F")[qi::_val = F];
|
false_ = qi::lit("F")[qi::_val = F];
|
||||||
}
|
}
|
||||||
} grammar;
|
} grammar;
|
||||||
|
|
||||||
// Create simpler parsing function to avoid the issue of only parsing a single row
|
|
||||||
bool parse_table(const string& spec, Signature::Table& table) {
|
|
||||||
// check for OR, AND on whole phrase
|
|
||||||
It f = spec.begin(), l = spec.end();
|
|
||||||
if (qi::parse(f, l,
|
|
||||||
qi::lit("OR")[ph::ref(table) = logic(false, true, true, true)]) ||
|
|
||||||
qi::parse(f, l,
|
|
||||||
qi::lit("AND")[ph::ref(table) = logic(false, false, false, true)]))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// tokenize into separate rows
|
|
||||||
istringstream iss(spec);
|
|
||||||
string token;
|
|
||||||
while (iss >> token) {
|
|
||||||
Signature::Row values;
|
|
||||||
It tf = token.begin(), tl = token.end();
|
|
||||||
bool r = qi::parse(tf, tl,
|
|
||||||
qi::double_[push_back(ph::ref(values), qi::_1)] >> +("/" >> qi::double_[push_back(ph::ref(values), qi::_1)]) |
|
|
||||||
qi::lit("T")[ph::ref(values) = T] |
|
|
||||||
qi::lit("F")[ph::ref(values) = F] );
|
|
||||||
if (!r)
|
|
||||||
return false;
|
|
||||||
table.push_back(values);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} // \namespace parser
|
} // \namespace parser
|
||||||
|
|
||||||
ostream& operator <<(ostream &os, const Signature::Row &row) {
|
ostream& operator <<(ostream &os, const Signature::Row &row) {
|
||||||
|
@ -166,14 +127,11 @@ namespace gtsam {
|
||||||
Signature& Signature::operator=(const string& spec) {
|
Signature& Signature::operator=(const string& spec) {
|
||||||
spec_.reset(spec);
|
spec_.reset(spec);
|
||||||
Table table;
|
Table table;
|
||||||
// NOTE: using simpler parse function to ensure boost back compatibility
|
parser::It f = spec.begin(), l = spec.end();
|
||||||
// parser::It f = spec.begin(), l = spec.end();
|
bool success =
|
||||||
bool success = //
|
qi::phrase_parse(f, l, parser::grammar.table, qi::space, table);
|
||||||
// qi::phrase_parse(f, l, parser::grammar.table, qi::space, table); // using full grammar
|
|
||||||
parser::parse_table(spec, table);
|
|
||||||
if (success) {
|
if (success) {
|
||||||
for(Row& row: table)
|
for (Row& row : table) normalize(row);
|
||||||
normalize(row);
|
|
||||||
table_.reset(table);
|
table_.reset(table);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
|
|
|
@ -103,7 +103,7 @@ namespace gtsam {
|
||||||
/** Add a parent */
|
/** Add a parent */
|
||||||
Signature& operator,(const DiscreteKey& parent);
|
Signature& operator,(const DiscreteKey& parent);
|
||||||
|
|
||||||
/** Add the CPT spec - Fails in boost 1.40 */
|
/** Add the CPT spec */
|
||||||
Signature& operator=(const std::string& spec);
|
Signature& operator=(const std::string& spec);
|
||||||
|
|
||||||
/** Add the CPT spec directly as a table */
|
/** Add the CPT spec directly as a table */
|
||||||
|
@ -122,7 +122,6 @@ namespace gtsam {
|
||||||
/**
|
/**
|
||||||
* Helper function to create Signature objects
|
* Helper function to create Signature objects
|
||||||
* example: Signature s(D % "99/1");
|
* example: Signature s(D % "99/1");
|
||||||
* Uses string parser, which requires BOOST 1.42 or higher
|
|
||||||
*/
|
*/
|
||||||
GTSAM_EXPORT Signature operator%(const DiscreteKey& key, const std::string& parent);
|
GTSAM_EXPORT Signature operator%(const DiscreteKey& key, const std::string& parent);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue