gtsam/wrap/tests/interface_parser_test.py

259 lines
7.7 KiB
Python
Raw Normal View History

# TODO(duy): make them proper tests!!!
import unittest
import sys, os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
Squashed 'wrap/' changes from dfa624e77..09f8bbf71 09f8bbf71 Merge pull request #25 from borglab/fix/function-name 0dbfb6c13 fix function name to be the correct one f69f8b01f Merge pull request #24 from borglab/fix/pip 6519a6627 use pip install to overcome superuser issues b11ecf4e8 Merge pull request #23 from borglab/fix/remove-pip-args 813030108 remove pip-args since we are using setup.py 498d233e0 Merge pull request #22 from borglab/fix/package-install 846212ac3 set correct flags for installing gtwrap package 62161cd20 Merge pull request #21 from borglab/feature/script-vars 93be1d9f8 set script variables and move pybind11 loading so gtwrap can be used under gtsam 8770e3c7e Merge pull request #20 from borglab/fix/pybind-include 8c3c83618 proper placement of pybind11 include a9ad4f504 Merge pull request #19 from borglab/feature/package 99d8a12c7 added more documentation 4cbec1579 change to macro so we don't have to deal with function scopes b83e405b8 updates to completely install the package 38a64b3de new scripts which will be installed to bin directory bf9646235 Merge pull request #18 from borglab/fix/cmake-min c7c280099 Consistent cmake minimum required 42df58f62 Merge pull request #17 from borglab/fix/cleanup e580b282d version bump 4ccd66fa5 More finegrained handling of Python version 6476fd710 Merge pull request #16 from borglab/feature/better-find-python 8ac1296a0 use setup.py to install dependencies e9ac473be install dependencies and support versions of CMake<3.12 cf272dbd2 Merge pull request #15 from borglab/feature/utils ffc9cc4f7 new utils to reduce boilerplate 20e8e8b7a Merge pull request #11 from borglab/feature/package 04b844bd6 use new version of FindPython and be consistent 3f9d7a32a Merge pull request #13 from borglab/add_license c791075a6 Add LICENSE 517b67c46 correct working directory for setup.py 1b22b47ae move matlab.h to root directory 37b407214 Proper source directory path for use in other projects 61696dd5d configure PybindWrap within the cmake directory 1b91fc9af add config file so we can use find_package a1e6f4f53 small typo da9f351be updated README and housekeeping 64b8f78d5 files needed to allow for packaging bddda7f54 package structure git-subtree-dir: wrap git-subtree-split: 09f8bbf7172ba8b1bd3d2484795743f16e1a5893
2021-01-05 02:11:36 +08:00
from gtwrap.interface_parser import *
class TestPyparsing(unittest.TestCase):
def test_argument_list(self):
arg_string = "int a, C1 c1, C2& c2, C3* c3, "\
"const C4 c4, const C5& c5,"\
"const C6* c6"
args = ArgumentList.rule.parseString(arg_string)
print(ArgumentList(args))
empty_args = ArgumentList.rule.parseString("")[0]
print(empty_args)
arg_string = "int a, C1 c1, C2& c2, C3* c3, "\
"const C4 c4, const C5& c5,"\
"const C6* c6"
args = ArgumentList.rule.parseString(arg_string)[0]
print(args)
# Test ReturnType
ReturnType.rule.parseString("pair<fdsa, rewcds>")[0]
ReturnType.rule.parseString("cdwdc")[0]
# expect throw
# ReturnType.parseString("int&")
# ReturnType.parseString("const int")
ret = Class.rule.parseString("""
virtual class SymbolicFactorGraph {
SymbolicFactorGraph();
SymbolicFactorGraph(const gtsam::SymbolicBayesNet& bayesNet);
SymbolicFactorGraph(const gtsam::SymbolicBayesTree& bayesTree);
// From FactorGraph.
void push_back(gtsam::SymbolicFactor* factor);
void print(string s) const;
bool equals(const gtsam::SymbolicFactorGraph& rhs, double tol) const;
size_t size() const;
bool exists(size_t idx) const;
// Standard interface
gtsam::KeySet keys() const;
void push_back(const gtsam::SymbolicFactorGraph& graph);
void push_back(const gtsam::SymbolicBayesNet& bayesNet);
void push_back(const gtsam::SymbolicBayesTree& bayesTree);
/* Advanced interface */
void push_factor(size_t key);
void push_factor(size_t key1, size_t key2);
void push_factor(size_t key1, size_t key2, size_t key3);
void push_factor(size_t key1, size_t key2, size_t key3, size_t key4);
gtsam::SymbolicBayesNet* eliminateSequential();
gtsam::SymbolicBayesNet* eliminateSequential(
const gtsam::Ordering& ordering);
gtsam::SymbolicBayesTree* eliminateMultifrontal();
gtsam::SymbolicBayesTree* eliminateMultifrontal(
const gtsam::Ordering& ordering);
pair<gtsam::SymbolicBayesNet*, gtsam::SymbolicFactorGraph*>
eliminatePartialSequential(const gtsam::Ordering& ordering);
pair<gtsam::SymbolicBayesNet*, gtsam::SymbolicFactorGraph*>
eliminatePartialSequential(const gtsam::KeyVector& keys);
pair<gtsam::SymbolicBayesTree*, gtsam::SymbolicFactorGraph*>
eliminatePartialMultifrontal(const gtsam::Ordering& ordering);
gtsam::SymbolicBayesNet* marginalMultifrontalBayesNet(
const gtsam::Ordering& ordering);
gtsam::SymbolicBayesNet* marginalMultifrontalBayesNet(
const gtsam::KeyVector& key_vector,
const gtsam::Ordering& marginalizedVariableOrdering);
gtsam::SymbolicFactorGraph* marginal(const gtsam::KeyVector& key_vector);
};
""")[0]
ret = Class.rule.parseString("""
virtual class Base {
};
""")[0]
ret = Class.rule.parseString("""
virtual class Null: gtsam::noiseModel::mEstimator::Base {
Null();
void print(string s) const;
static gtsam::noiseModel::mEstimator::Null* Create();
// enabling serialization functionality
void serializable() const;
};
""")[0]
retFactorIndices = Class.rule.parseString("""
class FactorIndices {};
""")[0]
retIsam2 = Class.rule.parseString("""
class ISAM2 {
ISAM2();
ISAM2(const gtsam::ISAM2Params& params);
ISAM2(const gtsam::ISAM2& other);
bool equals(const gtsam::ISAM2& other, double tol) const;
void print(string s) const;
void printStats() const;
void saveGraph(string s) const;
gtsam::ISAM2Result update();
gtsam::ISAM2Result update(const gtsam::NonlinearFactorGraph& newFactors,
const gtsam::Values& newTheta);
gtsam::ISAM2Result update(const gtsam::NonlinearFactorGraph& newFactors,
const gtsam::Values& newTheta, const gtsam::FactorIndices&
removeFactorIndices);
gtsam::ISAM2Result update(const gtsam::NonlinearFactorGraph& newFactors,
const gtsam::Values& newTheta,
const gtsam::FactorIndices& removeFactorIndices,
const gtsam::KeyGroupMap& constrainedKeys);
gtsam::Values getLinearizationPoint() const;
gtsam::Values calculateEstimate() const;
template <VALUE = {gtsam::Point2, gtsam::Rot2, gtsam::Pose2, gtsam::Point3,
gtsam::Rot3, gtsam::Pose3, gtsam::Cal3_S2, gtsam::Cal3DS2,
gtsam::Cal3Bundler, gtsam::EssentialMatrix,
gtsam::SimpleCamera, Vector, Matrix}>
VALUE calculateEstimate(size_t key) const;
gtsam::Values calculateBestEstimate() const;
Matrix marginalCovariance(size_t key) const;
gtsam::VectorValues getDelta() const;
gtsam::NonlinearFactorGraph getFactorsUnsafe() const;
gtsam::VariableIndex getVariableIndex() const;
gtsam::ISAM2Params params() const;
};
""")[0]
# if __name__ == '__main__':
# unittest.main()
typename = Typename.rule.parseString("rew")[0]
ret = ReturnType.rule.parseString("pair<fdsa, rewcds>")[0]
ret1 = Method.rule.parseString(
"int f(const int x, const Class& c, Class* t) const;")[0]
ret = Method.rule.parseString("int f() const;")[0]
ret1 = StaticMethod.rule.parseString(
"static int f(const int x, const Class& c, Class* t);")[0]
ret = StaticMethod.rule.parseString("static int f();")[0]
ret1 = Constructor.rule.parseString(
"f(const int x, const Class& c, Class* t);")[0]
ret = Constructor.rule.parseString("f();")[0]
typedef = TypedefTemplateInstantiation.rule.parseString("""
typedef gtsam::BearingFactor<gtsam::Pose2, gtsam::Point2, gtsam::Rot2>
BearingFactor2D;
""")[0]
include = Include.rule.parseString("#include <gtsam/slam/PriorFactor.h>")[0]
print(include)
fwd = ForwardDeclaration.rule.parseString(
"virtual class Test:gtsam::Point3;")[0]
func = GlobalFunction.rule.parseString("""
gtsam::Values localToWorld(const gtsam::Values& local,
const gtsam::Pose2& base, const gtsam::KeyVector& keys);
""")[0]
print(func)
try:
namespace = Namespace.rule.parseString("""
namespace gtsam {
#include <gtsam/geometry/Point2.h>
class Point2 {
Point2();
Point2(double x, double y);
double x() const;
double y() const;
int dim() const;
char returnChar() const;
void argChar(char a) const;
void argUChar(unsigned char a) const;
void eigenArguments(Vector v, Matrix m) const;
VectorNotEigen vectorConfusion();
};
#include <gtsam/geometry/Point3.h>
class Point3 {
Point3(double x, double y, double z);
double norm() const;
// static functions - use static keyword and uppercase
static double staticFunction();
static gtsam::Point3 StaticFunctionRet(double z);
// enabling serialization functionality
void serialize() const; // Just triggers a flag internally
};
}
""")
except ParseException as pe:
print(pe.markInputline())
# filename = "tools/workspace/pybind_wrapper/gtsam.h"
# with open(filename, "r") as f:
# content = f.read()
# module = Module.parseString(content)
module = Module.parseString("""
namespace one {
namespace two {
namespace three {
class Class123 {
};
}
class Class12a {
};
}
namespace two_dummy {
namespace three_dummy{
}
namespace fourth_dummy{
}
}
namespace two {
class Class12b {
};
}
}
class Global{
};
""")
print("module: ", module)
sub_namespace = find_sub_namespace(module, ['one', 'two', 'three'])
print("Found namespace:", sub_namespace[0].name)
print(find_sub_namespace(module, ['one', 'two_test', 'three']))
print(find_sub_namespace(module, ['one', 'two']))
found_class = module.find_class(
Typename(namespaces_name=['one', 'two', 'three', 'Class123']))
print(found_class)
found_class = module.find_class(
Typename(namespaces_name=['one', 'two', 'Class12b']))
print(found_class.name)
found_class = module.find_class(
Typename(namespaces_name=['one', 'two', 'Class12a']))
print(found_class.name)