### Install and Install Pre-commit Hook Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Install pre-commit and set it up as a git pre-commit hook to automatically format code. This ensures code consistency across the project. ```bash pip install pre-commit && pre-commit install ``` -------------------------------- ### Run Oxrdflib Tests Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Navigate to the tests directory and execute the unit tests using Python's unittest module. Ensure the package is installed first. ```bash cd tests && python -m unittest ``` -------------------------------- ### Install Oxrdflib for Development Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Install the package in editable mode to register stores in the rdflib plugin registry. This is a prerequisite for running tests. ```bash pip install -e . ``` -------------------------------- ### Install oxrdflib Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Install the oxrdflib package using pip. This makes the Oxigraph store automatically available as an rdflib store plugin. ```bash pip install oxrdflib ``` -------------------------------- ### Using Oxigraph Store with RDFLib Graph Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Demonstrates how to initialize an RDFLib Graph using the Oxigraph store as a drop-in replacement for the default store. ```APIDOC ## Using Oxigraph Store with RDFLib Graph ### Description To create a rdflib graph using the Oxigraph store use `rdflib.Graph(store="Oxigraph")` instead of the usual `rdflib.Graph()`. ### Method Python API ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import rdflib graph = rdflib.Graph(store="Oxigraph") ``` ### Response #### Success Response (200) An RDFLib Graph object initialized with the Oxigraph store. #### Response Example N/A ``` -------------------------------- ### Using Oxigraph Store with RDFLib Dataset Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Shows how to initialize an RDFLib Dataset using the Oxigraph store. ```APIDOC ## Using Oxigraph Store with RDFLib Dataset ### Description Similarly, to get a dataset, use `rdflib.Dataset(store="Oxigraph")` instead of the usual `rdflib.Dataset()`. ### Method Python API ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import rdflib dataset = rdflib.Dataset(store="Oxigraph") ``` ### Response #### Success Response (200) An RDFLib Dataset object initialized with the Oxigraph store. #### Response Example N/A ``` -------------------------------- ### Persisting Oxigraph Store to Disk Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Explains how to persist the Oxigraph store data on disk using the `open` method. ```APIDOC ## Persisting Oxigraph Store to Disk ### Description If you want to get the store data persisted on disk, use the `open` method on the `Graph` or `Dataset` object with the directory where data should be persisted. For example: ### Method Python API ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import rdflib graph = rdflib.Graph(store="Oxigraph", identifier="http://example.com") # without identifier, some blank node will be used graph.open("test_dir") ``` ### Response #### Success Response (200) The store is opened and data is persisted in the specified directory. #### Response Example N/A ### Notes The store is closed with the `close()` method or automatically when Python garbage collector collects the store object. If the `open` method is not called Oxigraph will automatically use a ramdisk on Linux and a temporary file in the other operating systems. ``` -------------------------------- ### Injecting pyoxigraph Store Object Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Shows how to directly inject a pyoxigraph `Store` object into an Oxrdflib store. ```APIDOC ## Injecting pyoxigraph Store Object ### Description It is also possible to directly inject a [pyoxigraph `Store` object](https://pyoxigraph.readthedocs.io/en/stable/store.html#pyoxigraph.Store) directly into an Oxrdflib store. This might be handy to e.g. open the database as read-only. ### Method Python API ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import rdflib import oxrdflib import pyoxigraph # Example with a standard store graph = rdflib.Graph(store=oxrdflib.OxigraphStore(store=pyoxigraph.Store("test_dir"))) # Example opening the database as read-only graph_read_only = rdflib.Graph(store=oxrdflib.OxigraphStore(store=pyoxigraph.Store.read_only("test_dir"))) ``` ### Response #### Success Response (200) An RDFLib Graph object initialized with a provided pyoxigraph `Store` object. #### Response Example N/A ``` -------------------------------- ### Manage Namespace Prefixes with OxigraphStore Source: https://context7.com/oxigraph/oxrdflib/llms.txt Use `bind` to associate prefixes with namespaces and `namespace`/`prefix` to look them up. These bindings are in-memory and not persisted. Prefixes are automatically available in SPARQL queries. ```python from rdflib import Graph, URIRef, Namespace EX = Namespace("http://example.com/") FOAF = Namespace("http://xmlns.com/foaf/0.1/") g = Graph(store="Oxigraph") # Bind prefixes g.bind("ex", EX) g.bind("foaf", FOAF) # Lookup prefix → namespace print(g.store.namespace("ex")) # http://example.com/ print(g.store.namespace("foaf")) # http://xmlns.com/foaf/0.1/ # Lookup namespace → prefix print(g.store.prefix(URIRef("http://example.com/"))) # ex # List all bindings for prefix, ns in g.store.namespaces(): print(f"{prefix}: {ns}") # ex: http://example.com/ # foaf: http://xmlns.com/foaf/0.1/ # Prefixes are injected automatically into SPARQL queries g.add((EX.alice, FOAF.name, __import__("rdflib").Literal("Alice"))) result = g.query("SELECT ?name WHERE { ex:alice foaf:name ?name }") print(list(result)[0].name) # Alice ``` -------------------------------- ### Manage Persistent Store Lifecycle with OxigraphStore Source: https://context7.com/oxigraph/oxrdflib/llms.txt Control the lifecycle of a persistent Oxigraph store using `open`, `close`, and `destroy`. `open` initializes or loads a store from a directory, `close` saves changes, and `destroy` removes the store's data. ```python from pathlib import Path from rdflib import ConjunctiveGraph, Namespace, RDF EX = Namespace("http://example.com/") # Open a persistent store g = ConjunctiveGraph(store="Oxigraph") g.open("data_dir") # ValueError if called after any RDF operation g.add((EX.foo, RDF.type, EX.Entity)) g.close() assert Path("data_dir").exists() # True: data persisted on disk # Reopen and verify g2 = ConjunctiveGraph(store="Oxigraph") g2.open("data_dir") print(len(list(g2))) # 1 g2.close() # Destroy the store (removes directory recursively) g2.destroy("data_dir") assert not Path("data_dir").exists() # True: store deleted ``` -------------------------------- ### Create an rdflib Dataset with Oxigraph Store Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Instantiate an rdflib Dataset using the 'Oxigraph' store identifier. This replaces the default rdflib Dataset. ```python rdflib.Dataset(store="Oxigraph") ``` -------------------------------- ### Create an rdflib Graph with Oxigraph Store Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Instantiate an rdflib Graph using the 'Oxigraph' store identifier. This replaces the default rdflib store. ```python rdflib.Graph(store="Oxigraph") ``` -------------------------------- ### Using Oxigraph Parsers and Serializers Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Explains how to use Oxigraph's parsers and serializers by prefixing format identifiers with `ox-`. ```APIDOC ## Using Oxigraph Parsers and Serializers ### Description To use Oxigraph parser, prefix the format identifiers with `ox-`. For example, to load data using the Oxigraph NTriples parser: `graph.parse(data, format="ox-nt")` and to serialize to Turtle: `graph.serialize(format="ox-ttl")`. ### Method Python API ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python # Example for parsing NTriples data graph.parse(data, format="ox-nt") # Example for serializing to Turtle graph.serialize(format="ox-ttl") ``` ### Response #### Success Response (200) Data is parsed into the graph or serialized from the graph using Oxigraph's optimized parsers and serializers. #### Response Example N/A ### Supported Formats - `ox-ntriples` (`ox-nt`) - `ox-nquads` (`ox-nq`) - `ox-turtle` (`ox-ttl`) - `ox-trig` - `ox-n3` - `ox-xml` - `ox-json-ld` (`ox-streaming-json-ld` for streaming JSON-LD, note that only JSON-LD 1.0 is supported) ``` -------------------------------- ### Execute SPARQL ASK Queries with OxigraphStore Source: https://context7.com/oxigraph/oxrdflib/llms.txt Execute SPARQL ASK queries which return a boolean result indicating whether the query pattern matches any triples in the graph. Supports `initBindings` for testing specific conditions. ```python from rdflib import ConjunctiveGraph, Graph, Namespace, RDF EX = Namespace("http://example.com/") g = ConjunctiveGraph(store="Oxigraph") g.add((EX.alice, RDF.type, EX.Person)) g.bind("ex", EX) # Basic ASK print(bool(g.query("ASK { ?s ?p ?o }"))) # True print(bool(g.query("ASK { ex:nobody a ex:Person }"))) # False # ASK with initBindings to test for a specific binding print(bool(g.query("ASK { ?s a ex:Person }", initBindings={"s": EX.alice}))) # True print(bool(g.query("ASK { ?s a ex:Person }", initBindings={"s": EX.nobody}))) # False # ASK in a specific named graph g1 = Graph(store=g.store, identifier=EX.g1) g1.add((EX.foo, RDF.type, EX.Entity)) print(bool(g1.query("ASK { ?s ?p ?o }"))) # True ``` -------------------------------- ### Execute SPARQL CONSTRUCT Queries with OxigraphStore Source: https://context7.com/oxigraph/oxrdflib/llms.txt Execute SPARQL CONSTRUCT queries to build new RDF graphs based on query results. The `result.graph` attribute holds the constructed graph, which can then be serialized. ```python from rdflib import Graph, Namespace, RDF, Literal EX = Namespace("http://example.com/") g = Graph(store="Oxigraph") g.bind("ex", EX) g.add((EX.alice, RDF.type, EX.Person)) g.add((EX.alice, EX.name, Literal("Alice"))) g.add((EX.bob, RDF.type, EX.Person)) # CONSTRUCT to create a subgraph with only Person triples result = g.query("CONSTRUCT WHERE { ?s a ex:Person }") constructed = result.graph print(len(constructed)) # 2 # Serialize constructed graph to N-Triples nt = result.serialize(format="ntriples").strip().decode() print(nt) ``` -------------------------------- ### Inject pyoxigraph Store Directly into OxigraphStore Source: https://context7.com/oxigraph/oxrdflib/llms.txt Inject a `pyoxigraph.Store` object directly into `OxigraphStore` for advanced configuration, such as read-only access. This allows for fine-grained control over the underlying storage engine. ```python import pyoxigraph import rdflib from rdflib import Graph, Namespace, RDF import oxrdflib EX = Namespace("http://example.com/") # Write some data to disk first store = pyoxigraph.Store("shared_dir") g = Graph(store=oxrdflib.OxigraphStore(store=store), identifier="http://example.com/") g.add((EX.carol, RDF.type, EX.Person)) del g, store # flush and close # Reopen as read-only ro_store = pyoxigraph.Store.read_only("shared_dir") g_ro = Graph(store=oxrdflib.OxigraphStore(store=ro_store), identifier="http://example.com/") print(len(g_ro)) # 1 print((EX.carol, RDF.type, EX.Person) in g_ro) # True import shutil shutil.rmtree("shared_dir") ``` -------------------------------- ### In-Memory OxigraphStore Graph Source: https://context7.com/oxigraph/oxrdflib/llms.txt Create an rdflib Graph backed by an in-memory Oxigraph store. Supports adding triples, checking for their existence, and iterating over them. ```python import rdflib from rdflib import Graph, Namespace, RDF, Literal, XSD EX = Namespace("http://example.com/") # Create an in-memory Oxigraph-backed graph g = Graph(store="Oxigraph") g.bind("ex", EX) # Add triples g.add((EX.alice, RDF.type, EX.Person)) g.add((EX.alice, EX.name, Literal("Alice"))) g.add((EX.alice, EX.age, Literal(30, datatype=XSD.integer))) print(len(g)) # 3 print((EX.alice, RDF.type, EX.Person) in g) # True # Iterate over triples for s, p, o in g: print(s, p, o) # # "Alice" # "30"^^ ``` -------------------------------- ### Execute SPARQL SELECT Queries with OxigraphStore Source: https://context7.com/oxigraph/oxrdflib/llms.txt Execute SPARQL SELECT queries using the native Oxigraph engine. Supports optional namespace and binding injection for dynamic queries. Results can be serialized to JSON. ```python import json from rdflib import Graph, Namespace, RDF, Literal, XSD EX = Namespace("http://example.com/") g = Graph(store="Oxigraph") g.bind("ex", EX) g.add((EX.alice, RDF.type, EX.Person)) g.add((EX.alice, EX.name, Literal("Alice"))) g.add((EX.alice, EX.age, Literal(30, datatype=XSD.integer))) g.add((EX.bob, RDF.type, EX.Person)) g.add((EX.bob, EX.name, Literal("Bob"))) g.add((EX.bob, EX.age, Literal(25, datatype=XSD.integer))) # Basic SELECT results = g.query("SELECT ?name ?age WHERE { ?s ex:name ?name ; ex:age ?age } ORDER BY ?name") for row in results: print(row.name, row.age) # Alice 30 # Bob 25 # With initNs and initBindings result = g.query( "SELECT ?name WHERE { ?s a ex:Person ; ex:name ?name . FILTER(?s = ?target) }", initNs={"ex": EX}, initBindings={"target": EX.alice}, ) print(list(result)[0].name) # Alice # Serialize to JSON json_result = json.loads(g.query("SELECT ?s WHERE { ?s a ex:Person }").serialize(format="json")) print(json_result["head"]["vars"]) # ['s'] ``` -------------------------------- ### OxigraphStore.bind / namespace — Prefix/Namespace Management Source: https://context7.com/oxigraph/oxrdflib/llms.txt Manage namespace prefix bindings stored in-memory (not persisted to disk). ```APIDOC ## OxigraphStore.bind / namespace — Prefix/Namespace Management ### Description Manage namespace prefix bindings stored in-memory (not persisted to disk). ### Methods * `bind(prefix, namespace)`: Binds a namespace prefix. * `namespace(prefix)`: Looks up a prefix and returns its corresponding namespace URI. * `prefix(namespace_uri)`: Looks up a namespace URI and returns its corresponding prefix. * `namespaces()`: Returns an iterator of (prefix, namespace_uri) pairs. ### Parameters * `prefix` (string) - The prefix to bind. * `namespace` (Namespace or URIRef) - The namespace URI. * `namespace_uri` (URIRef) - The namespace URI to look up. ### Request Example ```python from rdflib import Graph, URIRef, Namespace EX = Namespace("http://example.com/") FOAF = Namespace("http://xmlns.com/foaf/0.1/") g = Graph(store="Oxigraph") # Bind prefixes g.bind("ex", EX) g.bind("foaf", FOAF) # Lookup prefix → namespace print(g.store.namespace("ex")) print(g.store.namespace("foaf")) # Lookup namespace → prefix print(g.store.prefix(URIRef("http://example.com/"))) # List all bindings for prefix, ns in g.store.namespaces(): print(f"{prefix}: {ns}") # Prefixes are injected automatically into SPARQL queries g.add((EX.alice, FOAF.name, __import__("rdflib").Literal("Alice"))) result = g.query("SELECT ?name WHERE { ex:alice foaf:name ?name }") print(list(result)[0].name) ``` ### Response * `namespace(prefix)` returns a `URIRef`. * `prefix(namespace_uri)` returns a string. * `namespaces()` returns an iterator of `(string, URIRef)` tuples. ``` -------------------------------- ### Persist Oxigraph Store to Disk Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Open an rdflib Graph with the Oxigraph store and specify a directory for persistence. Data will be saved to 'test_dir'. ```python graph = rdflib.Graph(store="Oxigraph", identifier="http://example.com") # without identifier, some blank node will be used graph.open("test_dir") ``` -------------------------------- ### Inject pyoxigraph Store into Oxrdflib Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Create an rdflib Graph by directly injecting a pyoxigraph Store object. This allows for advanced configurations like read-only access. ```python graph = rdflib.Graph(store=oxrdflib.OxigraphStore(store=pyoxigraph.Store("test_dir"))) ``` ```python graph = rdflib.Graph(store=oxrdflib.OxigraphStore(store=pyoxigraph.Store.read_only("test_dir"))) ``` -------------------------------- ### Add Triples and Quads to OxigraphStore Source: https://context7.com/oxigraph/oxrdflib/llms.txt Use `add()` for individual triples and `addN()` for efficient bulk insertion of quads. `addN()` takes a list of tuples, where each tuple represents a quad (subject, predicate, object, graph). ```python from rdflib import Graph, Namespace, RDF, Literal, XSD, BNode EX = Namespace("http://example.com/") g = Graph(store="Oxigraph") # Add a single triple g.add((EX.item1, RDF.type, EX.Product)) g.add((EX.item1, EX.price, Literal("9.99", datatype=XSD.decimal))) g.add((EX.item1, EX.tag, BNode("b1"))) # Add multiple triples at once using addN (quads: subject, predicate, object, graph) g.addN([ (EX.item2, RDF.type, EX.Product, g), (EX.item2, EX.price, Literal("4.50", datatype=XSD.decimal), g), (EX.item3, RDF.type, EX.Product, g), ]) print(len(g)) # 6 # Remove a triple g.remove((EX.item1, EX.tag, BNode("b1"))) print(len(g)) # 5 ``` -------------------------------- ### Persistent Disk-Based OxigraphStore Graph Source: https://context7.com/oxigraph/oxrdflib/llms.txt Open a graph with disk persistence using the `open()` method. Data is stored in a RocksDB store on disk and can be reopened and queried. ```python import rdflib from rdflib import Graph, Namespace, RDF, Literal EX = Namespace("http://example.com/") # Create a persistent store at "my_graph_dir" g = Graph(store="Oxigraph", identifier="http://example.com/mygraph") g.open("my_graph_dir") # opens (or creates) the RocksDB store on disk g.add((EX.bob, RDF.type, EX.Person)) g.add((EX.bob, EX.name, Literal("Bob"))) g.close() # Reopen and query the persisted data g2 = Graph(store="Oxigraph", identifier="http://example.com/mygraph") g2.open("my_graph_dir") print(len(g2)) # 2 print((EX.bob, RDF.type, EX.Person) in g2) # True g2.close() # Clean up import shutil shutil.rmtree("my_graph_dir") ``` -------------------------------- ### OxigraphStore.open / close / destroy — Store Lifecycle Source: https://context7.com/oxigraph/oxrdflib/llms.txt Manage the lifecycle of a persistent store: open, close, and destroy. ```APIDOC ## OxigraphStore.open / close / destroy — Store Lifecycle ### Description Manage the lifecycle of a persistent store: open, close, and destroy. ### Methods * `open(path)`: Opens or creates a persistent store at the specified path. * `close()`: Closes the current store connection. * `destroy(path)`: Destroys the persistent store at the specified path. ### Parameters * `path` (string or Path) - The directory path for the persistent store. ### Request Example ```python from pathlib import Path from rdflib import ConjunctiveGraph, Namespace, RDF EX = Namespace("http://example.com/") # Open a persistent store g = ConjunctiveGraph(store="Oxigraph") g.open("data_dir") # ValueError if called after any RDF operation g.add((EX.foo, RDF.type, EX.Entity)) g.close() assert Path("data_dir").exists() # True: data persisted on disk # Reopen and verify g2 = ConjunctiveGraph(store="Oxigraph") g2.open("data_dir") print(len(list(g2))) # 1 g2.close() # Destroy the store (removes directory recursively) g2.destroy("data_dir") assert not Path("data_dir").exists() # True: store deleted ``` ### Response * `open()`: Opens the store. Raises `ValueError` if called after any RDF operation. * `close()`: Closes the store connection. * `destroy()`: Deletes the store directory. ``` -------------------------------- ### Load RDF Data with Oxigraph Native Parsers Source: https://context7.com/oxigraph/oxrdflib/llms.txt Utilize Oxigraph's high-performance native parsers (e.g., `ox-ttl`, `ox-nt`, `ox-json-ld`, `ox-nquads`) for parsing RDF data. Specify the parser using the `format` argument in the `parse` method. Bulk loading is available for large files. ```python from io import StringIO from rdflib import Graph, Dataset, URIRef s = URIRef("http://example.com/s") p = URIRef("http://example.com/vocab#p") o = URIRef("http://example.com/o") # Parse Turtle g = Graph(store="Oxigraph", identifier="http://example.com/") g.parse(StringIO("@prefix v: . v:p ."), format="ox-ttl", publicID="http://example.com/") print(list(g)) # [(s, p, o)] # Parse N-Triples g2 = Graph(store="Oxigraph", identifier="http://example.com/") g2.parse(StringIO(" .\n"), format="ox-nt", publicID="http://example.com/") print(list(g2)) # [(s, p, o)] # Parse JSON-LD g3 = Graph(store="Oxigraph", identifier="http://example.com/") g3.parse(StringIO('{"@context":{"v":"http://example.com/vocab#"},"@id":"s","v:p":{"@id":"o"}}'), format="ox-json-ld", publicID="http://example.com/") print(list(g3)) # [(s, p, o)] # Bulk (non-transactional) load for large files g4 = Graph(store="Oxigraph", identifier="http://example.com/") g4.parse("large_file.nt", format="ox-nt", transactional=False) # Parse N-Quads into a Dataset from rdflib import Dataset from rdflib.graph import DATASET_DEFAULT_GRAPH_ID ds = Dataset(store="Oxigraph") ds.parse(StringIO( " .\n" " .\n" ), format="ox-nquads", publicID="http://example.com/") print(set(ds)) ``` -------------------------------- ### Parsers — Loading RDF with Oxigraph Parsers Source: https://context7.com/oxigraph/oxrdflib/llms.txt Parse RDF data using Oxigraph's native parsers (prefixed with `ox-`) for improved performance. ```APIDOC ## Parsers — Loading RDF with Oxigraph Parsers ### Description Parse RDF data using Oxigraph's native parsers (prefixed with `ox-`) for improved performance. ### Methods * `parse(source, format, publicID, transactional)`: Parses RDF data from a source. ### Parameters * `source` (string or file-like object) - The RDF data source. * `format` (string) - The RDF format (e.g., `ox-ttl`, `ox-nt`, `ox-json-ld`, `ox-nquads`). * `publicID` (string, optional) - The public identifier for the data. * `transactional` (boolean, optional) - Whether to load data transactionally (default is True). ### Request Example ```python from io import StringIO from rdflib import Graph, Dataset, URIRef s = URIRef("http://example.com/s") p = URIRef("http://example.com/vocab#p") o = URIRef("http://example.com/o") # Parse Turtle g = Graph(store="Oxigraph", identifier="http://example.com/") g.parse(StringIO("@prefix v: . v:p ."), format="ox-ttl", publicID="http://example.com/") # Parse N-Triples g2 = Graph(store="Oxigraph", identifier="http://example.com/") g2.parse(StringIO(" .\n"), format="ox-nt", publicID="http://example.com/") # Parse JSON-LD g3 = Graph(store="Oxigraph", identifier="http://example.com/") g3.parse(StringIO('{"@context":{"v":"http://example.com/vocab#"},"@id":"s","v:p":{"@id":"o"}}'), format="ox-json-ld", publicID="http://example.com/") # Bulk (non-transactional) load for large files g4 = Graph(store="Oxigraph", identifier="http://example.com/") g4.parse("large_file.nt", format="ox-nt", transactional=False) # Parse N-Quads into a Dataset ds = Dataset(store="Oxigraph") ds.parse(StringIO( " .\n" " .\n" ), format="ox-nquads", publicID="http://example.com/") ``` ### Response * `parse()`: Populates the graph or dataset with the parsed RDF data. ``` -------------------------------- ### OxigraphStore Dataset (Named Graph Management) Source: https://context7.com/oxigraph/oxrdflib/llms.txt Use `Dataset` for explicit quad-based named graph management, including adding and removing graphs. This provides a structured way to handle multiple named graphs. ```python from rdflib import Dataset, Namespace, RDF, Literal, URIRef from rdflib.graph import DATASET_DEFAULT_GRAPH_ID EX = Namespace("http://example.com/") ds = Dataset(store="Oxigraph") ``` -------------------------------- ### OxigraphStore.add / addN — Triple and Quad Insertion Source: https://context7.com/oxigraph/oxrdflib/llms.txt Add individual triples with `add()` or add multiple quads efficiently in bulk with `addN()`. This method is used for inserting RDF data into the store. ```APIDOC ## OxigraphStore.add / addN — Triple and Quad Insertion Add individual triples with `add()` or add multiple quads efficiently in bulk with `addN()`. ```python from rdflib import Graph, Namespace, RDF, Literal, XSD, BNode EX = Namespace("http://example.com/") g = Graph(store="Oxigraph") # Add a single triple g.add((EX.item1, RDF.type, EX.Product)) g.add((EX.item1, EX.price, Literal("9.99", datatype=XSD.decimal))) g.add((EX.item1, EX.tag, BNode("b1"))) # Add multiple triples at once using addN (quads: subject, predicate, object, graph) g.addN([ (EX.item2, RDF.type, EX.Product, g), (EX.item2, EX.price, Literal("4.50", datatype=XSD.decimal), g), (EX.item3, RDF.type, EX.Product, g), ]) print(len(g)) # 6 # Remove a triple g.remove((EX.item1, EX.tag, BNode("b1"))) print(len(g)) # 5 ``` ``` -------------------------------- ### Pattern Matching with OxigraphStore.triples Source: https://context7.com/oxigraph/oxrdflib/llms.txt Query triples using wildcard patterns by providing `None` for subject, predicate, or object. This method is useful for finding all triples matching a partial pattern. ```python from rdflib import Graph, Namespace, RDF, Literal EX = Namespace("http://example.com/") g = Graph(store="Oxigraph") g.add((EX.alice, RDF.type, EX.Person)) g.add((EX.bob, RDF.type, EX.Person)) g.add((EX.alice, EX.knows, EX.bob)) g.add((EX.alice, EX.name, Literal("Alice"))) # Find all subjects of type Person persons = list(g.triples((None, RDF.type, EX.Person))) print([str(s) for s, _, _ in persons]) # ['http://example.com/alice', 'http://example.com/bob'] # Find all predicates and objects for alice alice_props = list(g.triples((EX.alice, None, None))) print(len(alice_props)) # 3 # Find all triples with a specific object knows_alice = list(g.triples((None, EX.knows, EX.bob))) print(len(knows_alice)) # 1 ``` -------------------------------- ### OxigraphStore.query — SPARQL CONSTRUCT Source: https://context7.com/oxigraph/oxrdflib/llms.txt Execute SPARQL CONSTRUCT queries to build new RDF graphs based on specified patterns. This is useful for transforming or extracting subgraphs. ```APIDOC ## OxigraphStore.query — SPARQL CONSTRUCT Execute SPARQL CONSTRUCT queries to build new RDF graphs. ```python from rdflib import Graph, Namespace, RDF, Literal EX = Namespace("http://example.com/") g = Graph(store="Oxigraph") g.bind("ex", EX) g.add((EX.alice, RDF.type, EX.Person)) g.add((EX.alice, EX.name, Literal("Alice"))) g.add((EX.bob, RDF.type, EX.Person)) # CONSTRUCT to create a subgraph with only Person triples result = g.query("CONSTRUCT WHERE { ?s a ex:Person }") constructed = result.graph print(len(constructed)) # 2 # Serialize constructed graph to N-Triples nt = result.serialize(format="ntriples").strip().decode() print(nt) ``` ``` -------------------------------- ### Serialize Data with Oxigraph Turtle Serializer Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Serialize RDF data to Turtle format using the Oxigraph serializer by specifying 'ox-ttl' as the format. ```python graph.serialize(format="ox-ttl") ``` -------------------------------- ### Write RDF Graph to File Source: https://context7.com/oxigraph/oxrdflib/llms.txt Writes the serialized RDF graph directly to a file in Turtle format. Ensure the file is opened in binary write mode (`"wb"`). ```python # Write directly to a file with open("output.ttl", "wb") as f: g.serialize(f, format="ox-ttl") ``` -------------------------------- ### OxigraphStore.triples — Pattern Matching Source: https://context7.com/oxigraph/oxrdflib/llms.txt Query triples using wildcard patterns with `None` as a wildcard for subject, predicate, or object. This allows for flexible retrieval of RDF statements. ```APIDOC ## OxigraphStore.triples — Pattern Matching Query triples using wildcard patterns with `None` as a wildcard for subject, predicate, or object. ```python from rdflib import Graph, Namespace, RDF, Literal EX = Namespace("http://example.com/") g = Graph(store="Oxigraph") g.add((EX.alice, RDF.type, EX.Person)) g.add((EX.bob, RDF.type, EX.Person)) g.add((EX.alice, EX.knows, EX.bob)) g.add((EX.alice, EX.name, Literal("Alice"))) # Find all subjects of type Person persons = list(g.triples((None, RDF.type, EX.Person))) print([str(s) for s, _, _ in persons]) # ['http://example.com/alice', 'http://example.com/bob'] # Find all predicates and objects for alice alice_props = list(g.triples((EX.alice, None, None))) print(len(alice_props)) # 3 # Find all triples with a specific object knows_alice = list(g.triples((None, EX.knows, EX.bob))) print(len(knows_alice)) # 1 ``` ``` -------------------------------- ### OxigraphStore.query — SPARQL ASK Source: https://context7.com/oxigraph/oxrdflib/llms.txt Execute SPARQL ASK queries returning a boolean result. This is useful for checking the existence of patterns within the RDF graph. ```APIDOC ## OxigraphStore.query — SPARQL ASK Execute SPARQL ASK queries returning a boolean result. ```python from rdflib import ConjunctiveGraph, Graph, Namespace, RDF EX = Namespace("http://example.com/") g = ConjunctiveGraph(store="Oxigraph") g.add((EX.alice, RDF.type, EX.Person)) g.bind("ex", EX) # Basic ASK print(bool(g.query("ASK { ?s ?p ?o }"))) # True print(bool(g.query("ASK { ex:nobody a ex:Person }"))) # False # ASK with initBindings to test for a specific binding print(bool(g.query("ASK { ?s a ex:Person }", initBindings={"s": EX.alice}))) print(bool(g.query("ASK { ?s a ex:Person }", initBindings={"s": EX.nobody}))) # ASK in a specific named graph g1 = Graph(store=g.store, identifier=EX.g1) g1.add((EX.foo, RDF.type, EX.Entity)) print(bool(g1.query("ASK { ?s ?p ?o }"))) # True ``` ``` -------------------------------- ### Parse Data with Oxigraph NTriples Parser Source: https://github.com/oxigraph/oxrdflib/blob/main/README.md Load RDF data using the Oxigraph NTriples parser by prefixing the format identifier with 'ox-'. ```python graph.parse(data, format="ox-nt") ``` -------------------------------- ### OxigraphStore.query — SPARQL SELECT Source: https://context7.com/oxigraph/oxrdflib/llms.txt Execute SPARQL SELECT queries via Oxigraph's native engine with optional namespace and binding injection. This method is used for retrieving specific data patterns. ```APIDOC ## OxigraphStore.query — SPARQL SELECT Execute SPARQL SELECT queries via Oxigraph's native engine with optional namespace and binding injection. ```python import json from rdflib import Graph, Namespace, RDF, Literal, XSD EX = Namespace("http://example.com/") g = Graph(store="Oxigraph") g.bind("ex", EX) g.add((EX.alice, RDF.type, EX.Person)) g.add((EX.alice, EX.name, Literal("Alice"))) g.add((EX.alice, EX.age, Literal(30, datatype=XSD.integer))) g.add((EX.bob, RDF.type, EX.Person)) g.add((EX.bob, EX.name, Literal("Bob"))) g.add((EX.bob, EX.age, Literal(25, datatype=XSD.integer))) # Basic SELECT results = g.query("SELECT ?name ?age WHERE { ?s ex:name ?name ; ex:age ?age } ORDER BY ?name") for row in results: print(row.name, row.age) # Alice 30 # Bob 25 # With initNs and initBindings result = g.query( "SELECT ?name WHERE { ?s a ex:Person ; ex:name ?name . FILTER(?s = ?target) }", initNs={"ex": EX}, initBindings={"target": EX.alice}, ) print(list(result)[0].name) # Alice # Serialize to JSON json_result = json.loads(g.query("SELECT ?s WHERE { ?s a ex:Person }").serialize(format="json")) print(json_result["head"]["vars"]) ``` ``` -------------------------------- ### OxigraphStore ConjunctiveGraph (Multiple Named Graphs) Source: https://context7.com/oxigraph/oxrdflib/llms.txt Use `ConjunctiveGraph` to work with multiple named graphs within a single Oxigraph store. Allows adding triples to specific named graphs and querying across all graphs. ```python from rdflib import ConjunctiveGraph, Graph, Namespace, RDF, Literal EX = Namespace("http://example.com/") cg = ConjunctiveGraph(store="Oxigraph") # Add triples to a named graph g1 = Graph(store=cg.store, identifier=EX.graph1) g1.add((EX.alice, RDF.type, EX.Person)) g1.add((EX.alice, EX.name, Literal("Alice"))) # Add triples to another named graph g2 = Graph(store=cg.store, identifier=EX.graph2) g2.add((EX.bob, RDF.type, EX.Person)) # Query all triples across all graphs print(len(list(cg))) # 3 # List all named graphs for ctx in cg.contexts(): print(ctx.identifier) # # ``` -------------------------------- ### Serialize RDF Dataset to TriG Source: https://context7.com/oxigraph/oxrdflib/llms.txt Serializes an RDF dataset, which can contain multiple named graphs, to the TriG format using the `ox-trig` identifier. This format is suitable for multi-graph RDF data. ```python # Serialize a Dataset to TriG (multi-graph format) ds = Dataset(store="Oxigraph") ds.add((EX.bob, RDF.type, EX.Person, EX.g1)) ds.add((EX.alice, RDF.type, EX.Person, EX.g2)) print(ds.serialize(format="ox-trig")) ``` -------------------------------- ### OxigraphStore.update — SPARQL UPDATE Source: https://context7.com/oxigraph/oxrdflib/llms.txt Execute SPARQL 1.1 UPDATE operations (INSERT, DELETE, etc.) natively via Oxigraph. ```APIDOC ## OxigraphStore.update — SPARQL UPDATE ### Description Execute SPARQL 1.1 UPDATE operations (INSERT, DELETE, etc.) natively via Oxigraph. ### Method `update(sparql_query)` ### Parameters * `sparql_query` (string) - The SPARQL 1.1 UPDATE query to execute. ### Request Example ```python from rdflib import Graph, Dataset, Namespace, RDF EX = Namespace("http://example.com/") # SPARQL UPDATE on a Graph g = Graph(store="Oxigraph") g.add((EX.alice, RDF.type, EX.Person)) g.update("INSERT { ?s a } WHERE { ?s a }") # SPARQL UPDATE with DELETE/INSERT g.update(""" DELETE { ?s a } INSERT { ?s a } WHERE { ?s a } """) # UPDATE on a Dataset with named graphs ds = Dataset(store="Oxigraph") ds.add((EX.bob, RDF.type, EX.Person, EX.g)) ds.update("INSERT { ?s a } WHERE { GRAPH ?g { ?s a } }") ``` ### Response This method does not return a value, but modifies the store. ``` -------------------------------- ### Execute SPARQL 1.1 UPDATE with OxigraphStore Source: https://context7.com/oxigraph/oxrdflib/llms.txt Use the `update` method to perform SPARQL UPDATE operations (INSERT, DELETE) directly on an Oxigraph store. This method supports both Graph and Dataset objects. ```python from rdflib import Graph, Dataset, Namespace, RDF EX = Namespace("http://example.com/") # SPARQL UPDATE on a Graph g = Graph(store="Oxigraph") g.add((EX.alice, RDF.type, EX.Person)) g.update("INSERT { ?s a } WHERE { ?s a }") print((EX.alice, RDF.type, EX.Human) in g) # True # SPARQL UPDATE with DELETE/INSERT g.update(""" DELETE { ?s a } INSERT { ?s a } WHERE { ?s a } """) print((EX.alice, RDF.type, EX.Person) in g) # False print((EX.alice, RDF.type, EX.Individual) in g) # True # UPDATE on a Dataset with named graphs ds = Dataset(store="Oxigraph") ds.add((EX.bob, RDF.type, EX.Person, EX.g)) ds.update("INSERT { ?s a } WHERE { GRAPH ?g { ?s a } }") print((EX.bob, RDF.type, EX.Entity2, ds.identifier) in ds) # True ``` -------------------------------- ### Serialize RDF Graph to RDF/XML Source: https://context7.com/oxigraph/oxrdflib/llms.txt Serializes an RDF graph to the RDF/XML format using the `ox-xml` identifier. This is a common, though verbose, XML-based serialization format. ```python # Serialize to RDF/XML print(g.serialize(format="ox-xml")) ``` -------------------------------- ### Serialize RDF Graph to N-Triples Source: https://context7.com/oxigraph/oxrdflib/llms.txt Serializes an RDF graph to the N-Triples format using the `ox-nt` identifier. This format is a simple, line-based representation of RDF triples. ```python # Serialize to N-Triples print(g.serialize(format="ox-nt")) ``` -------------------------------- ### Serialize RDF Graph to JSON-LD Source: https://context7.com/oxigraph/oxrdflib/llms.txt Serializes an RDF graph to JSON-LD format using the `ox-json-ld` identifier. JSON-LD is a method of encoding Linked Data using JSON. ```python # Serialize to JSON-LD print(g.serialize(format="ox-json-ld")) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.