Skip to content

get_hierarchy_info.py§

build_hierarchy(relationships) §

Build a hierarchical tree structure from a list of parent-child relationships.

Parameters:

Name Type Description Default
relationships List[Tuple[str, str]]

A list of tuples representing parent-child relationships.

required

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: A dictionary representing the hierarchical tree structure.

Source code in brickllm/utils/get_hierarchy_info.py
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
def build_hierarchy(relationships: List[Tuple[str, str]]) -> Dict[str, Any]:
    """
    Build a hierarchical tree structure from a list of parent-child relationships.

    Args:
        relationships (List[Tuple[str, str]]): A list of tuples representing parent-child relationships.

    Returns:
        Dict[str, Any]: A dictionary representing the hierarchical tree structure.
    """

    # Helper function to recursively build the tree structure
    def build_tree(node: str, tree_dict: Dict[str, List[str]]) -> Dict[str, Any]:
        return (
            {
                "name": node,
                "children": [build_tree(child, tree_dict) for child in tree_dict[node]],
            }
            if tree_dict[node]
            else {"name": node, "children": []}
        )

    # Create a dictionary to hold parent-children relationships
    tree_dict: Dict[str, List[str]] = defaultdict(list)
    nodes = set()

    # Fill the dictionary with data from relationships
    for parent, child in relationships:
        tree_dict[parent].append(child)
        nodes.update([parent, child])

    # Find the root (a node that is never a child)
    root_candidates = {
        node for node in nodes if node not in {child for _, child in relationships}
    }
    if not root_candidates:
        raise ValueError("No root found in relationships")
    root = next(iter(root_candidates))

    # Build the hierarchical structure starting from the root
    hierarchy = build_tree(root, tree_dict)
    return hierarchy

create_hierarchical_dict(elements, properties=False) §

Create a hierarchical dictionary from a list of elements, optionally including properties.

Parameters:

Name Type Description Default
elements List[str]

A list of elements to include in the hierarchy.

required
properties bool

Whether to include properties in the hierarchy. Defaults to False.

False

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: A dictionary representing the hierarchical structure.

Source code in brickllm/utils/get_hierarchy_info.py
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
def create_hierarchical_dict(
    elements: List[str], properties: bool = False
) -> Dict[str, Any]:
    """
    Create a hierarchical dictionary from a list of elements, optionally including properties.

    Args:
        elements (List[str]): A list of elements to include in the hierarchy.
        properties (bool, optional): Whether to include properties in the hierarchy. Defaults to False.

    Returns:
        Dict[str, Any]: A dictionary representing the hierarchical structure.
    """
    hierarchy: Dict[str, Any] = {}

    for category in elements:
        parents, _ = get_hierarchical_info(category)
        current_level = hierarchy

        for parent in parents:
            if parent not in current_level:
                current_level[parent] = {}
            current_level = current_level[parent]

        # Finally add the category itself
        if category not in current_level:
            if properties:
                elem_property = general_query(category)
                if len(elem_property.keys()) == 0:
                    continue
                elem_property = elem_property["property"]
                # remove "message" key from the dictionary
                for prop in elem_property.keys():
                    elem_property[prop].pop("message")
                current_level[category] = {"properties": elem_property}
            else:
                current_level[category] = {}

    return hierarchy

extract_ttl_content(input_string) §

Extract content between code block markers in a string.

Parameters:

Name Type Description Default
input_string str

The input string containing code blocks.

required

Returns:

Name Type Description
str str

The extracted content between the code block markers.

Source code in brickllm/utils/get_hierarchy_info.py
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
def extract_ttl_content(input_string: str) -> str:
    """
    Extract content between code block markers in a string.

    Args:
        input_string (str): The input string containing code blocks.

    Returns:
        str: The extracted content between the code block markers.
    """
    # Use regex to match content between ```code and ```
    match = re.search(r"```code\s*(.*?)\s*```", input_string, re.DOTALL)
    if match:
        return match.group(1).strip()
    return ""

filter_elements(elements) §

Filter elements based on their hierarchical relationships.

Parameters:

Name Type Description Default
elements List[str]

A list of elements to filter.

required

Returns:

Type Description
List[str]

List[str]: A list of filtered elements.

