ModelSpace
All Classes Namespaces Functions Variables Enumerations Pages
GraphTreeObject.h
1/******************************************************************************
2* Copyright (c) ATTX LLC 2024. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, LLC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, LLC.
15******************************************************************************/
16/*
17Graph tree header file
18
19Author: Alex Reynolds
20*/
21#ifndef GRAPH_TREE_H
22#define GRAPH_TREE_H
23
24#include <vector>
25#include <string>
26#include <stdio.h>
27#include <stdlib.h>
28#include <stdint.h>
29
30#include "core/clockwerkerrors.h"
31
32namespace clockwerk {
33
34 /// This enumeration defines the possible derivations of the graph tree object
35 /// which may be used to store helpful information on it. It's messy but it works
36 /// well. Derivations may be added here as they are developed. If not, they will
37 /// default to not defined.
38 enum graph_tree_types_e : unsigned char {
39 BASE_GRAPH_TREE,
40 DATAIO,
41 TASK,
42 EXECUTIVE,
43 FRAME,
44 BODY,
45 NODE
46 };
47
48 const std::string DELIMETER = ".";
49 const std::string DEFAULT_NAME = "_";
50
51 /// @brief Base class for object organization
52 ///
53 /// This file defines a base class for data organization into a format
54 /// we're calling here a "graph tree". The graph tree is set up to connect
55 /// a single parent to any number of children, which may be done upon construction
56 /// or at runtime. Graph trees allow connections between any number of inherited
57 /// objects and their parents/children, allowing for recursive math and search
58 /// functionality. In essence, the class is a doubly linked list with multiple
59 /// children and search functionality.
60 ///
61 /// Parent
62 /// |
63 /// ---------------------------------------
64 /// | | | |
65 /// Child 1 Child 2 Child 3 Child 4
66 ///
67 /// Also, note that trees objects are entirely independent of their position in
68 /// that tree, so a child can be treated as if it were root in the case where
69 /// a local search should be performed.
70 ///
71 /// String Address Functionality
72 /// String addressing is one of the most critical functions of the graph tree.
73 /// It allows for easy access to data, in addition to search and write functions.
74 /// String addresses are constructed from the names of individual objects on the
75 /// tree, separated by a delimiter defined in this file (a period by default). So
76 /// a standard string address could look like:
77 ///
78 /// .firstObj.secondObj.thirdObj.otherThing
79 ///
80 /// which would indicate four levels of heirarchy on the tree, with otherThing
81 /// being the furthest down on the tree. The object at the location otherThing
82 /// can be accessed by requesting the string address.
83 ///
84 /// Graph tree has the following restrictions driven by data size:
85 /// Number of children -- restricted by variable size and uint8 size
86 /// Number of descendants -- restricted by uint16 size
87 class GraphTreeObject {
88 public:
89 /// @brief Constructor with name for graph tree object
90 GraphTreeObject(const std::string &name = "");
91
92 /// Destructor
93 virtual ~GraphTreeObject() {}
94
95 /// Functions to get object's parent/children
96 GraphTreeObject* parent() {return _parent;}
97 std::vector<GraphTreeObject*>& children() {return _children;}
98
99 /// @brief Function to assign the node's parent via pointer
100 /// @param new_parent The new parent to assign the node to
101 /// @return Error code corresponding to success/failure
102 /// @note This function is the only method to ADD or REMOVE a node
103 /// from a tree. To add, pass the address to a GraphTreeObject
104 /// to this function. To remove, pass a nullptr. Note that
105 /// parents for objects on an existing tree can be reassigned
106 int parent(GraphTreeObject* new_parent);
107
108 /// @brief Function to assign the node's parent via reference
109 /// @param new_parent The new parent to assign the node to
110 /// @return Error code corresponding to success/failure
111 /// @note This function is the only method to ADD or REMOVE a node
112 /// from a tree. To add, pass the address to a GraphTreeObject
113 /// to this function. To remove, pass a nullptr. Note that
114 /// parents for objects on an existing tree can be reassigned
115 int parent(GraphTreeObject& new_parent);
116
117 /// Getters for number of children and descendants
118 unsigned int nChildren() {return _num_children;}
119 unsigned int nDescendants() {return _num_descendants;}
120
121 /// @brief Getter and setter for object name
122 std::string name() const {return _name;}
123 int name(const std::string &new_name);
124
125 /// @brief Getter for the object rank
126 int rank() {return _rank;}
127
128 /// @brief Function to indicate type -- -1 by default unless implemented downstream
129 /// @return Int indicating frame type -- unimplemented always -1
130 virtual int type() {return _graph_tree_type;}
131
132 /// @brief Getter for object logability flag -- note no setter because should
133 /// only be set by this or a derived class in its definition
134 bool loggable() {return _loggable;}
135
136 /// @brief Getter for object's string address
137 /// @return Returns the string address for the object
138 std::string address();
139
140 /// @brief Function to search through the graph tree by string address
141 /// @param address `The address or address chunk to search for
142 /// @return A vector of string addresses meeting the criteria specified by address
143 std::vector<std::string> search(const std::string &address);
144
145 /// @brief Function to get an object from the graph tree by string address
146 /// @param address The exact string address to access the variable with
147 /// @return Pointer to the graph tree object at the address, or nullptr if
148 /// address is not valid or returns
149 GraphTreeObject* getByAddress(const std::string &address);
150
151 /// @brief Function to dump the graph node, optionally including descendents
152 /// @param recursive Parameter indicating whether to dump only one node (default/false)
153 /// or the entire tree (true)
154 /// @note Recursive dumps the whole tree... be careful what you wish for
155 void dump(bool recursive=false);
156
157 /// @brief Function to return all info associated with a given graph node
158 void graphNodeInfo();
159
160 /// @brief Function to recurse through graph tree and print all node info
161 /// @return String containing all recursive graph node information in tab cascade format
162 void recurseGraphNodeInfo(unsigned int counter);
163
164 /// @brief Function to pass headers for this object to the logger
165 /// @param logger Pointer to logger
166 /// @return Error code corresponding to success/failure
167 virtual int header(void* logger) {return WARN_NOT_LOGGABLE;}
168
169 virtual std::vector<std::string> header_info() const {return std::vector<std::string>();}
170
171 /// @brief Function to find all loggable parameters in this node and its children
172 /// @param matches Expandable vector to hold all matches in the tree
173 void findLoggable(std::vector<GraphTreeObject*> &matches);
174
175 /// @brief Function to log data to a logger
176 /// @param logger Pointer to logger
177 /// @return Error code corresponding to success/failure
178 virtual int log(void* logger) {return WARN_NOT_LOGGABLE;}
179
180 /// @brief Function to lock the graph tree object and its children to changes
181 /// @param recursive Flag to indicate whether lock should be recursive
182 void lock(bool recursive = true);
183
184 /// @brief Function to unlock the graph tree and its children for changes
185 /// @param recursive Flag to indicate whether unlock should be recursive
186 void unlock(bool recursive = true);
187
188 /// @brief Function to return whether graph tree object is locked
189 bool locked() {return _locked;}
190
191 protected:
192 /// @brief Function to recursively re-calculate the number of descendants
193 /// of a given node on the tree
195
196 /// @brief Function to recursively re-calculate the rank of a given node on the tree
197 void recalculateRank();
198
199 /// @brief Function to decompose a string into a series of substrings via indexing
200 /// @param address The address to be decomposed
201 /// @return A vector of strings containing each delimeter-separated substring
202 std::vector<std::string> decomposeAddress(std::string address);
203
204 /// @brief Function to find all addresses mathcing the set of substrings
205 /// @parem subaddresses A vector of subaddresses to match
206 /// @param num_subaddresses The number of subaddresses to match
207 /// @param index The index of subaddresses to search in
208 /// @param match_found Variable indicating whether a match was found higher in the tree
209 /// @param matches Implicit return of all matches to the specified address
210 void findMatches(const std::vector<std::string> &subaddresses, const unsigned int &num_subaddresses,
211 unsigned int index, bool match_found, std::vector<GraphTreeObject*> &matches);
212
213 /// @brief Function to search through the graph tree by string address
214 /// @param address `The address or address chunk to search for
215 /// @return A vector of objects addresses meeting the criteria specified by address
216 std::vector<GraphTreeObject*> searchNodes(const std::string &address);
217
218 /// @brief Function to add a child to the graph tree object
219 /// @param child A pointer to the graph tree object to add to the tree
220 /// @return An error code corresponding to success/failure
221 int addChild(GraphTreeObject* child);
222
223 /// @brief Function to remove a child from the graph node's children
224 /// @param child The child to remove
225 /// @return Success/fail error code based on whether the child is present
226 int removeChild(GraphTreeObject* child);
227
228 /// Pointer to the object's parent -- should be null if the object is the root.
229 /// Is set to null by default
230 GraphTreeObject* _parent = nullptr;
231
232 /// Pointers to the object's children -- automatically set to an empty vector,
233 /// and can be increased to any size
234 std::vector<GraphTreeObject*> _children;
235
236 /// String name for object
237 std::string _name = DEFAULT_NAME;
238
239 /// Total number of descendants (includes children of children) of the tree,
240 /// again not including the tree itself
241 uint16_t _num_descendants = 0;
242
243 /// Number of direct children of the tree (not including the tree itself)
244 uint8_t _num_children = 0;
245
246 /// Rank for how far down on the graph tree the object is -- starts at zero
247 /// and maxes out at 256 via variable size
248 uint8_t _rank = 0;
249
250 /// Variable to store graph tree object type
251 uint8_t _graph_tree_type = BASE_GRAPH_TREE;
252
253 /// Variable to lock the graph tree object and prevent structural updates
254 bool _locked = false;
255
256 /// Variable to indicate whether the selected object is loggable. Set to false by default
257 bool _loggable = false;
258 };
259
260}
261
262#endif
Base class for object organization.
Definition GraphTreeObject.h:87
unsigned int nChildren()
Getters for number of children and descendants.
Definition GraphTreeObject.h:118
void lock(bool recursive=true)
Function to lock the graph tree object and its children to changes.
Definition GraphTreeObject.cpp:329
void recalculateRank()
Function to recursively re-calculate the rank of a given node on the tree.
Definition GraphTreeObject.cpp:263
int rank()
Getter for the object rank.
Definition GraphTreeObject.h:126
virtual ~GraphTreeObject()
Destructor.
Definition GraphTreeObject.h:93
int addChild(GraphTreeObject *child)
Function to add a child to the graph tree object.
Definition GraphTreeObject.cpp:110
bool loggable()
Getter for object logability flag – note no setter because should only be set by this or a derived cl...
Definition GraphTreeObject.h:134
uint8_t _rank
Rank for how far down on the graph tree the object is – starts at zero and maxes out at 256 via varia...
Definition GraphTreeObject.h:248
void recurseGraphNodeInfo(unsigned int counter)
Function to recurse through graph tree and print all node info.
Definition GraphTreeObject.cpp:298
std::vector< GraphTreeObject * > searchNodes(const std::string &address)
Function to search through the graph tree by string address.
Definition GraphTreeObject.cpp:200
uint8_t _num_children
Number of direct children of the tree (not including the tree itself)
Definition GraphTreeObject.h:244
GraphTreeObject * _parent
Pointer to the object's parent – should be null if the object is the root. Is set to null by default.
Definition GraphTreeObject.h:230
virtual int header(void *logger)
Function to pass headers for this object to the logger.
Definition GraphTreeObject.h:167
int parent(GraphTreeObject &new_parent)
Function to assign the node's parent via reference.
Definition GraphTreeObject.cpp:62
void dump(bool recursive=false)
Function to dump the graph node, optionally including descendents.
Definition GraphTreeObject.cpp:277
std::string _name
String name for object.
Definition GraphTreeObject.h:237
bool _loggable
Variable to indicate whether the selected object is loggable. Set to false by default.
Definition GraphTreeObject.h:257
std::vector< std::string > decomposeAddress(std::string address)
Function to decompose a string into a series of substrings via indexing.
Definition GraphTreeObject.cpp:142
uint16_t _num_descendants
Total number of descendants (includes children of children) of the tree, again not including the tree...
Definition GraphTreeObject.h:241
GraphTreeObject * parent()
Functions to get object's parent/children.
Definition GraphTreeObject.h:96
virtual int type()
Function to indicate type – -1 by default unless implemented downstream.
Definition GraphTreeObject.h:130
std::vector< GraphTreeObject * > _children
Pointers to the object's children – automatically set to an empty vector, and can be increased to any...
Definition GraphTreeObject.h:234
std::string name() const
Getter and setter for object name.
Definition GraphTreeObject.h:122
int parent(GraphTreeObject *new_parent)
Function to assign the node's parent via pointer.
Definition GraphTreeObject.cpp:66
virtual int log(void *logger)
Function to log data to a logger.
Definition GraphTreeObject.h:178
std::string address()
Getter for object's string address.
Definition GraphTreeObject.cpp:52
void findMatches(const std::vector< std::string > &subaddresses, const unsigned int &num_subaddresses, unsigned int index, bool match_found, std::vector< GraphTreeObject * > &matches)
Function to find all addresses mathcing the set of substrings @parem subaddresses A vector of subaddr...
Definition GraphTreeObject.cpp:166
int removeChild(GraphTreeObject *child)
Function to remove a child from the graph node's children.
Definition GraphTreeObject.cpp:126
std::vector< std::string > search(const std::string &address)
Function to search through the graph tree by string address.
Definition GraphTreeObject.cpp:215
void findLoggable(std::vector< GraphTreeObject * > &matches)
Function to find all loggable parameters in this node and its children.
Definition GraphTreeObject.cpp:315
bool _locked
Variable to lock the graph tree object and prevent structural updates.
Definition GraphTreeObject.h:254
GraphTreeObject(const std::string &name="")
Constructor with name for graph tree object.
Definition GraphTreeObject.cpp:24
GraphTreeObject * getByAddress(const std::string &address)
Function to get an object from the graph tree by string address.
Definition GraphTreeObject.cpp:227
uint8_t _graph_tree_type
Variable to store graph tree object type.
Definition GraphTreeObject.h:251
void unlock(bool recursive=true)
Function to unlock the graph tree and its children for changes.
Definition GraphTreeObject.cpp:339
void recalculateDescendants()
Function to recursively re-calculate the number of descendants of a given node on the tree.
Definition GraphTreeObject.cpp:244
bool locked()
Function to return whether graph tree object is locked.
Definition GraphTreeObject.h:189
void graphNodeInfo()
Function to return all info associated with a given graph node.
Definition GraphTreeObject.cpp:286
#define WARN_NOT_LOGGABLE
Definition clockwerkerrors.h:76