src/rdflib/plugins/stores/rdf4j.py
1from __future__ import annotations
2
3from textwrap import dedent
4from typing import Any, Generator, Iterable, Iterator, Mapping, Optional, Tuple
5
6from rdflib import Graph
7from rdflib.contrib.rdf4j import has_httpx
8from rdflib.contrib.rdf4j.exceptions import RepositoryNotFoundError
9from rdflib.graph import (
10 DATASET_DEFAULT_GRAPH_ID,
11 Dataset,
12 _ContextType,
13 _QuadType,
14 _TriplePatternType,
15 _TripleType,
16)
17from rdflib.store import VALID_STORE, Store
18from rdflib.term import BNode, Node, URIRef, Variable
19
20if has_httpx:
21 from rdflib.contrib.rdf4j import RDF4JClient
22
23
24def _inject_prefixes(query: str, extra_bindings: Mapping[str, Any]) -> str:
25 bindings = set(list(extra_bindings.items()))
26 if not bindings:
27 return query
28 return "\n".join(
29 [
30 "\n".join(["PREFIX %s: <%s>" % (k, v) for k, v in bindings]),
31 "", # separate ns_bindings from query with an empty line
32 query,
33 ]
34 )
35
36
37def _node_to_sparql(node: Node) -> str:
38 if isinstance(node, BNode):
39 raise Exception(
40 "SPARQL-based stores do not support BNodes"
41 "See http://www.w3.org/TR/sparql11-query/#BGPsparqlBNodes"
42 )
43 return node.n3()
44
45
46def _default_repo_config(repository_id: str) -> str:
47 return dedent(
48 f"""
49 PREFIX config: <tag:rdf4j.org,2023:config/>
50
51 [] a config:Repository ;
52 config:rep.id "{repository_id}" ;
53 config:rep.impl
54 [
55 config:rep.type "openrdf:SailRepository" ;
56 config:sail.impl
57 [
58 config:native.tripleIndexers "spoc,posc" ;
59 config:sail.defaultQueryEvaluationMode "STANDARD" ;
60 config:sail.iterationCacheSyncThreshold "10000" ;
61 config:sail.type "openrdf:NativeStore" ;
62 ] ;
63 ] ;
64 .
65 """
66 )
67
68
69class RDF4JStore(Store):
70 """An RDF4J store."""
71
72 context_aware = True
73 formula_aware = False
74 transaction_aware = False
75 graph_aware = True
76
77 def __init__(
78 self,
79 base_url: str,
80 repository_id: str,
81 configuration: str | None = None,
82 auth: tuple[str, str] | None = None,
83 timeout: float = 30.0,
84 create: bool = False,
85 **kwargs,
86 ):
87 if configuration is None:
88 configuration = _default_repo_config(repository_id)
89 self._client = RDF4JClient(base_url, auth, timeout, **kwargs)
90 self._repository_id = repository_id
91 self._repo = None
92 self.open(configuration, create)
93 super().__init__()
94
95 @property
96 def client(self):
97 return self._client
98
99 @property
100 def repo(self):
101 if self._repo is None:
102 self._repo = self.client.repositories.get(self._repository_id)
103 return self._repo
104
105 def open(
106 self, configuration: str | tuple[str, str] | None, create: bool = False
107 ) -> int | None:
108 try:
109 # Try connecting to the repository.
110 self.repo.health()
111 except RepositoryNotFoundError:
112 if create:
113 self.client.repositories.create(self._repository_id, configuration)
114 self.repo.health()
115 else:
116 raise Exception(f"Repository {self._repository_id} not found.")
117
118 return VALID_STORE
119
120 def close(self, commit_pending_transaction: bool = False) -> None:
121 self.client.close()
122
123 def add(
124 self,
125 triple: _TripleType,
126 context: _ContextType | None = None,
127 quoted: bool = False,
128 ) -> None:
129 s, p, o = triple
130 graph_name = (
131 ""
132 if context is None or context.identifier == DATASET_DEFAULT_GRAPH_ID
133 else context.identifier.n3()
134 )
135 statement = f"{s.n3()} {p.n3()} {o.n3()} {graph_name} ."
136 self.repo.upload(statement)
137
138 def addN(self, quads: Iterable[_QuadType]) -> None: # noqa: N802
139 statements = ""
140 for s, p, o, c in quads:
141 graph_name = (
142 ""
143 if c is None or c.identifier == DATASET_DEFAULT_GRAPH_ID
144 else c.identifier.n3()
145 )
146 statement = f"{s.n3()} {p.n3()} {o.n3()} {graph_name} .\n"
147 statements += statement
148 self.repo.upload(statements)
149
150 def remove(
151 self,
152 triple: _TriplePatternType,
153 context: Optional[_ContextType] = None,
154 ) -> None:
155 s, p, o = triple
156 g = context.identifier if context is not None else None
157 self.repo.delete(s, p, o, g)
158
159 def triples(
160 self,
161 triple_pattern: _TriplePatternType,
162 context: Optional[_ContextType] = None,
163 ) -> Iterator[Tuple[_TripleType, Iterator[Optional[_ContextType]]]]:
164 s, p, o = triple_pattern
165 graph_name = context.identifier if context is not None else None
166 result_graph = self.repo.get(s, p, o, graph_name)
167 if isinstance(result_graph, Dataset):
168 for s, p, o, g in result_graph:
169 yield (s, p, o), iter([Graph(self, identifier=g)])
170 else:
171 # It's a Graph object.
172 for triple in result_graph:
173 # Returning None for _ContextType as it's not used by the caller.
174 yield triple, iter([None])
175
176 def contexts(
177 self, triple: Optional[_TripleType] = None
178 ) -> Generator[_ContextType, None, None]:
179 if triple is None:
180 for graph_name in self.repo.graph_names():
181 yield Graph(self, identifier=graph_name)
182 else:
183 s, p, o = triple
184 params = (
185 _node_to_sparql(s if s else Variable("s")),
186 _node_to_sparql(p if p else Variable("p")),
187 _node_to_sparql(o if o else Variable("o")),
188 )
189 query = (
190 "SELECT DISTINCT ?graph WHERE { GRAPH ?graph { %s %s %s } }" % params
191 )
192 result = self.repo.query(query)
193 for row in result:
194 yield Graph(self, identifier=row["graph"])
195
196 def bind(self, prefix: str, namespace: URIRef, override: bool = True) -> None:
197 # Note: RDF4J namespaces always override.
198 self.repo.namespaces.set(prefix, namespace)
199
200 def prefix(self, namespace: URIRef) -> Optional[str]:
201 namespace_prefixes = dict(
202 [(x.namespace, x.prefix) for x in self.repo.namespaces.list()]
203 )
204 return namespace_prefixes.get(str(namespace))
205
206 def namespace(self, prefix: str) -> Optional[URIRef]:
207 result = self.repo.namespaces.get(prefix)
208 return URIRef(result) if result is not None else None
209
210 def namespaces(self) -> Iterator[Tuple[str, URIRef]]:
211 for result in self.repo.namespaces.list():
212 yield result.prefix, URIRef(result.namespace)
213
214 def add_graph(self, graph: Graph) -> None:
215 if graph.identifier != DATASET_DEFAULT_GRAPH_ID:
216 # Note: this is a no-op since RDF4J doesn't support empty named graphs.
217 self.repo.update(f"CREATE SILENT GRAPH {graph.identifier.n3()}")
218
219 def remove_graph(self, graph: Graph) -> None:
220 self.repo.graphs.clear(graph.identifier)
221
222 def __len__(self, context: _ContextType | None = None) -> int:
223 return self.repo.size(context if context is None else context.identifier)