Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

docs(cookbook): move Semantic Layer Basics from Advanced to Introduction

taoche0e7cef467740
diff --git a/cookbook/advanced/09_Semantic_Layer_Construction.ipynb b/cookbook/advanced/09_Semantic_Layer_Construction.ipynb
deleted file mode 100644
index de1ecd78..00000000
--- a/cookbook/advanced/09_Semantic_Layer_Construction.ipynb
+++ /dev/null
@@ -1,222 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/semantica-agi/semantica/blob/main/cookbook/advanced/09_Semantic_Layer_Construction.ipynb)\n",
- "\n",
- "# Semantic Layer Construction\n",
- "\n",
- "## Overview\n",
- "\n",
- "Build an enterprise semantic layer: construct knowledge graph, generate ontology, create semantic layer, export RDF, and store in triplet store.\n",
- "\n",
- "\n",
- "**Documentation**: [API Reference](https://semantica.readthedocs.io/concepts/)\n",
- "\n",
- "## Installation\n",
- "\n",
- "Install Semantica from PyPI:\n",
- "\n",
- "```bash\n",
- "pip install semantica\n",
- "# Or with all optional dependencies:\n",
- "pip install semantica[all]\n",
- "```\n",
- "\n",
- "## Workflow: Build KG → Generate Ontology → Create Semantic Layer → Export RDF \n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "!pip install -qU semantica\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "from semantica.kg import GraphBuilder\n",
- "from semantica.ontology import OntologyGenerator\n",
- "from semantica.export import RDFExporter\n",
- "from semantica.triplet_store import TripletStore\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Step 1: Build Knowledge Graph\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "builder = GraphBuilder()\n",
- "\n",
- "entities = [\n",
- " {\"id\": \"e1\", \"type\": \"Person\", \"name\": \"Alice\", \"properties\": {\"age\": 30, \"role\": \"Engineer\"}},\n",
- " {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Bob\", \"properties\": {\"age\": 35, \"role\": \"Manager\"}},\n",
- " {\"id\": \"e3\", \"type\": \"Organization\", \"name\": \"Tech Corp\", \"properties\": {\"founded\": 2010}},\n",
- " {\"id\": \"e4\", \"type\": \"Project\", \"name\": \"Project Alpha\", \"properties\": {\"status\": \"active\"}},\n",
- "]\n",
- "\n",
- "relationships = [\n",
- " {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"reports_to\"},\n",
- " {\"source\": \"e1\", \"target\": \"e3\", \"type\": \"works_for\"},\n",
- " {\"source\": \"e2\", \"target\": \"e3\", \"type\": \"works_for\"},\n",
- " {\"source\": \"e1\", \"target\": \"e4\", \"type\": \"works_on\"},\n",
- "]\n",
- "\n",
- "knowledge_graph = builder.build(entities, relationships)\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Step 2: Generate Ontology\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "generator = OntologyGenerator()\n",
- "ontology = generator.generate_from_graph(knowledge_graph)\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Step 3: Create Semantic Layer\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "def create_mappings(kg, ontology):\n",
- " mappings = {\n",
- " \"entity_type_mappings\": {},\n",
- " \"relationship_type_mappings\": {},\n",
- " \"property_mappings\": {}\n",
- " }\n",
- " \n",
- " entity_types = set(e.get(\"type\") for e in entities)\n",
- " ontology_classes = ontology.get(\"classes\", [])\n",
- " \n",
- " for entity_type in entity_types:\n",
- " matching_class = next((cls for cls in ontology_classes if cls.get(\"name\") == entity_type), None)\n",
- " if matching_class:\n",
- " mappings[\"entity_type_mappings\"][entity_type] = matching_class.get(\"uri\", entity_type)\n",
- " \n",
- " relationship_types = set(r.get(\"type\") for r in relationships)\n",
- " ontology_properties = ontology.get(\"properties\", [])\n",
- " \n",
- " for rel_type in relationship_types:\n",
- " matching_prop = next((prop for prop in ontology_properties if prop.get(\"name\") == rel_type), None)\n",
- " if matching_prop:\n",
- " mappings[\"relationship_type_mappings\"][rel_type] = matching_prop.get(\"uri\", rel_type)\n",
- " \n",
- " return mappings\n",
- "\n",
- "mappings = create_mappings(knowledge_graph, ontology)\n",
- "\n",
- "semantic_layer = {\n",
- " \"graph\": knowledge_graph,\n",
- " \"ontology\": ontology,\n",
- " \"mappings\": mappings,\n",
- " \"metadata\": {\n",
- " \"version\": \"1.0\",\n",
- " \"created_at\": \"2024-01-01\",\n",
- " \"description\": \"Enterprise semantic layer\"\n",
- " }\n",
- "}\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Step 4: Export RDF\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "exporter = RDFExporter()\n",
- "# Export Knowledge Graph\n",
- "exporter.export(knowledge_graph, \"knowledge_graph.ttl\", format=\"turtle\")\n",
- "print(\"Exported knowledge graph to knowledge_graph.ttl\")\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Summary\n",
- "\n",
- "Enterprise semantic layer construction:\n",
- "- Knowledge Graph Built\n",
- "- Ontology Generated\n",
- "- Semantic Layer Created with Mappings\n",
- "- RDF Export Completed\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": []
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": []
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "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.11.9"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
-}
diff --git a/cookbook/advanced/13_Manual_Ontology_Snowflake_Mapping.ipynb b/cookbook/advanced/13_Manual_Ontology_Snowflake_Mapping.ipynb
index 0647f3b9..d37eb6c1 100644
--- a/cookbook/advanced/13_Manual_Ontology_Snowflake_Mapping.ipynb
+++ b/cookbook/advanced/13_Manual_Ontology_Snowflake_Mapping.ipynb
@@ -26,6 +26,9 @@
"\n",
"> *\"I want to design the ontology myself — not have AI infer it from my tables — and then map Snowflake data to it explicitly.\"*\n",
"\n",
+ "> [!NOTE]\n",
+ "> **New to semantic layers?** Start with the introductory lesson [Semantic Layer Basics](../introduction/26_Semantic_Layer_Basics.ipynb), which composes a knowledge graph, a generated ontology, and explicit mappings into a small queryable semantic layer. This notebook is the production-oriented continuation: governed, hand-designed ontologies and explicit source mappings from a warehouse.\n",
+ "\n",
"### What this notebook demonstrates\n",
"\n",
"| Step | What happens | Who controls it |\n",
@@ -432,4 +435,4 @@
]
}
]
-}
\ No newline at end of file
+}
diff --git a/cookbook/introduction/14_Ontology.ipynb b/cookbook/introduction/14_Ontology.ipynb
index 64bbee06..ae08787d 100644
--- a/cookbook/introduction/14_Ontology.ipynb
+++ b/cookbook/introduction/14_Ontology.ipynb
@@ -497,7 +497,8 @@
"**Next Steps**:\n",
"* Try customizing the `NamespaceManager` to use your organization's URL.\n",
"* Explore `OntologyEvaluator` for deeper quality metrics.\n",
- "* Feed the generated ontology into the **Knowledge Graph** module to start reasoning over your data!"
+ "* Feed the generated ontology into the **Knowledge Graph** module to start reasoning over your data!\n",
+ "* Put the graph, ontology, and explicit mappings together in [Semantic Layer Basics](./26_Semantic_Layer_Basics.ipynb)."
]
}
],
diff --git a/cookbook/introduction/26_Semantic_Layer_Basics.ipynb b/cookbook/introduction/26_Semantic_Layer_Basics.ipynb
new file mode 100644
index 00000000..13a5683f
--- /dev/null
+++ b/cookbook/introduction/26_Semantic_Layer_Basics.ipynb
@@ -0,0 +1,378 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/semantica-agi/semantica/blob/main/cookbook/introduction/26_Semantic_Layer_Basics.ipynb)\n",
+ "\n",
+ "# Semantic Layer Basics: Putting the Knowledge Graph, Ontology, and Mappings Together\n",
+ "\n",
+ "## Overview\n",
+ "\n",
+ "This lesson connects three things you have already met — a knowledge graph, an ontology, and RDF export — into one minimal *semantic layer*: a knowledge graph whose types, relationships, and properties are **explicitly mapped** to ontology terms, so the resulting RDF can be queried with SPARQL against a shared vocabulary.\n",
+ "\n",
+ "**Documentation**: [API Reference](https://semantica.readthedocs.io/concepts/)\n",
+ "\n",
+ "### 🎯 Learning Objectives\n",
+ "\n",
+ "- Build a small knowledge graph with `GraphBuilder`\n",
+ "- Generate a starter ontology from the graph with `OntologyGenerator`\n",
+ "- Write **explicit** entity-type, relationship-type, and property mappings to ontology terms\n",
+ "- Produce ontology-aligned RDF and store it with `TripletStore`\n",
+ "- Answer a business question with one small SPARQL query\n",
+ "\n",
+ "### 📚 Prerequisites\n",
+ "\n",
+ "- [07_Building_Knowledge_Graphs.ipynb](./07_Building_Knowledge_Graphs.ipynb) — graphs from entities and relationships\n",
+ "- [14_Ontology.ipynb](./14_Ontology.ipynb) — ontology generation\n",
+ "- [20_Triplet_Store.ipynb](./20_Triplet_Store.ipynb) — triplet store backends\n",
+ "\n",
+ "> [!NOTE]\n",
+ "> **Teaching mappings vs. governed mappings.** The mappings in this lesson are a demo: they live in a Python dict and are derived from a generated ontology. A production semantic layer uses governed identifiers, hand-designed ontologies, explicit source mappings, validation (SHACL), provenance, and versioning — that workflow is covered in [Advanced: Manual Ontology + Snowflake Mapping](../advanced/13_Manual_Ontology_Snowflake_Mapping.ipynb).\n",
+ "\n",
+ "## Installation\n",
+ "\n",
+ "The triplet-store step uses the embedded Oxigraph backend, so install with that extra:\n",
+ "\n",
+ "```bash\n",
+ "pip install \"semantica[tripletstore-oxigraph]\"\n",
+ "```\n",
+ "\n",
+ "---\n",
+ "\n",
+ "## Step 1: Build a Knowledge Graph\n",
+ "\n",
+ "Start from a small, explicit set of entities and relationships — two people, an organization, and a project.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {},
+ "source": [
+ "!pip install \"semantica[tripletstore-oxigraph]\"\n"
+ ],
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {},
+ "source": [
+ "from semantica.kg import GraphBuilder\n",
+ "\n",
+ "entities = [\n",
+ " {\"id\": \"e1\", \"type\": \"Person\", \"name\": \"Alice\", \"properties\": {\"age\": 30, \"role\": \"Engineer\"}},\n",
+ " {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Bob\", \"properties\": {\"age\": 35, \"role\": \"Manager\"}},\n",
+ " {\"id\": \"e3\", \"type\": \"Organization\", \"name\": \"Tech Corp\", \"properties\": {\"founded\": 2010}},\n",
+ " {\"id\": \"e4\", \"type\": \"Project\", \"name\": \"Project Alpha\", \"properties\": {\"status\": \"active\"}},\n",
+ "]\n",
+ "\n",
+ "relationships = [\n",
+ " {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"reports_to\", \"properties\": {}},\n",
+ " {\"source\": \"e1\", \"target\": \"e3\", \"type\": \"works_for\", \"properties\": {}},\n",
+ " {\"source\": \"e2\", \"target\": \"e3\", \"type\": \"works_for\", \"properties\": {}},\n",
+ " {\"source\": \"e1\", \"target\": \"e4\", \"type\": \"works_on\", \"properties\": {}},\n",
+ "]\n",
+ "\n",
+ "builder = GraphBuilder()\n",
+ "knowledge_graph = builder.build({\"entities\": entities, \"relationships\": relationships})\n",
+ "\n",
+ "id_to_name = {entity[\"id\"]: entity[\"name\"] for entity in entities}\n",
+ "\n",
+ "print(f\"Entities ({len(knowledge_graph['entities'])}):\")\n",
+ "for entity in knowledge_graph[\"entities\"]:\n",
+ " print(f\" {entity['id']}: {entity['name']} ({entity['type']}) {entity['properties']}\")\n",
+ "\n",
+ "print(f\"\\nRelationships ({len(knowledge_graph['relationships'])}):\")\n",
+ "for relationship in knowledge_graph[\"relationships\"]:\n",
+ " print(f\" {id_to_name[relationship['source']]} \"\n",
+ " f\"--{relationship['type']}--> {id_to_name[relationship['target']]}\")"
+ ],
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 2: Generate a Starter Ontology\n",
+ "\n",
+ "`OntologyGenerator` infers OWL classes and properties from the graph. Two settings matter here:\n",
+ "\n",
+ "- `base_uri` puts every generated term in *your* namespace\n",
+ "- `min_occurrences=1` includes classes that occur only once (the default of 2 would drop `Organization` and `Project` from this tiny demo graph)\n",
+ "\n",
+ "Note that the generator normalizes names: the relationship type `works_for` becomes the ontology property `worksFor`. That is exactly why the next step maps terms **explicitly** instead of matching names.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {},
+ "source": [
+ "from semantica.ontology import OntologyGenerator\n",
+ "\n",
+ "BASE_URI = \"https://example.org/company/\"\n",
+ "\n",
+ "generator = OntologyGenerator(base_uri=BASE_URI, min_occurrences=1)\n",
+ "ontology = generator.generate_from_graph(knowledge_graph)\n",
+ "\n",
+ "print(\"Classes:\")\n",
+ "for ontology_class in ontology[\"classes\"]:\n",
+ " print(f\" {ontology_class['name']:<14} {ontology_class['uri']}\")\n",
+ "\n",
+ "print(\"\\nObject properties:\")\n",
+ "for prop in ontology[\"properties\"]:\n",
+ " if prop[\"type\"] == \"object\":\n",
+ " print(f\" {prop['name']:<14} {prop['uri']} \"\n",
+ " f\"(domain={prop['domain']}, range={prop['range']})\")\n",
+ "\n",
+ "assert len(ontology[\"classes\"]) == 3"
+ ],
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 3: Map the Graph to Ontology Terms\n",
+ "\n",
+ "The heart of a semantic layer is the mapping contract: which source type, relationship, and property corresponds to which ontology term.\n",
+ "\n",
+ "- **Entity types** and **relationship types**: each generated class/property records the source name it was inferred from (`metadata[\"inferred_from\"]`), so the mapping is read off the ontology itself — no fragile name matching between `works_for` and `worksFor`.\n",
+ "- **Properties**: the entity's `name` maps to the ontology's generated `name` datatype property; the remaining property keys (`age`, `role`, `founded`, `status`) are mapped to terms minted explicitly in our namespace.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {},
+ "source": [
+ "entity_type_mappings = {\n",
+ " ontology_class[\"metadata\"][\"inferred_from\"]: ontology_class[\"uri\"]\n",
+ " for ontology_class in ontology[\"classes\"]\n",
+ "}\n",
+ "\n",
+ "relationship_type_mappings = {\n",
+ " prop[\"metadata\"][\"inferred_from\"]: prop[\"uri\"]\n",
+ " for prop in ontology[\"properties\"]\n",
+ " if prop[\"type\"] == \"object\"\n",
+ "}\n",
+ "\n",
+ "datatype_property_uris = {\n",
+ " prop[\"metadata\"][\"inferred_from\"]: prop[\"uri\"]\n",
+ " for prop in ontology[\"properties\"]\n",
+ " if prop[\"type\"] != \"object\"\n",
+ "}\n",
+ "\n",
+ "property_mappings = {\n",
+ " \"name\": datatype_property_uris[\"name\"],\n",
+ " \"age\": BASE_URI + \"age\",\n",
+ " \"role\": BASE_URI + \"role\",\n",
+ " \"founded\": BASE_URI + \"founded\",\n",
+ " \"status\": BASE_URI + \"status\",\n",
+ "}\n",
+ "\n",
+ "semantic_layer = {\n",
+ " \"graph\": knowledge_graph,\n",
+ " \"ontology\": ontology,\n",
+ " \"mappings\": {\n",
+ " \"entity_type_mappings\": entity_type_mappings,\n",
+ " \"relationship_type_mappings\": relationship_type_mappings,\n",
+ " \"property_mappings\": property_mappings,\n",
+ " },\n",
+ "}\n",
+ "\n",
+ "for mapping_name, mapping in semantic_layer[\"mappings\"].items():\n",
+ " print(f\"{mapping_name}:\")\n",
+ " for source, target in mapping.items():\n",
+ " print(f\" {source:<12} -> {target}\")\n",
+ "\n",
+ "# Every type and relationship in the graph must have an ontology term\n",
+ "assert set(entity_type_mappings) == {entity[\"type\"] for entity in entities}\n",
+ "assert set(relationship_type_mappings) == {rel[\"type\"] for rel in relationships}"
+ ],
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 4: Apply the Mappings\n",
+ "\n",
+ "Applying the semantic layer means rewriting the graph so every type, relationship, and property key is an ontology term. This *aligned* graph — not the original one — is what gets exported and stored.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {},
+ "source": [
+ "aligned_graph = {\n",
+ " \"entities\": [\n",
+ " {\n",
+ " **entity,\n",
+ " \"type\": entity_type_mappings[entity[\"type\"]],\n",
+ " \"properties\": {\n",
+ " property_mappings[\"name\"]: entity[\"name\"],\n",
+ " **{\n",
+ " property_mappings[key]: value\n",
+ " for key, value in entity[\"properties\"].items()\n",
+ " },\n",
+ " },\n",
+ " }\n",
+ " for entity in knowledge_graph[\"entities\"]\n",
+ " ],\n",
+ " \"relationships\": [\n",
+ " {**rel, \"type\": relationship_type_mappings[rel[\"type\"]]}\n",
+ " for rel in knowledge_graph[\"relationships\"]\n",
+ " ],\n",
+ "}\n",
+ "\n",
+ "print(\"Aligned entity sample:\")\n",
+ "sample = aligned_graph[\"entities\"][0]\n",
+ "print(f\" id: {sample['id']}\")\n",
+ "print(f\" type: {sample['type']}\")\n",
+ "for key, value in sample[\"properties\"].items():\n",
+ " print(f\" {key} = {value}\")\n",
+ "\n",
+ "print(\"\\nAligned relationship sample:\")\n",
+ "print(f\" {aligned_graph['relationships'][0]['type']}\")"
+ ],
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 5: Export Ontology-Aligned RDF\n",
+ "\n",
+ "`RDFExporter` writes the aligned graph as Turtle. Entity types (`rdf:type`) and relationship predicates now use the ontology URIs.\n",
+ "\n",
+ "> [!NOTE]\n",
+ "> The file exporter serializes a compact projection of each entity (name and confidence); the complete property triples are produced by the triplet store in Step 6.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {},
+ "source": [
+ "from semantica.export import RDFExporter\n",
+ "\n",
+ "exporter = RDFExporter()\n",
+ "exporter.export(aligned_graph, \"semantic_layer.ttl\", format=\"turtle\")\n",
+ "\n",
+ "turtle = open(\"semantic_layer.ttl\").read()\n",
+ "print(turtle[:600])\n",
+ "\n",
+ "# The RDF reflects the ontology mappings, not the raw source labels\n",
+ "assert BASE_URI + \"Person\" in turtle\n",
+ "assert BASE_URI + \"worksFor\" in turtle\n",
+ "print(\"... exported semantic_layer.ttl\")"
+ ],
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 6: Store and Query\n",
+ "\n",
+ "`TripletStore.store()` loads the aligned graph together with the ontology. The embedded Oxigraph backend runs in memory, so there is nothing to install or start beyond the `pip` extra. With the data in the store, a business question — *who works for Tech Corp, and in which role?* — becomes one small SPARQL query over the ontology's terms.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {},
+ "source": [
+ "from semantica.triplet_store import TripletStore\n",
+ "\n",
+ "store = TripletStore(backend=\"oxigraph\")\n",
+ "result = store.store(aligned_graph, ontology)\n",
+ "print(f\"Stored triples: {result['processed']} (failed: {result['failed']})\")\n",
+ "\n",
+ "query = f\"\"\"\n",
+ "SELECT ?name ?role WHERE {{\n",
+ " ?person <{BASE_URI}worksFor> ?org .\n",
+ " ?person <{BASE_URI}name> ?name .\n",
+ " ?person <{BASE_URI}role> ?role .\n",
+ "}}\n",
+ "ORDER BY ?name\n",
+ "\"\"\"\n",
+ "query_result = store.execute_query(query)\n",
+ "\n",
+ "print(\"\\nWho works for Tech Corp, and in which role?\")\n",
+ "for binding in query_result.bindings:\n",
+ " print(f\" {binding['name']['value']} — {binding['role']['value']}\")\n",
+ "\n",
+ "assert result[\"failed\"] == 0\n",
+ "assert len(query_result.bindings) == 2"
+ ],
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 🧹 Optional: Clean Up\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {},
+ "source": [
+ "from pathlib import Path\n",
+ "\n",
+ "ttl_file = Path(\"semantic_layer.ttl\")\n",
+ "if ttl_file.exists():\n",
+ " ttl_file.unlink()\n",
+ " print(f\"Removed {ttl_file}\")"
+ ],
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Summary\n",
+ "\n",
+ "A minimal semantic layer is a composition, and you have now built each part:\n",
+ "\n",
+ "1. **Knowledge graph** — `GraphBuilder` from explicit entities and relationships\n",
+ "2. **Ontology** — `OntologyGenerator` with your `base_uri`\n",
+ "3. **Explicit mappings** — entity types, relationship types, and properties, each tied to an ontology term\n",
+ "4. **Ontology-aligned RDF** — the mappings applied to the graph, exported with `RDFExporter`\n",
+ "5. **Queryable store** — `TripletStore` (embedded Oxigraph) answering a SPARQL question over the shared vocabulary\n",
+ "\n",
+ "### Where to go next\n",
+ "\n",
+ "The production version of this workflow — hand-designed governed ontologies, explicit source-to-ontology mappings from a warehouse, n-ary modeling, SHACL validation, provenance, and versioning — is covered in [Advanced: Manual Ontology + Snowflake Mapping](../advanced/13_Manual_Ontology_Snowflake_Mapping.ipynb).\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3",
+ "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.11.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}