graph.py 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401
  1. """
  2. RDFLib defines the following kinds of Graphs:
  3. * [`Graph`][rdflib.graph.Graph]
  4. * [`QuotedGraph`][rdflib.graph.QuotedGraph]
  5. * [`ConjunctiveGraph`][rdflib.graph.ConjunctiveGraph]
  6. * [`Dataset`][rdflib.graph.Dataset]
  7. ## Graph
  8. An RDF graph is a set of RDF triples. Graphs support the python `in`
  9. operator, as well as iteration and some operations like union,
  10. difference and intersection.
  11. See [`Graph`][rdflib.graph.Graph]
  12. ## Conjunctive Graph
  13. !!! warning "Deprecation notice"
  14. `ConjunctiveGraph` is deprecated, use [`Dataset`][rdflib.graph.Dataset] instead.
  15. A Conjunctive Graph is the most relevant collection of graphs that are
  16. considered to be the boundary for closed world assumptions. This
  17. boundary is equivalent to that of the store instance (which is itself
  18. uniquely identified and distinct from other instances of
  19. [`Store`][rdflib.store.Store] that signify other Conjunctive Graphs). It is
  20. equivalent to all the named graphs within it and associated with a
  21. `_default_` graph which is automatically assigned a
  22. [`BNode`][rdflib.term.BNode] for an identifier - if one isn't given.
  23. See [`ConjunctiveGraph`][rdflib.graph.ConjunctiveGraph]
  24. ## Quoted graph
  25. The notion of an RDF graph [14] is extended to include the concept of
  26. a formula node. A formula node may occur wherever any other kind of
  27. node can appear. Associated with a formula node is an RDF graph that
  28. is completely disjoint from all other graphs; i.e. has no nodes in
  29. common with any other graph. (It may contain the same labels as other
  30. RDF graphs; because this is, by definition, a separate graph,
  31. considerations of tidiness do not apply between the graph at a formula
  32. node and any other graph.)
  33. This is intended to map the idea of "{ N3-expression }" that is used
  34. by N3 into an RDF graph upon which RDF semantics is defined.
  35. See [`QuotedGraph`][rdflib.graph.QuotedGraph]
  36. ## Dataset
  37. The RDF 1.1 Dataset, a small extension to the Conjunctive Graph. The
  38. primary term is "graphs in the datasets" and not "contexts with quads"
  39. so there is a separate method to set/retrieve a graph in a dataset and
  40. to operate with dataset graphs. As a consequence of this approach,
  41. dataset graphs cannot be identified with blank nodes, a name is always
  42. required (RDFLib will automatically add a name if one is not provided
  43. at creation time). This implementation includes a convenience method
  44. to directly add a single quad to a dataset graph.
  45. See [`Dataset`][rdflib.graph.Dataset]
  46. ## Working with graphs
  47. Instantiating Graphs with default store (Memory) and default identifier
  48. (a BNode):
  49. ```python
  50. >>> g = Graph()
  51. >>> g.store.__class__
  52. <class 'rdflib.plugins.stores.memory.Memory'>
  53. >>> g.identifier.__class__
  54. <class 'rdflib.term.BNode'>
  55. ```
  56. Instantiating Graphs with a Memory store and an identifier -
  57. <https://rdflib.github.io>:
  58. ```python
  59. >>> g = Graph('Memory', URIRef("https://rdflib.github.io"))
  60. >>> g.identifier
  61. rdflib.term.URIRef('https://rdflib.github.io')
  62. >>> str(g) # doctest: +NORMALIZE_WHITESPACE
  63. "<https://rdflib.github.io> a rdfg:Graph;rdflib:storage
  64. [a rdflib:Store;rdfs:label 'Memory']."
  65. ```
  66. Creating a ConjunctiveGraph - The top level container for all named Graphs
  67. in a "database":
  68. ```python
  69. >>> g = ConjunctiveGraph()
  70. >>> str(g.default_context)
  71. "[a rdfg:Graph;rdflib:storage [a rdflib:Store;rdfs:label 'Memory']]."
  72. ```
  73. Adding / removing reified triples to Graph and iterating over it directly or
  74. via triple pattern:
  75. ```python
  76. >>> g = Graph()
  77. >>> statementId = BNode()
  78. >>> print(len(g))
  79. 0
  80. >>> g.add((statementId, RDF.type, RDF.Statement)) # doctest: +ELLIPSIS
  81. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  82. >>> g.add((statementId, RDF.subject,
  83. ... URIRef("https://rdflib.github.io/store/ConjunctiveGraph"))) # doctest: +ELLIPSIS
  84. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  85. >>> g.add((statementId, RDF.predicate, namespace.RDFS.label)) # doctest: +ELLIPSIS
  86. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  87. >>> g.add((statementId, RDF.object, Literal("Conjunctive Graph"))) # doctest: +ELLIPSIS
  88. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  89. >>> print(len(g))
  90. 4
  91. >>> for s, p, o in g:
  92. ... print(type(s))
  93. ...
  94. <class 'rdflib.term.BNode'>
  95. <class 'rdflib.term.BNode'>
  96. <class 'rdflib.term.BNode'>
  97. <class 'rdflib.term.BNode'>
  98. >>> for s, p, o in g.triples((None, RDF.object, None)):
  99. ... print(o)
  100. ...
  101. Conjunctive Graph
  102. >>> g.remove((statementId, RDF.type, RDF.Statement)) # doctest: +ELLIPSIS
  103. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  104. >>> print(len(g))
  105. 3
  106. ```
  107. `None` terms in calls to [`triples()`][rdflib.graph.Graph.triples] can be
  108. thought of as "open variables".
  109. Graph support set-theoretic operators, you can add/subtract graphs, as
  110. well as intersection (with multiplication operator g1*g2) and xor (g1
  111. ^ g2).
  112. Note that BNode IDs are kept when doing set-theoretic operations, this
  113. may or may not be what you want. Two named graphs within the same
  114. application probably want share BNode IDs, two graphs with data from
  115. different sources probably not. If your BNode IDs are all generated
  116. by RDFLib they are UUIDs and unique.
  117. ```python
  118. >>> g1 = Graph()
  119. >>> g2 = Graph()
  120. >>> u = URIRef("http://example.com/foo")
  121. >>> g1.add([u, namespace.RDFS.label, Literal("foo")]) # doctest: +ELLIPSIS
  122. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  123. >>> g1.add([u, namespace.RDFS.label, Literal("bar")]) # doctest: +ELLIPSIS
  124. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  125. >>> g2.add([u, namespace.RDFS.label, Literal("foo")]) # doctest: +ELLIPSIS
  126. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  127. >>> g2.add([u, namespace.RDFS.label, Literal("bing")]) # doctest: +ELLIPSIS
  128. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  129. >>> len(g1 + g2) # adds bing as label
  130. 3
  131. >>> len(g1 - g2) # removes foo
  132. 1
  133. >>> len(g1 * g2) # only foo
  134. 1
  135. >>> g1 += g2 # now g1 contains everything
  136. ```
  137. Graph Aggregation - ConjunctiveGraphs and ReadOnlyGraphAggregate within
  138. the same store:
  139. ```python
  140. >>> store = plugin.get("Memory", Store)()
  141. >>> g1 = Graph(store)
  142. >>> g2 = Graph(store)
  143. >>> g3 = Graph(store)
  144. >>> stmt1 = BNode()
  145. >>> stmt2 = BNode()
  146. >>> stmt3 = BNode()
  147. >>> g1.add((stmt1, RDF.type, RDF.Statement)) # doctest: +ELLIPSIS
  148. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  149. >>> g1.add((stmt1, RDF.subject,
  150. ... URIRef('https://rdflib.github.io/store/ConjunctiveGraph'))) # doctest: +ELLIPSIS
  151. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  152. >>> g1.add((stmt1, RDF.predicate, namespace.RDFS.label)) # doctest: +ELLIPSIS
  153. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  154. >>> g1.add((stmt1, RDF.object, Literal('Conjunctive Graph'))) # doctest: +ELLIPSIS
  155. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  156. >>> g2.add((stmt2, RDF.type, RDF.Statement)) # doctest: +ELLIPSIS
  157. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  158. >>> g2.add((stmt2, RDF.subject,
  159. ... URIRef('https://rdflib.github.io/store/ConjunctiveGraph'))) # doctest: +ELLIPSIS
  160. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  161. >>> g2.add((stmt2, RDF.predicate, RDF.type)) # doctest: +ELLIPSIS
  162. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  163. >>> g2.add((stmt2, RDF.object, namespace.RDFS.Class)) # doctest: +ELLIPSIS
  164. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  165. >>> g3.add((stmt3, RDF.type, RDF.Statement)) # doctest: +ELLIPSIS
  166. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  167. >>> g3.add((stmt3, RDF.subject,
  168. ... URIRef('https://rdflib.github.io/store/ConjunctiveGraph'))) # doctest: +ELLIPSIS
  169. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  170. >>> g3.add((stmt3, RDF.predicate, namespace.RDFS.comment)) # doctest: +ELLIPSIS
  171. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  172. >>> g3.add((stmt3, RDF.object, Literal(
  173. ... 'The top-level aggregate graph - The sum ' +
  174. ... 'of all named graphs within a Store'))) # doctest: +ELLIPSIS
  175. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  176. >>> len(list(ConjunctiveGraph(store).subjects(RDF.type, RDF.Statement)))
  177. 3
  178. >>> len(list(ReadOnlyGraphAggregate([g1,g2]).subjects(
  179. ... RDF.type, RDF.Statement)))
  180. 2
  181. ```
  182. ConjunctiveGraphs have a [`quads()`][rdflib.graph.ConjunctiveGraph.quads] method
  183. which returns quads instead of triples, where the fourth item is the Graph
  184. (or subclass thereof) instance in which the triple was asserted:
  185. ```python
  186. >>> uniqueGraphNames = set(
  187. ... [graph.identifier for s, p, o, graph in ConjunctiveGraph(store
  188. ... ).quads((None, RDF.predicate, None))])
  189. >>> len(uniqueGraphNames)
  190. 3
  191. >>> unionGraph = ReadOnlyGraphAggregate([g1, g2])
  192. >>> uniqueGraphNames = set(
  193. ... [graph.identifier for s, p, o, graph in unionGraph.quads(
  194. ... (None, RDF.predicate, None))])
  195. >>> len(uniqueGraphNames)
  196. 2
  197. ```
  198. Parsing N3 from a string:
  199. ```python
  200. >>> g2 = Graph()
  201. >>> src = '''
  202. ... @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
  203. ... @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
  204. ... [ a rdf:Statement ;
  205. ... rdf:subject <https://rdflib.github.io/store#ConjunctiveGraph>;
  206. ... rdf:predicate rdfs:label;
  207. ... rdf:object "Conjunctive Graph" ] .
  208. ... '''
  209. >>> g2 = g2.parse(data=src, format="n3")
  210. >>> print(len(g2))
  211. 4
  212. ```
  213. Using Namespace class:
  214. ```python
  215. >>> RDFLib = Namespace("https://rdflib.github.io/")
  216. >>> RDFLib.ConjunctiveGraph
  217. rdflib.term.URIRef('https://rdflib.github.io/ConjunctiveGraph')
  218. >>> RDFLib["Graph"]
  219. rdflib.term.URIRef('https://rdflib.github.io/Graph')
  220. ```
  221. """
  222. from __future__ import annotations
  223. import logging
  224. import pathlib
  225. import random
  226. import warnings
  227. from io import BytesIO
  228. from typing import (
  229. IO,
  230. TYPE_CHECKING,
  231. Any,
  232. BinaryIO,
  233. Callable,
  234. Dict,
  235. Generator,
  236. Iterable,
  237. List,
  238. Mapping,
  239. NoReturn,
  240. Optional,
  241. Set,
  242. TextIO,
  243. Tuple,
  244. Type,
  245. TypeVar,
  246. Union,
  247. cast,
  248. overload,
  249. )
  250. from urllib.parse import urlparse
  251. from urllib.request import url2pathname
  252. import rdflib.exceptions as exceptions
  253. import rdflib.namespace as namespace # noqa: F401 # This is here because it is used in a docstring.
  254. import rdflib.plugin as plugin
  255. import rdflib.query as query
  256. import rdflib.util # avoid circular dependency
  257. from rdflib.collection import Collection
  258. from rdflib.exceptions import ParserError
  259. from rdflib.namespace import RDF, Namespace, NamespaceManager
  260. from rdflib.parser import InputSource, Parser, create_input_source
  261. from rdflib.paths import Path
  262. from rdflib.resource import Resource
  263. from rdflib.serializer import Serializer
  264. from rdflib.store import Store
  265. from rdflib.term import (
  266. BNode,
  267. Genid,
  268. IdentifiedNode,
  269. Identifier,
  270. Literal,
  271. Node,
  272. RDFLibGenid,
  273. URIRef,
  274. )
  275. if TYPE_CHECKING:
  276. import typing_extensions as te
  277. import rdflib.query
  278. from rdflib.plugins.sparql.sparql import Query, Update
  279. _SubjectType = Node
  280. _PredicateType = Node
  281. _ObjectType = Node
  282. _ContextIdentifierType = IdentifiedNode
  283. _TripleType = Tuple["_SubjectType", "_PredicateType", "_ObjectType"]
  284. _QuadType = Tuple["_SubjectType", "_PredicateType", "_ObjectType", "_ContextType"]
  285. _OptionalQuadType = Tuple[
  286. "_SubjectType", "_PredicateType", "_ObjectType", Optional["_ContextType"]
  287. ]
  288. _TripleOrOptionalQuadType = Union["_TripleType", "_OptionalQuadType"]
  289. _OptionalIdentifiedQuadType = Tuple[
  290. "_SubjectType", "_PredicateType", "_ObjectType", Optional["_ContextIdentifierType"]
  291. ]
  292. _TriplePatternType = Tuple[
  293. Optional["_SubjectType"], Optional["_PredicateType"], Optional["_ObjectType"]
  294. ]
  295. _TriplePathPatternType = Tuple[Optional["_SubjectType"], Path, Optional["_ObjectType"]]
  296. _QuadPatternType = Tuple[
  297. Optional["_SubjectType"],
  298. Optional["_PredicateType"],
  299. Optional["_ObjectType"],
  300. Optional["_ContextType"],
  301. ]
  302. _QuadPathPatternType = Tuple[
  303. Optional["_SubjectType"],
  304. Path,
  305. Optional["_ObjectType"],
  306. Optional["_ContextType"],
  307. ]
  308. _TripleOrQuadPatternType = Union["_TriplePatternType", "_QuadPatternType"]
  309. _TripleOrQuadPathPatternType = Union["_TriplePathPatternType", "_QuadPathPatternType"]
  310. _TripleSelectorType = Tuple[
  311. Optional["_SubjectType"],
  312. Optional[Union["Path", "_PredicateType"]],
  313. Optional["_ObjectType"],
  314. ]
  315. _QuadSelectorType = Tuple[
  316. Optional["_SubjectType"],
  317. Optional[Union["Path", "_PredicateType"]],
  318. Optional["_ObjectType"],
  319. Optional["_ContextType"],
  320. ]
  321. _TripleOrQuadSelectorType = Union["_TripleSelectorType", "_QuadSelectorType"]
  322. _TriplePathType = Tuple["_SubjectType", Path, "_ObjectType"]
  323. _TripleOrTriplePathType = Union["_TripleType", "_TriplePathType"]
  324. _TripleChoiceType = Union[
  325. Tuple[List[_SubjectType], Optional[_PredicateType], Optional[_ObjectType]],
  326. Tuple[Optional[_SubjectType], List[_PredicateType], Optional[_ObjectType]],
  327. Tuple[Optional[_SubjectType], Optional[_PredicateType], List[_ObjectType]],
  328. ]
  329. _GraphT = TypeVar("_GraphT", bound="Graph")
  330. _ConjunctiveGraphT = TypeVar("_ConjunctiveGraphT", bound="ConjunctiveGraph")
  331. _DatasetT = TypeVar("_DatasetT", bound="Dataset")
  332. # type error: Function "Type[Literal]" could always be true in boolean contex
  333. assert Literal # type: ignore[truthy-function] # avoid warning
  334. # type error: Function "Type[Namespace]" could always be true in boolean context
  335. assert Namespace # type: ignore[truthy-function] # avoid warning
  336. if TYPE_CHECKING:
  337. from rdflib._type_checking import _NamespaceSetString
  338. logger = logging.getLogger(__name__)
  339. __all__ = [
  340. "Graph",
  341. "ConjunctiveGraph",
  342. "QuotedGraph",
  343. "Seq",
  344. "ModificationException",
  345. "Dataset",
  346. "UnSupportedAggregateOperation",
  347. "ReadOnlyGraphAggregate",
  348. "BatchAddGraph",
  349. "_ConjunctiveGraphT",
  350. "_ContextIdentifierType",
  351. "_DatasetT",
  352. "_GraphT",
  353. "_ObjectType",
  354. "_OptionalIdentifiedQuadType",
  355. "_OptionalQuadType",
  356. "_PredicateType",
  357. "_QuadPathPatternType",
  358. "_QuadPatternType",
  359. "_QuadSelectorType",
  360. "_QuadType",
  361. "_SubjectType",
  362. "_TripleOrOptionalQuadType",
  363. "_TripleOrTriplePathType",
  364. "_TripleOrQuadPathPatternType",
  365. "_TripleOrQuadPatternType",
  366. "_TripleOrQuadSelectorType",
  367. "_TriplePathPatternType",
  368. "_TriplePathType",
  369. "_TriplePatternType",
  370. "_TripleSelectorType",
  371. "_TripleType",
  372. ]
  373. # : Transitive closure arg type.
  374. _TCArgT = TypeVar("_TCArgT")
  375. # Graph is a node because technically a formula-aware graph
  376. # take a Graph as subject or object, but we usually use QuotedGraph for that.
  377. class Graph(Node):
  378. """An RDF Graph: a Python object containing nodes and relations between them as
  379. RDF 'triples'.
  380. This is the central RDFLib object class and Graph objects are almost always present
  381. in all uses of RDFLib.
  382. Example:
  383. The basic use is to create a Graph and iterate through or query its content:
  384. ```python
  385. >>> from rdflib import Graph, URIRef
  386. >>> g = Graph()
  387. >>> g.add((
  388. ... URIRef("http://example.com/s1"), # subject
  389. ... URIRef("http://example.com/p1"), # predicate
  390. ... URIRef("http://example.com/o1"), # object
  391. ... )) # doctest: +ELLIPSIS
  392. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  393. >>> g.add((
  394. ... URIRef("http://example.com/s2"), # subject
  395. ... URIRef("http://example.com/p2"), # predicate
  396. ... URIRef("http://example.com/o2"), # object
  397. ... )) # doctest: +ELLIPSIS
  398. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  399. >>> for triple in sorted(g): # simple looping
  400. ... print(triple)
  401. (rdflib.term.URIRef('http://example.com/s1'), rdflib.term.URIRef('http://example.com/p1'), rdflib.term.URIRef('http://example.com/o1'))
  402. (rdflib.term.URIRef('http://example.com/s2'), rdflib.term.URIRef('http://example.com/p2'), rdflib.term.URIRef('http://example.com/o2'))
  403. >>> # get the object of the triple with subject s1 and predicate p1
  404. >>> o = g.value(
  405. ... subject=URIRef("http://example.com/s1"),
  406. ... predicate=URIRef("http://example.com/p1")
  407. ... )
  408. ```
  409. Args:
  410. store: The constructor accepts one argument, the "store" that will be used to store the
  411. graph data with the default being the [`Memory`][rdflib.plugins.stores.memory.Memory]
  412. (in memory) Store. Other Stores that persist content to disk using various file
  413. databases or Stores that use remote servers (SPARQL systems) are supported.
  414. All [builtin storetypes][rdflib.plugins.stores] can be accessed via
  415. their registered names.
  416. Other Stores not shipped with RDFLib can be added as plugins, such as
  417. [HDT](https://github.com/rdflib/rdflib-hdt/).
  418. Registration of external plugins
  419. is described in [`rdflib.plugin`][rdflib.plugin].
  420. Stores can be context-aware or unaware. Unaware stores take up
  421. (some) less space but cannot support features that require
  422. context, such as true merging/demerging of sub-graphs and
  423. provenance.
  424. Even if used with a context-aware store, Graph will only expose the quads which
  425. belong to the default graph. To access the rest of the data the
  426. `Dataset` class can be used instead.
  427. The Graph constructor can take an identifier which identifies the Graph
  428. by name. If none is given, the graph is assigned a BNode for its
  429. identifier.
  430. For more on Named Graphs, see the RDFLib `Dataset` class and the TriG Specification,
  431. <https://www.w3.org/TR/trig/>.
  432. identifier: identifier of the graph itself
  433. namespace_manager: Used namespace manager.
  434. Create with bind_namespaces if `None`.
  435. base: Base used for [URIs][rdflib.term.URIRef]
  436. bind_namespaces: Used bind_namespaces for namespace_manager
  437. Is only used, when no namespace_manager is provided.
  438. """
  439. context_aware: bool
  440. formula_aware: bool
  441. default_union: bool
  442. base: Optional[str]
  443. def __init__(
  444. self,
  445. store: Union[Store, str] = "default",
  446. identifier: Optional[Union[_ContextIdentifierType, str]] = None,
  447. namespace_manager: Optional[NamespaceManager] = None,
  448. base: Optional[str] = None,
  449. bind_namespaces: _NamespaceSetString = "rdflib",
  450. ):
  451. super(Graph, self).__init__()
  452. self.base = base
  453. self.__identifier: _ContextIdentifierType
  454. self.__identifier = identifier or BNode() # type: ignore[assignment]
  455. if not isinstance(self.__identifier, IdentifiedNode):
  456. self.__identifier = URIRef(self.__identifier) # type: ignore[unreachable]
  457. self.__store: Store
  458. if not isinstance(store, Store):
  459. # TODO: error handling
  460. self.__store = store = plugin.get(store, Store)()
  461. else:
  462. self.__store = store
  463. self.__namespace_manager = namespace_manager
  464. self._bind_namespaces = bind_namespaces
  465. self.context_aware = False
  466. self.formula_aware = False
  467. self.default_union = False
  468. @property
  469. def store(self) -> Store:
  470. return self.__store
  471. @property
  472. def identifier(self) -> _ContextIdentifierType:
  473. return self.__identifier
  474. @property
  475. def namespace_manager(self) -> NamespaceManager:
  476. """
  477. this graph's namespace-manager
  478. """
  479. if self.__namespace_manager is None:
  480. self.__namespace_manager = NamespaceManager(self, self._bind_namespaces)
  481. return self.__namespace_manager
  482. @namespace_manager.setter
  483. def namespace_manager(self, nm: NamespaceManager) -> None:
  484. self.__namespace_manager = nm
  485. def __repr__(self) -> str:
  486. return "<Graph identifier=%s (%s)>" % (self.identifier, type(self))
  487. def __str__(self) -> str:
  488. if isinstance(self.identifier, URIRef):
  489. return (
  490. "%s a rdfg:Graph;rdflib:storage " + "[a rdflib:Store;rdfs:label '%s']."
  491. ) % (self.identifier.n3(), self.store.__class__.__name__)
  492. else:
  493. return (
  494. "[a rdfg:Graph;rdflib:storage " + "[a rdflib:Store;rdfs:label '%s']]."
  495. ) % self.store.__class__.__name__
  496. def toPython(self: _GraphT) -> _GraphT: # noqa: N802
  497. return self
  498. def destroy(self: _GraphT, configuration: str) -> _GraphT:
  499. """Destroy the store identified by `configuration` if supported"""
  500. self.__store.destroy(configuration)
  501. return self
  502. # Transactional interfaces (optional)
  503. def commit(self: _GraphT) -> _GraphT:
  504. """Commits active transactions"""
  505. self.__store.commit()
  506. return self
  507. def rollback(self: _GraphT) -> _GraphT:
  508. """Rollback active transactions"""
  509. self.__store.rollback()
  510. return self
  511. def open(
  512. self, configuration: Union[str, tuple[str, str]], create: bool = False
  513. ) -> Optional[int]:
  514. """Open the graph store
  515. Might be necessary for stores that require opening a connection to a
  516. database or acquiring some resource.
  517. """
  518. return self.__store.open(configuration, create)
  519. def close(self, commit_pending_transaction: bool = False) -> None:
  520. """Close the graph store
  521. Might be necessary for stores that require closing a connection to a
  522. database or releasing some resource.
  523. """
  524. return self.__store.close(commit_pending_transaction=commit_pending_transaction)
  525. def add(self: _GraphT, triple: _TripleType) -> _GraphT:
  526. """Add a triple with self as context.
  527. Args:
  528. triple: The triple to add to the graph.
  529. Returns:
  530. The graph instance.
  531. """
  532. s, p, o = triple
  533. assert isinstance(s, Node), "Subject %s must be an rdflib term" % (s,)
  534. assert isinstance(p, Node), "Predicate %s must be an rdflib term" % (p,)
  535. assert isinstance(o, Node), "Object %s must be an rdflib term" % (o,)
  536. self.__store.add((s, p, o), self, quoted=False)
  537. return self
  538. def addN(self: _GraphT, quads: Iterable[_QuadType]) -> _GraphT: # noqa: N802
  539. """Add a sequence of triple with context"""
  540. self.__store.addN(
  541. (s, p, o, c)
  542. for s, p, o, c in quads
  543. if isinstance(c, Graph)
  544. and c.identifier is self.identifier
  545. and _assertnode(s, p, o)
  546. )
  547. return self
  548. def remove(self: _GraphT, triple: _TriplePatternType) -> _GraphT:
  549. """Remove a triple from the graph
  550. If the triple does not provide a context attribute, removes the triple
  551. from all contexts.
  552. """
  553. self.__store.remove(triple, context=self)
  554. return self
  555. @overload
  556. def triples(
  557. self,
  558. triple: _TriplePatternType,
  559. ) -> Generator[_TripleType, None, None]: ...
  560. @overload
  561. def triples(
  562. self,
  563. triple: _TriplePathPatternType,
  564. ) -> Generator[_TriplePathType, None, None]: ...
  565. @overload
  566. def triples(
  567. self,
  568. triple: _TripleSelectorType,
  569. ) -> Generator[_TripleOrTriplePathType, None, None]: ...
  570. def triples(
  571. self,
  572. triple: _TripleSelectorType,
  573. ) -> Generator[_TripleOrTriplePathType, None, None]:
  574. """Generator over the triple store.
  575. Returns triples that match the given triple pattern. If the triple pattern
  576. does not provide a context, all contexts will be searched.
  577. Args:
  578. triple: A triple pattern where each component can be a specific value or None
  579. as a wildcard. The predicate can also be a path expression.
  580. Yields:
  581. Triples matching the given pattern.
  582. """
  583. s, p, o = triple
  584. if isinstance(p, Path):
  585. for _s, _o in p.eval(self, s, o):
  586. yield _s, p, _o
  587. else:
  588. for (_s, _p, _o), cg in self.__store.triples((s, p, o), context=self):
  589. yield _s, _p, _o
  590. def __getitem__(self, item):
  591. """
  592. A graph can be "sliced" as a shortcut for the triples method
  593. The python slice syntax is (ab)used for specifying triples.
  594. A generator over matches is returned,
  595. the returned tuples include only the parts not given
  596. ```python
  597. >>> import rdflib
  598. >>> g = rdflib.Graph()
  599. >>> g.add((rdflib.URIRef("urn:bob"), namespace.RDFS.label, rdflib.Literal("Bob"))) # doctest: +ELLIPSIS
  600. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  601. >>> list(g[rdflib.URIRef("urn:bob")]) # all triples about bob
  602. [(rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#label'), rdflib.term.Literal('Bob'))]
  603. >>> list(g[:namespace.RDFS.label]) # all label triples
  604. [(rdflib.term.URIRef('urn:bob'), rdflib.term.Literal('Bob'))]
  605. >>> list(g[::rdflib.Literal("Bob")]) # all triples with bob as object
  606. [(rdflib.term.URIRef('urn:bob'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#label'))]
  607. ```
  608. Combined with SPARQL paths, more complex queries can be
  609. written concisely:
  610. - Name of all Bobs friends: `g[bob : FOAF.knows/FOAF.name ]`
  611. - Some label for Bob: `g[bob : DC.title|FOAF.name|RDFS.label]`
  612. - All friends and friends of friends of Bob: `g[bob : FOAF.knows * "+"]`
  613. - etc.
  614. !!! example "New in version 4.0"
  615. """
  616. if isinstance(item, slice):
  617. s, p, o = item.start, item.stop, item.step
  618. if s is None and p is None and o is None:
  619. return self.triples((s, p, o))
  620. elif s is None and p is None:
  621. # type error: Argument 1 to "subject_predicates" of "Graph" has incompatible type "Union[int, Any]"; expected "Optional[Node]"
  622. return self.subject_predicates(o) # type: ignore[arg-type]
  623. elif s is None and o is None:
  624. # type error: Argument 1 to "subject_objects" of "Graph" has incompatible type "Union[int, Any]"; expected "Union[Path, Node, None]"
  625. return self.subject_objects(p) # type: ignore[arg-type]
  626. elif p is None and o is None:
  627. # type error: Argument 1 to "predicate_objects" of "Graph" has incompatible type "Union[int, Any]"; expected "Optional[Node]"
  628. return self.predicate_objects(s) # type: ignore[arg-type]
  629. elif s is None:
  630. # type error: Argument 1 to "subjects" of "Graph" has incompatible type "Union[int, Any]"; expected "Union[Path, Node, None]"
  631. # Argument 2 to "subjects" of "Graph" has incompatible type "Union[int, Any]"; expected "Union[Node, List[Node], None]"
  632. return self.subjects(p, o) # type: ignore[arg-type]
  633. elif p is None:
  634. # type error: Argument 1 to "predicates" of "Graph" has incompatible type "Union[int, Any]"; expected "Optional[Node]"
  635. # Argument 2 to "predicates" of "Graph" has incompatible type "Union[int, Any]"; expected "Optional[Node]"
  636. return self.predicates(s, o) # type: ignore[arg-type]
  637. elif o is None:
  638. # type error: Argument 1 to "objects" of "Graph" has incompatible type "Union[int, Any]"; expected "Union[Node, List[Node], None]"
  639. # Argument 2 to "objects" of "Graph" has incompatible type "Union[int, Any]"; expected "Union[Path, Node, None]"
  640. return self.objects(s, p) # type: ignore[arg-type]
  641. else:
  642. # type error: Unsupported operand types for in ("Tuple[Union[int, Any], Union[int, Any], Union[int, Any]]" and "Graph")
  643. # all given
  644. return (s, p, o) in self # type: ignore[operator]
  645. elif isinstance(item, (Path, Node)):
  646. # type error: Argument 1 to "predicate_objects" of "Graph" has incompatible type "Union[Path, Node]"; expected "Optional[Node]"
  647. return self.predicate_objects(item) # type: ignore[arg-type]
  648. else:
  649. raise TypeError(
  650. "You can only index a graph by a single rdflib term or path, or a slice of rdflib terms."
  651. )
  652. def __len__(self) -> int:
  653. """Returns the number of triples in the graph.
  654. If context is specified then the number of triples in the context is
  655. returned instead.
  656. Returns:
  657. The number of triples in the graph.
  658. """
  659. # type error: Unexpected keyword argument "context" for "__len__" of "Store"
  660. return self.__store.__len__(context=self) # type: ignore[call-arg]
  661. def __iter__(self) -> Generator[_TripleType, None, None]:
  662. """Iterates over all triples in the store.
  663. Returns:
  664. A generator yielding all triples in the store.
  665. """
  666. return self.triples((None, None, None))
  667. def __contains__(self, triple: _TripleSelectorType) -> bool:
  668. """Support for 'triple in graph' syntax.
  669. Args:
  670. triple: The triple pattern to check for.
  671. Returns:
  672. True if the triple pattern exists in the graph, False otherwise.
  673. """
  674. for triple in self.triples(triple):
  675. return True
  676. return False
  677. def __hash__(self) -> int:
  678. return hash(self.identifier)
  679. def __cmp__(self, other) -> int:
  680. if other is None:
  681. return -1
  682. elif isinstance(other, Graph):
  683. return (self.identifier > other.identifier) - (
  684. self.identifier < other.identifier
  685. )
  686. else:
  687. # Note if None is considered equivalent to owl:Nothing
  688. # Then perhaps a graph with length 0 should be considered
  689. # equivalent to None (if compared to it)?
  690. return 1
  691. def __eq__(self, other) -> bool:
  692. return isinstance(other, Graph) and self.identifier == other.identifier
  693. def __lt__(self, other) -> bool:
  694. return (other is None) or (
  695. isinstance(other, Graph) and self.identifier < other.identifier
  696. )
  697. def __le__(self, other: Graph) -> bool:
  698. return self < other or self == other
  699. def __gt__(self, other) -> bool:
  700. return (isinstance(other, Graph) and self.identifier > other.identifier) or (
  701. other is not None
  702. )
  703. def __ge__(self, other: Graph) -> bool:
  704. return self > other or self == other
  705. def __iadd__(self: _GraphT, other: Iterable[_TripleType]) -> _GraphT:
  706. """Add all triples in Graph other to Graph.
  707. BNode IDs are not changed."""
  708. self.addN((s, p, o, self) for s, p, o in other)
  709. return self
  710. def __isub__(self: _GraphT, other: Iterable[_TripleType]) -> _GraphT:
  711. """Subtract all triples in Graph other from Graph.
  712. BNode IDs are not changed."""
  713. for triple in other:
  714. self.remove(triple)
  715. return self
  716. def __add__(self, other: Graph) -> Graph:
  717. """Set-theoretic union
  718. BNode IDs are not changed."""
  719. try:
  720. retval = type(self)()
  721. except TypeError:
  722. retval = Graph()
  723. for prefix, uri in set(list(self.namespaces()) + list(other.namespaces())):
  724. retval.bind(prefix, uri)
  725. for x in self:
  726. retval.add(x)
  727. for y in other:
  728. retval.add(y)
  729. return retval
  730. def __mul__(self, other: Graph) -> Graph:
  731. """Set-theoretic intersection.
  732. BNode IDs are not changed."""
  733. try:
  734. retval = type(self)()
  735. except TypeError:
  736. retval = Graph()
  737. for x in other:
  738. if x in self:
  739. retval.add(x)
  740. return retval
  741. def __sub__(self, other: Graph) -> Graph:
  742. """Set-theoretic difference.
  743. BNode IDs are not changed."""
  744. try:
  745. retval = type(self)()
  746. except TypeError:
  747. retval = Graph()
  748. for x in self:
  749. if x not in other:
  750. retval.add(x)
  751. return retval
  752. def __xor__(self, other: Graph) -> Graph:
  753. """Set-theoretic XOR.
  754. BNode IDs are not changed."""
  755. return (self - other) + (other - self)
  756. __or__ = __add__
  757. __and__ = __mul__
  758. # Conv. methods
  759. def set(
  760. self: _GraphT, triple: Tuple[_SubjectType, _PredicateType, _ObjectType]
  761. ) -> _GraphT:
  762. """Convenience method to update the value of object
  763. Remove any existing triples for subject and predicate before adding
  764. (subject, predicate, object).
  765. """
  766. (subject, predicate, object_) = triple
  767. assert (
  768. subject is not None
  769. ), "s can't be None in .set([s,p,o]), as it would remove (*, p, *)"
  770. assert (
  771. predicate is not None
  772. ), "p can't be None in .set([s,p,o]), as it would remove (s, *, *)"
  773. self.remove((subject, predicate, None))
  774. self.add((subject, predicate, object_))
  775. return self
  776. def subjects(
  777. self,
  778. predicate: Union[None, Path, _PredicateType] = None,
  779. object: Optional[Union[_ObjectType, List[_ObjectType]]] = None,
  780. unique: bool = False,
  781. ) -> Generator[_SubjectType, None, None]:
  782. """A generator of (optionally unique) subjects with the given
  783. predicate and object(s)
  784. Args:
  785. predicate: A specific predicate to match or None to match any predicate.
  786. object: A specific object or list of objects to match or None to match any object.
  787. unique: If True, only yield unique subjects.
  788. """
  789. # if the object is a list of Nodes, yield results from subject() call for each
  790. if isinstance(object, list):
  791. for obj in object:
  792. for s in self.subjects(predicate, obj, unique):
  793. yield s
  794. else:
  795. if not unique:
  796. for s, p, o in self.triples((None, predicate, object)):
  797. yield s
  798. else:
  799. subs = set()
  800. for s, p, o in self.triples((None, predicate, object)):
  801. if s not in subs:
  802. yield s
  803. try:
  804. subs.add(s)
  805. except MemoryError as e:
  806. logger.error(
  807. f"{e}. Consider not setting parameter 'unique' to True"
  808. )
  809. raise
  810. def predicates(
  811. self,
  812. subject: Optional[_SubjectType] = None,
  813. object: Optional[_ObjectType] = None,
  814. unique: bool = False,
  815. ) -> Generator[_PredicateType, None, None]:
  816. """Generate predicates with the given subject and object.
  817. Args:
  818. subject: A specific subject to match or None to match any subject.
  819. object: A specific object to match or None to match any object.
  820. unique: If True, only yield unique predicates.
  821. Yields:
  822. Predicates matching the given subject and object.
  823. """
  824. if not unique:
  825. for s, p, o in self.triples((subject, None, object)):
  826. yield p
  827. else:
  828. preds = set()
  829. for s, p, o in self.triples((subject, None, object)):
  830. if p not in preds:
  831. yield p
  832. try:
  833. preds.add(p)
  834. except MemoryError as e:
  835. logger.error(
  836. f"{e}. Consider not setting parameter 'unique' to True"
  837. )
  838. raise
  839. def objects(
  840. self,
  841. subject: Optional[Union[_SubjectType, List[_SubjectType]]] = None,
  842. predicate: Union[None, Path, _PredicateType] = None,
  843. unique: bool = False,
  844. ) -> Generator[_ObjectType, None, None]:
  845. """A generator of (optionally unique) objects with the given
  846. subject(s) and predicate
  847. Args:
  848. subject: A specific subject or a list of subjects to match or None to match any subject.
  849. predicate: A specific predicate to match or None to match any predicate.
  850. unique: If True, only yield unique objects.
  851. Yields:
  852. Objects matching the given subject and predicate.
  853. """
  854. if isinstance(subject, list):
  855. for subj in subject:
  856. for o in self.objects(subj, predicate, unique):
  857. yield o
  858. else:
  859. if not unique:
  860. for s, p, o in self.triples((subject, predicate, None)):
  861. yield o
  862. else:
  863. objs = set()
  864. for s, p, o in self.triples((subject, predicate, None)):
  865. if o not in objs:
  866. yield o
  867. try:
  868. objs.add(o)
  869. except MemoryError as e:
  870. logger.error(
  871. f"{e}. Consider not setting parameter 'unique' to True"
  872. )
  873. raise
  874. def subject_predicates(
  875. self, object: Optional[_ObjectType] = None, unique: bool = False
  876. ) -> Generator[Tuple[_SubjectType, _PredicateType], None, None]:
  877. """A generator of (optionally unique) (subject, predicate) tuples
  878. for the given object"""
  879. if not unique:
  880. for s, p, o in self.triples((None, None, object)):
  881. yield s, p
  882. else:
  883. subj_preds = set()
  884. for s, p, o in self.triples((None, None, object)):
  885. if (s, p) not in subj_preds:
  886. yield s, p
  887. try:
  888. subj_preds.add((s, p))
  889. except MemoryError as e:
  890. logger.error(
  891. f"{e}. Consider not setting parameter 'unique' to True"
  892. )
  893. raise
  894. def subject_objects(
  895. self,
  896. predicate: Union[None, Path, _PredicateType] = None,
  897. unique: bool = False,
  898. ) -> Generator[Tuple[_SubjectType, _ObjectType], None, None]:
  899. """A generator of (optionally unique) (subject, object) tuples
  900. for the given predicate"""
  901. if not unique:
  902. for s, p, o in self.triples((None, predicate, None)):
  903. yield s, o
  904. else:
  905. subj_objs = set()
  906. for s, p, o in self.triples((None, predicate, None)):
  907. if (s, o) not in subj_objs:
  908. yield s, o
  909. try:
  910. subj_objs.add((s, o))
  911. except MemoryError as e:
  912. logger.error(
  913. f"{e}. Consider not setting parameter 'unique' to True"
  914. )
  915. raise
  916. def predicate_objects(
  917. self, subject: Optional[_SubjectType] = None, unique: bool = False
  918. ) -> Generator[Tuple[_PredicateType, _ObjectType], None, None]:
  919. """A generator of (optionally unique) (predicate, object) tuples
  920. for the given subject"""
  921. if not unique:
  922. for s, p, o in self.triples((subject, None, None)):
  923. yield p, o
  924. else:
  925. pred_objs = set()
  926. for s, p, o in self.triples((subject, None, None)):
  927. if (p, o) not in pred_objs:
  928. yield p, o
  929. try:
  930. pred_objs.add((p, o))
  931. except MemoryError as e:
  932. logger.error(
  933. f"{e}. Consider not setting parameter 'unique' to True"
  934. )
  935. raise
  936. def triples_choices(
  937. self,
  938. triple: _TripleChoiceType,
  939. context: Optional[_ContextType] = None,
  940. ) -> Generator[_TripleType, None, None]:
  941. subject, predicate, object_ = triple
  942. # type error: Argument 1 to "triples_choices" of "Store" has incompatible type "Tuple[Union[List[Node], Node], Union[Node, List[Node]], Union[Node, List[Node]]]"; expected "Union[Tuple[List[Node], Node, Node], Tuple[Node, List[Node], Node], Tuple[Node, Node, List[Node]]]"
  943. # type error note: unpacking discards type info
  944. for (s, p, o), cg in self.store.triples_choices(
  945. (subject, predicate, object_), context=self # type: ignore[arg-type]
  946. ):
  947. yield s, p, o
  948. @overload
  949. def value(
  950. self,
  951. subject: None = ...,
  952. predicate: None = ...,
  953. object: Optional[_ObjectType] = ...,
  954. default: Optional[Node] = ...,
  955. any: bool = ...,
  956. ) -> None: ...
  957. @overload
  958. def value(
  959. self,
  960. subject: Optional[_SubjectType] = ...,
  961. predicate: None = ...,
  962. object: None = ...,
  963. default: Optional[Node] = ...,
  964. any: bool = ...,
  965. ) -> None: ...
  966. @overload
  967. def value(
  968. self,
  969. subject: None = ...,
  970. predicate: Optional[_PredicateType] = ...,
  971. object: None = ...,
  972. default: Optional[Node] = ...,
  973. any: bool = ...,
  974. ) -> None: ...
  975. @overload
  976. def value(
  977. self,
  978. subject: Optional[_SubjectType] = ...,
  979. predicate: Optional[_PredicateType] = ...,
  980. object: Optional[_ObjectType] = ...,
  981. default: Optional[Node] = ...,
  982. any: bool = ...,
  983. ) -> Optional[Node]: ...
  984. def value(
  985. self,
  986. subject: Optional[_SubjectType] = None,
  987. predicate: Optional[_PredicateType] = RDF.value,
  988. object: Optional[_ObjectType] = None,
  989. default: Optional[Node] = None,
  990. any: bool = True,
  991. ) -> Optional[Node]:
  992. """Get a value for a pair of two criteria
  993. Exactly one of subject, predicate, object must be None. Useful if one
  994. knows that there may only be one value.
  995. It is one of those situations that occur a lot, hence this
  996. 'macro' like utility
  997. Args:
  998. subject: Subject of the triple pattern, exactly one of subject, predicate, object must be None
  999. predicate: Predicate of the triple pattern, exactly one of subject, predicate, object must be None
  1000. object: Object of the triple pattern, exactly one of subject, predicate, object must be None
  1001. default: Value to be returned if no values found
  1002. any: If True, return any value in the case there is more than one, else, raise UniquenessError
  1003. """
  1004. retval = default
  1005. if (
  1006. (subject is None and predicate is None)
  1007. or (subject is None and object is None)
  1008. or (predicate is None and object is None)
  1009. ):
  1010. return None
  1011. if object is None:
  1012. values = self.objects(subject, predicate)
  1013. if subject is None:
  1014. values = self.subjects(predicate, object)
  1015. if predicate is None:
  1016. values = self.predicates(subject, object)
  1017. try:
  1018. retval = next(values)
  1019. except StopIteration:
  1020. retval = default
  1021. else:
  1022. if any is False:
  1023. try:
  1024. next(values)
  1025. msg = (
  1026. "While trying to find a value for (%s, %s, %s) the"
  1027. " following multiple values where found:\n"
  1028. % (subject, predicate, object)
  1029. )
  1030. triples = self.store.triples((subject, predicate, object), None)
  1031. for (s, p, o), contexts in triples:
  1032. msg += "(%s, %s, %s)\n (contexts: %s)\n" % (
  1033. s,
  1034. p,
  1035. o,
  1036. list(contexts),
  1037. )
  1038. raise exceptions.UniquenessError(msg)
  1039. except StopIteration:
  1040. pass
  1041. return retval
  1042. def items(self, list: Node) -> Generator[Node, None, None]:
  1043. """Generator over all items in the resource specified by list
  1044. Args:
  1045. list: An RDF collection.
  1046. """
  1047. chain = set([list])
  1048. while list:
  1049. item = self.value(list, RDF.first)
  1050. if item is not None:
  1051. yield item
  1052. # type error: Incompatible types in assignment (expression has type "Optional[Node]", variable has type "Node")
  1053. list = self.value(list, RDF.rest) # type: ignore[assignment]
  1054. if list in chain:
  1055. raise ValueError("List contains a recursive rdf:rest reference")
  1056. chain.add(list)
  1057. def transitiveClosure( # noqa: N802
  1058. self,
  1059. func: Callable[[_TCArgT, Graph], Iterable[_TCArgT]],
  1060. arg: _TCArgT,
  1061. seen: Optional[Dict[_TCArgT, int]] = None,
  1062. ):
  1063. """Generates transitive closure of a user-defined function against the graph
  1064. ```python
  1065. from rdflib.collection import Collection
  1066. g = Graph()
  1067. a = BNode("foo")
  1068. b = BNode("bar")
  1069. c = BNode("baz")
  1070. g.add((a,RDF.first,RDF.type))
  1071. g.add((a,RDF.rest,b))
  1072. g.add((b,RDF.first,namespace.RDFS.label))
  1073. g.add((b,RDF.rest,c))
  1074. g.add((c,RDF.first,namespace.RDFS.comment))
  1075. g.add((c,RDF.rest,RDF.nil))
  1076. def topList(node,g):
  1077. for s in g.subjects(RDF.rest, node):
  1078. yield s
  1079. def reverseList(node,g):
  1080. for f in g.objects(node, RDF.first):
  1081. print(f)
  1082. for s in g.subjects(RDF.rest, node):
  1083. yield s
  1084. [rt for rt in g.transitiveClosure(
  1085. topList,RDF.nil)]
  1086. # [rdflib.term.BNode('baz'),
  1087. # rdflib.term.BNode('bar'),
  1088. # rdflib.term.BNode('foo')]
  1089. [rt for rt in g.transitiveClosure(
  1090. reverseList,RDF.nil)]
  1091. # http://www.w3.org/2000/01/rdf-schema#comment
  1092. # http://www.w3.org/2000/01/rdf-schema#label
  1093. # http://www.w3.org/1999/02/22-rdf-syntax-ns#type
  1094. # [rdflib.term.BNode('baz'),
  1095. # rdflib.term.BNode('bar'),
  1096. # rdflib.term.BNode('foo')]
  1097. ```
  1098. Args:
  1099. func: A function that generates a sequence of nodes
  1100. arg: The starting node
  1101. seen: A dict of visited nodes
  1102. """
  1103. if seen is None:
  1104. seen = {}
  1105. elif arg in seen:
  1106. return
  1107. seen[arg] = 1
  1108. for rt in func(arg, self):
  1109. yield rt
  1110. for rt_2 in self.transitiveClosure(func, rt, seen):
  1111. yield rt_2
  1112. def transitive_objects(
  1113. self,
  1114. subject: Optional[_SubjectType],
  1115. predicate: Optional[_PredicateType],
  1116. remember: Optional[Dict[Optional[_SubjectType], int]] = None,
  1117. ) -> Generator[Optional[_SubjectType], None, None]:
  1118. """Transitively generate objects for the ``predicate`` relationship
  1119. Generated objects belong to the depth first transitive closure of the
  1120. `predicate` relationship starting at `subject`.
  1121. Args:
  1122. subject: The subject to start the transitive closure from
  1123. predicate: The predicate to follow
  1124. remember: A dict of visited nodes
  1125. """
  1126. if remember is None:
  1127. remember = {}
  1128. if subject in remember:
  1129. return
  1130. remember[subject] = 1
  1131. yield subject
  1132. for object in self.objects(subject, predicate):
  1133. for o in self.transitive_objects(object, predicate, remember):
  1134. yield o
  1135. def transitive_subjects(
  1136. self,
  1137. predicate: Optional[_PredicateType],
  1138. object: Optional[_ObjectType],
  1139. remember: Optional[Dict[Optional[_ObjectType], int]] = None,
  1140. ) -> Generator[Optional[_ObjectType], None, None]:
  1141. """Transitively generate subjects for the ``predicate`` relationship
  1142. Generated subjects belong to the depth first transitive closure of the
  1143. `predicate` relationship starting at `object`.
  1144. Args:
  1145. predicate: The predicate to follow
  1146. object: The object to start the transitive closure from
  1147. remember: A dict of visited nodes
  1148. """
  1149. if remember is None:
  1150. remember = {}
  1151. if object in remember:
  1152. return
  1153. remember[object] = 1
  1154. yield object
  1155. for subject in self.subjects(predicate, object):
  1156. for s in self.transitive_subjects(predicate, subject, remember):
  1157. yield s
  1158. def qname(self, uri: str) -> str:
  1159. return self.namespace_manager.qname(uri)
  1160. def compute_qname(self, uri: str, generate: bool = True) -> Tuple[str, URIRef, str]:
  1161. return self.namespace_manager.compute_qname(uri, generate)
  1162. def bind(
  1163. self,
  1164. prefix: Optional[str],
  1165. namespace: Any, # noqa: F811
  1166. override: bool = True,
  1167. replace: bool = False,
  1168. ) -> None:
  1169. """Bind prefix to namespace
  1170. If override is True will bind namespace to given prefix even
  1171. if namespace was already bound to a different prefix.
  1172. if replace, replace any existing prefix with the new namespace
  1173. Args:
  1174. prefix: The prefix to bind
  1175. namespace: The namespace to bind the prefix to
  1176. override: If True, override any existing prefix binding
  1177. replace: If True, replace any existing namespace binding
  1178. Example:
  1179. ```python
  1180. graph.bind("foaf", "http://xmlns.com/foaf/0.1/")
  1181. ```
  1182. """
  1183. # TODO FIXME: This method's behaviour should be simplified and made
  1184. # more robust. If the method cannot do what it is asked it should raise
  1185. # an exception, it is also unclear why this method has all the
  1186. # different modes. It seems to just make it more complex to use, maybe
  1187. # it should be clarified when someone will need to use override=False
  1188. # and replace=False. And also why silent failure here is preferred over
  1189. # raising an exception.
  1190. return self.namespace_manager.bind(
  1191. prefix, namespace, override=override, replace=replace
  1192. )
  1193. def namespaces(self) -> Generator[Tuple[str, URIRef], None, None]:
  1194. """Generator over all the prefix, namespace tuples
  1195. Returns:
  1196. Generator yielding prefix, namespace tuples
  1197. """
  1198. for prefix, namespace in self.namespace_manager.namespaces(): # noqa: F402
  1199. yield prefix, namespace
  1200. def absolutize(self, uri: str, defrag: int = 1) -> URIRef:
  1201. """Turn uri into an absolute URI if it's not one already"""
  1202. return self.namespace_manager.absolutize(uri, defrag)
  1203. # no destination and non-None positional encoding
  1204. @overload
  1205. def serialize(
  1206. self,
  1207. destination: None,
  1208. format: str,
  1209. base: Optional[str],
  1210. encoding: str,
  1211. **args: Any,
  1212. ) -> bytes: ...
  1213. # no destination and non-None keyword encoding
  1214. @overload
  1215. def serialize(
  1216. self,
  1217. destination: None = ...,
  1218. format: str = ...,
  1219. base: Optional[str] = ...,
  1220. *,
  1221. encoding: str,
  1222. **args: Any,
  1223. ) -> bytes: ...
  1224. # no destination and None encoding
  1225. @overload
  1226. def serialize(
  1227. self,
  1228. destination: None = ...,
  1229. format: str = ...,
  1230. base: Optional[str] = ...,
  1231. encoding: None = ...,
  1232. **args: Any,
  1233. ) -> str: ...
  1234. # non-None destination
  1235. @overload
  1236. def serialize(
  1237. self,
  1238. destination: Union[str, pathlib.PurePath, IO[bytes]],
  1239. format: str = ...,
  1240. base: Optional[str] = ...,
  1241. encoding: Optional[str] = ...,
  1242. **args: Any,
  1243. ) -> Graph: ...
  1244. # fallback
  1245. @overload
  1246. def serialize(
  1247. self,
  1248. destination: Optional[Union[str, pathlib.PurePath, IO[bytes]]] = ...,
  1249. format: str = ...,
  1250. base: Optional[str] = ...,
  1251. encoding: Optional[str] = ...,
  1252. **args: Any,
  1253. ) -> Union[bytes, str, Graph]: ...
  1254. def serialize(
  1255. self: _GraphT,
  1256. destination: Optional[Union[str, pathlib.PurePath, IO[bytes]]] = None,
  1257. format: str = "turtle",
  1258. base: Optional[str] = None,
  1259. encoding: Optional[str] = None,
  1260. **args: Any,
  1261. ) -> Union[bytes, str, _GraphT]:
  1262. """Serialize the graph.
  1263. Args:
  1264. destination: The destination to serialize the graph to. This can be a path as a
  1265. string or pathlib.PurePath object, or it can be an IO[bytes] like object.
  1266. If this parameter is not supplied the serialized graph will be returned.
  1267. format: The format that the output should be written in. This value
  1268. references a Serializer plugin.
  1269. Format support is managed with [plugins][rdflib.plugin].
  1270. Defaults to "turtle" and some other formats are builtin,
  1271. like "xml" and "trix".
  1272. See also [serialize module][rdflib.plugins.parsers].
  1273. base: The base IRI for formats that support it. For the turtle format this
  1274. will be used as the @base directive.
  1275. encoding: Encoding of output.
  1276. args: Additional arguments to pass to the Serializer that will be used.
  1277. Returns:
  1278. The serialized graph if `destination` is None. The serialized graph is returned
  1279. as str if no encoding is specified, and as bytes if an encoding is specified.
  1280. self (i.e. the Graph instance) if `destination` is not None.
  1281. """
  1282. # if base is not given as attribute use the base set for the graph
  1283. if base is None:
  1284. base = self.base
  1285. serializer = plugin.get(format, Serializer)(self)
  1286. stream: IO[bytes]
  1287. if destination is None:
  1288. stream = BytesIO()
  1289. if encoding is None:
  1290. serializer.serialize(stream, base=base, encoding="utf-8", **args)
  1291. return stream.getvalue().decode("utf-8")
  1292. else:
  1293. serializer.serialize(stream, base=base, encoding=encoding, **args)
  1294. return stream.getvalue()
  1295. if hasattr(destination, "write"):
  1296. stream = cast(IO[bytes], destination)
  1297. serializer.serialize(stream, base=base, encoding=encoding, **args)
  1298. else:
  1299. if isinstance(destination, pathlib.PurePath):
  1300. os_path = str(destination)
  1301. else:
  1302. location = cast(str, destination)
  1303. scheme, netloc, path, params, _query, fragment = urlparse(location)
  1304. if scheme == "file":
  1305. if netloc != "":
  1306. raise ValueError(
  1307. f"the file URI {location!r} has an authority component which is not supported"
  1308. )
  1309. os_path = url2pathname(path)
  1310. else:
  1311. os_path = location
  1312. with open(os_path, "wb") as stream:
  1313. serializer.serialize(stream, base=base, encoding=encoding, **args)
  1314. return self
  1315. def print(
  1316. self,
  1317. format: str = "turtle",
  1318. encoding: str = "utf-8",
  1319. out: Optional[TextIO] = None,
  1320. ) -> None:
  1321. print(
  1322. self.serialize(None, format=format, encoding=encoding).decode(encoding),
  1323. file=out,
  1324. flush=True,
  1325. )
  1326. def parse(
  1327. self,
  1328. source: Optional[
  1329. Union[IO[bytes], TextIO, InputSource, str, bytes, pathlib.PurePath]
  1330. ] = None,
  1331. publicID: Optional[str] = None, # noqa: N803
  1332. format: Optional[str] = None,
  1333. location: Optional[str] = None,
  1334. file: Optional[Union[BinaryIO, TextIO]] = None,
  1335. data: Optional[Union[str, bytes]] = None,
  1336. **args: Any,
  1337. ) -> Graph:
  1338. """Parse an RDF source adding the resulting triples to the Graph.
  1339. The source is specified using one of source, location, file or data.
  1340. Args:
  1341. source: An `xml.sax.xmlreader.InputSource`, file-like object,
  1342. `pathlib.Path` like object, or string. In the case of a string the string
  1343. is the location of the source.
  1344. publicID: The logical URI to use as the document base. If None
  1345. specified the document location is used (at least in the case where
  1346. there is a document location). This is used as the base URI when
  1347. resolving relative URIs in the source document, as defined in `IETF
  1348. RFC 3986 <https://datatracker.ietf.org/doc/html/rfc3986#section-5.1.4>`_,
  1349. given the source document does not define a base URI.
  1350. format: Used if format can not be determined from source, e.g.
  1351. file extension or Media Type. Format support is managed
  1352. with [plugins][rdflib.plugin].
  1353. Available formats are e.g. "turle", "xml", "n3", "nt" and "trix"
  1354. or see [parser module][rdflib.plugins.parsers].
  1355. location: A string indicating the relative or absolute URL of the
  1356. source. `Graph`'s absolutize method is used if a relative location
  1357. is specified.
  1358. file: A file-like object.
  1359. data: A string containing the data to be parsed.
  1360. args: Additional arguments to pass to the parser.
  1361. Returns:
  1362. self, i.e. the Graph instance.
  1363. Example:
  1364. ```python
  1365. >>> my_data = '''
  1366. ... <rdf:RDF
  1367. ... xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  1368. ... xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  1369. ... >
  1370. ... <rdf:Description>
  1371. ... <rdfs:label>Example</rdfs:label>
  1372. ... <rdfs:comment>This is really just an example.</rdfs:comment>
  1373. ... </rdf:Description>
  1374. ... </rdf:RDF>
  1375. ... '''
  1376. >>> import os, tempfile
  1377. >>> fd, file_name = tempfile.mkstemp()
  1378. >>> f = os.fdopen(fd, "w")
  1379. >>> dummy = f.write(my_data) # Returns num bytes written
  1380. >>> f.close()
  1381. >>> g = Graph()
  1382. >>> result = g.parse(data=my_data, format="application/rdf+xml")
  1383. >>> len(g)
  1384. 2
  1385. >>> g = Graph()
  1386. >>> result = g.parse(location=file_name, format="application/rdf+xml")
  1387. >>> len(g)
  1388. 2
  1389. >>> g = Graph()
  1390. >>> with open(file_name, "r") as f:
  1391. ... result = g.parse(f, format="application/rdf+xml")
  1392. >>> len(g)
  1393. 2
  1394. >>> os.remove(file_name)
  1395. >>> # default turtle parsing
  1396. >>> result = g.parse(data="<http://example.com/a> <http://example.com/a> <http://example.com/a> .")
  1397. >>> len(g)
  1398. 3
  1399. ```
  1400. !!! warning "Caution"
  1401. This method can access directly or indirectly requested network or
  1402. file resources, for example, when parsing JSON-LD documents with
  1403. `@context` directives that point to a network location.
  1404. When processing untrusted or potentially malicious documents,
  1405. measures should be taken to restrict network and file access.
  1406. For information on available security measures, see the RDFLib
  1407. [Security Considerations](../security_considerations.md)
  1408. documentation.
  1409. """
  1410. source = create_input_source(
  1411. source=source,
  1412. publicID=publicID,
  1413. location=location,
  1414. file=file,
  1415. data=data,
  1416. format=format,
  1417. )
  1418. if format is None:
  1419. format = source.content_type
  1420. could_not_guess_format = False
  1421. if format is None:
  1422. if (
  1423. hasattr(source, "file")
  1424. and getattr(source.file, "name", None)
  1425. and isinstance(source.file.name, str)
  1426. ):
  1427. format = rdflib.util.guess_format(source.file.name)
  1428. if format is None:
  1429. format = "turtle"
  1430. could_not_guess_format = True
  1431. try:
  1432. parser = plugin.get(format, Parser)()
  1433. except plugin.PluginException:
  1434. # Handle the case when a URLInputSource returns RDF but with the headers
  1435. # as a format that does not exist in the plugin system.
  1436. # Use guess_format to guess the format based on the input's file suffix.
  1437. format = rdflib.util.guess_format(
  1438. source if not isinstance(source, InputSource) else str(source)
  1439. )
  1440. if format is None:
  1441. raise
  1442. parser = plugin.get(format, Parser)()
  1443. try:
  1444. # TODO FIXME: Parser.parse should have **kwargs argument.
  1445. parser.parse(source, self, **args)
  1446. except SyntaxError as se:
  1447. if could_not_guess_format:
  1448. raise ParserError(
  1449. "Could not guess RDF format for %r from file extension so tried Turtle but failed."
  1450. "You can explicitly specify format using the format argument."
  1451. % source
  1452. )
  1453. else:
  1454. raise se
  1455. finally:
  1456. if source.auto_close:
  1457. source.close()
  1458. return self
  1459. def query(
  1460. self,
  1461. query_object: Union[str, Query],
  1462. processor: Union[str, query.Processor] = "sparql",
  1463. result: Union[str, Type[query.Result]] = "sparql",
  1464. initNs: Optional[Mapping[str, Any]] = None, # noqa: N803
  1465. initBindings: Optional[Mapping[str, Identifier]] = None, # noqa: N803
  1466. use_store_provided: bool = True,
  1467. **kwargs: Any,
  1468. ) -> query.Result:
  1469. """
  1470. Query this graph.
  1471. Args:
  1472. query_object: The query string or object to execute.
  1473. processor: The query processor to use. Default is "sparql".
  1474. result: The result format to use. Default is "sparql".
  1475. initNs: Initial namespaces to use for resolving prefixes in the query.
  1476. If none are given, the namespaces from the graph's namespace manager are used.
  1477. initBindings: Initial variable bindings to use. A type of 'prepared queries'
  1478. can be realized by providing these bindings.
  1479. use_store_provided: Whether to use the store's query method if available.
  1480. kwargs: Additional arguments to pass to the query processor.
  1481. Returns:
  1482. A [`rdflib.query.Result`][`rdflib.query.Result`] instance.
  1483. !!! warning "Caution"
  1484. This method can access indirectly requested network endpoints, for
  1485. example, query processing will attempt to access network endpoints
  1486. specified in `SERVICE` directives.
  1487. When processing untrusted or potentially malicious queries, measures
  1488. should be taken to restrict network and file access.
  1489. For information on available security measures, see the RDFLib
  1490. [Security Considerations](../security_considerations.md)
  1491. documentation.
  1492. """
  1493. initBindings = initBindings or {} # noqa: N806
  1494. initNs = initNs or dict(self.namespaces()) # noqa: N806
  1495. if self.default_union:
  1496. query_graph = "__UNION__"
  1497. elif isinstance(self, ConjunctiveGraph):
  1498. query_graph = self.default_context.identifier
  1499. else:
  1500. query_graph = self.identifier
  1501. if hasattr(self.store, "query") and use_store_provided:
  1502. try:
  1503. return self.store.query(
  1504. query_object,
  1505. initNs,
  1506. initBindings,
  1507. query_graph,
  1508. **kwargs,
  1509. )
  1510. except NotImplementedError:
  1511. pass # store has no own implementation
  1512. if not isinstance(result, query.Result):
  1513. result = plugin.get(cast(str, result), query.Result)
  1514. if not isinstance(processor, query.Processor):
  1515. processor = plugin.get(processor, query.Processor)(self)
  1516. # type error: Argument 1 to "Result" has incompatible type "Mapping[str, Any]"; expected "str"
  1517. return result(processor.query(query_object, initBindings, initNs, **kwargs)) # type: ignore[arg-type]
  1518. def update(
  1519. self,
  1520. update_object: Union[Update, str],
  1521. processor: Union[str, rdflib.query.UpdateProcessor] = "sparql",
  1522. initNs: Optional[Mapping[str, Any]] = None, # noqa: N803
  1523. initBindings: Optional[Mapping[str, Identifier]] = None, # noqa: N803
  1524. use_store_provided: bool = True,
  1525. **kwargs: Any,
  1526. ) -> None:
  1527. """Update this graph with the given update query.
  1528. Args:
  1529. update_object: The update query string or object to execute.
  1530. processor: The update processor to use. Default is "sparql".
  1531. initNs: Initial namespaces to use for resolving prefixes in the query.
  1532. If none are given, the namespaces from the graph's namespace manager are used.
  1533. initBindings: Initial variable bindings to use.
  1534. use_store_provided: Whether to use the store's update method if available.
  1535. kwargs: Additional arguments to pass to the update processor.
  1536. !!! warning "Caution"
  1537. This method can access indirectly requested network endpoints, for
  1538. example, query processing will attempt to access network endpoints
  1539. specified in `SERVICE` directives.
  1540. When processing untrusted or potentially malicious queries, measures
  1541. should be taken to restrict network and file access.
  1542. For information on available security measures, see the RDFLib
  1543. Security Considerations documentation.
  1544. """
  1545. initBindings = initBindings or {} # noqa: N806
  1546. initNs = initNs or dict(self.namespaces()) # noqa: N806
  1547. if self.default_union:
  1548. query_graph = "__UNION__"
  1549. elif isinstance(self, ConjunctiveGraph):
  1550. query_graph = self.default_context.identifier
  1551. else:
  1552. query_graph = self.identifier
  1553. if hasattr(self.store, "update") and use_store_provided:
  1554. try:
  1555. return self.store.update(
  1556. update_object,
  1557. initNs,
  1558. initBindings,
  1559. query_graph,
  1560. **kwargs,
  1561. )
  1562. except NotImplementedError:
  1563. pass # store has no own implementation
  1564. if not isinstance(processor, query.UpdateProcessor):
  1565. processor = plugin.get(processor, query.UpdateProcessor)(self)
  1566. return processor.update(update_object, initBindings, initNs, **kwargs)
  1567. def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str:
  1568. """Return an n3 identifier for the Graph"""
  1569. return "[%s]" % self.identifier.n3(namespace_manager=namespace_manager)
  1570. def __reduce__(self) -> Tuple[Type[Graph], Tuple[Store, _ContextIdentifierType]]:
  1571. return (
  1572. Graph,
  1573. (
  1574. self.store,
  1575. self.identifier,
  1576. ),
  1577. )
  1578. def isomorphic(self, other: Graph) -> bool:
  1579. """Check if this graph is isomorphic to another graph.
  1580. Performs a basic check if these graphs are the same.
  1581. If no BNodes are involved, this is accurate.
  1582. Args:
  1583. other: The graph to compare with.
  1584. Returns:
  1585. True if the graphs are isomorphic, False otherwise.
  1586. Note:
  1587. This is only an approximation. See rdflib.compare for a correct
  1588. implementation of isomorphism checks.
  1589. """
  1590. # TODO: this is only an approximation.
  1591. if len(self) != len(other):
  1592. return False
  1593. for s, p, o in self:
  1594. if not isinstance(s, BNode) and not isinstance(o, BNode):
  1595. if not (s, p, o) in other: # noqa: E713
  1596. return False
  1597. for s, p, o in other:
  1598. if not isinstance(s, BNode) and not isinstance(o, BNode):
  1599. if not (s, p, o) in self: # noqa: E713
  1600. return False
  1601. # TODO: very well could be a false positive at this point yet.
  1602. return True
  1603. def connected(self) -> bool:
  1604. """Check if the Graph is connected.
  1605. The Graph is considered undirectional.
  1606. Returns:
  1607. True if all nodes have been visited and there are no unvisited nodes left,
  1608. False otherwise.
  1609. Note:
  1610. Performs a search on the Graph, starting from a random node. Then
  1611. iteratively goes depth-first through the triplets where the node is
  1612. subject and object.
  1613. """
  1614. all_nodes = list(self.all_nodes())
  1615. discovered = []
  1616. # take a random one, could also always take the first one, doesn't
  1617. # really matter.
  1618. if not all_nodes:
  1619. return False
  1620. visiting = [all_nodes[random.randrange(len(all_nodes))]]
  1621. while visiting:
  1622. x = visiting.pop()
  1623. if x not in discovered:
  1624. discovered.append(x)
  1625. for new_x in self.objects(subject=x):
  1626. if new_x not in discovered and new_x not in visiting:
  1627. visiting.append(new_x)
  1628. for new_x in self.subjects(object=x):
  1629. if new_x not in discovered and new_x not in visiting:
  1630. visiting.append(new_x)
  1631. # optimisation by only considering length, since no new objects can
  1632. # be introduced anywhere.
  1633. if len(all_nodes) == len(discovered):
  1634. return True
  1635. else:
  1636. return False
  1637. def all_nodes(self) -> Set[Node]:
  1638. res = set(self.objects())
  1639. res.update(self.subjects())
  1640. return res
  1641. def collection(self, identifier: _SubjectType) -> Collection:
  1642. """Create a new `Collection` instance.
  1643. Args:
  1644. identifier: A URIRef or BNode instance.
  1645. Returns:
  1646. A new Collection instance.
  1647. Example:
  1648. ```python
  1649. >>> graph = Graph()
  1650. >>> uri = URIRef("http://example.org/resource")
  1651. >>> collection = graph.collection(uri)
  1652. >>> assert isinstance(collection, Collection)
  1653. >>> assert collection.uri is uri
  1654. >>> assert collection.graph is graph
  1655. >>> collection += [ Literal(1), Literal(2) ]
  1656. ```
  1657. """
  1658. return Collection(self, identifier)
  1659. def resource(self, identifier: Union[Node, str]) -> Resource:
  1660. """Create a new ``Resource`` instance.
  1661. Args:
  1662. identifier: A URIRef or BNode instance.
  1663. Returns:
  1664. A new Resource instance.
  1665. Example:
  1666. ```python
  1667. >>> graph = Graph()
  1668. >>> uri = URIRef("http://example.org/resource")
  1669. >>> resource = graph.resource(uri)
  1670. >>> assert isinstance(resource, Resource)
  1671. >>> assert resource.identifier is uri
  1672. >>> assert resource.graph is graph
  1673. ```
  1674. """
  1675. if not isinstance(identifier, Node):
  1676. identifier = URIRef(identifier)
  1677. return Resource(self, identifier)
  1678. def _process_skolem_tuples(
  1679. self, target: Graph, func: Callable[[_TripleType], _TripleType]
  1680. ) -> None:
  1681. for t in self.triples((None, None, None)):
  1682. target.add(func(t))
  1683. def skolemize(
  1684. self,
  1685. new_graph: Optional[Graph] = None,
  1686. bnode: Optional[BNode] = None,
  1687. authority: Optional[str] = None,
  1688. basepath: Optional[str] = None,
  1689. ) -> Graph:
  1690. def do_skolemize(bnode: BNode, t: _TripleType) -> _TripleType:
  1691. (s, p, o) = t
  1692. if s == bnode:
  1693. if TYPE_CHECKING:
  1694. assert isinstance(s, BNode)
  1695. s = s.skolemize(authority=authority, basepath=basepath)
  1696. if o == bnode:
  1697. if TYPE_CHECKING:
  1698. assert isinstance(o, BNode)
  1699. o = o.skolemize(authority=authority, basepath=basepath)
  1700. return s, p, o
  1701. def do_skolemize2(t: _TripleType) -> _TripleType:
  1702. (s, p, o) = t
  1703. if isinstance(s, BNode):
  1704. s = s.skolemize(authority=authority, basepath=basepath)
  1705. if isinstance(o, BNode):
  1706. o = o.skolemize(authority=authority, basepath=basepath)
  1707. return s, p, o
  1708. retval = Graph() if new_graph is None else new_graph
  1709. if bnode is None:
  1710. self._process_skolem_tuples(retval, do_skolemize2)
  1711. elif isinstance(bnode, BNode):
  1712. # type error: Argument 1 to "do_skolemize" has incompatible type "Optional[BNode]"; expected "BNode"
  1713. self._process_skolem_tuples(retval, lambda t: do_skolemize(bnode, t)) # type: ignore[arg-type, unused-ignore]
  1714. return retval
  1715. def de_skolemize(
  1716. self, new_graph: Optional[Graph] = None, uriref: Optional[URIRef] = None
  1717. ) -> Graph:
  1718. def do_de_skolemize(uriref: URIRef, t: _TripleType) -> _TripleType:
  1719. (s, p, o) = t
  1720. if s == uriref:
  1721. if TYPE_CHECKING:
  1722. assert isinstance(s, URIRef)
  1723. s = s.de_skolemize()
  1724. if o == uriref:
  1725. if TYPE_CHECKING:
  1726. assert isinstance(o, URIRef)
  1727. o = o.de_skolemize()
  1728. return s, p, o
  1729. def do_de_skolemize2(t: _TripleType) -> _TripleType:
  1730. (s, p, o) = t
  1731. if RDFLibGenid._is_rdflib_skolem(s):
  1732. # type error: Argument 1 to "RDFLibGenid" has incompatible type "Node"; expected "str"
  1733. s = RDFLibGenid(s).de_skolemize() # type: ignore[arg-type]
  1734. elif Genid._is_external_skolem(s):
  1735. # type error: Argument 1 to "Genid" has incompatible type "Node"; expected "str"
  1736. s = Genid(s).de_skolemize() # type: ignore[arg-type]
  1737. if isinstance(o, URIRef):
  1738. if RDFLibGenid._is_rdflib_skolem(o):
  1739. o = RDFLibGenid(o).de_skolemize()
  1740. elif Genid._is_external_skolem(o):
  1741. o = Genid(o).de_skolemize()
  1742. return s, p, o
  1743. retval = Graph() if new_graph is None else new_graph
  1744. if uriref is None:
  1745. self._process_skolem_tuples(retval, do_de_skolemize2)
  1746. elif isinstance(uriref, Genid):
  1747. # type error: Argument 1 to "do_de_skolemize" has incompatible type "Optional[URIRef]"; expected "URIRef"
  1748. self._process_skolem_tuples(retval, lambda t: do_de_skolemize(uriref, t)) # type: ignore[arg-type, unused-ignore]
  1749. return retval
  1750. def cbd(
  1751. self,
  1752. resource: _SubjectType,
  1753. *,
  1754. target_graph: Graph | None = None,
  1755. include_reifications: bool = True,
  1756. ) -> Graph:
  1757. """Retrieves the Concise Bounded Description of a Resource from a Graph.
  1758. Args:
  1759. resource: A URIRef object, the Resource to query for.
  1760. target_graph: Optionally, a graph to add the CBD to; otherwise,
  1761. a new graph is created for the CBD.
  1762. include_reifications: If False, skip Rule 3 to exclude reified statements (default: True)
  1763. Returns:
  1764. A Graph, subgraph of self if no graph was provided otherwise the provided graph.
  1765. Note:
  1766. Concise Bounded Description (CBD) is defined as:
  1767. Given a particular node (the starting node) in a particular RDF graph (the source graph),
  1768. a subgraph of that particular graph, taken to comprise a concise bounded description of
  1769. the resource denoted by the starting node, can be identified as follows:
  1770. 1. Include in the subgraph all statements in the source graph where the subject of the
  1771. statement is the starting node;
  1772. 2. Recursively, for all statements identified in the subgraph thus far having a blank
  1773. node object, include in the subgraph all statements in the source graph where the
  1774. subject of the statement is the blank node in question and which are not already
  1775. included in the subgraph.
  1776. 3. Recursively, for all statements included in the subgraph thus far, for all
  1777. reifications of each statement in the source graph, include the concise bounded
  1778. description beginning from the rdf:Statement node of each reification.
  1779. This results in a subgraph where the object nodes are either URI references, literals,
  1780. or blank nodes not serving as the subject of any statement in the graph.
  1781. If include_reifications is set to False, Rule 3 above will be skipped, meaning reified statements
  1782. will not be included in the CBD. This can improve performance when reified statements are not present
  1783. or can be used to deliberately exclude them from the result.
  1784. """
  1785. # If no target graph is provided, create a new Graph to hold the CBD.
  1786. if target_graph is None:
  1787. subgraph = Graph()
  1788. else:
  1789. subgraph = target_graph
  1790. def add_to_cbd(uri: _SubjectType) -> None:
  1791. # Rule 3 Preparation:
  1792. # If reifications are to be included, build an index mapping triples with
  1793. # this subject to the set of reification nodes (rdf:Statement nodes) that reify them.
  1794. if include_reifications:
  1795. reif_index: dict[_TripleType, set[_SubjectType]] = {}
  1796. for stmt in self.subjects(RDF.subject, uri):
  1797. p: _PredicateType = self.value(stmt, RDF.predicate) # type: ignore[assignment]
  1798. o = self.value(stmt, RDF.object)
  1799. if p is not None and o is not None:
  1800. triple = (uri, p, o)
  1801. if triple not in reif_index:
  1802. reif_index[triple] = {stmt}
  1803. else:
  1804. reif_index[triple].add(stmt)
  1805. # For all triples where the subject is the current subject (Rule 1)
  1806. for s, p, o in self.triples((uri, None, None)):
  1807. # Add the triple to the CBD subgraph
  1808. subgraph.add((s, p, o))
  1809. # If the object is a blank node, recursively add its CBD (Rule 2)
  1810. if type(o) is BNode and (o, None, None) not in subgraph:
  1811. add_to_cbd(o)
  1812. # If including reifications (Rule 3):
  1813. if include_reifications:
  1814. # For each reification node of this triple, recursively add its CBD
  1815. stmts = reif_index.get((s, p, o), set())
  1816. for stmt in stmts:
  1817. if (stmt, None, None) not in subgraph:
  1818. add_to_cbd(stmt)
  1819. # Start the CBD construction from the given resource
  1820. add_to_cbd(resource)
  1821. return subgraph
  1822. _ContextType = Graph
  1823. class ConjunctiveGraph(Graph):
  1824. """A ConjunctiveGraph is an (unnamed) aggregation of all the named
  1825. graphs in a store.
  1826. !!! warning "Deprecation notice"
  1827. ConjunctiveGraph is deprecated, use [`rdflib.graph.Dataset`][rdflib.graph.Dataset] instead.
  1828. It has a `default` graph, whose name is associated with the
  1829. graph throughout its life. Constructor can take an identifier
  1830. to use as the name of this default graph or it will assign a
  1831. BNode.
  1832. All methods that add triples work against this default graph.
  1833. All queries are carried out against the union of all graphs.
  1834. """
  1835. _default_context: _ContextType
  1836. def __init__(
  1837. self,
  1838. store: Union[Store, str] = "default",
  1839. identifier: Optional[Union[IdentifiedNode, str]] = None,
  1840. default_graph_base: Optional[str] = None,
  1841. ):
  1842. super(ConjunctiveGraph, self).__init__(store, identifier=identifier)
  1843. if type(self) is ConjunctiveGraph:
  1844. warnings.warn(
  1845. "ConjunctiveGraph is deprecated, use Dataset instead.",
  1846. DeprecationWarning,
  1847. stacklevel=2,
  1848. )
  1849. assert self.store.context_aware, (
  1850. "ConjunctiveGraph must be backed by" " a context aware store."
  1851. )
  1852. self.context_aware = True
  1853. self.default_union = True # Conjunctive!
  1854. self._default_context: _ContextType = Graph(
  1855. store=self.store, identifier=identifier or BNode(), base=default_graph_base
  1856. )
  1857. @property
  1858. def default_context(self):
  1859. return self._default_context
  1860. @default_context.setter
  1861. def default_context(self, value):
  1862. self._default_context = value
  1863. def __str__(self) -> str:
  1864. pattern = (
  1865. "[a rdflib:ConjunctiveGraph;rdflib:storage "
  1866. "[a rdflib:Store;rdfs:label '%s']]"
  1867. )
  1868. return pattern % self.store.__class__.__name__
  1869. @overload
  1870. def _spoc(
  1871. self,
  1872. triple_or_quad: _QuadType,
  1873. default: bool = False,
  1874. ) -> _QuadType: ...
  1875. @overload
  1876. def _spoc(
  1877. self,
  1878. triple_or_quad: Union[_TripleType, _OptionalQuadType],
  1879. default: bool = False,
  1880. ) -> _OptionalQuadType: ...
  1881. @overload
  1882. def _spoc(
  1883. self,
  1884. triple_or_quad: None,
  1885. default: bool = False,
  1886. ) -> Tuple[None, None, None, Optional[Graph]]: ...
  1887. @overload
  1888. def _spoc(
  1889. self,
  1890. triple_or_quad: Optional[_TripleOrQuadPatternType],
  1891. default: bool = False,
  1892. ) -> _QuadPatternType: ...
  1893. @overload
  1894. def _spoc(
  1895. self,
  1896. triple_or_quad: _TripleOrQuadSelectorType,
  1897. default: bool = False,
  1898. ) -> _QuadSelectorType: ...
  1899. @overload
  1900. def _spoc(
  1901. self,
  1902. triple_or_quad: Optional[_TripleOrQuadSelectorType],
  1903. default: bool = False,
  1904. ) -> _QuadSelectorType: ...
  1905. def _spoc(
  1906. self,
  1907. triple_or_quad: Optional[_TripleOrQuadSelectorType],
  1908. default: bool = False,
  1909. ) -> _QuadSelectorType:
  1910. """
  1911. helper method for having methods that support
  1912. either triples or quads
  1913. """
  1914. if triple_or_quad is None:
  1915. return (None, None, None, self.default_context if default else None)
  1916. if len(triple_or_quad) == 3:
  1917. c = self.default_context if default else None
  1918. # type error: Too many values to unpack (3 expected, 4 provided)
  1919. (s, p, o) = triple_or_quad # type: ignore[misc, unused-ignore]
  1920. elif len(triple_or_quad) == 4:
  1921. # type error: Need more than 3 values to unpack (4 expected)
  1922. (s, p, o, c) = triple_or_quad # type: ignore[misc, unused-ignore]
  1923. c = self._graph(c)
  1924. return s, p, o, c
  1925. def __contains__(self, triple_or_quad: _TripleOrQuadSelectorType) -> bool:
  1926. """Support for 'triple/quad in graph' syntax"""
  1927. s, p, o, c = self._spoc(triple_or_quad)
  1928. for t in self.triples((s, p, o), context=c):
  1929. return True
  1930. return False
  1931. def add(
  1932. self: _ConjunctiveGraphT,
  1933. triple_or_quad: _TripleOrOptionalQuadType,
  1934. ) -> _ConjunctiveGraphT:
  1935. """Add a triple or quad to the store.
  1936. if a triple is given it is added to the default context
  1937. """
  1938. s, p, o, c = self._spoc(triple_or_quad, default=True)
  1939. _assertnode(s, p, o)
  1940. # type error: Argument "context" to "add" of "Store" has incompatible type "Optional[Graph]"; expected "Graph"
  1941. self.store.add((s, p, o), context=c, quoted=False) # type: ignore[arg-type]
  1942. return self
  1943. @overload
  1944. def _graph(self, c: Union[Graph, _ContextIdentifierType, str]) -> Graph: ...
  1945. @overload
  1946. def _graph(self, c: None) -> None: ...
  1947. def _graph(
  1948. self,
  1949. c: Optional[Union[Graph, _ContextIdentifierType, str]],
  1950. ) -> Optional[Graph]:
  1951. if c is None:
  1952. return None
  1953. if isinstance(c, (Dataset, ConjunctiveGraph)):
  1954. # Preserve the old behaviour for datasets.
  1955. return c
  1956. if isinstance(c, Graph):
  1957. # Resolve to a graph backed by this dataset store.
  1958. return self.get_context(c.identifier)
  1959. return self.get_context(c)
  1960. def addN( # noqa: N802
  1961. self: _ConjunctiveGraphT, quads: Iterable[_QuadType]
  1962. ) -> _ConjunctiveGraphT:
  1963. """Add a sequence of triples with context"""
  1964. self.store.addN(
  1965. (s, p, o, self._graph(c)) for s, p, o, c in quads if _assertnode(s, p, o)
  1966. )
  1967. return self
  1968. # type error: Argument 1 of "remove" is incompatible with supertype "Graph"; supertype defines the argument type as "Tuple[Optional[Node], Optional[Node], Optional[Node]]"
  1969. def remove(self: _ConjunctiveGraphT, triple_or_quad: _TripleOrOptionalQuadType) -> _ConjunctiveGraphT: # type: ignore[override]
  1970. """Removes a triple or quads
  1971. if a triple is given it is removed from all contexts
  1972. a quad is removed from the given context only
  1973. """
  1974. s, p, o, c = self._spoc(triple_or_quad)
  1975. self.store.remove((s, p, o), context=c)
  1976. return self
  1977. @overload
  1978. def triples(
  1979. self,
  1980. triple_or_quad: _TripleOrQuadPatternType,
  1981. context: Optional[_ContextType] = ...,
  1982. ) -> Generator[_TripleType, None, None]: ...
  1983. @overload
  1984. def triples(
  1985. self,
  1986. triple_or_quad: _TripleOrQuadPathPatternType,
  1987. context: Optional[_ContextType] = ...,
  1988. ) -> Generator[_TriplePathType, None, None]: ...
  1989. @overload
  1990. def triples(
  1991. self,
  1992. triple_or_quad: _TripleOrQuadSelectorType,
  1993. context: Optional[_ContextType] = ...,
  1994. ) -> Generator[_TripleOrTriplePathType, None, None]: ...
  1995. def triples(
  1996. self,
  1997. triple_or_quad: _TripleOrQuadSelectorType,
  1998. context: Optional[_ContextType] = None,
  1999. ) -> Generator[_TripleOrTriplePathType, None, None]:
  2000. """Iterate over all the triples in the entire conjunctive graph
  2001. For legacy reasons, this can take the context to query either
  2002. as a fourth element of the quad, or as the explicit context
  2003. keyword parameter. The kw param takes precedence.
  2004. """
  2005. s, p, o, c = self._spoc(triple_or_quad)
  2006. context = self._graph(context or c)
  2007. if self.default_union:
  2008. if context == self.default_context:
  2009. context = None
  2010. else:
  2011. if context is None:
  2012. context = self.default_context
  2013. if isinstance(p, Path):
  2014. if context is None:
  2015. context = self
  2016. for s, o in p.eval(context, s, o):
  2017. yield s, p, o
  2018. else:
  2019. for (s, p, o), cg in self.store.triples((s, p, o), context=context):
  2020. yield s, p, o
  2021. def quads(
  2022. self, triple_or_quad: Optional[_TripleOrQuadPatternType] = None
  2023. ) -> Generator[_OptionalQuadType, None, None]:
  2024. """Iterate over all the quads in the entire conjunctive graph"""
  2025. s, p, o, c = self._spoc(triple_or_quad)
  2026. for (s, p, o), cg in self.store.triples((s, p, o), context=c):
  2027. for ctx in cg:
  2028. yield s, p, o, ctx
  2029. def triples_choices(
  2030. self,
  2031. triple: _TripleChoiceType,
  2032. context: Optional[_ContextType] = None,
  2033. ) -> Generator[_TripleType, None, None]:
  2034. """Iterate over all the triples in the entire conjunctive graph"""
  2035. s, p, o = triple
  2036. if context is None:
  2037. if not self.default_union:
  2038. context = self.default_context
  2039. else:
  2040. context = self._graph(context)
  2041. # type error: Argument 1 to "triples_choices" of "Store" has incompatible type "Tuple[Union[List[Node], Node], Union[Node, List[Node]], Union[Node, List[Node]]]"; expected "Union[Tuple[List[Node], Node, Node], Tuple[Node, List[Node], Node], Tuple[Node, Node, List[Node]]]"
  2042. # type error note: unpacking discards type info
  2043. for (s1, p1, o1), cg in self.store.triples_choices((s, p, o), context=context): # type: ignore[arg-type]
  2044. yield s1, p1, o1
  2045. def __len__(self) -> int:
  2046. """Number of triples in the entire conjunctive graph"""
  2047. return self.store.__len__()
  2048. def contexts(
  2049. self, triple: Optional[_TripleType] = None
  2050. ) -> Generator[_ContextType, None, None]:
  2051. """Iterate over all contexts in the graph
  2052. If triple is specified, iterate over all contexts the triple is in.
  2053. """
  2054. for context in self.store.contexts(triple):
  2055. if isinstance(context, Graph):
  2056. # TODO: One of these should never happen and probably
  2057. # should raise an exception rather than smoothing over
  2058. # the weirdness - see #225
  2059. yield context
  2060. else:
  2061. # type error: Statement is unreachable
  2062. yield self.get_context(context) # type: ignore[unreachable]
  2063. def get_graph(self, identifier: _ContextIdentifierType) -> Union[Graph, None]:
  2064. """Returns the graph identified by given identifier"""
  2065. return [x for x in self.contexts() if x.identifier == identifier][0]
  2066. def get_context(
  2067. self,
  2068. identifier: Optional[Union[_ContextIdentifierType, str]],
  2069. quoted: bool = False,
  2070. base: Optional[str] = None,
  2071. ) -> Graph:
  2072. """Return a context graph for the given identifier
  2073. identifier must be a URIRef or BNode.
  2074. """
  2075. return Graph(
  2076. store=self.store,
  2077. identifier=identifier,
  2078. namespace_manager=self.namespace_manager,
  2079. base=base,
  2080. )
  2081. def remove_context(self, context: _ContextType) -> None:
  2082. """Removes the given context from the graph"""
  2083. self.store.remove((None, None, None), context)
  2084. def context_id(self, uri: str, context_id: Optional[str] = None) -> URIRef:
  2085. """URI#context"""
  2086. uri = uri.split("#", 1)[0]
  2087. if context_id is None:
  2088. context_id = "#context"
  2089. return URIRef(context_id, base=uri)
  2090. def parse(
  2091. self,
  2092. source: Optional[
  2093. Union[IO[bytes], TextIO, InputSource, str, bytes, pathlib.PurePath]
  2094. ] = None,
  2095. publicID: Optional[str] = None, # noqa: N803
  2096. format: Optional[str] = None,
  2097. location: Optional[str] = None,
  2098. file: Optional[Union[BinaryIO, TextIO]] = None,
  2099. data: Optional[Union[str, bytes]] = None,
  2100. **args: Any,
  2101. ) -> Graph:
  2102. """Parse source adding the resulting triples to its own context (sub graph
  2103. of this graph).
  2104. See [`rdflib.graph.Graph.parse`][rdflib.graph.Graph.parse] for documentation on arguments.
  2105. Args:
  2106. source: The source to parse
  2107. publicID: The public ID of the source
  2108. format: The format of the source
  2109. location: The location of the source
  2110. file: The file object to parse
  2111. data: The data to parse
  2112. **args: Additional arguments
  2113. Returns:
  2114. The graph into which the source was parsed. In the case of n3 it returns
  2115. the root context.
  2116. Note:
  2117. If the source is in a format that does not support named graphs its triples
  2118. will be added to the default graph (i.e. ConjunctiveGraph.default_context).
  2119. !!! warning "Caution"
  2120. This method can access directly or indirectly requested network or
  2121. file resources, for example, when parsing JSON-LD documents with
  2122. `@context` directives that point to a network location.
  2123. When processing untrusted or potentially malicious documents,
  2124. measures should be taken to restrict network and file access.
  2125. For information on available security measures, see the RDFLib
  2126. Security Considerations documentation.
  2127. !!! example "Changed in 7.0"
  2128. The `publicID` argument is no longer used as the identifier (i.e. name)
  2129. of the default graph as was the case before version 7.0. In the case of
  2130. sources that do not support named graphs, the `publicID` parameter will
  2131. also not be used as the name for the graph that the data is loaded into,
  2132. and instead the triples from sources that do not support named graphs will
  2133. be loaded into the default graph (i.e. ConjunctiveGraph.default_context).
  2134. """
  2135. source = create_input_source(
  2136. source=source,
  2137. publicID=publicID,
  2138. location=location,
  2139. file=file,
  2140. data=data,
  2141. format=format,
  2142. )
  2143. # NOTE on type hint: `xml.sax.xmlreader.InputSource.getPublicId` has no
  2144. # type annotations but given that systemId should be a string, and
  2145. # given that there is no specific mention of type for publicId, it
  2146. # seems reasonable to assume it should also be a string. Furthermore,
  2147. # create_input_source will ensure that publicId is not None, though it
  2148. # would be good if this guarantee was made more explicit i.e. by type
  2149. # hint on InputSource (TODO/FIXME).
  2150. context = self.default_context
  2151. context.parse(source, publicID=publicID, format=format, **args)
  2152. return self
  2153. def __reduce__(self) -> Tuple[Type[Graph], Tuple[Store, _ContextIdentifierType]]:
  2154. return ConjunctiveGraph, (self.store, self.identifier)
  2155. DATASET_DEFAULT_GRAPH_ID = URIRef("urn:x-rdflib:default")
  2156. class Dataset(ConjunctiveGraph):
  2157. """
  2158. An RDFLib Dataset is an object that stores multiple Named Graphs - instances of
  2159. RDFLib Graph identified by IRI - within it and allows whole-of-dataset or single
  2160. Graph use.
  2161. RDFLib's Dataset class is based on the [RDF 1.2. 'Dataset' definition](https://www.w3.org/TR/rdf12-datasets/):
  2162. An RDF dataset is a collection of RDF graphs, and comprises:
  2163. - Exactly one default graph, being an RDF graph. The default graph does not
  2164. have a name and MAY be empty.
  2165. - Zero or more named graphs. Each named graph is a pair consisting of an IRI or
  2166. a blank node (the graph name), and an RDF graph. Graph names are unique
  2167. within an RDF dataset.
  2168. Accordingly, a Dataset allows for `Graph` objects to be added to it with
  2169. [`URIRef`][rdflib.term.URIRef] or [`BNode`][rdflib.term.BNode] identifiers and always
  2170. creats a default graph with the [`URIRef`][rdflib.term.URIRef] identifier
  2171. `urn:x-rdflib:default`.
  2172. Dataset extends Graph's Subject, Predicate, Object (s, p, o) 'triple'
  2173. structure to include a graph identifier - archaically called Context - producing
  2174. 'quads' of s, p, o, g.
  2175. Triples, or quads, can be added to a Dataset. Triples, or quads with the graph
  2176. identifer :code:`urn:x-rdflib:default` go into the default graph.
  2177. !!! warning "Deprecation notice"
  2178. Dataset builds on the `ConjunctiveGraph` class but that class's direct
  2179. use is now deprecated (since RDFLib 7.x) and it should not be used.
  2180. `ConjunctiveGraph` will be removed from future RDFLib versions.
  2181. Examples of usage and see also the `examples/datast.py` file:
  2182. ```python
  2183. >>> # Create a new Dataset
  2184. >>> ds = Dataset()
  2185. >>> # simple triples goes to default graph
  2186. >>> ds.add((
  2187. ... URIRef("http://example.org/a"),
  2188. ... URIRef("http://www.example.org/b"),
  2189. ... Literal("foo")
  2190. ... )) # doctest: +ELLIPSIS
  2191. <Graph identifier=... (<class 'rdflib.graph.Dataset'>)>
  2192. >>> # Create a graph in the dataset, if the graph name has already been
  2193. >>> # used, the corresponding graph will be returned
  2194. >>> # (ie, the Dataset keeps track of the constituent graphs)
  2195. >>> g = ds.graph(URIRef("http://www.example.com/gr"))
  2196. >>> # add triples to the new graph as usual
  2197. >>> g.add((
  2198. ... URIRef("http://example.org/x"),
  2199. ... URIRef("http://example.org/y"),
  2200. ... Literal("bar")
  2201. ... )) # doctest: +ELLIPSIS
  2202. <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
  2203. >>> # alternatively: add a quad to the dataset -> goes to the graph
  2204. >>> ds.add((
  2205. ... URIRef("http://example.org/x"),
  2206. ... URIRef("http://example.org/z"),
  2207. ... Literal("foo-bar"),
  2208. ... g
  2209. ... )) # doctest: +ELLIPSIS
  2210. <Graph identifier=... (<class 'rdflib.graph.Dataset'>)>
  2211. >>> # querying triples return them all regardless of the graph
  2212. >>> for t in ds.triples((None,None,None)): # doctest: +SKIP
  2213. ... print(t) # doctest: +NORMALIZE_WHITESPACE
  2214. (rdflib.term.URIRef("http://example.org/a"),
  2215. rdflib.term.URIRef("http://www.example.org/b"),
  2216. rdflib.term.Literal("foo"))
  2217. (rdflib.term.URIRef("http://example.org/x"),
  2218. rdflib.term.URIRef("http://example.org/z"),
  2219. rdflib.term.Literal("foo-bar"))
  2220. (rdflib.term.URIRef("http://example.org/x"),
  2221. rdflib.term.URIRef("http://example.org/y"),
  2222. rdflib.term.Literal("bar"))
  2223. >>> # querying quads() return quads; the fourth argument can be unrestricted
  2224. >>> # (None) or restricted to a graph
  2225. >>> for q in ds.quads((None, None, None, None)): # doctest: +SKIP
  2226. ... print(q) # doctest: +NORMALIZE_WHITESPACE
  2227. (rdflib.term.URIRef("http://example.org/a"),
  2228. rdflib.term.URIRef("http://www.example.org/b"),
  2229. rdflib.term.Literal("foo"),
  2230. None)
  2231. (rdflib.term.URIRef("http://example.org/x"),
  2232. rdflib.term.URIRef("http://example.org/y"),
  2233. rdflib.term.Literal("bar"),
  2234. rdflib.term.URIRef("http://www.example.com/gr"))
  2235. (rdflib.term.URIRef("http://example.org/x"),
  2236. rdflib.term.URIRef("http://example.org/z"),
  2237. rdflib.term.Literal("foo-bar"),
  2238. rdflib.term.URIRef("http://www.example.com/gr"))
  2239. >>> # unrestricted looping is equivalent to iterating over the entire Dataset
  2240. >>> for q in ds: # doctest: +SKIP
  2241. ... print(q) # doctest: +NORMALIZE_WHITESPACE
  2242. (rdflib.term.URIRef("http://example.org/a"),
  2243. rdflib.term.URIRef("http://www.example.org/b"),
  2244. rdflib.term.Literal("foo"),
  2245. None)
  2246. (rdflib.term.URIRef("http://example.org/x"),
  2247. rdflib.term.URIRef("http://example.org/y"),
  2248. rdflib.term.Literal("bar"),
  2249. rdflib.term.URIRef("http://www.example.com/gr"))
  2250. (rdflib.term.URIRef("http://example.org/x"),
  2251. rdflib.term.URIRef("http://example.org/z"),
  2252. rdflib.term.Literal("foo-bar"),
  2253. rdflib.term.URIRef("http://www.example.com/gr"))
  2254. >>> # resticting iteration to a graph:
  2255. >>> for q in ds.quads((None, None, None, g)): # doctest: +SKIP
  2256. ... print(q) # doctest: +NORMALIZE_WHITESPACE
  2257. (rdflib.term.URIRef("http://example.org/x"),
  2258. rdflib.term.URIRef("http://example.org/y"),
  2259. rdflib.term.Literal("bar"),
  2260. rdflib.term.URIRef("http://www.example.com/gr"))
  2261. (rdflib.term.URIRef("http://example.org/x"),
  2262. rdflib.term.URIRef("http://example.org/z"),
  2263. rdflib.term.Literal("foo-bar"),
  2264. rdflib.term.URIRef("http://www.example.com/gr"))
  2265. >>> # Note that in the call above -
  2266. >>> # ds.quads((None,None,None,"http://www.example.com/gr"))
  2267. >>> # would have been accepted, too
  2268. >>> # graph names in the dataset can be queried:
  2269. >>> for c in ds.graphs(): # doctest: +SKIP
  2270. ... print(c.identifier) # doctest:
  2271. urn:x-rdflib:default
  2272. http://www.example.com/gr
  2273. >>> # A graph can be created without specifying a name; a skolemized genid
  2274. >>> # is created on the fly
  2275. >>> h = ds.graph()
  2276. >>> for c in ds.graphs(): # doctest: +SKIP
  2277. ... print(c) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
  2278. DEFAULT
  2279. https://rdflib.github.io/.well-known/genid/rdflib/N...
  2280. http://www.example.com/gr
  2281. >>> # Note that the Dataset.graphs() call returns names of empty graphs,
  2282. >>> # too. This can be restricted:
  2283. >>> for c in ds.graphs(empty=False): # doctest: +SKIP
  2284. ... print(c) # doctest: +NORMALIZE_WHITESPACE
  2285. DEFAULT
  2286. http://www.example.com/gr
  2287. >>> # a graph can also be removed from a dataset via ds.remove_graph(g)
  2288. ```
  2289. !!! example "New in version 4.0"
  2290. """
  2291. def __init__(
  2292. self,
  2293. store: Union[Store, str] = "default",
  2294. default_union: bool = False,
  2295. default_graph_base: Optional[str] = None,
  2296. ):
  2297. super(Dataset, self).__init__(store=store, identifier=None)
  2298. if not self.store.graph_aware:
  2299. raise Exception("Dataset must be backed by a graph-aware store!")
  2300. self._default_context = Graph(
  2301. store=self.store,
  2302. identifier=DATASET_DEFAULT_GRAPH_ID,
  2303. base=default_graph_base,
  2304. )
  2305. self.default_union = default_union
  2306. @property
  2307. def default_context(self):
  2308. warnings.warn(
  2309. "Dataset.default_context is deprecated, use Dataset.default_graph instead.",
  2310. DeprecationWarning,
  2311. stacklevel=2,
  2312. )
  2313. return self._default_context
  2314. @default_context.setter
  2315. def default_context(self, value):
  2316. warnings.warn(
  2317. "Dataset.default_context is deprecated, use Dataset.default_graph instead.",
  2318. DeprecationWarning,
  2319. stacklevel=2,
  2320. )
  2321. self._default_context = value
  2322. @property
  2323. def default_graph(self):
  2324. return self._default_context
  2325. @default_graph.setter
  2326. def default_graph(self, value):
  2327. self._default_context = value
  2328. @property
  2329. def identifier(self):
  2330. warnings.warn(
  2331. "Dataset.identifier is deprecated and will be removed in future versions.",
  2332. DeprecationWarning,
  2333. stacklevel=2,
  2334. )
  2335. return super(Dataset, self).identifier
  2336. def __str__(self) -> str:
  2337. pattern = (
  2338. "[a rdflib:Dataset;rdflib:storage " "[a rdflib:Store;rdfs:label '%s']]"
  2339. )
  2340. return pattern % self.store.__class__.__name__
  2341. # type error: Return type "Tuple[Type[Dataset], Tuple[Store, bool]]" of "__reduce__" incompatible with return type "Tuple[Type[Graph], Tuple[Store, IdentifiedNode]]" in supertype "ConjunctiveGraph"
  2342. # type error: Return type "Tuple[Type[Dataset], Tuple[Store, bool]]" of "__reduce__" incompatible with return type "Tuple[Type[Graph], Tuple[Store, IdentifiedNode]]" in supertype "Graph"
  2343. def __reduce__(self) -> Tuple[Type[Dataset], Tuple[Store, bool]]: # type: ignore[override]
  2344. return (type(self), (self.store, self.default_union))
  2345. def __getstate__(self) -> Tuple[Store, _ContextIdentifierType, _ContextType, bool]:
  2346. return self.store, self.identifier, self.default_graph, self.default_union
  2347. def __setstate__(
  2348. self, state: Tuple[Store, _ContextIdentifierType, _ContextType, bool]
  2349. ) -> None:
  2350. # type error: Property "store" defined in "Graph" is read-only
  2351. # type error: Property "identifier" defined in "Graph" is read-only
  2352. self.store, self.identifier, self.default_graph, self.default_union = state # type: ignore[misc]
  2353. def __iadd__(self: _DatasetT, other: Iterable[_QuadType]) -> _DatasetT: # type: ignore[override, misc]
  2354. """Add all quads in Dataset other to Dataset.
  2355. BNode IDs are not changed."""
  2356. self.addN((s, p, o, g) for s, p, o, g in other)
  2357. return self
  2358. def graph(
  2359. self,
  2360. identifier: Optional[Union[_ContextIdentifierType, _ContextType, str]] = None,
  2361. base: Optional[str] = None,
  2362. ) -> Graph:
  2363. if identifier is None:
  2364. from rdflib.term import _SKOLEM_DEFAULT_AUTHORITY, rdflib_skolem_genid
  2365. self.bind(
  2366. "genid",
  2367. _SKOLEM_DEFAULT_AUTHORITY + rdflib_skolem_genid,
  2368. override=False,
  2369. )
  2370. identifier = BNode().skolemize()
  2371. graph_to_copy: Optional[Graph] = None
  2372. if isinstance(identifier, Graph) and not isinstance(
  2373. identifier, (Dataset, ConjunctiveGraph)
  2374. ):
  2375. # Preserve #3259 semantics: importing an external graph through
  2376. # Dataset.graph()/add_graph() should copy its statements.
  2377. graph_to_copy = identifier
  2378. g = self._graph(identifier)
  2379. if graph_to_copy is not None:
  2380. g.__iadd__(graph_to_copy)
  2381. g.base = base
  2382. self.store.add_graph(g)
  2383. return g
  2384. def parse(
  2385. self,
  2386. source: Optional[
  2387. Union[IO[bytes], TextIO, InputSource, str, bytes, pathlib.PurePath]
  2388. ] = None,
  2389. publicID: Optional[str] = None, # noqa: N803
  2390. format: Optional[str] = None,
  2391. location: Optional[str] = None,
  2392. file: Optional[Union[BinaryIO, TextIO]] = None,
  2393. data: Optional[Union[str, bytes]] = None,
  2394. **args: Any,
  2395. ) -> Dataset:
  2396. """Parse an RDF source adding the resulting triples to the Graph.
  2397. Args:
  2398. source: The source to parse. See rdflib.graph.Graph.parse for details.
  2399. publicID: The public ID of the source.
  2400. format: The format of the source.
  2401. location: The location of the source.
  2402. file: The file object to parse.
  2403. data: The data to parse.
  2404. **args: Additional arguments.
  2405. Returns:
  2406. The graph that the source was parsed into.
  2407. Note:
  2408. The source is specified using one of source, location, file or data.
  2409. If the source is in a format that does not support named graphs its triples
  2410. will be added to the default graph
  2411. (i.e. :attr:`.Dataset.default_graph`).
  2412. If the source is in a format that does not support named graphs its triples
  2413. will be added to the default graph (i.e. Dataset.default_graph).
  2414. !!! warning "Caution"
  2415. This method can access directly or indirectly requested network or
  2416. file resources, for example, when parsing JSON-LD documents with
  2417. `@context` directives that point to a network location.
  2418. When processing untrusted or potentially malicious documents,
  2419. measures should be taken to restrict network and file access.
  2420. For information on available security measures, see the RDFLib
  2421. Security Considerations documentation.
  2422. !!! example "Changed in 7.0"
  2423. The `publicID` argument is no longer used as the identifier (i.e. name)
  2424. of the default graph as was the case before version 7.0. In the case of
  2425. sources that do not support named graphs, the `publicID` parameter will
  2426. also not be used as the name for the graph that the data is loaded into,
  2427. and instead the triples from sources that do not support named graphs will
  2428. be loaded into the default graph (i.e. Dataset.default_graph).
  2429. """
  2430. ConjunctiveGraph.parse(
  2431. self, source, publicID, format, location, file, data, **args
  2432. )
  2433. return self
  2434. def add_graph(
  2435. self, g: Optional[Union[_ContextIdentifierType, _ContextType, str]]
  2436. ) -> Graph:
  2437. """alias of graph for consistency"""
  2438. return self.graph(g)
  2439. def remove_graph(
  2440. self: _DatasetT, g: Optional[Union[_ContextIdentifierType, _ContextType, str]]
  2441. ) -> _DatasetT:
  2442. if not isinstance(g, Graph):
  2443. g = self.get_context(g)
  2444. self.store.remove_graph(g)
  2445. if g is None or g == self.default_graph:
  2446. # default graph cannot be removed
  2447. # only triples deleted, so add it back in
  2448. self.store.add_graph(self.default_graph)
  2449. return self
  2450. def contexts(
  2451. self, triple: Optional[_TripleType] = None
  2452. ) -> Generator[_ContextType, None, None]:
  2453. warnings.warn(
  2454. "Dataset.contexts is deprecated, use Dataset.graphs instead.",
  2455. DeprecationWarning,
  2456. stacklevel=2,
  2457. )
  2458. default = False
  2459. for c in super(Dataset, self).contexts(triple):
  2460. default |= c.identifier == DATASET_DEFAULT_GRAPH_ID
  2461. yield c
  2462. if not default:
  2463. yield self.graph(DATASET_DEFAULT_GRAPH_ID)
  2464. def graphs(
  2465. self, triple: Optional[_TripleType] = None
  2466. ) -> Generator[_ContextType, None, None]:
  2467. default = False
  2468. for c in super(Dataset, self).contexts(triple):
  2469. default |= c.identifier == DATASET_DEFAULT_GRAPH_ID
  2470. yield c
  2471. if not default:
  2472. yield self.graph(DATASET_DEFAULT_GRAPH_ID)
  2473. # type error: Return type "Generator[Tuple[Node, Node, Node, Optional[Node]], None, None]" of "quads" incompatible with return type "Generator[Tuple[Node, Node, Node, Optional[Graph]], None, None]" in supertype "ConjunctiveGraph"
  2474. def quads( # type: ignore[override]
  2475. self, quad: Optional[_TripleOrQuadPatternType] = None
  2476. ) -> Generator[_OptionalIdentifiedQuadType, None, None]:
  2477. for s, p, o, c in super(Dataset, self).quads(quad):
  2478. # type error: Item "None" of "Optional[Graph]" has no attribute "identifier"
  2479. if c.identifier == self.default_graph: # type: ignore[union-attr]
  2480. yield s, p, o, None
  2481. else:
  2482. # type error: Item "None" of "Optional[Graph]" has no attribute "identifier" [union-attr]
  2483. yield s, p, o, c.identifier # type: ignore[union-attr]
  2484. # type error: Return type "Generator[Tuple[Node, URIRef, Node, Optional[IdentifiedNode]], None, None]" of "__iter__" incompatible with return type "Generator[Tuple[IdentifiedNode, IdentifiedNode, Union[IdentifiedNode, Literal]], None, None]" in supertype "Graph"
  2485. def __iter__( # type: ignore[override]
  2486. self,
  2487. ) -> Generator[_OptionalIdentifiedQuadType, None, None]:
  2488. """Iterates over all quads in the store"""
  2489. return self.quads((None, None, None, None))
  2490. @overload
  2491. def serialize(
  2492. self,
  2493. destination: None,
  2494. format: str,
  2495. base: Optional[str],
  2496. encoding: str,
  2497. **args: Any,
  2498. ) -> bytes: ...
  2499. # no destination and non-None keyword encoding
  2500. @overload
  2501. def serialize(
  2502. self,
  2503. destination: None = ...,
  2504. format: str = ...,
  2505. base: Optional[str] = ...,
  2506. *,
  2507. encoding: str,
  2508. **args: Any,
  2509. ) -> bytes: ...
  2510. # no destination and None encoding
  2511. @overload
  2512. def serialize(
  2513. self,
  2514. destination: None = ...,
  2515. format: str = ...,
  2516. base: Optional[str] = ...,
  2517. encoding: None = ...,
  2518. **args: Any,
  2519. ) -> str: ...
  2520. # non-None destination
  2521. @overload
  2522. def serialize(
  2523. self,
  2524. destination: Union[str, pathlib.PurePath, IO[bytes]],
  2525. format: str = ...,
  2526. base: Optional[str] = ...,
  2527. encoding: Optional[str] = ...,
  2528. **args: Any,
  2529. ) -> Graph: ...
  2530. # fallback
  2531. @overload
  2532. def serialize(
  2533. self,
  2534. destination: Optional[Union[str, pathlib.PurePath, IO[bytes]]] = ...,
  2535. format: str = ...,
  2536. base: Optional[str] = ...,
  2537. encoding: Optional[str] = ...,
  2538. **args: Any,
  2539. ) -> Union[bytes, str, Graph]: ...
  2540. def serialize(
  2541. self,
  2542. destination: Optional[Union[str, pathlib.PurePath, IO[bytes]]] = None,
  2543. format: str = "trig",
  2544. base: Optional[str] = None,
  2545. encoding: Optional[str] = None,
  2546. **args: Any,
  2547. ) -> Union[bytes, str, Graph]:
  2548. return super(Dataset, self).serialize(
  2549. destination=destination, format=format, base=base, encoding=encoding, **args
  2550. )
  2551. class QuotedGraph(Graph):
  2552. """
  2553. Quoted Graphs are intended to implement Notation 3 formulae. They are
  2554. associated with a required identifier that the N3 parser *must* provide
  2555. in order to maintain consistent formulae identification for scenarios
  2556. such as implication and other such processing.
  2557. """
  2558. def __init__(
  2559. self,
  2560. store: Union[Store, str],
  2561. identifier: Optional[Union[_ContextIdentifierType, str]],
  2562. ):
  2563. super(QuotedGraph, self).__init__(store, identifier)
  2564. def add(self: _GraphT, triple: _TripleType) -> _GraphT:
  2565. """Add a triple with self as context"""
  2566. s, p, o = triple
  2567. assert isinstance(s, Node), "Subject %s must be an rdflib term" % (s,)
  2568. assert isinstance(p, Node), "Predicate %s must be an rdflib term" % (p,)
  2569. assert isinstance(o, Node), "Object %s must be an rdflib term" % (o,)
  2570. self.store.add((s, p, o), self, quoted=True)
  2571. return self
  2572. def addN(self: _GraphT, quads: Iterable[_QuadType]) -> _GraphT: # noqa: N802
  2573. """Add a sequence of triple with context"""
  2574. self.store.addN(
  2575. (s, p, o, c)
  2576. for s, p, o, c in quads
  2577. if isinstance(c, QuotedGraph)
  2578. and c.identifier is self.identifier
  2579. and _assertnode(s, p, o)
  2580. )
  2581. return self
  2582. def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str:
  2583. """Return an n3 identifier for the Graph"""
  2584. return "{%s}" % self.identifier.n3(namespace_manager=namespace_manager)
  2585. def __str__(self) -> str:
  2586. identifier = self.identifier.n3()
  2587. label = self.store.__class__.__name__
  2588. pattern = (
  2589. "{this rdflib.identifier %s;rdflib:storage "
  2590. "[a rdflib:Store;rdfs:label '%s']}"
  2591. )
  2592. return pattern % (identifier, label)
  2593. def __reduce__(self) -> Tuple[Type[Graph], Tuple[Store, _ContextIdentifierType]]:
  2594. return QuotedGraph, (self.store, self.identifier)
  2595. # Make sure QuotedGraph is ordered correctly
  2596. # wrt to other Terms.
  2597. # this must be done here, as the QuotedGraph cannot be
  2598. # circularily imported in term.py
  2599. rdflib.term._ORDERING[QuotedGraph] = 11
  2600. class Seq:
  2601. """Wrapper around an RDF Seq resource
  2602. It implements a container type in Python with the order of the items
  2603. returned corresponding to the Seq content. It is based on the natural
  2604. ordering of the predicate names _1, _2, _3, etc, which is the
  2605. 'implementation' of a sequence in RDF terms.
  2606. Args:
  2607. graph: the graph containing the Seq
  2608. subject:the subject of a Seq. Note that the init does not
  2609. check whether this is a Seq, this is done in whoever
  2610. creates this instance!
  2611. """
  2612. def __init__(self, graph: Graph, subject: _SubjectType):
  2613. self._list: List[Tuple[int, _ObjectType]]
  2614. _list = self._list = list()
  2615. LI_INDEX = URIRef(str(RDF) + "_") # noqa: N806
  2616. for p, o in graph.predicate_objects(subject):
  2617. # type error: "Node" has no attribute "startswith"
  2618. if p.startswith(LI_INDEX): # type: ignore[attr-defined] # != RDF.Seq:
  2619. # type error: "Node" has no attribute "replace"
  2620. i = int(p.replace(LI_INDEX, "")) # type: ignore[attr-defined]
  2621. _list.append((i, o))
  2622. # here is the trick: the predicates are _1, _2, _3, etc. Ie,
  2623. # by sorting the keys (by integer) we have what we want!
  2624. _list.sort()
  2625. def toPython(self) -> Seq: # noqa: N802
  2626. return self
  2627. def __iter__(self) -> Generator[_ObjectType, None, None]:
  2628. """Generator over the items in the Seq"""
  2629. for _, item in self._list:
  2630. yield item
  2631. def __len__(self) -> int:
  2632. """Length of the Seq"""
  2633. return len(self._list)
  2634. def __getitem__(self, index) -> _ObjectType:
  2635. """Item given by index from the Seq"""
  2636. index, item = self._list.__getitem__(index)
  2637. return item
  2638. class ModificationException(Exception): # noqa: N818
  2639. def __init__(self) -> None:
  2640. pass
  2641. def __str__(self) -> str:
  2642. return (
  2643. "Modifications and transactional operations not allowed on "
  2644. "ReadOnlyGraphAggregate instances"
  2645. )
  2646. class UnSupportedAggregateOperation(Exception): # noqa: N818
  2647. def __init__(self) -> None:
  2648. pass
  2649. def __str__(self) -> str:
  2650. return "This operation is not supported by ReadOnlyGraphAggregate " "instances"
  2651. class ReadOnlyGraphAggregate(ConjunctiveGraph):
  2652. """Utility class for treating a set of graphs as a single graph
  2653. Only read operations are supported (hence the name). Essentially a
  2654. ConjunctiveGraph over an explicit subset of the entire store.
  2655. """
  2656. def __init__(self, graphs: List[Graph], store: Union[str, Store] = "default"):
  2657. if store is not None:
  2658. super(ReadOnlyGraphAggregate, self).__init__(store)
  2659. Graph.__init__(self, store)
  2660. self.__namespace_manager = None
  2661. assert (
  2662. isinstance(graphs, list)
  2663. and graphs
  2664. and [g for g in graphs if isinstance(g, Graph)]
  2665. ), "graphs argument must be a list of Graphs!!"
  2666. self.graphs = graphs
  2667. def __repr__(self) -> str:
  2668. return "<ReadOnlyGraphAggregate: %s graphs>" % len(self.graphs)
  2669. def destroy(self, configuration: str) -> NoReturn:
  2670. raise ModificationException()
  2671. # Transactional interfaces (optional)
  2672. def commit(self) -> NoReturn:
  2673. raise ModificationException()
  2674. def rollback(self) -> NoReturn:
  2675. raise ModificationException()
  2676. def open(self, configuration: str | tuple[str, str], create: bool = False) -> None:
  2677. # TODO: is there a use case for this method?
  2678. for graph in self.graphs:
  2679. # type error: Too many arguments for "open" of "Graph"
  2680. # type error: Argument 1 to "open" of "Graph" has incompatible type "ReadOnlyGraphAggregate"; expected "str" [arg-type]
  2681. # type error: Argument 2 to "open" of "Graph" has incompatible type "str"; expected "bool" [arg-type]
  2682. graph.open(self, configuration, create) # type: ignore[call-arg, arg-type]
  2683. # type error: Signature of "close" incompatible with supertype "Graph"
  2684. def close(self) -> None: # type: ignore[override]
  2685. for graph in self.graphs:
  2686. graph.close()
  2687. def add(self, triple: _TripleOrOptionalQuadType) -> NoReturn:
  2688. raise ModificationException()
  2689. def addN(self, quads: Iterable[_QuadType]) -> NoReturn: # noqa: N802
  2690. raise ModificationException()
  2691. # type error: Argument 1 of "remove" is incompatible with supertype "Graph"; supertype defines the argument type as "Tuple[Optional[Node], Optional[Node], Optional[Node]]"
  2692. def remove(self, triple: _TripleOrOptionalQuadType) -> NoReturn: # type: ignore[override]
  2693. raise ModificationException()
  2694. # type error: Signature of "triples" incompatible with supertype "ConjunctiveGraph"
  2695. @overload # type: ignore[override]
  2696. def triples(
  2697. self,
  2698. triple: _TriplePatternType,
  2699. ) -> Generator[_TripleType, None, None]: ...
  2700. @overload
  2701. def triples(
  2702. self,
  2703. triple: _TriplePathPatternType,
  2704. ) -> Generator[_TriplePathType, None, None]: ...
  2705. @overload
  2706. def triples(
  2707. self,
  2708. triple: _TripleSelectorType,
  2709. ) -> Generator[_TripleOrTriplePathType, None, None]: ...
  2710. def triples(
  2711. self,
  2712. triple: _TripleSelectorType,
  2713. ) -> Generator[_TripleOrTriplePathType, None, None]:
  2714. s, p, o = triple
  2715. for graph in self.graphs:
  2716. if isinstance(p, Path):
  2717. for s, o in p.eval(self, s, o):
  2718. yield s, p, o
  2719. else:
  2720. for s1, p1, o1 in graph.triples((s, p, o)):
  2721. yield s1, p1, o1
  2722. def __contains__(self, triple_or_quad: _TripleOrQuadSelectorType) -> bool:
  2723. context = None
  2724. if len(triple_or_quad) == 4:
  2725. # type error: Tuple index out of range
  2726. context = triple_or_quad[3] # type: ignore [misc, unused-ignore]
  2727. for graph in self.graphs:
  2728. if context is None or graph.identifier == context.identifier:
  2729. if triple_or_quad[:3] in graph:
  2730. return True
  2731. return False
  2732. # type error: Signature of "quads" incompatible with supertype "ConjunctiveGraph"
  2733. def quads( # type: ignore[override]
  2734. self, triple_or_quad: _TripleOrQuadSelectorType
  2735. ) -> Generator[
  2736. Tuple[_SubjectType, Union[Path, _PredicateType], _ObjectType, _ContextType],
  2737. None,
  2738. None,
  2739. ]:
  2740. """Iterate over all the quads in the entire aggregate graph"""
  2741. c = None
  2742. if len(triple_or_quad) == 4:
  2743. # type error: Need more than 3 values to unpack (4 expected)
  2744. s, p, o, c = triple_or_quad # type: ignore[misc, unused-ignore]
  2745. else:
  2746. # type error: Too many values to unpack (3 expected, 4 provided)
  2747. s, p, o = triple_or_quad # type: ignore[misc, unused-ignore]
  2748. if c is not None:
  2749. for graph in [g for g in self.graphs if g == c]:
  2750. for s1, p1, o1 in graph.triples((s, p, o)):
  2751. yield s1, p1, o1, graph
  2752. else:
  2753. for graph in self.graphs:
  2754. for s1, p1, o1 in graph.triples((s, p, o)):
  2755. yield s1, p1, o1, graph
  2756. def __len__(self) -> int:
  2757. return sum(len(g) for g in self.graphs)
  2758. def __hash__(self) -> NoReturn:
  2759. raise UnSupportedAggregateOperation()
  2760. def __cmp__(self, other) -> int:
  2761. if other is None:
  2762. return -1
  2763. elif isinstance(other, Graph):
  2764. return -1
  2765. elif isinstance(other, ReadOnlyGraphAggregate):
  2766. return (self.graphs > other.graphs) - (self.graphs < other.graphs)
  2767. else:
  2768. return -1
  2769. def __iadd__(self: _GraphT, other: Iterable[_TripleType]) -> NoReturn:
  2770. raise ModificationException()
  2771. def __isub__(self: _GraphT, other: Iterable[_TripleType]) -> NoReturn:
  2772. raise ModificationException()
  2773. # Conv. methods
  2774. def triples_choices(
  2775. self,
  2776. triple: _TripleChoiceType,
  2777. context: Optional[_ContextType] = None,
  2778. ) -> Generator[_TripleType, None, None]:
  2779. subject, predicate, object_ = triple
  2780. for graph in self.graphs:
  2781. # type error: Argument 1 to "triples_choices" of "Graph" has incompatible type "Tuple[Union[List[Node], Node], Union[Node, List[Node]], Union[Node, List[Node]]]"; expected "Union[Tuple[List[Node], Node, Node], Tuple[Node, List[Node], Node], Tuple[Node, Node, List[Node]]]"
  2782. # type error note: unpacking discards type info
  2783. choices = graph.triples_choices((subject, predicate, object_)) # type: ignore[arg-type]
  2784. for s, p, o in choices:
  2785. yield s, p, o
  2786. def qname(self, uri: str) -> str:
  2787. if hasattr(self, "namespace_manager") and self.namespace_manager:
  2788. return self.namespace_manager.qname(uri)
  2789. raise UnSupportedAggregateOperation()
  2790. def compute_qname(self, uri: str, generate: bool = True) -> Tuple[str, URIRef, str]:
  2791. if hasattr(self, "namespace_manager") and self.namespace_manager:
  2792. return self.namespace_manager.compute_qname(uri, generate)
  2793. raise UnSupportedAggregateOperation()
  2794. # type error: Signature of "bind" incompatible with supertype "Graph"
  2795. def bind( # type: ignore[override]
  2796. self, prefix: Optional[str], namespace: Any, override: bool = True # noqa: F811
  2797. ) -> NoReturn:
  2798. raise UnSupportedAggregateOperation()
  2799. def namespaces(self) -> Generator[Tuple[str, URIRef], None, None]:
  2800. if hasattr(self, "namespace_manager"):
  2801. for prefix, namespace in self.namespace_manager.namespaces():
  2802. yield prefix, namespace
  2803. else:
  2804. for graph in self.graphs:
  2805. for prefix, namespace in graph.namespaces():
  2806. yield prefix, namespace
  2807. def absolutize(self, uri: str, defrag: int = 1) -> NoReturn:
  2808. raise UnSupportedAggregateOperation()
  2809. # type error: Signature of "parse" incompatible with supertype "ConjunctiveGraph"
  2810. def parse( # type: ignore[override]
  2811. self,
  2812. source: Optional[
  2813. Union[IO[bytes], TextIO, InputSource, str, bytes, pathlib.PurePath]
  2814. ],
  2815. publicID: Optional[str] = None, # noqa: N803
  2816. format: Optional[str] = None,
  2817. **args: Any,
  2818. ) -> NoReturn:
  2819. raise ModificationException()
  2820. def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> NoReturn:
  2821. raise UnSupportedAggregateOperation()
  2822. def __reduce__(self) -> NoReturn:
  2823. raise UnSupportedAggregateOperation()
  2824. @overload
  2825. def _assertnode(*terms: Node) -> te.Literal[True]: ...
  2826. @overload
  2827. def _assertnode(*terms: Any) -> bool: ...
  2828. def _assertnode(*terms: Any) -> bool:
  2829. for t in terms:
  2830. assert isinstance(t, Node), "Term %s must be an rdflib term" % (t,)
  2831. return True
  2832. class BatchAddGraph:
  2833. """Wrapper around graph that turns batches of calls to Graph's add
  2834. (and optionally, addN) into calls to batched calls to addN`.
  2835. Args:
  2836. graph: The graph to wrap
  2837. batch_size: The maximum number of triples to buffer before passing to
  2838. Graph's addN
  2839. batch_addn: If True, then even calls to `addN` will be batched according to
  2840. batch_size
  2841. Attributes:
  2842. graph: The wrapped graph
  2843. count: The number of triples buffered since initialization or the last call to reset
  2844. batch: The current buffer of triples
  2845. """
  2846. def __init__(self, graph: Graph, batch_size: int = 1000, batch_addn: bool = False):
  2847. if not batch_size or batch_size < 2:
  2848. raise ValueError("batch_size must be a positive number")
  2849. self.graph = graph
  2850. self.__graph_tuple = (graph,)
  2851. self.__batch_size = batch_size
  2852. self.__batch_addn = batch_addn
  2853. self.reset()
  2854. def reset(self) -> BatchAddGraph:
  2855. """
  2856. Manually clear the buffered triples and reset the count to zero
  2857. """
  2858. self.batch: List[_QuadType] = []
  2859. self.count = 0
  2860. return self
  2861. def add(
  2862. self,
  2863. triple_or_quad: Union[
  2864. _TripleType,
  2865. _QuadType,
  2866. ],
  2867. ) -> BatchAddGraph:
  2868. """Add a triple to the buffer.
  2869. Args:
  2870. triple_or_quad: The triple or quad to add
  2871. Returns:
  2872. The BatchAddGraph instance
  2873. """
  2874. if len(self.batch) >= self.__batch_size:
  2875. self.graph.addN(self.batch)
  2876. self.batch = []
  2877. self.count += 1
  2878. if len(triple_or_quad) == 3:
  2879. # type error: Argument 1 to "append" of "list" has incompatible type "Tuple[Node, ...]"; expected "Tuple[Node, Node, Node, Graph]"
  2880. self.batch.append(triple_or_quad + self.__graph_tuple) # type: ignore[arg-type, unused-ignore]
  2881. else:
  2882. # type error: Argument 1 to "append" of "list" has incompatible type "Union[Tuple[Node, Node, Node], Tuple[Node, Node, Node, Graph]]"; expected "Tuple[Node, Node, Node, Graph]"
  2883. self.batch.append(triple_or_quad) # type: ignore[arg-type, unused-ignore]
  2884. return self
  2885. def addN(self, quads: Iterable[_QuadType]) -> BatchAddGraph: # noqa: N802
  2886. if self.__batch_addn:
  2887. for q in quads:
  2888. self.add(q)
  2889. else:
  2890. self.graph.addN(quads)
  2891. return self
  2892. def __enter__(self) -> BatchAddGraph:
  2893. self.reset()
  2894. return self
  2895. def __exit__(self, *exc) -> None:
  2896. if exc[0] is None:
  2897. self.graph.addN(self.batch)