Browse Source

add various __str__ fcts

refactoring
Tom Janson 8 years ago
parent
commit
8c92b248db
  1. 10
      src/core/input.cpp
  2. 1
      src/mod_expressions.cpp
  3. 1
      src/mod_info.cpp
  4. 5
      src/storage/bitvector.cpp
  5. 25
      src/storage/matrix.cpp
  6. 28
      src/storage/model.cpp
  7. 12
      src/utility/shortestPaths.cpp

10
src/core/input.cpp

@ -1,10 +1,13 @@
#include "input.h"
#include "src/helpers.h"
void define_property(py::module& m) {
py::class_<storm::jani::Property>(m, "Property", "Property")
.def(py::init<std::string const&, std::shared_ptr<storm::logic::Formula const> 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<std::string const&, std::shared_ptr<storm::logic::Formula const> 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<storm::jani::Property>)
;
}
@ -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<storm::prism::Program>)
;
// SymbolicModelDescription

1
src/mod_expressions.cpp

@ -1,5 +1,4 @@
#include "common.h"
#include "helpers.h"
#include "storm/storage/expressions/ExpressionManager.h"
PYBIND11_PLUGIN(expressions) {

1
src/mod_info.cpp

@ -1,5 +1,4 @@
#include "common.h"
#include "helpers.h"
#include "storm/utility/storm-version.h"
PYBIND11_PLUGIN(info) {

5
src/storage/bitvector.cpp

@ -1,7 +1,6 @@
#include "bitvector.h"
#include "storm/storage/BitVector.h"
#include <sstream>
#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<BitVector>)
// TODO (when needed): iterator
;

25
src/storage/matrix.cpp

@ -1,5 +1,6 @@
#include "matrix.h"
#include "storm/storage/SparseMatrix.h"
#include "src/helpers.h"
typedef storm::storage::SparseMatrix<double>::index_type entry_index;
typedef unsigned int row_index;
@ -10,11 +11,7 @@ void define_sparse_matrix(py::module& m) {
// MatrixEntry
py::class_<storm::storage::MatrixEntry<entry_index, double>>(m, "SparseMatrixEntry", "Entry of sparse matrix")
.def("__str__", [](storm::storage::MatrixEntry<entry_index, double> const& entry) {
std::stringstream stream;
stream << entry;
return stream.str();
})
.def("__str__", &streamToString<storm::storage::MatrixEntry<entry_index, double>>)
//def_property threw "pointer being freed not allocated" after exiting
.def("value", &storm::storage::MatrixEntry<entry_index, double>::getValue, "Value")
.def("set_value", &storm::storage::MatrixEntry<entry_index, double>::setValue, py::arg("value"), "Set value")
@ -22,11 +19,7 @@ void define_sparse_matrix(py::module& m) {
;
py::class_<storm::storage::MatrixEntry<parametric_entry_index, storm::RationalFunction>>(m, "ParametricSparseMatrixEntry", "Entry of parametric sparse matrix")
.def("__str__", [](storm::storage::MatrixEntry<parametric_entry_index, storm::RationalFunction> const& entry) {
std::stringstream stream;
stream << entry;
return stream.str();
})
.def("__str__", &streamToString<storm::storage::MatrixEntry<parametric_entry_index, storm::RationalFunction>>)
//def_property threw "pointer being freed not allocated" after exiting
.def("value", &storm::storage::MatrixEntry<parametric_entry_index, storm::RationalFunction>::getValue, "Value")
.def("set_value", &storm::storage::MatrixEntry<parametric_entry_index, storm::RationalFunction>::setValue, py::arg("value"), "Set value")
@ -38,11 +31,7 @@ void define_sparse_matrix(py::module& m) {
.def("__iter__", [](storm::storage::SparseMatrix<double>& 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<double> const& matrix) {
std::stringstream stream;
stream << matrix;
return stream.str();
})
.def("__str__", &streamToString<storm::storage::SparseMatrix<double>>)
.def_property_readonly("nr_rows", &storm::storage::SparseMatrix<double>::getRowCount, "Number of rows")
.def_property_readonly("nr_columns", &storm::storage::SparseMatrix<double>::getColumnCount, "Number of columns")
.def_property_readonly("nr_entries", &storm::storage::SparseMatrix<double>::getEntryCount, "Number of non-zero entries")
@ -71,11 +60,7 @@ void define_sparse_matrix(py::module& m) {
.def("__iter__", [](storm::storage::SparseMatrix<storm::RationalFunction>& 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<storm::RationalFunction> const& matrix) {
std::stringstream stream;
stream << matrix;
return stream.str();
})
.def("__str__", &streamToString<storm::storage::SparseMatrix<storm::RationalFunction>>)
.def_property_readonly("nr_rows", &storm::storage::SparseMatrix<storm::RationalFunction>::getRowCount, "Number of rows")
.def_property_readonly("nr_columns", &storm::storage::SparseMatrix<storm::RationalFunction>::getColumnCount, "Number of columns")
.def_property_readonly("nr_entries", &storm::storage::SparseMatrix<storm::RationalFunction>::getEntryCount, "Number of non-zero entries")

28
src/storage/model.cpp

@ -6,6 +6,10 @@
#include "storm/models/sparse/StandardRewardModel.h"
#include "storm/utility/ModelInstantiator.h"
#include <functional>
#include <string>
#include <sstream>
// Thin wrapper for getting initial states
template<typename ValueType>
std::vector<storm::storage::sparse::state_type> getInitialStates(storm::models::sparse::Model<ValueType> const& model) {
@ -30,6 +34,24 @@ std::set<storm::RationalFunctionVariable> rewardVariables(storm::models::sparse:
return storm::models::sparse::getRewardParameters(model);
}
template<typename ValueType>
std::function<std::string (storm::models::sparse::Model<ValueType> const&)> getModelInfoPrinter(std::string name = "Model") {
// look, C++ has lambdas and stuff!
return [name](storm::models::sparse::Model<ValueType> 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<double>::getLabelsOfState, py::arg("state"), "Get labels of state")
.def_property_readonly("initial_states", &getInitialStates<double>, "Initial states")
.def_property_readonly("transition_matrix", &getTransitionMatrix<double>, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Transition matrix")
.def("__str__", getModelInfoPrinter<double>())
;
py::class_<storm::models::sparse::Dtmc<double>, std::shared_ptr<storm::models::sparse::Dtmc<double>>>(m, "SparseDtmc", "DTMC in sparse representation", model)
.def("__str__", getModelInfoPrinter<double>("DTMC"))
;
py::class_<storm::models::sparse::Mdp<double>, std::shared_ptr<storm::models::sparse::Mdp<double>>>(m, "SparseMdp", "MDP in sparse representation", model)
.def("__str__", getModelInfoPrinter<double>("MDP"))
;
py::class_<storm::models::sparse::Model<storm::RationalFunction>, std::shared_ptr<storm::models::sparse::Model<storm::RationalFunction>>> 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<storm::RationalFunction>::getLabelsOfState, py::arg("state"), "Get labels of state")
.def_property_readonly("initial_states", &getInitialStates<storm::RationalFunction>, "Initial states")
.def_property_readonly("transition_matrix", &getTransitionMatrix<storm::RationalFunction>, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Transition matrix")
.def("__str__", getModelInfoPrinter<storm::RationalFunction>("ParametricModel"))
;
py::class_<storm::models::sparse::Dtmc<storm::RationalFunction>, std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>>>(m, "SparseParametricDtmc", "pDTMC in sparse representation", modelRatFunc)
.def("__str__", getModelInfoPrinter<storm::RationalFunction>("ParametricDTMC"))
;
py::class_<storm::models::sparse::Mdp<storm::RationalFunction>, std::shared_ptr<storm::models::sparse::Mdp<storm::RationalFunction>>>(m, "SparseParametricMdp", "pMDP in sparse representation", modelRatFunc)
.def("__str__", getModelInfoPrinter<storm::RationalFunction>("ParametricMDP"))
;
}

12
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 <sstream>
#include <string>
#include <boost/optional/optional_io.hpp>
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 << "<Path with predecessorNode: '" << ((p.predecessorNode) ? std::to_string(p.predecessorNode.get()) : "None");
oss << "' predecessorK: '" << p.predecessorK << "' distance: '" << p.distance << "'>";
return oss.str();
})
.def("__str__", &streamToString<Path>)
.def_readwrite("predecessorNode", &Path::predecessorNode) // TODO (un-)wrap boost::optional so it's usable
.def_readwrite("predecessorK", &Path::predecessorK)

Loading…
Cancel
Save