SPARQL builder¶
app.sparql_builder
¶
SPARQL generation from EntityShape descriptors plus the active filters.
Subject(var, type_var, suffix, declare_type)
dataclass
¶
Which variable a set of filter clauses constrains.
Filters read the same for a pin the map draws and for the pin that puts a
region on the map, but they cannot share variable names: the region branch
nests its copy inside FILTER EXISTS, where the outer ?type is already
bound to compass:CountryArea.
Attributes:
| Name | Type | Description |
|---|---|---|
var |
str
|
Subject variable (e.g. |
type_var |
str
|
Variable holding the entity class IRI. |
suffix |
str
|
Keeps helper variables distinct across the two copies. |
declare_type |
bool
|
Bind |
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
17 18 19 20 21 22 23 24 25 26 27 28 29 | |
_is_iri_value(value)
¶
True when a filter value names a concept rather than a literal tag.
A tag dimension can be filtered either by concept IRI or by literal text, so the two are told apart by shape. The IRI check is the grammar's, not a guess: a value that cannot be written as an 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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
build_optional(spec, lang)
¶
Build the OPTIONAL clause that binds one EntityShape property.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
EntityShape
|
Property descriptor from SHACL. |
required |
lang
|
str
|
Preferred language for labels / langString filters. |
required |
Returns:
| Type | Description |
|---|---|
str
|
SPARQL OPTIONAL fragment, or empty string for unknown categories. |
Source code in src/backend/app/sparql_builder.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
build_select_expr(spec)
¶
GROUP_CONCAT for multi-valued properties, SAMPLE for single-valued ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
_pin_branch(where_clauses, indent=' ')
¶
Build the UNION of the four entity classes that carry coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where_clauses
|
list[str]
|
Extra FILTER / pattern lines applied to each pin. |
required |
indent
|
str
|
Leading whitespace for generated lines. |
' '
|
Returns:
| Type | Description |
|---|---|
str
|
SPARQL WHERE fragment for map pins. |
Source code in src/backend/app/sparql_builder.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
_region_branch(where_clauses, indent=' ')
¶
Build the Country/Area branch reachable only through a matching pin.
A region is a shaded polygon rather than a result, and it carries no tags of its own: it reaches the map because some pin passing the same filters records it, so shading always means "matching pins are in here".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where_clauses
|
list[str]
|
Filter clauses applied to the nested |
required |
indent
|
str
|
Leading whitespace for generated lines. |
' '
|
Returns:
| Type | Description |
|---|---|
str
|
SPARQL WHERE fragment for shaded regions. |
Source code in src/backend/app/sparql_builder.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
_shared_optionals(lang)
¶
Geometry and label binding, applied to pins and regions alike.
Regions have no coordinates and label themselves with skos:prefLabel, so geometry is OPTIONAL and the label is COALESCEd across both properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lang
|
str
|
Preferred language tag. |
required |
Returns:
| Type | Description |
|---|---|
str
|
SPARQL OPTIONAL / BIND / FILTER block. |
Source code in src/backend/app/sparql_builder.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
_special_optionals()
¶
Return OPTIONAL patterns for properties not declared on entity NodeShapes.
Returns:
| Type | Description |
|---|---|
str
|
SPARQL fragment fetching |
Source code in src/backend/app/sparql_builder.py
203 204 205 206 207 208 209 210 211 | |
_special_selects()
¶
Return SELECT projections for special (non-SHACL) properties.
Returns:
| Type | Description |
|---|---|
str
|
SPARQL SELECT fragment for |
Source code in src/backend/app/sparql_builder.py
214 215 216 217 218 219 220 | |
_union_or_single(parts)
¶
Join alternative graph patterns with UNION, or return the sole pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parts
|
list[str]
|
Individual pattern strings. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A single pattern or a braced UNION of several. |
Source code in src/backend/app/sparql_builder.py
223 224 225 226 227 228 229 230 231 232 233 234 | |
_build_where_clauses(query_params, filter_map, range_filters, date_filters, subject=PIN, exclude_key=None)
¶
Translate HTTP query params into SPARQL WHERE fragments.
exclude_key drops that dimension's own constraints, so facet counts for a
dimension are not shrunk by the selection within it (drill-down faceting).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query_params
|
Any
|
Starlette/FastAPI query parameter multi-dict. |
required |
filter_map
|
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 |
subject
|
Subject
|
Variable naming for pin vs region-pin copies. |
PIN
|
exclude_key
|
str | None
|
Dimension id to ignore (faceting). |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of SPARQL pattern / FILTER lines. |
Source code in src/backend/app/sparql_builder.py
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 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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |
_categorize_specs(specs)
¶
Split EntityShape list into multiselect, range, and date filter maps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
specs
|
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
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
build_facet_query(specs, lang, query_params, target_id)
¶
Count entities per value of one tag dimension.
Regions are background context rather than results (see the map's result badge, which counts point features only), so only the pin branch is counted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
specs
|
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
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 378 379 380 381 382 383 384 385 386 | |
sparql_for_instances(specs, lang, query_params)
¶
Compile the main entity SELECT (pins UNION regions) for the map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
specs
|
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
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |