BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
NodeUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Param/Node/NodeUtils.cpp
6 //! @brief Implements collection of utility functions for INode.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "Param/Node/NodeUtils.h"
16 #include "Base/Utils/Assert.h"
19 #include "Param/Node/INode.h"
20 #include <algorithm>
21 #include <functional>
22 #include <iterator>
23 #include <sstream>
24 
25 namespace {
26 
27 // Returns string filled with '.'
28 std::string s_indent(int depth)
29 {
30  const int multiplier = 4;
31  return std::string(multiplier * depth, '.');
32 }
33 
34 // Returns single line string representing pool parameters of given node.
35 std::string poolToString(const INode& node)
36 {
37  std::ostringstream result;
38 
39  const std::vector<RealParameter*> pars = node.parameterPool()->parameters();
40  if (pars.empty())
41  return {};
42 
43  result << " (";
44  size_t index(0);
45  for (auto par : pars) {
46  result << "'" << par->getName() << "':" << par->value();
47  ++index;
48  if (index != pars.size())
49  result << " ";
50  }
51  result << ")";
52 
53  return result.str();
54 }
55 
56 // Returns a string representing given node.
57 std::string nodeString(const INode& node, int depth)
58 {
59  std::ostringstream result;
60  result << s_indent(depth) << node.displayName() << poolToString(node) << "\n";
61  return result.str();
62 }
63 } // namespace
64 
65 // ************************************************************************************************
66 // namespace NodeUtils
67 // ************************************************************************************************
68 
69 std::vector<std::tuple<const INode*, int, const INode*>> NodeUtils::progenyPlus(const INode* node,
70  int level)
71 {
72  std::vector<std::tuple<const INode*, int, const INode*>> result;
73  result.push_back({node, level, nullptr});
74  for (const auto* child : node->getChildren()) {
75  for (const auto& [subchild, sublevel, subparent] : progenyPlus(child, level + 1))
76  result.push_back({subchild, sublevel, child});
77  }
78  return result;
79 }
80 
81 std::string NodeUtils::nodeToString(const INode* node)
82 {
83  std::ostringstream result;
84  for (const auto& [child, depth, parent] : progenyPlus(node))
85  result << nodeString(*child, depth);
86  return result.str();
87 }
88 
89 std::string NodeUtils::nodePath(const INode* node, const INode* root)
90 {
91  std::vector<std::string> pathElements;
92  const INode* current = node;
93  while (current && current != root) {
94  pathElements.push_back(current->displayName());
95  pathElements.push_back("/");
96  current = current->parent();
97  }
98  if (root != nullptr && current != root)
99  throw std::runtime_error("NodeUtils::nodePath() -> Error. Node doesn't "
100  "belong to root's branch");
101  std::reverse(pathElements.begin(), pathElements.end());
102  std::ostringstream result;
103  std::copy(pathElements.begin(), pathElements.end(), std::ostream_iterator<std::string>(result));
104  return result.str();
105 }
Defines the macro ASSERT.
Defines interface INode.
Defines collection of utility functions for INode.
Defines class ParameterPool.
Defines class RealParameter.
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:49
std::string displayName() const
Returns display name, composed from the name of node and it's copy number.
Definition: INode.cpp:115
const INode * parent() const
Definition: INode.cpp:84
virtual std::vector< const INode * > getChildren() const
Returns a vector of children.
Definition: INode.cpp:63
ParameterPool * parameterPool() const
Returns pointer to the parameter pool.
const std::vector< RealParameter * > parameters() const
Returns full vector of parameters.
Definition: ParameterPool.h:51
std::vector< std::tuple< const INode *, int, const INode * > > progenyPlus(const INode *node, int level=0)
Returns a vector of triples (descendant, depth, parent)
Definition: NodeUtils.cpp:69
std::string nodeToString(const INode *node)
Returns multiline string representing tree structure starting from given node.
Definition: NodeUtils.cpp:81
std::string nodePath(const INode *node, const INode *root=nullptr)
Returns path composed of node's displayName, with respect to root node.
Definition: NodeUtils.cpp:89