Source code in brickllm/utils/get_hierarchy_info.py
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def filter_elements(elements: List[str]) -> List[str]:
    """
    Filter elements based on their hierarchical relationships.

    Args:
        elements (List[str]): A list of elements to filter.

    Returns:
        List[str]: A list of filtered elements.
    """
    elements_info = {element: get_hierarchical_info(element) for element in elements}
    filtered_elements = []

    for element, (parents, children) in elements_info.items():
        # Discard elements with no parents and no children
        if not parents and not children:
            continue
        # Check if the element is a parent of any other element
        is_parent = any(element in p_list for p_list, _ in elements_info.values())
        if is_parent:
            continue
        filtered_elements.append(element)

    return filtered_elements

find_parents(current_data, target, parents=None) §

Recursively find the parent nodes of a target node in a hierarchical data structure.

Parameters:

Name Type Description Default
current_data Dict[str, Any]

The current level of the hierarchy to search.

required
target str

The target node to find parents for.

required
parents Optional[List[str]]

Accumulated list of parent nodes. Defaults to None.

None

Returns:

Type Description
Tuple[bool, List[str]]

Tuple[bool, List[str]]: A tuple containing a boolean indicating if the target was found and a list of parent nodes.

Source code in brickllm/utils/get_hierarchy_info.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def find_parents(
    current_data: Dict[str, Any], target: str, parents: Optional[List[str]] = None
) -> Tuple[bool, List[str]]:
    """
    Recursively find the parent nodes of a target node in a hierarchical data structure.

    Args:
        current_data (Dict[str, Any]): The current level of the hierarchy to search.
        target (str): The target node to find parents for.
        parents (Optional[List[str]], optional): Accumulated list of parent nodes. Defaults to None.

    Returns:
        Tuple[bool, List[str]]: A tuple containing a boolean indicating if the target was found and a list of parent nodes.
    """
    if parents is None:
        parents = []
    for key, value in current_data.items():
        if key == target:
            return True, parents
        if isinstance(value, dict):
            found, result = find_parents(value, target, parents + [key])
            if found:
                return True, result
    return False, []

find_sensor_paths(tree, path=None) §

Find paths to sensor nodes in a hierarchical tree structure.

Parameters:

Name Type Description Default
tree Dict[str, Any]

The hierarchical tree structure.

required
path Optional[List[str]]

Accumulated path to the current node. Defaults to None.

None

Returns:

Type Description
List[Dict[str, str]]

List[Dict[str, str]]: A list of dictionaries containing sensor names and their paths.

Source code in brickllm/utils/get_hierarchy_info.py
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
def find_sensor_paths(
    tree: Dict[str, Any], path: Optional[List[str]] = None
) -> List[Dict[str, str]]:
    """
    Find paths to sensor nodes in a hierarchical tree structure.

    Args:
        tree (Dict[str, Any]): The hierarchical tree structure.
        path (Optional[List[str]], optional): Accumulated path to the current node. Defaults to None.

    Returns:
        List[Dict[str, str]]: A list of dictionaries containing sensor names and their paths.
    """
    if path is None:
        path = []

    current_path = path + [tree.get("name", "")]
    if "children" not in tree or not tree["children"]:
        if re.search(r"Sensor", tree.get("name", "")):
            sensor_path = ">".join(current_path[:-1])
            return [{"name": tree.get("name", ""), "path": sensor_path}]
        return []

    sensor_paths = []
    for child in tree["children"]:
        sensor_paths.extend(find_sensor_paths(child, current_path))

    return sensor_paths

flatten_hierarchy(current_data, parent=None, result=None) §

Flatten a hierarchical data structure into a list of parent-child tuples.

Parameters:

Name Type Description Default
current_data Dict[str, Any]

The current level of the hierarchy to flatten.

required
parent Optional[str]

The parent node. Defaults to None.

None
result Optional[List[Tuple[str, str]]]

Accumulated list of parent-child tuples. Defaults to None.

None

Returns:

Type Description
List[Tuple[str, str]]

List[Tuple[str, str]]: A list of tuples representing parent-child relationships.

