From 8c92b248db7077179177e6884be967491e14a1cf Mon Sep 17 00:00:00 2001 From: Tom Janson Date: Wed, 15 Mar 2017 20:59:05 +0100 Subject: [PATCH] add various __str__ fcts --- src/core/input.cpp | 10 +++++++--- src/mod_expressions.cpp | 1 - src/mod_info.cpp | 1 - src/storage/bitvector.cpp | 5 ++--- src/storage/matrix.cpp | 25 +++++-------------------- src/storage/model.cpp | 28 ++++++++++++++++++++++++++++ src/utility/shortestPaths.cpp | 12 ++---------- 7 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/core/input.cpp b/src/core/input.cpp index ae986f5..ef13ff9 100644 --- a/src/core/input.cpp +++ b/src/core/input.cpp @@ -1,10 +1,13 @@ #include "input.h" +#include "src/helpers.h" void define_property(py::module& m) { py::class_(m, "Property", "Property") - .def(py::init const&, std::string const&>(), "Construct property from formula", py::arg("name"), py::arg("formula"), py::arg("comment") = "") - .def_property_readonly("name", &storm::jani::Property::getName, "Obtain the name of the property") - .def_property_readonly("raw_formula", &storm::jani::Property::getRawFormula, "Obtain the formula directly"); + .def(py::init const&, std::string const&>(), "Construct property from formula", py::arg("name"), py::arg("formula"), py::arg("comment") = "") + .def_property_readonly("name", &storm::jani::Property::getName, "Obtain the name of the property") + .def_property_readonly("raw_formula", &storm::jani::Property::getRawFormula, "Obtain the formula directly") + .def("__str__", &streamToString) + ; } @@ -29,6 +32,7 @@ void define_input(py::module& m) { .def_property_readonly("nr_modules", &storm::prism::Program::getNumberOfModules, "Number of modules") .def_property_readonly("model_type", &storm::prism::Program::getModelType, "Model type") .def_property_readonly("has_undefined_constants", &storm::prism::Program::hasUndefinedConstants, "Flag if program has undefined constants") + .def("__str__", &streamToString) ; // SymbolicModelDescription diff --git a/src/mod_expressions.cpp b/src/mod_expressions.cpp index 87a1d5a..aad8006 100644 --- a/src/mod_expressions.cpp +++ b/src/mod_expressions.cpp @@ -1,5 +1,4 @@ #include "common.h" -#include "helpers.h" #include "storm/storage/expressions/ExpressionManager.h" PYBIND11_PLUGIN(expressions) { diff --git a/src/mod_info.cpp b/src/mod_info.cpp index 427b1f8..77ed769 100644 --- a/src/mod_info.cpp +++ b/src/mod_info.cpp @@ -1,5 +1,4 @@ #include "common.h" -#include "helpers.h" #include "storm/utility/storm-version.h" PYBIND11_PLUGIN(info) { diff --git a/src/storage/bitvector.cpp b/src/storage/bitvector.cpp index dc24960..03e9d66 100644 --- a/src/storage/bitvector.cpp +++ b/src/storage/bitvector.cpp @@ -1,7 +1,6 @@ #include "bitvector.h" #include "storm/storage/BitVector.h" - -#include +#include "src/helpers.h" void define_bitvector(py::module& m) { using BitVector = storm::storage::BitVector; @@ -31,7 +30,7 @@ void define_bitvector(py::module& m) { .def(py::self &= py::self) .def(py::self |= py::self) - .def("__repr__", [](BitVector const& b) { std::ostringstream oss; oss << b; return oss.str(); }) + .def("__str__", &streamToString) // TODO (when needed): iterator ; diff --git a/src/storage/matrix.cpp b/src/storage/matrix.cpp index 887a866..9ba8504 100644 --- a/src/storage/matrix.cpp +++ b/src/storage/matrix.cpp @@ -1,5 +1,6 @@ #include "matrix.h" #include "storm/storage/SparseMatrix.h" +#include "src/helpers.h" typedef storm::storage::SparseMatrix::index_type entry_index; typedef unsigned int row_index; @@ -10,11 +11,7 @@ void define_sparse_matrix(py::module& m) { // MatrixEntry py::class_>(m, "SparseMatrixEntry", "Entry of sparse matrix") - .def("__str__", [](storm::storage::MatrixEntry const& entry) { - std::stringstream stream; - stream << entry; - return stream.str(); - }) + .def("__str__", &streamToString>) //def_property threw "pointer being freed not allocated" after exiting .def("value", &storm::storage::MatrixEntry::getValue, "Value") .def("set_value", &storm::storage::MatrixEntry::setValue, py::arg("value"), "Set value") @@ -22,11 +19,7 @@ void define_sparse_matrix(py::module& m) { ; py::class_>(m, "ParametricSparseMatrixEntry", "Entry of parametric sparse matrix") - .def("__str__", [](storm::storage::MatrixEntry const& entry) { - std::stringstream stream; - stream << entry; - return stream.str(); - }) + .def("__str__", &streamToString>) //def_property threw "pointer being freed not allocated" after exiting .def("value", &storm::storage::MatrixEntry::getValue, "Value") .def("set_value", &storm::storage::MatrixEntry::setValue, py::arg("value"), "Set value") @@ -38,11 +31,7 @@ void define_sparse_matrix(py::module& m) { .def("__iter__", [](storm::storage::SparseMatrix& matrix) { return py::make_iterator(matrix.begin(), matrix.end()); }, py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */) - .def("__str__", [](storm::storage::SparseMatrix const& matrix) { - std::stringstream stream; - stream << matrix; - return stream.str(); - }) + .def("__str__", &streamToString>) .def_property_readonly("nr_rows", &storm::storage::SparseMatrix::getRowCount, "Number of rows") .def_property_readonly("nr_columns", &storm::storage::SparseMatrix::getColumnCount, "Number of columns") .def_property_readonly("nr_entries", &storm::storage::SparseMatrix::getEntryCount, "Number of non-zero entries") @@ -71,11 +60,7 @@ void define_sparse_matrix(py::module& m) { .def("__iter__", [](storm::storage::SparseMatrix& matrix) { return py::make_iterator(matrix.begin(), matrix.end()); }, py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */) - .def("__str__", [](storm::storage::SparseMatrix const& matrix) { - std::stringstream stream; - stream << matrix; - return stream.str(); - }) + .def("__str__", &streamToString>) .def_property_readonly("nr_rows", &storm::storage::SparseMatrix::getRowCount, "Number of rows") .def_property_readonly("nr_columns", &storm::storage::SparseMatrix::getColumnCount, "Number of columns") .def_property_readonly("nr_entries", &storm::storage::SparseMatrix::getEntryCount, "Number of non-zero entries") diff --git a/src/storage/model.cpp b/src/storage/model.cpp index 8b11429..bcd2c27 100644 --- a/src/storage/model.cpp +++ b/src/storage/model.cpp @@ -6,6 +6,10 @@ #include "storm/models/sparse/StandardRewardModel.h" #include "storm/utility/ModelInstantiator.h" +#include +#include +#include + // Thin wrapper for getting initial states template std::vector getInitialStates(storm::models::sparse::Model const& model) { @@ -30,6 +34,24 @@ std::set rewardVariables(storm::models::sparse: return storm::models::sparse::getRewardParameters(model); } +template +std::function const&)> getModelInfoPrinter(std::string name = "Model") { + // look, C++ has lambdas and stuff! + return [name](storm::models::sparse::Model const& model) { + std::stringstream ss; + model.printModelInformationToStream(ss); + + // attempting a slightly readable output + std::string text = name + " ("; + std::string line; + for (int i = 0; std::getline(ss, line); i++) { + if (line != "-------------------------------------------------------------- ") + text += line + " "; + } + return text + ")"; + }; +} + // Define python bindings void define_model(py::module& m) { @@ -65,10 +87,13 @@ void define_model(py::module& m) { .def("labels_state", &storm::models::sparse::Model::getLabelsOfState, py::arg("state"), "Get labels of state") .def_property_readonly("initial_states", &getInitialStates, "Initial states") .def_property_readonly("transition_matrix", &getTransitionMatrix, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Transition matrix") + .def("__str__", getModelInfoPrinter()) ; py::class_, std::shared_ptr>>(m, "SparseDtmc", "DTMC in sparse representation", model) + .def("__str__", getModelInfoPrinter("DTMC")) ; py::class_, std::shared_ptr>>(m, "SparseMdp", "MDP in sparse representation", model) + .def("__str__", getModelInfoPrinter("MDP")) ; py::class_, std::shared_ptr>> modelRatFunc(m, "_SparseParametricModel", "A probabilistic model where transitions are represented by rational functions and saved in a sparse matrix", modelBase); @@ -80,10 +105,13 @@ void define_model(py::module& m) { .def("labels_state", &storm::models::sparse::Model::getLabelsOfState, py::arg("state"), "Get labels of state") .def_property_readonly("initial_states", &getInitialStates, "Initial states") .def_property_readonly("transition_matrix", &getTransitionMatrix, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Transition matrix") + .def("__str__", getModelInfoPrinter("ParametricModel")) ; py::class_, std::shared_ptr>>(m, "SparseParametricDtmc", "pDTMC in sparse representation", modelRatFunc) + .def("__str__", getModelInfoPrinter("ParametricDTMC")) ; py::class_, std::shared_ptr>>(m, "SparseParametricMdp", "pMDP in sparse representation", modelRatFunc) + .def("__str__", getModelInfoPrinter("ParametricMDP")) ; } diff --git a/src/utility/shortestPaths.cpp b/src/utility/shortestPaths.cpp index cd8fdeb..3fed97e 100644 --- a/src/utility/shortestPaths.cpp +++ b/src/utility/shortestPaths.cpp @@ -1,15 +1,12 @@ #include "shortestPaths.h" #include "storm/utility/shortestPaths.h" +#include "src/helpers.h" // only forward declaring Model leads to pybind compilation error // this may be avoidable. but including certainly works. #include "storm/models/sparse/Model.h" #include "storm/models/sparse/StandardRewardModel.h" -#include -#include - -#include void define_ksp(py::module& m) { @@ -39,12 +36,7 @@ void define_ksp(py::module& m) { .def(py::self == py::self, "Compares predecessor node and index, ignoring distance") - .def("__repr__", [](Path const& p) { - std::ostringstream oss; - oss << ""; - return oss.str(); - }) + .def("__str__", &streamToString) .def_readwrite("predecessorNode", &Path::predecessorNode) // TODO (un-)wrap boost::optional so it's usable .def_readwrite("predecessorK", &Path::predecessorK)