BornAgain  1.18.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 scattering at grazing incidence
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/Types/Exceptions.h"
21 #include <algorithm>
22 #include <functional>
23 #include <iterator>
24 #include <sstream>
25 
26 namespace
27 {
28 
29 // Returns string filled with '.'
30 std::string s_indent(int depth)
31 {
32  const int multiplier = 4;
33  return std::string(multiplier * depth, '.');
34 }
35 
36 // Returns single line string representing pool parameters of given node.
37 std::string poolToString(const INode& node)
38 {
39  std::ostringstream result;
40 
41  const std::vector<RealParameter*> pars = node.parameterPool()->parameters();
42  if (pars.empty())
43  return {};
44 
45  result << " (";
46  size_t index(0);
47  for (auto par : pars) {
48  result << "'" << par->getName() << "':" << par->value();
49  ++index;
50  if (index != pars.size())
51  result << " ";
52  }
53  result << ")";
54 
55  return result.str();
56 }
57 
58 // Returns a string representing given node.
59 std::string nodeString(const INode& node, int depth)
60 {
61  std::ostringstream result;
62  result << s_indent(depth) << node.displayName() << poolToString(node) << "\n";
63  return result.str();
64 }
65 } // namespace
66 
67 std::string NodeUtils::nodeToString(const INode& node)
68 {
69  std::ostringstream result;
70 
72  it.first();
73  while (!it.isDone()) {
74  const INode* child = it.getCurrent();
75  result << nodeString(*child, it.depth() - 1);
76  it.next();
77  }
78 
79  return result.str();
80 }
81 
82 std::string NodeUtils::nodePath(const INode& node, const INode* root)
83 {
84  std::vector<std::string> pathElements;
85  const INode* current = &node;
86  while (current && current != root) {
87  pathElements.push_back(current->displayName());
88  pathElements.push_back("/");
89  current = current->parent();
90  }
91 
92  if (root != nullptr && current != root) {
93  throw Exceptions::RuntimeErrorException("NodeUtils::nodePath() -> Error. Node doesn't "
94  "belong to root's branch");
95  }
96 
97  std::reverse(pathElements.begin(), pathElements.end());
98  std::ostringstream result;
99  std::copy(pathElements.begin(), pathElements.end(), std::ostream_iterator<std::string>(result));
100  return result.str();
101 }
Defines many exception classes in namespace Exceptionss.
Defines class IterationStrategy and children.
Defines classes IteratorState, IteratorMemento and NodeIterator.
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:105
const INode * parent() const
Definition: INode.cpp:74
ParameterPool * parameterPool() const
Returns pointer to the parameter pool.
Iterator through INode tree of objects.
Definition: NodeIterator.h:90
int depth() const
Definition: NodeIterator.h:132
const INode * getCurrent()
Definition: NodeIterator.h:122
bool isDone() const
Definition: NodeIterator.h:127
const std::vector< RealParameter * > parameters() const
Returns full vector of parameters.
Definition: ParameterPool.h:52
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:82
std::string nodeToString(const INode &node)
Returns multiline string representing tree structure starting from given node.
Definition: NodeUtils.cpp:67
std::string nodeString(const INode &node, int depth)
Definition: NodeUtils.cpp:59
std::string s_indent(int depth)
Definition: NodeUtils.cpp:30
std::string poolToString(const INode &node)
Definition: NodeUtils.cpp:37