gen_component.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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_component.h"
  15. #include "api/component.h"
  16. #include "api/component_type.h"
  17. namespace MCell {
  18. namespace API {
  19. void GenComponent::check_semantics() const {
  20. if (!is_set(component_type)) {
  21. throw ValueError("Parameter 'component_type' must be set.");
  22. }
  23. }
  24. void GenComponent::set_initialized() {
  25. if (is_set(component_type)) {
  26. component_type->set_initialized();
  27. }
  28. initialized = true;
  29. }
  30. void GenComponent::set_all_attributes_as_default_or_unset() {
  31. class_name = "Component";
  32. component_type = nullptr;
  33. state = "STATE_UNSET";
  34. bond = BOND_UNBOUND;
  35. }
  36. std::shared_ptr<Component> GenComponent::copy_component() const {
  37. std::shared_ptr<Component> res = std::make_shared<Component>(DefaultCtorArgType());
  38. res->class_name = class_name;
  39. res->component_type = component_type;
  40. res->state = state;
  41. res->bond = bond;
  42. return res;
  43. }
  44. std::shared_ptr<Component> GenComponent::deepcopy_component(py::dict) const {
  45. std::shared_ptr<Component> res = std::make_shared<Component>(DefaultCtorArgType());
  46. res->class_name = class_name;
  47. res->component_type = is_set(component_type) ? component_type->deepcopy_component_type() : nullptr;
  48. res->state = state;
  49. res->bond = bond;
  50. return res;
  51. }
  52. bool GenComponent::__eq__(const Component& other) const {
  53. return
  54. (
  55. (is_set(component_type)) ?
  56. (is_set(other.component_type) ?
  57. (component_type->__eq__(*other.component_type)) :
  58. false
  59. ) :
  60. (is_set(other.component_type) ?
  61. false :
  62. true
  63. )
  64. ) &&
  65. state == other.state &&
  66. bond == other.bond;
  67. }
  68. bool GenComponent::eq_nonarray_attributes(const Component& other, const bool ignore_name) const {
  69. return
  70. (
  71. (is_set(component_type)) ?
  72. (is_set(other.component_type) ?
  73. (component_type->__eq__(*other.component_type)) :
  74. false
  75. ) :
  76. (is_set(other.component_type) ?
  77. false :
  78. true
  79. )
  80. ) &&
  81. state == other.state &&
  82. bond == other.bond;
  83. }
  84. std::string GenComponent::to_str(const bool all_details, const std::string ind) const {
  85. std::stringstream ss;
  86. ss << get_object_name() << ": " <<
  87. "\n" << ind + " " << "component_type=" << "(" << ((component_type != nullptr) ? component_type->to_str(all_details, ind + " ") : "null" ) << ")" << ", " << "\n" << ind + " " <<
  88. "state=" << state << ", " <<
  89. "bond=" << bond;
  90. return ss.str();
  91. }
  92. py::class_<Component> define_pybinding_Component(py::module& m) {
  93. return py::class_<Component, std::shared_ptr<Component>>(m, "Component", "Instance of a component type belonging to a molecule instance.\nA component instance must have its state set if there is at least one allowed state.\nIt is also used to connect molecule instance in a complex instance through bonds.\n")
  94. .def(
  95. py::init<
  96. std::shared_ptr<ComponentType>,
  97. const std::string&,
  98. const int
  99. >(),
  100. py::arg("component_type"),
  101. py::arg("state") = "STATE_UNSET",
  102. py::arg("bond") = BOND_UNBOUND
  103. )
  104. .def("check_semantics", &Component::check_semantics)
  105. .def("__copy__", &Component::copy_component)
  106. .def("__deepcopy__", &Component::deepcopy_component, py::arg("memo"))
  107. .def("__str__", &Component::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  108. .def("__eq__", &Component::__eq__, py::arg("other"))
  109. .def("to_bngl_str", &Component::to_bngl_str, "Creates a string that corresponds to this component's BNGL representation.")
  110. .def("dump", &Component::dump)
  111. .def_property("component_type", &Component::get_component_type, &Component::set_component_type, "Reference to a component type.")
  112. .def_property("state", &Component::get_state, &Component::set_state, "Specific state value of this component instance.")
  113. .def_property("bond", &Component::get_bond, &Component::set_bond, "Specific bond for this component instance.\nIt is either a numberical value such as in A(c!1),\nor one of special values BOND_UNBOUND in A(c), \nBOND_BOUND in A(c!+) or BOND_ANY in A(c!?).\n \n")
  114. ;
  115. }
  116. std::string GenComponent::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  117. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  118. return ctx.get_exported_name(this);
  119. }
  120. std::string exported_name = "component_" + std::to_string(ctx.postinc_counter("component"));
  121. if (!export_even_if_already_exported()) {
  122. ctx.add_exported(this, exported_name);
  123. }
  124. bool str_export = export_as_string_without_newlines();
  125. std::string nl = "";
  126. std::string ind = " ";
  127. std::stringstream ss;
  128. if (!str_export) {
  129. nl = "\n";
  130. ind = " ";
  131. ss << exported_name << " = ";
  132. }
  133. ss << "m.Component(" << nl;
  134. ss << ind << "component_type = " << component_type->export_to_python(out, ctx) << "," << nl;
  135. if (state != "STATE_UNSET") {
  136. ss << ind << "state = " << "'" << state << "'" << "," << nl;
  137. }
  138. if (bond != BOND_UNBOUND) {
  139. ss << ind << "bond = " << bond << "," << nl;
  140. }
  141. ss << ")" << nl << nl;
  142. if (!str_export) {
  143. out << ss.str();
  144. return exported_name;
  145. }
  146. else {
  147. return ss.str();
  148. }
  149. }
  150. } // namespace API
  151. } // namespace MCell