Args: ds_meta: DatasetMeta containing the dataset subject: Subject URI or literal predicate: Predicate URI obj: Object URI or literal source: Source of this fact (default: ‘agent’) reason: Optional reason for adding
Args: ds_meta: DatasetMeta containing the dataset subject: Subject URI or None (wildcard) predicate: Predicate URI or None (wildcard) obj: Object URI/literal or None (wildcard) source: Source of this retraction reason: Optional reason for removing
Promote triples from scratch graph to mem with provenance.
Args: ds_meta: DatasetMeta containing the dataset task_id: Task identifier for work graph source: Source label for provenance reason: Optional reason for promotion
Useful for debugging/replay. Note: The snapshot preserves the original dataset name in graph URIs, so if you want to use the original name, extract it from the graph URIs.
Args: path: Path to snapshot file ns: Namespace dict where Dataset will be stored name: Variable name for the Dataset handle
Filter result rows by column value or regex pattern.
Args: result: ResultTable or list of dicts column: Column name to filter on pattern: Optional regex pattern to match value: Optional exact value to match limit: Maximum matching rows to return (default: 100)
Returns: List of matching rows
res_head
def res_head( result, n:int=10)->list:
Get first N rows of a result set.
Args: result: ResultTable, list of dicts, or list of tuples n: Number of rows to return
Mount ontology into dataset as read-only onto/ graph.
If index_shacl=True and SHACL content detected, also builds SHACLIndex and stores in ns[’{ont_name}_shacl’].
If index_queries=True and sh:SPARQLExecutable detected, also builds QueryIndex and stores in ns[’{ont_name}_queries’].
Args: ds_meta: DatasetMeta containing the dataset ns: Namespace dict (for compatibility with setup_ontology_context) path: Path to ontology file ont_name: Name for the ontology index_shacl: Whether to detect and index SHACL shapes (default: True) index_queries: Whether to detect and index query templates (default: True)
DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
ds_meta.prov.add((event_uri, RLM_PROV.timestamp, Literal(datetime.utcnow().isoformat() + 'Z', datatype=XSD.dateTime)))
✓ Dataset creation works
Created dataset 'test_ds' with session_id=cb576fb5
# Test mem_add with provenancetest_ns = {}setup_dataset_context(test_ns)ds_meta = test_ns['ds_meta']result = mem_add(ds_meta, 'http://ex.org/alice', 'http://ex.org/knows', 'http://ex.org/bob', source='test', reason='Testing')assertlen(ds_meta.mem) ==1assertlen(ds_meta.prov) >0print("✓ mem_add works")print(result)
✓ mem_add works
Added triple to mem: (http://ex.org/alice, http://ex.org/knows, http://ex.org/bob)
DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
ds_meta.prov.add((event_uri, RLM_PROV.timestamp, Literal(datetime.utcnow().isoformat() + 'Z', datatype=XSD.dateTime)))
# Test mem_querytest_ns = {}setup_dataset_context(test_ns)ds_meta = test_ns['ds_meta']mem_add(ds_meta, 'http://ex.org/alice', 'http://ex.org/age', '30')mem_add(ds_meta, 'http://ex.org/bob', 'http://ex.org/age', '25')results = mem_query(ds_meta, 'SELECT ?s ?age WHERE { ?s <http://ex.org/age> ?age }')assertlen(results) ==2assertall('s'in r and'age'in r for r in results)print("✓ mem_query works")print(results)
DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
ds_meta.prov.add((event_uri, RLM_PROV.timestamp, Literal(datetime.utcnow().isoformat() + 'Z', datatype=XSD.dateTime)))
DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
ds_meta.prov.add((event_uri, RLM_PROV.timestamp, Literal(datetime.utcnow().isoformat() + 'Z', datatype=XSD.dateTime)))
<ipython-input-1-e3f77e94d507>:36: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
ds_meta.prov.add((event_uri, RLM_PROV.timestamp, Literal(datetime.utcnow().isoformat() + 'Z', datatype=XSD.dateTime)))
DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
ds_meta.prov.add((event_uri, RLM_PROV.timestamp, Literal(datetime.utcnow().isoformat() + 'Z', datatype=XSD.dateTime)))
# Test index invalidationtest_ns = {}setup_dataset_context(test_ns)ds_meta = test_ns['ds_meta']# Access cached propertyinitial_version = ds_meta._version_ = ds_meta.graph_stats# Mutatemem_add(ds_meta, 'http://ex.org/alice', 'http://ex.org/age', '30')# Check version incrementedassert ds_meta._version > initial_versionprint("✓ Index invalidation works")
✓ Index invalidation works
DeprecationWarning: Dataset.contexts is deprecated, use Dataset.graphs instead.
for ctx in self.dataset.contexts():
<ipython-input-1-3a8dafc08295>:33: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
ds_meta.prov.add((event_uri, RLM_PROV.timestamp, Literal(datetime.utcnow().isoformat() + 'Z', datatype=XSD.dateTime)))
# Test work graph lifecycletest_ns = {}setup_dataset_context(test_ns)ds_meta = test_ns['ds_meta']# Create work graphuri, graph = work_create(ds_meta, task_id='test_task')assert'work/test_task'in uriassertlen(ds_meta.work_graphs) ==1# Add some triples to work graphgraph.add((URIRef('http://ex.org/alice'), URIRef('http://ex.org/temp'), Literal('value')))assertlen(graph) ==1# Promote to memresult = work_to_mem(ds_meta, 'test_task', reason='Test promotion')assertlen(ds_meta.mem) ==1assert'Promoted 1 triples'in result# Cleanupresult = work_cleanup(ds_meta, task_id='test_task')assert'Removed 1 work'in resultassertlen(ds_meta.work_graphs) ==0print("✓ Work graph lifecycle works")
✓ Work graph lifecycle works
DeprecationWarning: Dataset.contexts is deprecated, use Dataset.graphs instead.
return [str(ctx.identifier) for ctx in self.dataset.contexts()
<ipython-input-1-661dda18a793>:32: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
ds_meta.prov.add((event_uri, RLM_PROV.timestamp, Literal(datetime.utcnow().isoformat() + 'Z', datatype=XSD.dateTime)))
# Test snapshotimport tempfileimport ostest_ns = {}setup_dataset_context(test_ns)ds_meta = test_ns['ds_meta']# Add some datamem_add(ds_meta, 'http://ex.org/alice', 'http://ex.org/age', '30')# Take snapshotwith tempfile.NamedTemporaryFile(mode='w', suffix='.trig', delete=False) as f: snapshot_path = f.nameresult = snapshot_dataset(ds_meta, path=snapshot_path)assert os.path.exists(snapshot_path)assert'Snapshot saved'in result# Load snapshot (let it auto-detect the name 'ds' from graph URIs)test_ns2 = {}result = load_snapshot(snapshot_path, test_ns2, name='restored')assert'restored'in test_ns2assert'restored_meta'in test_ns2# Should auto-detect original name 'ds' and use it for URIsassertlen(test_ns2['restored_meta'].mem) ==1# Also test loading with same nametest_ns3 = {}result = load_snapshot(snapshot_path, test_ns3, name='ds')assert'ds'in test_ns3assert'ds_meta'in test_ns3assertlen(test_ns3['ds_meta'].mem) ==1# Cleanupos.unlink(snapshot_path)print("✓ Snapshot roundtrip works")
✓ Snapshot roundtrip works
# Test bounded view functionstest_ns = {}setup_dataset_context(test_ns)ds_meta = test_ns['ds_meta']# Add some datamem_add(ds_meta, 'http://ex.org/alice', 'http://ex.org/age', '30')work_create(ds_meta, 'task1')work_create(ds_meta, 'task2')# Test dataset_statsstats = dataset_stats(ds_meta)assert'mem: 1 triples'in statsassert'work graphs: 2'in stats# Test list_graphsgraphs = list_graphs(ds_meta)assertlen(graphs) >=4# mem, prov, work/task1, work/task2# Test list_graphs with patternwork_graphs = list_graphs(ds_meta, pattern='work/')assertlen(work_graphs) ==2# Test graph_samplemem_uri =f'urn:rlm:{ds_meta.name}:mem'sample = graph_sample(ds_meta, mem_uri)assertlen(sample) ==1print("✓ Bounded view functions work")
✓ Bounded view functions work
DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
ds_meta.prov.add((event_uri, RLM_PROV.timestamp, Literal(datetime.utcnow().isoformat() + 'Z', datatype=XSD.dateTime)))
<ipython-input-1-338468221890>:54: DeprecationWarning: Dataset.contexts is deprecated, use Dataset.graphs instead.
return [str(ctx.identifier) for ctx in self.dataset.contexts()
<ipython-input-1-338468221890>:44: DeprecationWarning: Dataset.contexts is deprecated, use Dataset.graphs instead.
for ctx in self.dataset.contexts():
<ipython-input-1-0d2d1cee68f5>:13: DeprecationWarning: Dataset.contexts is deprecated, use Dataset.graphs instead.
for ctx in ds_meta.dataset.contexts():
Tests
Usage Examples
# Basic usage in RLM contextns = {}setup_dataset_context(ns)# RLM can now use: mem_add, mem_query, mem_describe, etc.ns['mem_add']('http://ex.org/alice', 'http://ex.org/knows', 'http://ex.org/bob')results = ns['mem_query']('SELECT ?s ?p ?o WHERE { ?s ?p ?o }')print(results)
# Integration with ontologyfrom rlm.ontology import setup_ontology_contextns = {}setup_dataset_context(ns)setup_ontology_context('ontology/prov.ttl', ns, name='prov')# Mount ontology into datasetns['mount_ontology']('ontology/prov.ttl', 'prov')# Now ontology is in dataset as onto/prov graphgraphs = ns['list_graphs']()print(graphs)