{ "cells": [ { "cell_type": "markdown", "id": "7fb27b941602401d91542211134fc71a", "metadata": {}, "source": [ "# Demo: Retraction of RDFS Inference\n", "\n", "This notebook extends [demo-rdfs-inference.ipynb](demo-rdfs-inference.ipynb) from one-way materialization to update-aware materialization.\n", "The goal is small but important: after an asserted fact is removed, consequences that depended on that fact should disappear as well, while unrelated schema consequences remain available.\n", "\n", "That behavior is essential for agent-facing memory and tool state.\n", "A Research Agent may revise a belief, retract a claim, or replace an extracted statement; the reasoning layer should not keep stale inferred facts alive merely because they were true in an earlier graph state.\n", "\n", "## Where does this notebook fit?\n", "\n", "| Topic | Notebook |\n", "|-------|----------|\n", "| RDFS inference + derivation inspection | [demo-rdfs-inference.ipynb](demo-rdfs-inference.ipynb) |\n", "| OWL 2 RL inference + derivation inspection | [demo-owl2-rl-inference.ipynb](demo-owl2-rl-inference.ipynb) |\n", "| RDFS inference retraction after graph updates | **this notebook** |\n", "| Contradiction diagnostics + explanation | [demo-contradiction-rules.ipynb](demo-contradiction-rules.ipynb) |\n", "\n", "---\n", "\n", "## 1. Inference-Backed RDFLib Graph\n", "\n", "We use an in-memory RDFLib store wrapped by `RETEStore` so that ordinary graph updates trigger RDFS materialization.\n", "Fresh instances can be easily obtained from the `RDFS_RETE_MATERIALIZED` reasoner specification." ] }, { "cell_type": "code", "execution_count": null, "id": "acae54e37e7d407bbb7b55eff062a284", "metadata": {}, "outputs": [], "source": [ "from rdflib import Dataset, Graph, URIRef\n", "from rdflib_reasoning.engine import RDFS_RETE_MATERIALIZED\n", "\n", "dataset: Dataset = RDFS_RETE_MATERIALIZED.dataset()\n", "graph: Graph = dataset.default_graph" ] }, { "cell_type": "markdown", "id": "e7c5e24a", "metadata": {}, "source": [ "## 2. Triggering Inference\n", "\n", "We create some basic terms and construct a minimal ontology of 3 statements.\n", "Due to our forward-chaining inference engine, the production RDFS profile materializes a visible closure beyond those asserted triples.\n", "In this run, the graph grows from 0 triples to 9 triples: the 3 asserted triples, the expected user-facing type entailments for `alice`, and schema-level consequences that follow from the subclass chain.\n", "\n", "The important user experience is the same as in the first RDFS demo: using a graph like this should look like working with a normal RDFLib graph.\n", "The reasoner is present, but ordinary RDFLib calls such as `graph.add(...)`, `len(graph)`, and Turtle serialization remain the surface area." ] }, { "cell_type": "code", "execution_count": 2, "id": "8dd0d8092fe74a7c96281538738b07e2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "triples before assertions: 0\n", "triples after assertions: 9\n", "--------------------------------------------------------------------------------\n", "@prefix ex: .\n", "@prefix rdfs: .\n", "\n", "ex:Animal a rdfs:Class .\n", "\n", "ex:Mammal a rdfs:Class ;\n", " rdfs:subClassOf ex:Animal .\n", "\n", "ex:Person a rdfs:Class ;\n", " rdfs:subClassOf ex:Animal,\n", " ex:Mammal .\n", "\n", "ex:alice a ex:Animal,\n", " ex:Mammal,\n", " ex:Person .\n", "\n", "\n" ] } ], "source": [ "from rdflib import RDF, RDFS\n", "\n", "graph.bind(\"ex\", \"urn:example:\")\n", "\n", "alice = URIRef(\"urn:example:alice\")\n", "person = URIRef(\"urn:example:Person\")\n", "mammal = URIRef(\"urn:example:Mammal\")\n", "animal = URIRef(\"urn:example:Animal\")\n", "\n", "print(\"triples before assertions:\", len(graph))\n", "\n", "graph.add((alice, RDF.type, person))\n", "graph.add((person, RDFS.subClassOf, mammal))\n", "graph.add((mammal, RDFS.subClassOf, animal))\n", "\n", "print(\"triples after assertions:\", len(graph))\n", "print(\"-\" * 80)\n", "print(graph.serialize(format=\"turtle\"))" ] }, { "cell_type": "markdown", "id": "11bd14e3", "metadata": {}, "source": [ "## 3. Triggering Retraction\n", "\n", "Now we remove only the asserted triple `(alice, rdf:type, Person)`.\n", "That assertion was the source of the inferred facts that `alice` is also a `Mammal` and an `Animal` through `rdfs9`, so those dependent facts should be retracted from the materialized graph.\n", "\n", "The class hierarchy itself should remain: `Person subClassOf Mammal`, `Mammal subClassOf Animal`, and the derived `Person subClassOf Animal` are still supported by the remaining schema assertions.\n", "After retraction, the graph therefore contains 6 triples rather than returning all the way to an empty graph." ] }, { "cell_type": "code", "execution_count": 3, "id": "a653f0a0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "triples after retraction: 6\n", "--------------------------------------------------------------------------------\n", "@prefix ex: .\n", "@prefix rdfs: .\n", "\n", "ex:Animal a rdfs:Class .\n", "\n", "ex:Mammal a rdfs:Class ;\n", " rdfs:subClassOf ex:Animal .\n", "\n", "ex:Person a rdfs:Class ;\n", " rdfs:subClassOf ex:Animal,\n", " ex:Mammal .\n", "\n", "\n" ] } ], "source": [ "graph.remove((alice, RDF.type, person))\n", "\n", "print(\"triples after retraction:\", len(graph))\n", "print(\"-\" * 80)\n", "print(graph.serialize(format=\"turtle\"))" ] }, { "cell_type": "markdown", "id": "af72d06c", "metadata": {}, "source": [ "## Notes\n", "\n", "- Retraction is dependency-aware: the asserted type for `alice` is removed, and the inferred `alice` types that depended on it are removed with it.\n", "- Schema consequences with independent support remain materialized after the update.\n", "- This notebook complements [demo-contradiction-rules.ipynb](demo-contradiction-rules.ipynb): contradictions show how the engine surfaces invalid states, while retraction shows how the engine recovers when a supporting claim is withdrawn.\n", "- Agentic systems tie-in: mutable symbolic memory is a practical requirement for agent systems that need auditable, revisable beliefs rather than append-only text traces." ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.3" } }, "nbformat": 4, "nbformat_minor": 5 }