gen_warnings.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2021 by
  4. * The Salk Institute for Biological Studies
  5. *
  6. * Use of this source code is governed by an MIT-style
  7. * license that can be found in the LICENSE file or at
  8. * https://opensource.org/licenses/MIT.
  9. *
  10. ******************************************************************************/
  11. #include <sstream>
  12. #include "api/pybind11_stl_include.h"
  13. #include "api/python_export_utils.h"
  14. #include "gen_warnings.h"
  15. #include "api/warnings.h"
  16. namespace MCell {
  17. namespace API {
  18. void GenWarnings::check_semantics() const {
  19. }
  20. void GenWarnings::set_initialized() {
  21. initialized = true;
  22. }
  23. void GenWarnings::set_all_attributes_as_default_or_unset() {
  24. class_name = "Warnings";
  25. high_reaction_probability = WarningLevel::IGNORE;
  26. molecule_placement_failure = WarningLevel::ERROR;
  27. }
  28. std::shared_ptr<Warnings> GenWarnings::copy_warnings() const {
  29. std::shared_ptr<Warnings> res = std::make_shared<Warnings>(DefaultCtorArgType());
  30. res->class_name = class_name;
  31. res->high_reaction_probability = high_reaction_probability;
  32. res->molecule_placement_failure = molecule_placement_failure;
  33. return res;
  34. }
  35. std::shared_ptr<Warnings> GenWarnings::deepcopy_warnings(py::dict) const {
  36. std::shared_ptr<Warnings> res = std::make_shared<Warnings>(DefaultCtorArgType());
  37. res->class_name = class_name;
  38. res->high_reaction_probability = high_reaction_probability;
  39. res->molecule_placement_failure = molecule_placement_failure;
  40. return res;
  41. }
  42. bool GenWarnings::__eq__(const Warnings& other) const {
  43. return
  44. high_reaction_probability == other.high_reaction_probability &&
  45. molecule_placement_failure == other.molecule_placement_failure;
  46. }
  47. bool GenWarnings::eq_nonarray_attributes(const Warnings& other, const bool ignore_name) const {
  48. return
  49. high_reaction_probability == other.high_reaction_probability &&
  50. molecule_placement_failure == other.molecule_placement_failure;
  51. }
  52. std::string GenWarnings::to_str(const bool all_details, const std::string ind) const {
  53. std::stringstream ss;
  54. ss << get_object_name() << ": " <<
  55. "high_reaction_probability=" << high_reaction_probability << ", " <<
  56. "molecule_placement_failure=" << molecule_placement_failure;
  57. return ss.str();
  58. }
  59. py::class_<Warnings> define_pybinding_Warnings(py::module& m) {
  60. return py::class_<Warnings, std::shared_ptr<Warnings>>(m, "Warnings", "This class contains warnings settings. For now it contains only one configurable \nwarning.\n")
  61. .def(
  62. py::init<
  63. const WarningLevel,
  64. const WarningLevel
  65. >(),
  66. py::arg("high_reaction_probability") = WarningLevel::IGNORE,
  67. py::arg("molecule_placement_failure") = WarningLevel::ERROR
  68. )
  69. .def("check_semantics", &Warnings::check_semantics)
  70. .def("__copy__", &Warnings::copy_warnings)
  71. .def("__deepcopy__", &Warnings::deepcopy_warnings, py::arg("memo"))
  72. .def("__str__", &Warnings::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  73. .def("__eq__", &Warnings::__eq__, py::arg("other"))
  74. .def("dump", &Warnings::dump)
  75. .def_property("high_reaction_probability", &Warnings::get_high_reaction_probability, &Warnings::set_high_reaction_probability, "Print a warning when a bimolecular reaction probability is over 0.5 but less or equal than 1.\nWarning when probability is greater than 1 is always printed.\nCannot be set to WarningLevel.ERROR.\n")
  76. .def_property("molecule_placement_failure", &Warnings::get_molecule_placement_failure, &Warnings::set_molecule_placement_failure, "Print a warning or end with an error when a release of a molecule fails.\n")
  77. ;
  78. }
  79. std::string GenWarnings::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  80. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  81. return ctx.get_exported_name(this);
  82. }
  83. std::string exported_name = "warnings_" + std::to_string(ctx.postinc_counter("warnings"));
  84. if (!export_even_if_already_exported()) {
  85. ctx.add_exported(this, exported_name);
  86. }
  87. bool str_export = export_as_string_without_newlines();
  88. std::string nl = "";
  89. std::string ind = " ";
  90. std::stringstream ss;
  91. if (!str_export) {
  92. nl = "\n";
  93. ind = " ";
  94. ss << exported_name << " = ";
  95. }
  96. ss << "m.Warnings(" << nl;
  97. if (high_reaction_probability != WarningLevel::IGNORE) {
  98. ss << ind << "high_reaction_probability = " << high_reaction_probability << "," << nl;
  99. }
  100. if (molecule_placement_failure != WarningLevel::ERROR) {
  101. ss << ind << "molecule_placement_failure = " << molecule_placement_failure << "," << nl;
  102. }
  103. ss << ")" << nl << nl;
  104. if (!str_export) {
  105. out << ss.str();
  106. return exported_name;
  107. }
  108. else {
  109. return ss.str();
  110. }
  111. }
  112. } // namespace API
  113. } // namespace MCell