gen_release_pattern.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_release_pattern.h"
  15. #include "api/release_pattern.h"
  16. namespace MCell {
  17. namespace API {
  18. void GenReleasePattern::check_semantics() const {
  19. }
  20. void GenReleasePattern::set_initialized() {
  21. initialized = true;
  22. }
  23. void GenReleasePattern::set_all_attributes_as_default_or_unset() {
  24. class_name = "ReleasePattern";
  25. name = STR_UNSET;
  26. release_interval = TIME_INFINITY;
  27. train_duration = TIME_INFINITY;
  28. train_interval = TIME_INFINITY;
  29. number_of_trains = 1;
  30. }
  31. std::shared_ptr<ReleasePattern> GenReleasePattern::copy_release_pattern() const {
  32. std::shared_ptr<ReleasePattern> res = std::make_shared<ReleasePattern>(DefaultCtorArgType());
  33. res->class_name = class_name;
  34. res->name = name;
  35. res->release_interval = release_interval;
  36. res->train_duration = train_duration;
  37. res->train_interval = train_interval;
  38. res->number_of_trains = number_of_trains;
  39. return res;
  40. }
  41. std::shared_ptr<ReleasePattern> GenReleasePattern::deepcopy_release_pattern(py::dict) const {
  42. std::shared_ptr<ReleasePattern> res = std::make_shared<ReleasePattern>(DefaultCtorArgType());
  43. res->class_name = class_name;
  44. res->name = name;
  45. res->release_interval = release_interval;
  46. res->train_duration = train_duration;
  47. res->train_interval = train_interval;
  48. res->number_of_trains = number_of_trains;
  49. return res;
  50. }
  51. bool GenReleasePattern::__eq__(const ReleasePattern& other) const {
  52. return
  53. name == other.name &&
  54. release_interval == other.release_interval &&
  55. train_duration == other.train_duration &&
  56. train_interval == other.train_interval &&
  57. number_of_trains == other.number_of_trains;
  58. }
  59. bool GenReleasePattern::eq_nonarray_attributes(const ReleasePattern& other, const bool ignore_name) const {
  60. return
  61. (ignore_name || name == other.name) &&
  62. release_interval == other.release_interval &&
  63. train_duration == other.train_duration &&
  64. train_interval == other.train_interval &&
  65. number_of_trains == other.number_of_trains;
  66. }
  67. std::string GenReleasePattern::to_str(const bool all_details, const std::string ind) const {
  68. std::stringstream ss;
  69. ss << get_object_name() << ": " <<
  70. "name=" << name << ", " <<
  71. "release_interval=" << release_interval << ", " <<
  72. "train_duration=" << train_duration << ", " <<
  73. "train_interval=" << train_interval << ", " <<
  74. "number_of_trains=" << number_of_trains;
  75. return ss.str();
  76. }
  77. py::class_<ReleasePattern> define_pybinding_ReleasePattern(py::module& m) {
  78. return py::class_<ReleasePattern, std::shared_ptr<ReleasePattern>>(m, "ReleasePattern", "Defines a release pattern that specifies repeating molecule releases. \nCan be used by a ReleaseSite.\n")
  79. .def(
  80. py::init<
  81. const std::string&,
  82. const double,
  83. const double,
  84. const double,
  85. const int
  86. >(),
  87. py::arg("name") = STR_UNSET,
  88. py::arg("release_interval") = TIME_INFINITY,
  89. py::arg("train_duration") = TIME_INFINITY,
  90. py::arg("train_interval") = TIME_INFINITY,
  91. py::arg("number_of_trains") = 1
  92. )
  93. .def("check_semantics", &ReleasePattern::check_semantics)
  94. .def("__copy__", &ReleasePattern::copy_release_pattern)
  95. .def("__deepcopy__", &ReleasePattern::deepcopy_release_pattern, py::arg("memo"))
  96. .def("__str__", &ReleasePattern::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  97. .def("__eq__", &ReleasePattern::__eq__, py::arg("other"))
  98. .def("dump", &ReleasePattern::dump)
  99. .def_property("name", &ReleasePattern::get_name, &ReleasePattern::set_name, "Name of the release pattern.")
  100. .def_property("release_interval", &ReleasePattern::get_release_interval, &ReleasePattern::set_release_interval, "During a train of releases, release molecules after every t seconds. \nDefault is to release only once.\n")
  101. .def_property("train_duration", &ReleasePattern::get_train_duration, &ReleasePattern::set_train_duration, "The train of releases lasts for t seconds before turning off. \nDefault is to never turn off.\n")
  102. .def_property("train_interval", &ReleasePattern::get_train_interval, &ReleasePattern::set_train_interval, "A new train of releases happens every t seconds. \nDefault is to never have a new train. \nThe train interval must not be shorter than the train duration.\n")
  103. .def_property("number_of_trains", &ReleasePattern::get_number_of_trains, &ReleasePattern::set_number_of_trains, "Repeat the release process for n trains of releases. Default is one train.\nFor unlimited number of trains use a constant NUMBER_OF_TRAINS_UNLIMITED.\n")
  104. ;
  105. }
  106. std::string GenReleasePattern::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  107. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  108. return ctx.get_exported_name(this);
  109. }
  110. std::string exported_name = std::string("release_pattern") + "_" + (is_set(name) ? fix_id(name) : std::to_string(ctx.postinc_counter("release_pattern")));
  111. if (!export_even_if_already_exported()) {
  112. ctx.add_exported(this, exported_name);
  113. }
  114. bool str_export = export_as_string_without_newlines();
  115. std::string nl = "";
  116. std::string ind = " ";
  117. std::stringstream ss;
  118. if (!str_export) {
  119. nl = "\n";
  120. ind = " ";
  121. ss << exported_name << " = ";
  122. }
  123. ss << "m.ReleasePattern(" << nl;
  124. if (name != STR_UNSET) {
  125. ss << ind << "name = " << "'" << name << "'" << "," << nl;
  126. }
  127. if (release_interval != TIME_INFINITY) {
  128. ss << ind << "release_interval = " << f_to_str(release_interval) << "," << nl;
  129. }
  130. if (train_duration != TIME_INFINITY) {
  131. ss << ind << "train_duration = " << f_to_str(train_duration) << "," << nl;
  132. }
  133. if (train_interval != TIME_INFINITY) {
  134. ss << ind << "train_interval = " << f_to_str(train_interval) << "," << nl;
  135. }
  136. if (number_of_trains != 1) {
  137. ss << ind << "number_of_trains = " << number_of_trains << "," << nl;
  138. }
  139. ss << ")" << nl << nl;
  140. if (!str_export) {
  141. out << ss.str();
  142. return exported_name;
  143. }
  144. else {
  145. return ss.str();
  146. }
  147. }
  148. } // namespace API
  149. } // namespace MCell