{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fdd9e064-f928-4bda-bb19-2e131b80bdf9",
   "metadata": {},
   "source": [
    "# Demo 2026-03-31\n",
    "\n",
    "dieses Notebook: Durchstich"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "76946e7d-67dc-4800-8213-8ba19f43bc9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ! pip install rdflib owlrl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3dec5f03-b8e7-4f69-a1cf-038cc0cfd007",
   "metadata": {},
   "outputs": [],
   "source": [
    "import rdflib\n",
    "import owlrl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "25aae3f3-5e00-491c-a9ea-8ba47c07f69e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def focus(focus_string_list, ttl):\n",
    "    return \"\\n\\n\".join( [ paragraph for paragraph in ttl.split(\"\\n\\n\") \\\n",
    "        if any( [ focus_string in paragraph for focus_string in focus_string_list ] ) ] )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dbd04f9b-8587-4e42-9dc8-9d44cda93d9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "ttl = \"\"\"\n",
    "# https://www.w3.org/wiki/TheUsualPrefixes\n",
    "\n",
    "@prefix jb: <https://www.jbusse.de/semweb/o/prof-studi-ontologie.ttl#> .\n",
    "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n",
    "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n",
    "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n",
    "\n",
    "jb:BUJ\n",
    "    jb:unterrichtet jb:IF640 ;\n",
    "    a jb:Prof .\n",
    "    \n",
    "jb:011112\n",
    "    jb:belegt jb:IF640 .    \n",
    "\n",
    "jb:Prof  rdfs:subClassOf jb:Mensch .\n",
    "jb:Studi rdfs:subClassOf jb:Mensch .\n",
    "\n",
    "jb:belegt\n",
    "    rdfs:domain jb:Studi ;\n",
    "    rdfs:range jb:Modul .\n",
    "\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cbb5062a-9b08-4f3e-bdbf-8ce47f566172",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "# https://www.w3.org/wiki/TheUsualPrefixes\n",
      "\n",
      "@prefix jb: <https://www.jbusse.de/semweb/o/prof-studi-ontologie.ttl#> .\n",
      "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n",
      "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n",
      "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n",
      "\n",
      "jb:BUJ\n",
      "    jb:unterrichtet jb:IF640 ;\n",
      "    a jb:Prof .\n",
      "\n",
      "jb:011112\n",
      "    jb:belegt jb:IF640 .    \n",
      "\n",
      "jb:Prof  rdfs:subClassOf jb:Mensch .\n",
      "jb:Studi rdfs:subClassOf jb:Mensch .\n",
      "\n",
      "jb:belegt\n",
      "    rdfs:domain jb:Studi ;\n",
      "    rdfs:range jb:Modul .\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(ttl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0322c979-a2c3-4ce7-b7ca-82dbb7319350",
   "metadata": {},
   "outputs": [],
   "source": [
    "g = rdflib.Graph().parse( data=ttl )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b356d27e-4023-424f-8fa8-e62e1585337a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "@prefix jb: <https://www.jbusse.de/semweb/o/prof-studi-ontologie.ttl#> .\n",
      "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n",
      "\n",
      "jb:011112 jb:belegt jb:IF640 .\n",
      "\n",
      "jb:BUJ a jb:Prof ;\n",
      "    jb:unterrichtet jb:IF640 .\n",
      "\n",
      "jb:belegt rdfs:domain jb:Studi ;\n",
      "    rdfs:range jb:Modul .\n",
      "\n",
      "jb:Prof rdfs:subClassOf jb:Mensch .\n",
      "\n",
      "jb:Studi rdfs:subClassOf jb:Mensch .\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print( g.serialize(format=\"ttl\") )  # https://rdflib.readthedocs.io/en/7.1.1/plugin_serializers.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "45316d5b-63bb-415b-889a-1587ff0721b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "owlrl.DeductiveClosure(owlrl.OWLRL_Semantics,\n",
    "    axiomatic_triples = False).expand(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f10e6fa5-342b-45ea-8e44-51a10b7941be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jb:011112 a jb:Mensch,\n",
      "        jb:Studi ;\n",
      "    owl:sameAs jb:011112 ;\n",
      "    jb:belegt jb:IF640 .\n",
      "\n",
      "jb:BUJ a jb:Mensch,\n",
      "        jb:Prof ;\n",
      "    owl:sameAs jb:BUJ ;\n",
      "    jb:unterrichtet jb:IF640 .\n",
      "\n",
      "jb:IF640 a jb:Modul ;\n",
      "    owl:sameAs jb:IF640 .\n"
     ]
    }
   ],
   "source": [
    "print( focus( [\"BUJ\", \"IF640\" ], g.serialize(format=\"ttl\") ) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c234563-eb1e-4d19-a1df-c92d4ede21ce",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff3853e8-be0e-48a4-af49-c4f3ce8659c5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
