{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e11c10a5",
   "metadata": {},
   "source": [
    "# Semantics: BY ... SOME ..."
   ]
  },
  {
   "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_bysome_mm_Screenshot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aade173d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GenDifS 0.63 (2023-06-19)\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_bysome.mm: 6 nodes\n"
     ]
    }
   ],
   "source": [
    "mindmap = \"semantics_bysome\" # filename without .mm extension\n",
    "\n",
    "o = GenDifS_Map(f\"../mm/{mindmap}.mm\", \n",
    "                pattern = [ \"owl\", \"owl-test\", \"owl-classify\", \"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 24 entries in `t_and_a_box_records`\n",
      "Collected 19 code lines, pattern: ['owl', 'owl-test', 'owl-classify', 'docu', 'ALL']\n",
      "rdflib: 39 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": "markdown",
   "id": "efcefd2a",
   "metadata": {},
   "source": [
    "## Graph without inferencing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "57bb8462",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['owl', 'owl-test', 'owl-classify', 'docu', 'ALL']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "o.pattern"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cf8a72c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "39"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(o.rdflib_graph.serialize(destination=f\"../ttl/{mindmap}_gd06.ttl\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88911a43",
   "metadata": {},
   "source": [
    "Code directly generated by our script:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "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",
      "#   Milk\n",
      "\n",
      "\n",
      "# SUBTAXON.TAXONOMY.a\n",
      "# owl: declare class *Milk* being a subclass of topConcept\n",
      ":Milk\n",
      "   a owl:Class ;\n",
      "   rdfs:subClassOf :topConcept .\n",
      "\n",
      "\n",
      "# SUBTAXON.TAXONOMY.b\n",
      "# owl-test: allocate an example instance\n",
      "ex:Milk_12_563_091\n",
      "   a :Milk .\n",
      "\n",
      "\n",
      "\n",
      "# BY.0\n",
      "# docu: --------------------\n",
      "# Milk\n",
      "#   BY has_source SOME Animal\n",
      "\n",
      "\n",
      "# BY.a\n",
      "# owl: declare *has_source* as a object property.\n",
      ":has_source\n",
      "   rdf:type owl:ObjectProperty .\n",
      "\n",
      "\n",
      "# BY.b\n",
      "# owl: declare *Animal* as a owl:Class .\n",
      ":Animal\n",
      "   a owl:Class .\n",
      "\n",
      "\n",
      "# BY.c\n",
      "# owl-test: allocate a test instance of *Animal* .\n",
      "ex:Animal_1_612_661_532\n",
      "   a :Animal .\n",
      "\n",
      "\n",
      "\n",
      "# SUBTAXON.BY.a.03 ----\n",
      "# docu: \n",
      "# Milk\n",
      "#   BY ... SOME ...\n",
      "#      Goatmilk\n",
      "\n",
      "\n",
      "# SUBTAXON.BY.a\n",
      "# owl: declare class *Goatmilk* being a subclass of Milk\n",
      ":Goatmilk\n",
      "   a owl:Class ;\n",
      "   rdfs:subClassOf :Milk .\n",
      "\n",
      "\n",
      "# SUBTAXON.BY.b\n",
      "# owl-test: allocate an example instance\n",
      "ex:Goatmilk_1_208_031_273\n",
      "   a :Goatmilk .\n",
      "\n",
      "\n",
      "\n",
      "# SOME.0 ----\n",
      "# docu: \n",
      "# Milk\n",
      "#   BY has_source SOME Animal\n",
      "#       Goatmilk\n",
      "#         SOME Goat\n",
      "\n",
      "\n",
      "# SOME.b\n",
      "# owl: declare *Goat* as a owl:Class\n",
      ":Goat\n",
      "   a owl:Class ;\n",
      "   rdfs:subClassOf :Animal .\n",
      "\n",
      "\n",
      "# SOME.c\n",
      "# owl-test: allocate an example instance of *Goat*\n",
      "ex:Goat_1_017_958_217\n",
      "   a :Goat .\n",
      "\n",
      "\n",
      "\n",
      "# SOME.g\n",
      "# owl-classify: \n",
      ":SOME_1_017_958_217_restriction\n",
      "   a owl:Class ;\n",
      "   rdfs:label \"BY has_source BYSOME Animal SOME Goat\" ;\n",
      "   rdfs:subClassOf :gendifs_restrictions ;\n",
      "   owl:equivalentClass [ a owl:Restriction ;\n",
      "      owl:onProperty :has_source ;\n",
      "      owl:someValuesFrom :Goat ] .\n",
      "\n",
      "\n",
      "\n",
      "# SOME.h\n",
      "# owl-classify: intersection of the superiors \n",
      ":SOME_1_017_958_217_intersection\n",
      "   a owl:Class ;\n",
      "   rdfs:label \"(BY has_source BYSOME Animal SOME Goat) INTERSECT Milk\" ;\n",
      "   rdfs:subClassOf :Goatmilk , :gendifs_intersections ;\n",
      "   owl:equivalentClass [ a owl:Class ;\n",
      "      owl:intersectionOf (\n",
      "          :SOME_1_017_958_217_restriction\n",
      "          :Milk ) ] .\n",
      "\n",
      "\n",
      "\n",
      "# SOME.i\n",
      "# owl-classify: \n",
      "# Milk has_source Goat -> Goatmilk\n",
      "ex:Milk_1_017_958_217_GAP1\n",
      "   a :Milk , :GAP1 .\n",
      "ex:Milk_1_017_958_217_GAP2\n",
      "   a :Milk , :GAP2 ;\n",
      "   :has_source ex:Goat_1_017_958_217 ;\n",
      "   ex:classifyLike ex:Goatmilk_1_017_958_217_target .\n",
      "ex:Goat_1_017_958_217\n",
      "   a :Goat .\n",
      "ex:Goatmilk_1_017_958_217_target \n",
      "   a :Goatmilk .\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(o.ttl_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f9ee452",
   "metadata": {},
   "source": [
    "Our ttl imported into and serialized by rdflib:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "96997393",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "39"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(o.rdflib_graph.serialize(destination=f\"../ttl/{mindmap}_rdflib.ttl\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "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 rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n",
      "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n",
      "\n",
      ":SOME_1_017_958_217_intersection a owl:Class ;\n",
      "    rdfs:label \"(BY has_source BYSOME Animal SOME Goat) INTERSECT Milk\" ;\n",
      "    rdfs:subClassOf :Goatmilk,\n",
      "        :gendifs_intersections ;\n",
      "    owl:equivalentClass [ a owl:Class ;\n",
      "            owl:intersectionOf ( :SOME_1_017_958_217_restriction :Milk ) ] .\n",
      "\n",
      "ex:Animal_1_612_661_532 a :Animal .\n",
      "\n",
      "ex:Goatmilk_1_208_031_273 a :Goatmilk .\n",
      "\n",
      "ex:Milk_12_563_091 a :Milk .\n",
      "\n",
      "ex:Milk_1_017_958_217_GAP1 a :GAP1,\n",
      "        :Milk .\n",
      "\n",
      "ex:Milk_1_017_958_217_GAP2 a :GAP2,\n",
      "        :Milk ;\n",
      "    :has_source ex:Goat_1_017_958_217 ;\n",
      "    ex:classifyLike ex:Goatmilk_1_017_958_217_target .\n",
      "\n",
      "ex:classifyLike a owl:ObjectProperty .\n",
      "\n",
      ":SOME_1_017_958_217_restriction a owl:Class ;\n",
      "    rdfs:label \"BY has_source BYSOME Animal SOME Goat\" ;\n",
      "    rdfs:subClassOf :gendifs_restrictions ;\n",
      "    owl:equivalentClass [ a owl:Restriction ;\n",
      "            owl:onProperty :has_source ;\n",
      "            owl:someValuesFrom :Goat ] .\n",
      "\n",
      ":has_source a owl:ObjectProperty .\n",
      "\n",
      "ex:Goat_1_017_958_217 a :Goat .\n",
      "\n",
      "ex:Goatmilk_1_017_958_217_target a :Goatmilk .\n",
      "\n",
      ":Animal a owl:Class .\n",
      "\n",
      ":Goat a owl:Class ;\n",
      "    rdfs:subClassOf :Animal .\n",
      "\n",
      ":Goatmilk a owl:Class ;\n",
      "    rdfs:subClassOf :Milk .\n",
      "\n",
      ":Milk a owl:Class ;\n",
      "    rdfs:subClassOf :topConcept .\n",
      "\n",
      "[] a owl:Ontology .\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(o.rdflib_graph.serialize())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "125fabd5",
   "metadata": {},
   "source": [
    "## Graph after inferencing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b823abbd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "owlrl: 264 triples.\n"
     ]
    }
   ],
   "source": [
    "o.owlrl()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3c0394fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "264"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(o.owlrl_graph.serialize(destination=f\"../ttl/{mindmap}_owlrl.ttl\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d1ff88a3",
   "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",
      "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n",
      "\n",
      "ex:Animal_1_612_661_532 a :Animal,\n",
      "        owl:Thing ;\n",
      "    owl:sameAs ex:Animal_1_612_661_532 .\n",
      "\n",
      "ex:Goatmilk_1_208_031_273 a :Goatmilk,\n",
      "        :Milk,\n",
      "        :topConcept,\n",
      "        owl:Thing ;\n",
      "    owl:sameAs ex:Goatmilk_1_208_031_273 .\n",
      "\n",
      "ex:Milk_12_563_091 a :Milk,\n",
      "        :topConcept,\n",
      "        owl:Thing ;\n",
      "    owl:sameAs ex:Milk_12_563_091 .\n",
      "\n",
      "ex:Milk_1_017_958_217_GAP1 a :GAP1,\n",
      "        :Milk,\n",
      "        :topConcept,\n",
      "        owl:Thing ;\n",
      "    owl:sameAs ex:Milk_1_017_958_217_GAP1 .\n",
      "\n",
      "ex:Milk_1_017_958_217_GAP2 a _:nb3d4b6c96b15481288dfaf5fdf6efc61b2,\n",
      "        _:nb3d4b6c96b15481288dfaf5fdf6efc61b3,\n",
      "        :GAP2,\n",
      "        :Goatmilk,\n",
      "        :Milk,\n",
      "        :SOME_1_017_958_217_intersection,\n",
      "        :SOME_1_017_958_217_restriction,\n",
      "        :gendifs_intersections,\n",
      "        :gendifs_restrictions,\n",
      "        :topConcept,\n",
      "        owl:Thing ;\n",
      "    :has_source ex:Goat_1_017_958_217 ;\n",
      "    ex:classifyLike ex:Goatmilk_1_017_958_217_target ;\n",
      "    owl:sameAs ex:Milk_1_017_958_217_GAP2 .\n",
      "\n",
      "rdf:HTML a rdfs:Datatype ;\n",
      "    owl:sameAs rdf:HTML .\n",
      "\n",
      "rdf:PlainLiteral a rdfs:Datatype ;\n",
      "    owl:sameAs rdf:PlainLiteral .\n",
      "\n",
      "rdf:XMLLiteral a rdfs:Datatype ;\n",
      "    owl:sameAs rdf:XMLLiteral .\n",
      "\n",
      "rdf:first owl:sameAs rdf:first .\n",
      "\n",
      "rdf:langString a rdfs:Datatype ;\n",
      "    owl:sameAs rdf:langString .\n",
      "\n",
      "rdf:rest owl:sameAs rdf:rest .\n",
      "\n",
      "rdf:type owl:sameAs rdf:type .\n",
      "\n",
      "rdfs:Literal a rdfs:Datatype ;\n",
      "    owl:sameAs rdfs:Literal .\n",
      "\n",
      "rdfs:comment a owl:AnnotationProperty ;\n",
      "    owl:sameAs rdfs:comment .\n",
      "\n",
      "rdfs:isDefinedBy a owl:AnnotationProperty ;\n",
      "    owl:sameAs rdfs:isDefinedBy .\n",
      "\n",
      "rdfs:label a owl:AnnotationProperty ;\n",
      "    owl:sameAs rdfs:label .\n",
      "\n",
      "rdfs:seeAlso a owl:AnnotationProperty ;\n",
      "    owl:sameAs rdfs:seeAlso .\n",
      "\n",
      "rdfs:subClassOf owl:sameAs rdfs:subClassOf .\n",
      "\n",
      "rdfs:subPropertyOf owl:sameAs rdfs:subPropertyOf .\n",
      "\n",
      "xsd:NCName a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:NCName .\n",
      "\n",
      "xsd:NMTOKEN a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:NMTOKEN .\n",
      "\n",
      "xsd:Name a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:Name .\n",
      "\n",
      "xsd:anyURI a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:anyURI .\n",
      "\n",
      "xsd:base64Binary a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:base64Binary .\n",
      "\n",
      "xsd:boolean a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:boolean .\n",
      "\n",
      "xsd:byte a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:byte .\n",
      "\n",
      "xsd:date a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:date .\n",
      "\n",
      "xsd:dateTime a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:dateTime .\n",
      "\n",
      "xsd:dateTimeStamp a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:dateTimeStamp .\n",
      "\n",
      "xsd:decimal a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:decimal .\n",
      "\n",
      "xsd:double a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:double .\n",
      "\n",
      "xsd:float a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:float .\n",
      "\n",
      "xsd:hexBinary a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:hexBinary .\n",
      "\n",
      "xsd:int a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:int .\n",
      "\n",
      "xsd:integer a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:integer .\n",
      "\n",
      "xsd:language a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:language .\n",
      "\n",
      "xsd:long a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:long .\n",
      "\n",
      "xsd:negativeInteger a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:negativeInteger .\n",
      "\n",
      "xsd:nonNegativeInteger a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:nonNegativeInteger .\n",
      "\n",
      "xsd:nonPositiveInteger a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:nonPositiveInteger .\n",
      "\n",
      "xsd:normalizedString a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:normalizedString .\n",
      "\n",
      "xsd:positiveInteger a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:positiveInteger .\n",
      "\n",
      "xsd:short a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:short .\n",
      "\n",
      "xsd:string a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:string .\n",
      "\n",
      "xsd:time a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:time .\n",
      "\n",
      "xsd:token a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:token .\n",
      "\n",
      "xsd:unsignedByte a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:unsignedByte .\n",
      "\n",
      "xsd:unsignedInt a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:unsignedInt .\n",
      "\n",
      "xsd:unsignedLong a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:unsignedLong .\n",
      "\n",
      "xsd:unsignedShort a rdfs:Datatype ;\n",
      "    owl:sameAs xsd:unsignedShort .\n",
      "\n",
      "owl:backwardCompatibleWith a owl:AnnotationProperty ;\n",
      "    owl:sameAs owl:backwardCompatibleWith .\n",
      "\n",
      "owl:deprecated a owl:AnnotationProperty ;\n",
      "    owl:sameAs owl:deprecated .\n",
      "\n",
      "owl:equivalentClass owl:sameAs owl:equivalentClass .\n",
      "\n",
      "owl:equivalentProperty owl:sameAs owl:equivalentProperty .\n",
      "\n",
      "owl:incompatibleWith a owl:AnnotationProperty ;\n",
      "    owl:sameAs owl:incompatibleWith .\n",
      "\n",
      "owl:intersectionOf owl:sameAs owl:intersectionOf .\n",
      "\n",
      "owl:onProperty owl:sameAs owl:onProperty .\n",
      "\n",
      "owl:priorVersion a owl:AnnotationProperty ;\n",
      "    owl:sameAs owl:priorVersion .\n",
      "\n",
      "owl:sameAs owl:sameAs owl:sameAs .\n",
      "\n",
      "owl:someValuesFrom owl:sameAs owl:someValuesFrom .\n",
      "\n",
      "owl:versionInfo a owl:AnnotationProperty ;\n",
      "    owl:sameAs owl:versionInfo .\n",
      "\n",
      ":GAP1 owl:sameAs :GAP1 .\n",
      "\n",
      ":GAP2 owl:sameAs :GAP2 .\n",
      "\n",
      "ex:Goat_1_017_958_217 a :Animal,\n",
      "        :Goat,\n",
      "        owl:Thing ;\n",
      "    owl:sameAs ex:Goat_1_017_958_217 .\n",
      "\n",
      "ex:Goatmilk_1_017_958_217_target a :Goatmilk,\n",
      "        :Milk,\n",
      "        :topConcept,\n",
      "        owl:Thing ;\n",
      "    owl:sameAs ex:Goatmilk_1_017_958_217_target .\n",
      "\n",
      "() owl:sameAs () .\n",
      "\n",
      "owl:Ontology owl:sameAs owl:Ontology .\n",
      "\n",
      "owl:Restriction owl:sameAs owl:Restriction .\n",
      "\n",
      "\"(BY has_source BYSOME Animal SOME Goat) INTERSECT Milk\" owl:sameAs \"(BY has_source BYSOME Animal SOME Goat) INTERSECT Milk\" .\n",
      "\n",
      "\"BY has_source BYSOME Animal SOME Goat\" owl:sameAs \"BY has_source BYSOME Animal SOME Goat\" .\n",
      "\n",
      "ex:classifyLike a owl:ObjectProperty ;\n",
      "    rdfs:subPropertyOf ex:classifyLike ;\n",
      "    owl:equivalentProperty ex:classifyLike ;\n",
      "    owl:sameAs ex:classifyLike .\n",
      "\n",
      "owl:Nothing a owl:Class ;\n",
      "    rdfs:subClassOf _:nb3d4b6c96b15481288dfaf5fdf6efc61b2,\n",
      "        _:nb3d4b6c96b15481288dfaf5fdf6efc61b3,\n",
      "        :Animal,\n",
      "        :Goat,\n",
      "        :Goatmilk,\n",
      "        :Milk,\n",
      "        :SOME_1_017_958_217_intersection,\n",
      "        :SOME_1_017_958_217_restriction,\n",
      "        :gendifs_intersections,\n",
      "        :gendifs_restrictions,\n",
      "        :topConcept,\n",
      "        owl:Nothing,\n",
      "        owl:Thing ;\n",
      "    owl:equivalentClass owl:Nothing ;\n",
      "    owl:sameAs owl:Nothing .\n",
      "\n",
      "owl:ObjectProperty owl:sameAs owl:ObjectProperty .\n",
      "\n",
      ":has_source a owl:ObjectProperty ;\n",
      "    rdfs:subPropertyOf :has_source ;\n",
      "    owl:equivalentProperty :has_source ;\n",
      "    owl:sameAs :has_source .\n",
      "\n",
      ":gendifs_intersections owl:sameAs :gendifs_intersections .\n",
      "\n",
      ":Goat a owl:Class ;\n",
      "    rdfs:subClassOf :Animal,\n",
      "        :Goat,\n",
      "        owl:Thing ;\n",
      "    owl:equivalentClass :Goat ;\n",
      "    owl:sameAs :Goat .\n",
      "\n",
      ":Animal a owl:Class ;\n",
      "    rdfs:subClassOf :Animal,\n",
      "        owl:Thing ;\n",
      "    owl:equivalentClass :Animal ;\n",
      "    owl:sameAs :Animal .\n",
      "\n",
      ":SOME_1_017_958_217_intersection a owl:Class ;\n",
      "    rdfs:label \"(BY has_source BYSOME Animal SOME Goat) INTERSECT Milk\" ;\n",
      "    rdfs:subClassOf _:nb3d4b6c96b15481288dfaf5fdf6efc61b2,\n",
      "        _:nb3d4b6c96b15481288dfaf5fdf6efc61b3,\n",
      "        :Goatmilk,\n",
      "        :Milk,\n",
      "        :SOME_1_017_958_217_intersection,\n",
      "        :SOME_1_017_958_217_restriction,\n",
      "        :gendifs_intersections,\n",
      "        :gendifs_restrictions,\n",
      "        :topConcept,\n",
      "        owl:Thing ;\n",
      "    owl:equivalentClass _:nb3d4b6c96b15481288dfaf5fdf6efc61b3,\n",
      "        :SOME_1_017_958_217_intersection ;\n",
      "    owl:sameAs :SOME_1_017_958_217_intersection .\n",
      "\n",
      ":gendifs_restrictions owl:sameAs :gendifs_restrictions .\n",
      "\n",
      ":Goatmilk a owl:Class ;\n",
      "    rdfs:subClassOf :Goatmilk,\n",
      "        :Milk,\n",
      "        :topConcept,\n",
      "        owl:Thing ;\n",
      "    owl:equivalentClass :Goatmilk ;\n",
      "    owl:sameAs :Goatmilk .\n",
      "\n",
      ":SOME_1_017_958_217_restriction a owl:Class ;\n",
      "    rdfs:label \"BY has_source BYSOME Animal SOME Goat\" ;\n",
      "    rdfs:subClassOf _:nb3d4b6c96b15481288dfaf5fdf6efc61b2,\n",
      "        :SOME_1_017_958_217_restriction,\n",
      "        :gendifs_restrictions,\n",
      "        owl:Thing ;\n",
      "    owl:equivalentClass _:nb3d4b6c96b15481288dfaf5fdf6efc61b2,\n",
      "        :SOME_1_017_958_217_restriction ;\n",
      "    owl:sameAs :SOME_1_017_958_217_restriction .\n",
      "\n",
      "owl:AnnotationProperty owl:sameAs owl:AnnotationProperty .\n",
      "\n",
      "owl:Class owl:sameAs owl:Class .\n",
      "\n",
      ":topConcept owl:sameAs :topConcept .\n",
      "\n",
      ":Milk a owl:Class ;\n",
      "    rdfs:subClassOf :Milk,\n",
      "        :topConcept,\n",
      "        owl:Thing ;\n",
      "    owl:equivalentClass :Milk ;\n",
      "    owl:sameAs :Milk .\n",
      "\n",
      "owl:Thing a owl:Class ;\n",
      "    rdfs:subClassOf owl:Thing ;\n",
      "    owl:equivalentClass owl:Thing ;\n",
      "    owl:sameAs owl:Thing .\n",
      "\n",
      "rdfs:Datatype owl:sameAs rdfs:Datatype .\n",
      "\n",
      "_:nb3d4b6c96b15481288dfaf5fdf6efc61b1 a owl:Ontology ;\n",
      "    owl:sameAs _:nb3d4b6c96b15481288dfaf5fdf6efc61b1 .\n",
      "\n",
      "_:nb3d4b6c96b15481288dfaf5fdf6efc61b4 rdf:first :SOME_1_017_958_217_restriction ;\n",
      "    rdf:rest _:nb3d4b6c96b15481288dfaf5fdf6efc61b5 ;\n",
      "    owl:sameAs _:nb3d4b6c96b15481288dfaf5fdf6efc61b4 .\n",
      "\n",
      "_:nb3d4b6c96b15481288dfaf5fdf6efc61b5 rdf:first :Milk ;\n",
      "    rdf:rest () ;\n",
      "    owl:sameAs _:nb3d4b6c96b15481288dfaf5fdf6efc61b5 .\n",
      "\n",
      "_:nb3d4b6c96b15481288dfaf5fdf6efc61b3 a owl:Class ;\n",
      "    rdfs:subClassOf _:nb3d4b6c96b15481288dfaf5fdf6efc61b2,\n",
      "        _:nb3d4b6c96b15481288dfaf5fdf6efc61b3,\n",
      "        :Goatmilk,\n",
      "        :Milk,\n",
      "        :SOME_1_017_958_217_intersection,\n",
      "        :SOME_1_017_958_217_restriction,\n",
      "        :gendifs_intersections,\n",
      "        :gendifs_restrictions,\n",
      "        :topConcept,\n",
      "        owl:Thing ;\n",
      "    owl:equivalentClass _:nb3d4b6c96b15481288dfaf5fdf6efc61b3,\n",
      "        :SOME_1_017_958_217_intersection ;\n",
      "    owl:intersectionOf _:nb3d4b6c96b15481288dfaf5fdf6efc61b4 ;\n",
      "    owl:sameAs _:nb3d4b6c96b15481288dfaf5fdf6efc61b3 .\n",
      "\n",
      "_:nb3d4b6c96b15481288dfaf5fdf6efc61b2 a owl:Restriction ;\n",
      "    rdfs:subClassOf _:nb3d4b6c96b15481288dfaf5fdf6efc61b2,\n",
      "        :SOME_1_017_958_217_restriction,\n",
      "        :gendifs_restrictions,\n",
      "        owl:Thing ;\n",
      "    owl:equivalentClass _:nb3d4b6c96b15481288dfaf5fdf6efc61b2,\n",
      "        :SOME_1_017_958_217_restriction ;\n",
      "    owl:onProperty :has_source ;\n",
      "    owl:sameAs _:nb3d4b6c96b15481288dfaf5fdf6efc61b2 ;\n",
      "    owl:someValuesFrom :Goat .\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(o.owlrl_graph.serialize())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cc7ea4f",
   "metadata": {},
   "source": [
    "## ask graph with SPARQL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "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": 14,
   "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": 15,
   "id": "32424d70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ex:Milk_1_017_958_217_GAP2', ':Milk']\n",
      "['ex:Milk_1_017_958_217_GAP2', ':GAP2']\n"
     ]
    }
   ],
   "source": [
    "show_rows(o.rdflib_graph, q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "bc0ace52",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ex:Milk_1_017_958_217_GAP2', ':Milk']\n",
      "['ex:Milk_1_017_958_217_GAP2', ':GAP2']\n",
      "['ex:Milk_1_017_958_217_GAP2', '_:nb3d4b6c96b15481288dfaf5fdf6efc61b2']\n",
      "['ex:Milk_1_017_958_217_GAP2', ':topConcept']\n",
      "['ex:Milk_1_017_958_217_GAP2', ':SOME_1_017_958_217_restriction']\n",
      "['ex:Milk_1_017_958_217_GAP2', 'owl:Thing']\n",
      "['ex:Milk_1_017_958_217_GAP2', ':gendifs_restrictions']\n",
      "['ex:Milk_1_017_958_217_GAP2', '_:nb3d4b6c96b15481288dfaf5fdf6efc61b3']\n",
      "['ex:Milk_1_017_958_217_GAP2', ':gendifs_intersections']\n",
      "['ex:Milk_1_017_958_217_GAP2', ':Goatmilk']\n",
      "['ex:Milk_1_017_958_217_GAP2', ':SOME_1_017_958_217_intersection']\n"
     ]
    }
   ],
   "source": [
    "show_rows(o.owlrl_graph, q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e38cfc8d",
   "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.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
