{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e11c10a5",
   "metadata": {},
   "source": [
    "# Semantics: ISA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0662535c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('../py/')\n",
    "from gd06 import GenDifS_Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f06cc3e",
   "metadata": {},
   "source": [
    "![](../images/semantics_isa_mm_Screenshot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aade173d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GenDifS 0.63 (2023-06-06)\n",
      "cwd into input_dir: /home/dsci/a/l/LA_2023_ss/gendifs/mm\n",
      "reading /home/dsci/a/l/LA_2023_ss/gendifs/mm/semantics_isa.mm: 5 nodes\n"
     ]
    }
   ],
   "source": [
    "mindmap = \"semantics_isa\" # filename without .mm extension\n",
    "\n",
    "o = GenDifS_Map(f\"../mm/{mindmap}.mm\", \n",
    "                pattern = [ \"owl\", \"owl-test\", \"docu\" ], \n",
    "                # use_rdflib = False, # default: true\n",
    "                # remove_attributes= False, # default: true\n",
    "                verbose = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4554a9b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lexer: found #1 start nodes: TAXONOMY\n",
      "codegen: generated 13 entries in `t_and_a_box_records`\n",
      "Collected 10 code lines, pattern: ['owl', 'owl-test', 'docu', 'ALL']\n",
      "rdflib: 10 triples.\n"
     ]
    }
   ],
   "source": [
    "o.compile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "819dd6b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# write back ttl in order to highlight source nodes\n",
    "# o.mindmap.write(f\"../mm/{mindmap}.mm\", pretty_print=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f2ee7229",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "@prefix : <http://example.net/namespace/default#> .\n",
      "@prefix ex: <http://example.net/namespace/ex#> .\n",
      "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n",
      "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n",
      "\n",
      "ex:Animal_12_563_091 a :Animal .\n",
      "\n",
      "ex:Cat_1_208_031_273 a :Cat ;\n",
      "    ex:classifyLike ex:Animal_1_208_031_273 .\n",
      "\n",
      ":Cat a owl:Class ;\n",
      "    rdfs:subClassOf :Animal .\n",
      "\n",
      "ex:Animal_1_208_031_273 a :Animal .\n",
      "\n",
      ":Animal a owl:Class ;\n",
      "    rdfs:subClassOf :topConcept .\n"
     ]
    }
   ],
   "source": [
    "print(o.select_molecules(o.rdflib_graph, [\":Animal\", \":Cat\", \"ex:\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b823abbd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "owlrl: 34 triples.\n"
     ]
    }
   ],
   "source": [
    "o.owlrl()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "685f2b85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "@prefix : <http://example.net/namespace/default#> .\n",
      "@prefix ex: <http://example.net/namespace/ex#> .\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",
      "ex:Animal_12_563_091 a :Animal,\n",
      "        :topConcept,\n",
      "        rdfs:Resource .\n",
      "\n",
      "ex:Cat_1_208_031_273 a :Animal,\n",
      "        :Cat,\n",
      "        :topConcept,\n",
      "        rdfs:Resource ;\n",
      "    ex:classifyLike ex:Animal_1_208_031_273 .\n",
      "\n",
      ":Cat a rdfs:Resource,\n",
      "        owl:Class ;\n",
      "    rdfs:subClassOf :Animal,\n",
      "        :topConcept .\n",
      "\n",
      "ex:Animal_1_208_031_273 a :Animal,\n",
      "        :topConcept,\n",
      "        rdfs:Resource .\n",
      "\n",
      ":Animal a rdfs:Resource,\n",
      "        owl:Class ;\n",
      "    rdfs:subClassOf :topConcept .\n"
     ]
    }
   ],
   "source": [
    "print(o.select_molecules(o.owlrl_graph, [\":Animal\", \":Cat\", \"ex:\" ]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "920235a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "q = \"\"\"\n",
    "SELECT ?a ?A\n",
    "WHERE {\n",
    "?a rdf:type ?A . \n",
    "?a ex:classifyLike ?b .\n",
    "}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "47378a54",
   "metadata": {},
   "outputs": [],
   "source": [
    "def show_rows(g, q):\n",
    "    for row in g.query(q):\n",
    "        print([item.n3(g.namespace_manager) for item in row])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "32424d70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ex:Cat_1_208_031_273', ':Cat']\n"
     ]
    }
   ],
   "source": [
    "show_rows(o.rdflib_graph, q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "bc0ace52",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ex:Cat_1_208_031_273', ':Cat']\n",
      "['ex:Cat_1_208_031_273', ':Animal']\n",
      "['ex:Cat_1_208_031_273', 'rdfs:Resource']\n",
      "['ex:Cat_1_208_031_273', ':topConcept']\n"
     ]
    }
   ],
   "source": [
    "show_rows(o.owlrl_graph, q)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efcefd2a",
   "metadata": {},
   "source": [
    "## The complete graphs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5440493b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "# __init__\n",
      "# ALL: \n",
      "@prefix ex: <http://example.net/namespace/ex#> .\n",
      "@prefix cpt: <http://example.net/namespace/cpt#> .\n",
      "@prefix sheet: <http://example.net/namespace/sheet#> .\n",
      "@prefix : <http://example.net/namespace/default#> .\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 xml: <http://www.w3.org/XML/1998/namespace> .\n",
      "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n",
      "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n",
      "@prefix skos: <http://www.w3.org/2004/02/skos/core#> .\n",
      "@prefix gendifs: <http://jbusse.de/gendifs#> .\n",
      "\n",
      "\n",
      "# __init__\n",
      "# owl-test: \n",
      "ex:classifyLike a owl:ObjectProperty .\n",
      "\n",
      "\n",
      "# __init__\n",
      "# ALL: \n",
      "[ rdf:type owl:Ontology ] .\n",
      "\n",
      "\n",
      "\n",
      "# SUBTAXON.TAXONOMY.a.01 ----\n",
      "# docu: \n",
      "# TAXONOMY\n",
      "#   Animal\n",
      "\n",
      "\n",
      "# SUBTAXON.TAXONOMY.a\n",
      "# owl: declare class *Animal* being a subclass of topConcept\n",
      ":Animal\n",
      "   a owl:Class ;\n",
      "   rdfs:subClassOf :topConcept .\n",
      "\n",
      "\n",
      "# SUBTAXON.TAXONOMY.b\n",
      "# owl-test: allocate an example instance\n",
      "ex:Animal_12_563_091\n",
      "   a :Animal .\n",
      "\n",
      "\n",
      "\n",
      "# SUBTAXON.ISA.a.02 ----\n",
      "# docu: \n",
      "# Animal\n",
      "#   ISA\n",
      "#      Cat\n",
      "\n",
      "\n",
      "# SUBTAXON.ISA.a.02b ----\n",
      "# owl-test: \n",
      "ex:Cat_1_208_031_273\n",
      "   ex:classifyLike ex:Animal_1_208_031_273 .\n",
      "ex:Cat_1_208_031_273 a :Cat .\n",
      "ex:Animal_1_208_031_273 a :Animal .\n",
      "\n",
      "\n",
      "\n",
      "# SUBTAXON.ISA.a\n",
      "# owl: declare class *Cat* being a subclass of Animal\n",
      ":Cat\n",
      "   a owl:Class ;\n",
      "   rdfs:subClassOf :Animal .\n",
      "\n",
      "\n",
      "# SUBTAXON.ISA.b\n",
      "# owl-test: allocate an example instance\n",
      "ex:Cat_1_208_031_273\n",
      "   a :Cat .\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(o.ttl_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "88f13537",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "@prefix : <http://example.net/namespace/default#> .\n",
      "@prefix ex: <http://example.net/namespace/ex#> .\n",
      "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n",
      "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n",
      "\n",
      "ex:Animal_12_563_091 a :Animal .\n",
      "\n",
      "ex:Cat_1_208_031_273 a :Cat ;\n",
      "    ex:classifyLike ex:Animal_1_208_031_273 .\n",
      "\n",
      "ex:classifyLike a owl:ObjectProperty .\n",
      "\n",
      ":Cat a owl:Class ;\n",
      "    rdfs:subClassOf :Animal .\n",
      "\n",
      "ex:Animal_1_208_031_273 a :Animal .\n",
      "\n",
      ":Animal a owl:Class ;\n",
      "    rdfs:subClassOf :topConcept .\n",
      "\n",
      "[] a owl:Ontology .\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(o.rdflib_graph.serialize())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "da8c4d53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "@prefix : <http://example.net/namespace/default#> .\n",
      "@prefix ex: <http://example.net/namespace/ex#> .\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",
      "ex:Animal_12_563_091 a :Animal,\n",
      "        :topConcept,\n",
      "        rdfs:Resource .\n",
      "\n",
      "ex:Cat_1_208_031_273 a :Animal,\n",
      "        :Cat,\n",
      "        :topConcept,\n",
      "        rdfs:Resource ;\n",
      "    ex:classifyLike ex:Animal_1_208_031_273 .\n",
      "\n",
      ":Cat a rdfs:Resource,\n",
      "        owl:Class ;\n",
      "    rdfs:subClassOf :Animal,\n",
      "        :topConcept .\n",
      "\n",
      "ex:Animal_1_208_031_273 a :Animal,\n",
      "        :topConcept,\n",
      "        rdfs:Resource .\n",
      "\n",
      "ex:classifyLike a rdf:Property,\n",
      "        rdfs:Resource,\n",
      "        owl:ObjectProperty ;\n",
      "    rdfs:subPropertyOf ex:classifyLike .\n",
      "\n",
      "rdf:type a rdf:Property ;\n",
      "    rdfs:subPropertyOf rdf:type .\n",
      "\n",
      "rdfs:subClassOf a rdf:Property ;\n",
      "    rdfs:subPropertyOf rdfs:subClassOf .\n",
      "\n",
      "rdfs:subPropertyOf a rdf:Property ;\n",
      "    rdfs:subPropertyOf rdfs:subPropertyOf .\n",
      "\n",
      "owl:ObjectProperty a rdfs:Resource .\n",
      "\n",
      "owl:Ontology a rdfs:Resource .\n",
      "\n",
      "owl:Class a rdfs:Resource .\n",
      "\n",
      ":Animal a rdfs:Resource,\n",
      "        owl:Class ;\n",
      "    rdfs:subClassOf :topConcept .\n",
      "\n",
      ":topConcept a rdfs:Resource .\n",
      "\n",
      "[] a rdfs:Resource,\n",
      "        owl:Ontology .\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(o.owlrl_graph.serialize())"
   ]
  }
 ],
 "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.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
