Coverage for target/native-coverage/profiles/11fb979bfcf049d7ab666c331dc8db2a/project/.venv/lib/python3.13/site-packages/pyshacl_turbo/graph_abstraction.py: 100%

164 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-30 14:51 +0000

1"""Live RDFLib graph views used by the public validation and rule runners. 

2 

3The wrapper retains the caller's graph/store; it neither snapshots nor reasons. 

4Native I/O explicitly uses the underlying graph to preserve its real scope. 

5""" 

6 

7import sys 

8 

9from rdflib import URIRef 

10from rdflib.graph import Dataset, Graph 

11 

12 

13def _native_view(graph): 

14 while isinstance(graph, DataGraph): 

15 graph = graph.impl 

16 runtime = sys.modules.get("rdflib_turbo") 

17 if runtime is not None and isinstance(graph, (runtime.Graph, runtime.StoreDataset)): 

18 return graph 

19 return None 

20 

21 

22def _reject_native_store_graph(graph): 

23 while isinstance(graph, DataGraph): 

24 graph = graph.impl 

25 if getattr(graph, "_turbo_native_graph", False) and _native_view(graph) is None: 

26 raise NotImplementedError( 

27 "SHACL input through the native Store Graph facade is not implemented" 

28 ) 

29 

30 

31class DataGraph(Dataset): 

32 def __new__(cls, store, impl=None, locked_context=None): 

33 return object.__new__(RdfLibDataGraph if cls is DataGraph else cls) 

34 

35 @classmethod 

36 def from_rdflib_graph(cls, graph): 

37 return cls(graph.store, impl=graph) 

38 

39 @classmethod 

40 def from_rdflib_dataset(cls, dataset): 

41 return cls(dataset.store, impl=dataset) 

42 

43 @classmethod 

44 def from_rdflib(cls, source): 

45 _reject_native_store_graph(source) 

46 if not isinstance(source, Graph): 

47 raise ValueError("Invalid rdflib source type") # noqa: TRY004 - reference API 

48 return cls(source.store, impl=source) 

49 

50 

51class RdfLibDataGraph(DataGraph): 

52 def __init__(self, store, impl=None, locked_context=None): 

53 self.impl = impl if impl is not None else Dataset(store=store) 

54 _reject_native_store_graph(self.impl) 

55 self._backing_store = store 

56 self.is_oxigraph = False 

57 self.is_multi_graph = isinstance(self.impl, Dataset) 

58 self._default_union = self.impl.default_union if self.is_multi_graph else False 

59 super().__init__(store=store, default_union=self._default_union) 

60 if isinstance(locked_context, Graph): 

61 self.locked_context = locked_context 

62 elif isinstance(locked_context, str): 

63 self.locked_context = self.get_context(locked_context) 

64 else: 

65 self.locked_context = None 

66 

67 @property 

68 def identifier(self): 

69 if self.is_multi_graph: 

70 return ( 

71 self.locked_context.identifier 

72 if self.locked_context is not None 

73 else "RDFLibDataset" 

74 ) 

75 return str(self.impl.identifier) 

76 

77 @property 

78 def store(self): 

79 return self._backing_store 

80 

81 # Reference DataGraph deliberately removes RDFLib's inherited setters. 

82 @property # type: ignore[misc] 

83 def namespace_manager(self): 

84 return self.impl.namespace_manager 

85 

86 @property # type: ignore[misc] # Same read-only reference contract as above. 

87 def default_graph(self): 

88 return self.impl.default_graph if self.is_multi_graph else self.impl 

89 

90 @property 

91 def default_union(self): 

92 return self._default_union 

93 

94 @default_union.setter 

95 def default_union(self, value): 

96 self._default_union = value 

97 if self.impl and isinstance(self.impl, Dataset): 

98 self.impl.default_union = value 

99 

100 def is_multigraph(self): 

101 return self.is_multi_graph 

102 

103 @property 

104 def _view(self): 

105 return self.locked_context if self.locked_context is not None else self.impl 

106 

107 @property 

108 def _validate_shared_shapes(self): 

109 if _native_view(self) is None: 

110 raise AttributeError("Ordinary RDFLib DataGraph has no borrowed native validation") 

111 return self._view._validate_shared_shapes 

112 

113 @property 

114 def _expand_shared_shapes(self): 

115 if _native_view(self) is None: 

116 raise AttributeError("Ordinary RDFLib DataGraph has no borrowed native rules") 

117 return self._view._expand_shared_shapes 

118 

119 @property 

120 def _compile_shared_shapes(self): 

121 if _native_view(self) is None: 

