Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix: resolve TTL export alias failure and add RDF notebook example (#355)
KaifAhmad18c4e5e59681a
diff --git a/cookbook/introduction/15_Export.ipynb b/cookbook/introduction/15_Export.ipynbindex 0db9d510..3ccdb37d 100644--- a/cookbook/introduction/15_Export.ipynb+++ b/cookbook/introduction/15_Export.ipynb@@ -178,6 +178,13 @@"rdf_exporter.export(kg, \"output.ttl\", format=\"turtle\")"]},+ {+ "cell_type": "code",+ "source": "# TTL alias: format=\"ttl\" is equivalent to format=\"turtle\"\nrdf_data = {\n \"entities\": [\n {\"id\": \"e1\", \"text\": \"Apple Inc.\", \"type\": \"ORG\", \"confidence\": 0.95},\n {\"id\": \"e2\", \"text\": \"Steve Jobs\", \"type\": \"PERSON\", \"confidence\": 0.97},\n ],\n \"relationships\": [\n {\"source_id\": \"e2\", \"target_id\": \"e1\", \"type\": \"founded_by\", \"confidence\": 0.91},\n ],\n}\n\nrdf_exporter.export(rdf_data, \"output.ttl\", format=\"ttl\")\n\nresult = rdf_exporter.validate_rdf(rdf_data)\nprint(f\"Valid: {result['valid']}\")",+ "metadata": {},+ "execution_count": null,+ "outputs": []+ },{"cell_type": "markdown","metadata": {},@@ -363,4 +370,4 @@},"nbformat": 4,"nbformat_minor": 2-}+}\ No newline at end of filediff --git a/semantica/export/rdf_exporter.py b/semantica/export/rdf_exporter.pyindex 67a8a6cc..85f33ebb 100644--- a/semantica/export/rdf_exporter.py+++ b/semantica/export/rdf_exporter.py@@ -851,6 +851,15 @@ class RDFExporter:# Supported RDF formatsself.supported_formats = ["turtle", "rdfxml", "jsonld", "ntriples", "n3"]+ # Format aliases (common extensions/shorthands → canonical names)+ self._format_aliases = {+ "ttl": "turtle",+ "nt": "ntriples",+ "xml": "rdfxml",+ "rdf": "rdfxml",+ "json-ld": "jsonld",+ }+# Initialize progress trackerself.progress_tracker = get_progress_tracker()@@ -892,6 +901,7 @@ class RDFExporter:)try:+ format = self._format_aliases.get(format.lower(), format.lower())if format not in self.supported_formats:raise ValidationError(f"Unsupported RDF format: {format}. "diff --git a/tests/export/__init__.py b/tests/export/__init__.pynew file mode 100644index 00000000..e69de29bdiff --git a/tests/export/test_rdf_exporter.py b/tests/export/test_rdf_exporter.pynew file mode 100644index 00000000..2f662d18--- /dev/null+++ b/tests/export/test_rdf_exporter.py@@ -0,0 +1,73 @@+"""Tests for RDFExporter format alias resolution (issue #355)."""++import pytest+from semantica.export import RDFExporter+++RDF_DATA = {+ "entities": [+ {"id": "e1", "text": "Apple Inc.", "type": "ORG", "confidence": 0.95},+ {"id": "e2", "text": "Steve Jobs", "type": "PERSON", "confidence": 0.97},+ ],+ "relationships": [+ {"source_id": "e2", "target_id": "e1", "type": "founded_by", "confidence": 0.91},+ ],+}+++def exporter():+ return RDFExporter()+++def test_ttl_alias_produces_same_output_as_turtle(exporter):+ """format='ttl' must produce identical output to format='turtle'."""+ result_turtle = exporter.export_to_rdf(RDF_DATA, format="turtle")+ result_ttl = exporter.export_to_rdf(RDF_DATA, format="ttl")+ assert result_ttl == result_turtle+++def test_nt_alias_produces_same_output_as_ntriples(exporter):+ result_canonical = exporter.export_to_rdf(RDF_DATA, format="ntriples")+ result_alias = exporter.export_to_rdf(RDF_DATA, format="nt")+ assert result_alias == result_canonical+++def test_xml_alias_produces_same_output_as_rdfxml(exporter):+ result_canonical = exporter.export_to_rdf(RDF_DATA, format="rdfxml")+ result_alias = exporter.export_to_rdf(RDF_DATA, format="xml")+ assert result_alias == result_canonical+++def test_rdf_alias_produces_same_output_as_rdfxml(exporter):+ result_canonical = exporter.export_to_rdf(RDF_DATA, format="rdfxml")+ result_alias = exporter.export_to_rdf(RDF_DATA, format="rdf")+ assert result_alias == result_canonical+++def test_json_ld_alias_produces_same_output_as_jsonld(exporter):+ result_canonical = exporter.export_to_rdf(RDF_DATA, format="jsonld")+ result_alias = exporter.export_to_rdf(RDF_DATA, format="json-ld")+ assert result_alias == result_canonical+++def test_canonical_formats_unaffected(exporter):+ """Existing canonical format names must continue to work."""+ # n3 is listed in supported_formats but has no serializer implementation yet+ for fmt in ("turtle", "rdfxml", "jsonld", "ntriples"):+ result = exporter.export_to_rdf(RDF_DATA, format=fmt)+ assert result is not None and len(result) > 0+++def test_unsupported_format_raises(exporter):+ from semantica.utils.exceptions import ValidationError++ with pytest.raises(ValidationError):+ exporter.export_to_rdf(RDF_DATA, format="parquet")+++def test_ttl_export_to_file(exporter, tmp_path):+ out = tmp_path / "output.ttl"+ exporter.export(RDF_DATA, str(out), format="ttl")+ assert out.exists()+ assert out.stat().st_size > 0