0
# RDF Terms
1
2
Core RDF data types representing the fundamental building blocks of RDF graphs. These classes provide the foundation for creating, manipulating, and serializing RDF data.
3
4
## Capabilities
5
6
### URIRef - URI References
7
8
Represents URI/IRI references in RDF graphs. URIRefs identify resources and properties in RDF data.
9
10
```python { .api }
11
# Type imports for annotations
12
from typing import Optional, Any, Union
13
from rdflib.term import URIRef
14
15
class URIRef:
16
def __new__(cls, value: str, base: Optional[str] = None) -> 'URIRef':
17
"""
18
Create a URI reference.
19
20
Parameters:
21
- value: The URI string
22
"""
23
24
def toPython(self):
25
"""
26
Return the URI as a Python string.
27
28
Returns:
29
str: The URI string
30
"""
31
32
def n3(self, namespace_manager=None) -> str:
33
"""
34
Return N3 representation.
35
36
Parameters:
37
- namespace_manager: Optional namespace manager for prefixed notation
38
39
Returns:
40
str: N3 formatted string
41
"""
42
43
def defrag(self) -> 'URIRef':
44
"""
45
Remove fragment identifier from URI.
46
47
Returns:
48
URIRef: URI without fragment
49
"""
50
51
def __str__(self) -> str:
52
"""Return string representation."""
53
54
def __hash__(self) -> int:
55
"""Return hash value."""
56
57
def de_skolemize(self) -> 'BNode':
58
"""
59
Convert skolemized URI back to blank node.
60
61
Returns:
62
BNode: Corresponding blank node
63
"""
64
```
65
66
### BNode - Blank Nodes
67
68
Represents blank nodes (anonymous resources) in RDF graphs. Blank nodes are local identifiers within a graph.
69
70
```python { .api }
71
class BNode:
72
def __new__(cls, value: Optional[str] = None) -> 'BNode':
73
"""
74
Create a blank node.
75
76
Parameters:
77
- value: Optional identifier string; auto-generated if None
78
"""
79
80
def toPython(self):
81
"""
82
Return the blank node identifier.
83
84
Returns:
85
str: The blank node identifier
86
"""
87
88
def n3(self, namespace_manager=None) -> str:
89
"""
90
Return N3 representation.
91
92
Returns:
93
str: N3 formatted blank node
94
"""
95
96
def skolemize(self, authority: Optional[str] = None, basepath: Optional[str] = None) -> URIRef:
97
"""
98
Convert blank node to skolem IRI.
99
100
Parameters:
101
- authority: Authority for the skolem IRI
102
103
Returns:
104
URIRef: Skolemized URI
105
"""
106
107
```
108
109
### Literal - Literal Values
110
111
Represents literal values in RDF graphs with optional datatype and language tags.
112
113
```python { .api }
114
class Literal:
115
def __new__(cls, lexical_or_value: Any, lang: Optional[str] = None, datatype: Optional[str] = None, normalize: Optional[bool] = None) -> 'Literal':
116
"""
117
Create a literal.
118
119
Parameters:
120
- lexical_or_value: The literal value or lexical form
121
- lang: Language tag (e.g., 'en', 'fr')
122
- datatype: Datatype URI (e.g., XSD.int, XSD.string)
123
- normalize: Whether to normalize the literal
124
"""
125
126
def toPython(self):
127
"""
128
Convert literal to appropriate Python type.
129
130
Returns:
131
Python object corresponding to the datatype
132
"""
133
134
def n3(self, namespace_manager=None) -> str:
135
"""
136
Return N3 representation.
137
138
Returns:
139
str: N3 formatted literal
140
"""
141
142
@property
143
def value(self):
144
"""
145
Get the Python value of the literal.
146
147
Returns:
148
Python object
149
"""
150
151
@property
152
def datatype(self) -> Optional[URIRef]:
153
"""
154
Get the datatype URI.
155
156
Returns:
157
URIRef: Datatype URI or None
158
"""
159
160
@property
161
def language(self) -> Optional[str]:
162
"""
163
Get the language tag.
164
165
Returns:
166
str: Language tag or None
167
"""
168
169
def __str__(self) -> str:
170
"""Return string representation."""
171
172
def __add__(self, other):
173
"""Add literals or convert to Python types for addition."""
174
175
def __sub__(self, other):
176
"""Subtract literals or convert to Python types for subtraction."""
177
178
def __lt__(self, other) -> bool:
179
"""Less than comparison."""
180
181
def __le__(self, other) -> bool:
182
"""Less than or equal comparison."""
183
184
def __gt__(self, other) -> bool:
185
"""Greater than comparison."""
186
187
def __ge__(self, other) -> bool:
188
"""Greater than or equal comparison."""
189
```
190
191
### Variable - SPARQL Variables
192
193
Represents variables in SPARQL queries and N3 formulas.
194
195
```python { .api }
196
class Variable:
197
def __init__(self, value: str):
198
"""
199
Create a variable.
200
201
Parameters:
202
- value: Variable name (without ? or $ prefix)
203
"""
204
205
def n3(self, namespace_manager=None) -> str:
206
"""
207
Return N3 representation.
208
209
Returns:
210
str: N3 formatted variable (with ? prefix)
211
"""
212
213
def toPython(self):
214
"""
215
Return the variable name.
216
217
Returns:
218
str: Variable name
219
"""
220
```
221
222
### Variable - SPARQL Variables
223
224
Represents variables in SPARQL queries and expressions. Variables are identified by names starting with '?' or '$'.
225
226
```python { .api }
227
class Variable:
228
def __new__(cls, name: str) -> 'Variable':
229
"""
230
Create a SPARQL variable.
231
232
Parameters:
233
- name: Variable name (without ? or $ prefix)
234
"""
235
236
def toPython(self) -> str:
237
"""
238
Return the variable name.
239
240
Returns:
241
str: Variable name
242
"""
243
244
def n3(self, namespace_manager=None) -> str:
245
"""
246
Return N3 representation with ? prefix.
247
248
Returns:
249
str: N3 formatted variable
250
"""
251
252
@property
253
def name(self) -> str:
254
"""
255
Get the variable name.
256
257
Returns:
258
str: Variable name without prefix
259
"""
260
```
261
262
### Node - Base Class
263
264
Abstract base class for all RDF nodes.
265
266
```python { .api }
267
class Node:
268
"""Abstract base class for RDF nodes."""
269
270
def n3(self, namespace_manager=None) -> str:
271
"""Return N3 representation."""
272
273
def toPython(self):
274
"""Convert to appropriate Python representation."""
275
```
276
277
### IdentifiedNode - Base for Identifiable Nodes
278
279
Abstract base class for nodes that can be identified (URIRef and BNode).
280
281
```python { .api }
282
class IdentifiedNode(Node):
283
"""Abstract base class for identifiable nodes (URIRef, BNode)."""
284
```
285
286
## Utility Functions
287
288
### bind
289
290
Utility for namespace binding in term contexts.
291
292
```python { .api }
293
def bind(namespace: str, term: str) -> URIRef:
294
"""
295
Bind a namespace to create a URIRef.
296
297
Parameters:
298
- namespace: Namespace URI
299
- term: Local term name
300
301
Returns:
302
URIRef: Combined URI reference
303
"""
304
```
305
306
### _is_valid_uri
307
308
URI validation utility.
309
310
```python { .api }
311
def _is_valid_uri(uri: str) -> bool:
312
"""
313
Check if a string is a valid URI.
314
315
Parameters:
316
- uri: URI string to validate
317
318
Returns:
319
bool: True if valid URI
320
"""
321
```
322
323
## Usage Examples
324
325
### Creating Terms
326
327
```python
328
from rdflib import URIRef, BNode, Literal
329
from rdflib.namespace import XSD
330
331
# Create a URI reference
332
person_uri = URIRef("http://example.org/person/1")
333
334
# Create a blank node
335
blank = BNode()
336
337
# Create literals with different datatypes
338
name = Literal("John Doe") # string literal
339
age = Literal(30, datatype=XSD.integer)
340
height = Literal(5.9, datatype=XSD.decimal)
341
is_active = Literal(True, datatype=XSD.boolean)
342
343
# Create a literal with language tag
344
description = Literal("Une personne", lang="fr")
345
```
346
347
### Working with Literals
348
349
```python
350
from rdflib import Literal
351
from rdflib.namespace import XSD
352
353
# Create typed literals
354
num1 = Literal(10, datatype=XSD.integer)
355
num2 = Literal(20, datatype=XSD.integer)
356
357
# Convert to Python values
358
python_val = num1.toPython() # Returns 10 as int
359
360
# Perform operations
361
result = num1 + num2 # Returns Literal(30)
362
363
# Access properties
364
print(num1.datatype) # XSD.integer
365
print(description.language) # 'fr'
366
```
367
368
### Blank Node Operations
369
370
```python
371
from rdflib import BNode
372
373
# Create blank nodes
374
blank1 = BNode()
375
blank2 = BNode("custom_id")
376
377
# Skolemize blank node
378
skolem_uri = blank1.skolemize("http://example.org")
379
print(skolem_uri) # URIRef with skolem authority
380
381
# De-skolemize back to blank node
382
back_to_blank = skolem_uri.de_skolemize()
383
```