57 | | == Connecting to the Hierarchy == |
58 | | |
59 | | - ''tbw'' |
| 57 | == Subset Definition == |
| 58 | |
| 59 | To perform lookups in the hierarchy, you need to define a subset, which is an S3Hierarchy instance. |
| 60 | |
| 61 | With the tablename as only parameter, the subset would include all records in the hierarchical table (...which are accessible for the user): |
| 62 | |
| 63 | {{{#!python |
| 64 | subset = S3Hierarchy("hierarchical_type_table") |
| 65 | }}} |
| 66 | |
| 67 | To filter the records, a filter query can be specified as keyword parameter: |
| 68 | |
| 69 | {{{#!python |
| 70 | query = (FS("filter_field") == 5) |
| 71 | subset = S3Hierarchy("hierarchical_type_table, filter=query) |
| 72 | }}} |
| 73 | |
| 74 | == Performing Lookups == |
| 75 | |
| 76 | All lookup attributes or methods of the subset use node IDs. |
| 77 | |
| 78 | The node IDs are either the record IDs (for simple self-reference) or the super-IDs (for super-entity self-reference) of the records in the subset. |
| 79 | |
| 80 | All lookup attributes and methods return either a single node ID (long), or a '''set''' of node IDs. The only exception is path() which returns an ordered list of node IDs. |
| 81 | |
| 82 | === Root Nodes === |
| 83 | |
| 84 | To get all root nodes of the subset, use: |
| 85 | |
| 86 | {{{#!python |
| 87 | # Returns a set of node IDs |
| 88 | root_nodes = subset.roots |
| 89 | }}} |
| 90 | |
| 91 | To get the root node for a particular node, use: |
| 92 | |
| 93 | {{{#!python |
| 94 | # Returns the root node ID for node_id (or node_id if it is a root node itself) |
| 95 | root = subset.root(node_id) |
| 96 | }}} |
| 97 | |
| 98 | === Child Nodes === |
| 99 | |
| 100 | To get all child nodes of a node, use: |
| 101 | |
| 102 | {{{#!python |
| 103 | # Returns the first generation of child nodes for node_id |
| 104 | children = subset.children(node_id) |
| 105 | }}} |
| 106 | |
| 107 | To get all descendants of a node, use: |
| 108 | |
| 109 | {{{#!python |
| 110 | # Returns all descendant nodes (any generation) for node_id |
| 111 | children = subset.findall(node_id) |
| 112 | }}} |
| 113 | |
| 114 | It is possible to use findall to get a union set of descendants for multiple parent nodes: |
| 115 | {{{#!python |
| 116 | # Returns all descendants in all specified nodes |
| 117 | children = subset.findall((node_id_1, node_id_2, node_id_3)) |
| 118 | }}} |
| 119 | |
| 120 | === Parent Nodes === |
| 121 | |
| 122 | To get the parent node ID for a node, use: |
| 123 | |
| 124 | {{{#!python |
| 125 | # Returns the parent node ID for node_id (or None if node_id is a root node) |
| 126 | parent = subset.parent(node_id) |
| 127 | }}} |
| 128 | |
| 129 | === Sibling Nodes === |
| 130 | |
| 131 | To get all sibling node IDs for a node, use: |
| 132 | |
| 133 | {{{#!python |
| 134 | # Returns all sibling node IDs for node_id |
| 135 | siblings = subset.siblings(node_id) |
| 136 | }}} |
| 137 | |
| 138 | This does not normally return node_id itself, unless you specify inclusive=True: |
| 139 | |
| 140 | {{{#!python |
| 141 | # Returns all sibling node IDs for node_id - including node_id itself |
| 142 | siblings = subset.siblings(node_id, inclusive=True) |
| 143 | }}} |
| 144 | |
| 145 | === Path === |
| 146 | |
| 147 | The path of a node is an ordered list of all generations of parent node IDs from the root node down to the node itself. It can be requested by: |
| 148 | |
| 149 | {{{#!python |
| 150 | # Returns the path of a node (root node first) as ordered list |
| 151 | path = subset.path(node_id) |
| 152 | }}} |
| 153 | |
| 154 | == Using categories == |
| 155 | |
| 156 | Categories can be used to classify nodes "horizontally", e.g. to indicate a hierarchy "level". To use them with the hierarchy toolkit, an additional category-field must be defined in the hierarchy configuration: |
| 157 | |
| 158 | {{{#!python |
| 159 | self.configure(tablename, hierarchy=(parent_field, category_field)) |
| 160 | }}} |
| 161 | |
| 162 | Categories are neither managed nor inferred by the hierarchy toolkit, but they can be used to filter the lookup axis. |
| 163 | |
| 164 | === Filtering the Lookup Axis === |
| 165 | |
| 166 | This is useful e.g. to find all descendants of a node of a specific category: |
| 167 | |
| 168 | {{{#!python |
| 169 | # Define a hierarchy of locations with "parent" as parent-reference and "level" as category |
| 170 | subset = S3Hierarchy("gis_location", hierarchy=("parent", "level")) |
| 171 | |
| 172 | # Lookup all descendants of location #3 with category "L3" |
| 173 | communes = subset.findall(3, category="L3", inclusive=True) |
| 174 | }}} |
| 175 | |
| 176 | When performing a root lookup, we may be interested in the closest parent of a particular category rather than the absolute root: |
| 177 | |
| 178 | {{{#!python |
| 179 | # Lookup the closest "L1" parent of location #454 |
| 180 | state = subset.root(454, category="L1") |
| 181 | }}} |
| 182 | |
| 183 | This does also work with path lookups: |
| 184 | |
| 185 | {{{#!python |
| 186 | # Lookup the path of location #378 down from the closest "L1" parent |
| 187 | path = subset.path(378, category="L1") |
| 188 | }}} |
| 189 | |
| 190 | The category parameter can be used analogously with the children() and siblings() methods. |
| 191 | |
| 192 | === Looking up the Category of a Node === |
| 193 | |
| 194 | To lookup the category of a node, use: |
| 195 | |
| 196 | {{{#!python |
| 197 | # Returns the category for node_id (e.g. "L1") |
| 198 | category = subset.category(node_id) |
| 199 | }}} |
| 200 | |
| 201 | To get the category of each node in the result of the parent(), root(), path(), children(), findall(), or siblings() methods, use the classify-flag like: |
| 202 | |
| 203 | {{{#!python |
| 204 | # Returns the children of location #328 as set of tuples like: set([(367, "L3"), (368, "L3")]) |
| 205 | children = subset.children(328, classify=True) |
| 206 | }}} |
| 207 | |