component_type.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2020 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 "api/component_type.h"
  12. #include <set>
  13. using namespace std;
  14. namespace MCell {
  15. namespace API {
  16. bool ComponentType::__eq__(const ComponentType& other) const {
  17. return
  18. eq_nonarray_attributes(other) &&
  19. std::set<string>(states.begin(), states.end()) ==
  20. std::set<string>(other.states.begin(), other.states.end());
  21. }
  22. std::string ComponentType::to_bngl_str() const {
  23. std::string res;
  24. res = name;
  25. for (const string& s: states) {
  26. res += "~" + s;
  27. }
  28. return res;
  29. }
  30. // useful when we need to put component types to a set
  31. bool ComponentType::operator < (const ComponentType& other) const {
  32. if (name == other.name) {
  33. if (!__eq__(other)) {
  34. throw RuntimeError(
  35. "Cannot define ordering (less) between " + to_bngl_str() + " and " + other.to_bngl_str() + ", "
  36. "they have the same name but different states. " +
  37. "Error might have occurred due to a call to " + NAME_CLASS_ELEMENTARY_MOLECULE_TYPE + ".__eq__().");
  38. }
  39. return false;
  40. }
  41. else {
  42. return name < other.name;
  43. }
  44. }
  45. std::string ComponentType::get_canonical_name() const {
  46. std::string res;
  47. res = name;
  48. set<string> sorted(states.begin(), states.end());
  49. for (const string& s: sorted) {
  50. res += "~" + s;
  51. }
  52. return res;
  53. }
  54. } // namespace API
  55. } // namespace MCell