ontocast.tool.sparql¶
SPARQL tool for incremental graph updates.
This module provides functionality for executing SPARQL operations on RDF graphs, enabling incremental updates instead of full graph replacement.
Attributes¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
SPARQLTool
¶
Tool for executing SPARQL operations on RDF graphs.
Source code in ontocast/tool/sparql.py
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 | |
Attributes¶
last_finalize_metrics = {}
instance-attribute
¶
triple_store_manager = triple_store_manager
instance-attribute
¶
Methods:¶
__init__(triple_store_manager=None)
¶
Initialize SPARQL tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triple_store_manager
|
TripleStoreManager | None
|
Optional triple store manager for persistent storage. |
None
|
Source code in ontocast/tool/sparql.py
aget_induced_subgraph(entity_uris, entity_relevance=None, entity_roles=None, ontology_iris=None, depth=1, max_total_triples=300, estimated_triples_per_query=24, ontology_version_filters=None, ontology_hash_filters=None, hub_seed_count=8, ancestor_closure_depth=3, ontologies=None, merged=None, type_promotion_score_factor=1.0, seed_order='score', entity_groups=None, extra_description_predicates=())
async
¶
Like get_induced_subgraph but uses afetch_ontologies for I/O.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontologies
|
list[Ontology] | None
|
Pre-fetched catalog to build from. When |
None
|
merged
|
tuple[RDFGraph, dict[str, str]] | None
|
Pre-merged |
None
|
Source code in ontocast/tool/sparql.py
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 | |
get_induced_subgraph(entity_uris, entity_relevance=None, entity_roles=None, ontology_iris=None, depth=1, max_total_triples=300, estimated_triples_per_query=24, ontology_version_filters=None, ontology_hash_filters=None, hub_seed_count=8, ancestor_closure_depth=3, ontologies=None, merged=None, type_promotion_score_factor=1.0, seed_order='score', entity_groups=None, extra_description_predicates=())
¶
Fetch a deterministic induced subgraph around selected entities.
This is a primitive: SPARQLTool holds no vector-store settings, so
the budget arguments here are conservative literals, not the
deployment's configured budget. OntologyPatchRetriever -- the
production caller -- passes every one of them from
ONTOLOGY_PATCH_INDUCED_SUBGRAPH_*. Direct callers who want the
configured behaviour should do the same rather than rely on these.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontologies
|
list[Ontology] | None
|
Pre-fetched catalog to build from. When |
None
|
merged
|
tuple[RDFGraph, dict[str, str]] | None
|
Pre-merged |
None
|
Source code in ontocast/tool/sparql.py
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 | |
Functions:¶
build_candidate_subgraph_query(seed_irefs, graph_irefs, *, depth)
¶
Build a CONSTRUCT for everything :func:_build_induced_subgraph may read.
Five branches, each a direct translation of a read pattern in the builder:
owl:Ontologyheader triples, which populate theontology_subjectsexclusion set — plus thesh:declareblank-node subtrees hanging off them, so persisted author prefix names reach the candidate path too.- Triples incident to any node within
depthhops of a seed -- what :func:_bfs_expand_from_seedvisits and materializes. - Triples incident to the
rdfs:subClassOfancestors of the seeds and of their types -- :func:_add_subclass_ancestor_closureafter seed promotion. Unbounded*rather than the configured hop limit, deliberately: a superset is safe, a subset is not. - Definition triples of properties whose
rdfs:domain/rdfs:rangeis a seed or a seed's type -- :func:_crosslink_property_seeds.
Not covered: the cross-component schema-path repair
(:func:_find_schema_path_in_merged_graph) can search up to
_SCHEMA_PATH_MAX_DEPTH hops from nodes that are themselves depth + 1
hops out, so a bridge may lie outside this candidate set. The consequence is a
missing bridge -- a smaller, still-correct snapshot -- never a wrong triple.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed_irefs
|
Sequence[str]
|
Seed IRIs, already escaped as |
required |
graph_irefs
|
Sequence[str]
|
Named graph IRIs to restrict to, escaped as IRIREFs. |
required |
depth
|
int
|
Neighborhood hop count, matching the builder's |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A SPARQL CONSTRUCT query. |
Source code in ontocast/tool/sparql.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
filter_overbroad_namespace_map(ns_map)
¶
Drop namespace bindings whose URI is a strict prefix of another in the map.
Source code in ontocast/tool/sparql.py
merge_ontology_graphs(ontologies)
¶
Union ontology graphs into one graph carrying their prefix bindings.
Prefix bindings are harvested from each source graph's namespace manager -- they are serialization metadata rather than triples, so they only exist here because the sources were parsed from Turtle.
The result is treated as read-only by every consumer: the induced-subgraph builder reads it as an oracle and writes exclusively to its own result graph. That is what makes the merge safe to cache and share across content units.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontologies
|
Sequence[Ontology]
|
Ontology versions to merge. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
RDFGraph
|
The merged graph and the surviving prefix → namespace map, which |
dict[str, str]
|
the caller binds onto the snapshot it builds. The map is returned rather |
|
tuple[RDFGraph, dict[str, str]]
|
than re-read from the merged graph so callers see exactly the author |
|
tuple[RDFGraph, dict[str, str]]
|
bindings, not rdflib's built-in ones. |
Source code in ontocast/tool/sparql.py
select_relevant_ontologies(ontologies, ontology_iris, ontology_version_filters, ontology_hash_filters)
¶
Filter a catalog down to the ontologies an induced subgraph may draw on.
An empty ontology_iris means "no restriction". Version and hash filters
only apply to IRIs they mention, so an ontology absent from both passes
through untouched.
Generic over lineage-bearing records so the same predicate runs on graph-less
:class:~ontocast.onto.ontology_header.OntologyHeader values -- which is what
the SPARQL candidate path filters, having no graphs to filter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontologies
|
Sequence[LineageT]
|
Candidate catalog ontologies or headers. |
required |
ontology_iris
|
list[str] | None
|
Allowed ontology IRIs, or empty/None for all. |
required |
ontology_version_filters
|
dict[str, set[str]] | None
|
Allowed semantic versions per ontology IRI. |
required |
ontology_hash_filters
|
dict[str, set[str]] | None
|
Allowed content hashes per ontology IRI. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list[LineageT]
|
The surviving records, in input order. |
Source code in ontocast/tool/sparql.py
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 324 325 326 327 328 329 330 331 | |