122 raise AttributeError("Ordinary RDFLib DataGraph has no borrowed native compiler") 

123 return self._view._compile_shared_shapes 

124 

125 def triples(self, triple): 

126 yield from self._view.triples(triple) 

127 

128 def quads(self, quad=None): 

129 if self.is_multi_graph: 

130 yield from self.impl.quads(quad) 

131 else: 

132 triple = (None, None, None) if quad is None else quad[:3] 

133 for s, p, o in self._view.triples(triple): 

134 yield s, p, o, self._view.identifier 

135 

136 def graphs(self, triple=None): 

137 if self.is_multi_graph: 

138 yield from self.impl.graphs(triple) 

139 else: 

140 yield self.impl 

141 

142 contexts = graphs 

143 

144 def add(self, triple): 

145 return self._view.add(triple) 

146 

147 def remove(self, triple): 

148 return self._view.remove(triple) 

149 

150 def __contains__(self, triple): 

151 return triple in self._view 

152 

153 def __len__(self): 

154 return len(self._view) 

155 

156 def objects(self, subject=None, predicate=None, unique=False): 

157 subjects = subject if isinstance(subject, (list, tuple)) else [subject] 

158 for item in subjects: 

159 yield from self._view.objects(item, predicate, unique=unique) 

160 

161 def subjects(self, predicate=None, object=None, unique=False): 

162 objects = object if isinstance(object, (list, tuple)) else [object] 

163 for item in objects: 

164 yield from self._view.subjects(predicate, item, unique=unique) 

165 

166 def predicate_objects(self, subject=None, unique=False): 

167 return self._view.predicate_objects(subject, unique=unique) 

168 

169 def subject_objects(self, predicate=None, unique=False): 

170 return self._view.subject_objects(predicate, unique=unique) 

171 

172 def subject_predicates(self, object=None, unique=False): 

173 return self._view.subject_predicates(object, unique=unique) 

174 

175 def transitive_objects(self, subject, predicate, remember=None): 

176 return self._view.transitive_objects(subject, predicate, remember=remember) 

177 

178 def transitive_subjects(self, predicate, object, remember=None): 

179 return self._view.transitive_subjects(predicate, object, remember=remember) 

180 

181 def items(self, list): 

182 return self._view.items(list) 

183 

184 def bind(self, prefix, namespace, **kwargs): 

185 return self.impl.bind(prefix, namespace, **kwargs) 

186 

187 def query(self, *args, **kwargs): 

188 return self.impl.query(*args, **kwargs) 

189 

190 def get_context(self, identifier): 

191 return self.impl.graph(URIRef(identifier)) if self.is_multi_graph else self.impl 

192 

193 def with_locked_context(self, identifier): 

194 if not self.is_multi_graph: 

195 raise ValueError("Cannot create a locked context on a non-dataset graph") 

196 context = self.get_context(identifier) 

197 return RdfLibDataGraph(context.store, context, context) 

198 

199 def clone(self, destination=None, identifier=None): 

200 _reject_native_store_graph(self) 

201 from .rdf import merge_graph 

202 

203 native = _native_view(self) 

204 if native is not None and destination is None: 

205 if ( 

206 identifier is not None 

207 and not self.is_multi_graph 

208 and identifier != native.identifier 

209 ): 

210 raise NotImplementedError("Renaming a native cloned graph is not implemented") 

211 result = native.copy() 

212 return DataGraph.from_rdflib(result) 

213 if destination is not None: 

214 result = destination 

215 elif self.is_multi_graph: 

216 result = Dataset(default_union=self.impl.default_union) 

217 result.default_graph = result.graph(self.impl.default_graph.identifier) 

218 else: 

219 selected = self.impl.identifier if identifier is None else identifier 

220 result = Graph(identifier=selected) 

221 result = merge_graph(result, self.impl) 

222 for prefix, namespace in self.impl.namespaces(): 

223 result.bind(prefix, namespace) 

224 return DataGraph.from_rdflib(result) 

225 

226 def register_custom_function( 

227 self, 

228 function_name, 

229 rdflib_fn, 

230 oxigraph_fn=None, 

231 override=False, 

232 raw=False, 

233 is_aggregate=False, 

234 ): 

235 from rdflib.plugins.sparql.operators import register_custom_function 

236 

237 register_custom_function(function_name, rdflib_fn, override, raw) 

238 

239 def unregister_custom_function(self, function_name, rdflib_fn, oxigraph_fn=None): 

240 from rdflib.plugins.sparql.operators import unregister_custom_function 

241 

242 unregister_custom_function(function_name, rdflib_fn)