Source code in brickllm/utils/get_hierarchy_info.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def flatten_hierarchy(
    current_data: Dict[str, Any],
    parent: Optional[str] = None,
    result: Optional[List[Tuple[str, str]]] = None,
) -> List[Tuple[str, str]]:
    """
    Flatten a hierarchical data structure into a list of parent-child tuples.

    Args:
        current_data (Dict[str, Any]): The current level of the hierarchy to flatten.
        parent (Optional[str], optional): The parent node. Defaults to None.
        result (Optional[List[Tuple[str, str]]], optional): Accumulated list of parent-child tuples. Defaults to None.

    Returns:
        List[Tuple[str, str]]: A list of tuples representing parent-child relationships.
    """
    if result is None:
        result = []
    for key, value in current_data.items():
        if parent:
            result.append((parent, key))
        if isinstance(value, dict):
            flatten_hierarchy(value, key, result)
    return result

get_all_subchildren(current_data, target) §

Recursively get all children and subchildren of a target node.

Parameters:

Name Type Description Default
current_data Dict[str, Any]

The current level of the hierarchy to search.

required
target str

The target node to find children for.

required

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: A dictionary representing the subtree of the target node.

Source code in brickllm/utils/get_hierarchy_info.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def get_all_subchildren(current_data: Dict[str, Any], target: str) -> Dict[str, Any]:
    """
    Recursively get all children and subchildren of a target node.

    Args:
        current_data (Dict[str, Any]): The current level of the hierarchy to search.
        target (str): The target node to find children for.

    Returns:
        Dict[str, Any]: A dictionary representing the subtree of the target node.
    """
    if target in current_data:
        sub_tree = current_data[target]
        if isinstance(sub_tree, dict):
            return sub_tree
        else:
            return {}
    for key, value in current_data.items():
        if isinstance(value, dict):
            result = get_all_subchildren(value, target)
            if result:
                return result
    return {}

get_children(current_data, target) §

Get the children of a target node in a hierarchical data structure.

Parameters:

Name Type Description Default
current_data Dict[str, Any]

The current level of the hierarchy to search.

required
target str

The target node to find children for.

required

Returns:

Type Description
List[str]

List[str]: A list of child nodes.

Source code in brickllm/utils/get_hierarchy_info.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def get_children(current_data: Dict[str, Any], target: str) -> List[str]:
    """
    Get the children of a target node in a hierarchical data structure.

    Args:
        current_data (Dict[str, Any]): The current level of the hierarchy to search.
        target (str): The target node to find children for.

    Returns:
        List[str]: A list of child nodes.
    """
    if target in current_data:
        return list(current_data[target].keys())
    for key, value in current_data.items():
        if isinstance(value, dict):
            children = get_children(value, target)
            if children:
                return children
    return []

get_children_hierarchy(key, flatten=False) §

Get the hierarchy of children for a target node, optionally flattening the result.

Parameters:

Name Type Description Default
key str

The target node to get children for.

required
flatten bool

Whether to flatten the hierarchy. Defaults to False.

False

Returns:

Type Description
Union[Dict[str, Any], List[Tuple[str, str]]]

Union[Dict[str, Any], List[Tuple[str, str]]]: A dictionary representing the hierarchy or a list of parent-child tuples if flattened.

Source code in brickllm/utils/get_hierarchy_info.py
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def get_children_hierarchy(
    key: str, flatten: bool = False
) -> Union[Dict[str, Any], List[Tuple[str, str]]]:
    """
    Get the hierarchy of children for a target node, optionally flattening the result.

    Args:
        key (str): The target node to get children for.
        flatten (bool, optional): Whether to flatten the hierarchy. Defaults to False.

    Returns:
        Union[Dict[str, Any], List[Tuple[str, str]]]: A dictionary representing the hierarchy or a list of parent-child tuples if flattened.
    """
    if flatten:
        return flatten_hierarchy(get_all_subchildren(data, key))
    return get_all_subchildren(data, key)

get_hierarchical_info(key) §

Get the hierarchical information of a node, including its parents and children.

Parameters:

Name Type Description Default
key str

The target node to get information for.

required

Returns:

Type Description
Tuple[List[str], List[str]]

Tuple[List[str], List[str]]: A tuple containing a list of parent nodes and a list of child nodes.

Source code in brickllm/utils/get_hierarchy_info.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def get_hierarchical_info(key: str) -> Tuple[List[str], List[str]]:
    """
    Get the hierarchical information of a node, including its parents and children.

    Args:
        key (str): The target node to get information for.

    Returns:
        Tuple[List[str], List[str]]: A tuple containing a list of parent nodes and a list of child nodes.
    """
    # Get parents
    found, parents = find_parents(data, key)
    # Get children
    children = get_children(data, key)
    return (parents, children)