SPARQL builder¶
app.sparql_builder
¶
SPARQL generation from EntityShape descriptors plus the active filters.
Every generated pattern constrains ?s, the pin, whose class is bound to
?type.
to_prefixed(iri)
¶
Convert a full IRI to a SPARQL prefixed name (e.g. compass:country).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iri
|
str
|
Absolute property IRI. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Prefixed name when the namespace is known, otherwise an |
Source code in src/backend/app/sparql_builder.py
31 32 33 34 35 36 37 38 39 40 41 42 43 | |
_is_iri_value(value)
¶
Tell a concept IRI filter value from a literal one.
A value that is not a safe IRIREF is treated as a literal rather than interpolated between brackets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Raw query-parameter value. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether value is a safe absolute HTTP(S) IRI. |
Source code in src/backend/app/sparql_builder.py
46 47 48 49 50 51 52 53 54 55 56 57 58 | |
build_optional(shape, lang)
¶
Build the OPTIONAL clause that binds one EntityShape property.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
EntityShape
|
Property descriptor from SHACL. |
required |
lang
|
str
|
Preferred language for labels / langString filters. |
required |
Returns:
| Type | Description |
|---|---|
str
|
SPARQL OPTIONAL fragment. |
Source code in src/backend/app/sparql_builder.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
_group_concat(shape)
¶
Build the GROUP_CONCAT folding a multi-valued property into ?<id>Agg.
Source code in src/backend/app/sparql_builder.py
101 102 103 104 105 106 107 108 | |
build_select_expr(shape)
¶
Build the SELECT projection sampling one property from its group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
EntityShape
|
Property descriptor from SHACL. |
required |
Returns:
| Type | Description |
|---|---|
str
|
SELECT projection expression(s) for this property. |
Source code in src/backend/app/sparql_builder.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
_class_union(names, indent)
¶
Build the UNION of class branches, each binding ?type to its class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
tuple[str, ...]
|
Local names of the entity classes to match. |
required |
indent
|
str
|
Leading whitespace for generated lines. |
required |
Returns:
| Type | Description |
|---|---|
str
|
SPARQL group pattern alternatives, without a trailing newline. |
Source code in src/backend/app/sparql_builder.py
131 132 133 134 135 136 137 138 139 140 141 142 143 | |
_pin_branch(where_clauses, *, with_always_on=False)
¶
Build the UNION of the entity classes that carry coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where_clauses
|
list[str]
|
Extra FILTER / pattern lines applied to each filtered pin. |
required |
with_always_on
|
bool
|
Also emit |
False
|
Returns:
| Type | Description |
|---|---|
str
|
SPARQL WHERE fragment for map pins. |
Source code in src/backend/app/sparql_builder.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
_shared_optionals(lang)
¶
Bind geometry and labels for every pin.
Coordinates stay OPTIONAL so a pin missing them reaches the decoder, which logs it. The name is required, so a pin without one in lang drops out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lang
|
str
|
Preferred language tag. |
required |
Returns:
| Type | Description |
|---|---|
str
|
SPARQL OPTIONAL / FILTER block. |
Source code in src/backend/app/sparql_builder.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
_build_where_clauses(query_params, value_filters, range_filters, date_filters, *, exclude_key=None)
¶
Translate HTTP query params into SPARQL WHERE fragments.
Values within one tag dimension, and dimensions with each other, combine
with AND. entityType values combine with OR: an entity has exactly one
class, so requiring two would match nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query_params
|
Any
|
Starlette/FastAPI query parameter multi-dict. |
required |
value_filters
|
dict[str, str]
|
Multiselect/toggle property id -> prefixed predicate. |
required |
range_filters
|
RangeFilters
|
Slider property id -> (predicate, datatype). |
required |
date_filters
|
dict[str, str]
|
Datepicker property id -> prefixed predicate. |
required |
exclude_key
|
str | None
|
Dimension id whose own constraints are dropped. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of SPARQL pattern / FILTER lines. |
Source code in src/backend/app/sparql_builder.py
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 226 227 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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
_categorize_shapes(shapes)
¶
Split EntityShapes into value, range, and date filter maps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapes
|
list[EntityShape]
|
SHACL-projected property descriptors. |
required |
Returns:
| Type | Description |
|---|---|
tuple[dict[str, str], RangeFilters, dict[str, str]]
|
Tuple of |
Source code in src/backend/app/sparql_builder.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
build_facet_query(shapes, lang, query_params, target_id)
¶
Count entities per value of one dimension.
A count reads "how many results if I also pick this", so a tag dimension
keeps its own selection. entityType drops its own picks instead:
being disjunctive, every unpicked class would otherwise count zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapes
|
list[EntityShape]
|
EntityShape list for the ontology. |
required |
lang
|
str
|
Preferred language for shared optionals. |
required |
query_params
|
Any
|
Active filter query parameters. |
required |
target_id
|
str
|
Dimension whose values are counted. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Complete SPARQL SELECT counting |
Source code in src/backend/app/sparql_builder.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
sparql_for_instances(shapes, lang, query_params)
¶
Compile the main entity SELECT for the map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapes
|
list[EntityShape]
|
EntityShape list for the ontology. |
required |
lang
|
str
|
Preferred language. |
required |
query_params
|
Any
|
Active filter query parameters. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Complete SPARQL SELECT returning one grouped row per entity. |
Source code in src/backend/app/sparql_builder.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |