{ "cells": [ { "cell_type": "markdown", "id": "befb2aa1-2b10-4363-8ea6-54a721aee0ff", "metadata": {}, "source": [ "# x in A or x in B (or in both)" ] }, { "cell_type": "markdown", "id": "785104e3-4ad0-4232-bd1e-29054dd29aea", "metadata": {}, "source": [ "somebody says: \"x is in A or B (or in both)\".\n", "\n", "\"in\" ... \"is Element of\"\n", "\n", "We do have two interpretations:\n", "\n", "* (1) x is in A, in B, or in both A and B\n", "* (2) x is in the union of A and B" ] }, { "cell_type": "code", "execution_count": 1, "id": "ddcc01ec-6932-4444-9615-9502a1d16a0e", "metadata": {}, "outputs": [], "source": [ "import rdflib\n", "import owlrl" ] }, { "cell_type": "code", "execution_count": 2, "id": "716a8eea-5cb8-434a-939a-3f05b5a48b03", "metadata": {}, "outputs": [], "source": [ "ttl = \"\"\"\n", "@prefix : .\n", "@prefix rdf: .\n", "@prefix rdfs: .\n", "@prefix owl: .\n", "@prefix dct: .\n", "@prefix openwemi: .\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 3, "id": "a131edc0-167e-4096-a1b6-b89a2d3609b0", "metadata": {}, "outputs": [], "source": [ "ttl += \"\"\"\n", "\n", "#:Work rdfs:subClassOf :WEMI .\n", "#:Expression rdfs:subClassOf :WEMI .\n", "#:Manifestation rdfs:subClassOf :WEMI .\n", "#:Item rdfs:subClassOf :WEMI .\n", "\n", ":WEMI a owl:Class;\n", " owl:unionOf (\n", " :Work\n", " :Expression\n", " :Manifestation\n", " :Item ) .\n", "\n", ":inst :instantiates :unknown1 .\n", "\n", ":man :manifests :unknown2 .\n", "\n", ":instantiates\n", " a rdf:Property ;\n", " rdfs:domain :Item ;\n", " rdfs:range :WEM .\n", "\n", ":WEM a owl:Class;\n", " owl:unionOf (\n", " :Work\n", " :Expression\n", " :Manifestation) .\n", "\n", ":manifests\n", " a rdf:Property ;\n", " rdfs:domain :Manifestation ;\n", " rdfs:range :WE .\n", "\n", "\n", ":WE a owl:Class;\n", " owl:unionOf (\n", " :Work\n", " :Expression ) .\n", "\n", "\n", "\"\"\"\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "5293beb3-8880-41df-af4e-06c3a9e13b80", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initially g has 32 triples\n" ] } ], "source": [ "g = rdflib.Graph().parse(data= ttl)\n", "print(f\"Initially g has {len(g)} triples\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "b7e5da4e-1c1a-46f0-b247-ea30cfac619a", "metadata": {}, "outputs": [], "source": [ "def focus(focus_curie_list, ttl): # ll ... 'RDF_RDFtest_OWL_OWLtest'\n", " result = \"\\n\\n\".join( [ paragraph for paragraph in ttl.split(\"\\n\\n\") \\\n", " if any( [ focus_curie in paragraph for focus_curie in focus_curie_list ] ) ] )\n", " return result" ] }, { "cell_type": "code", "execution_count": 6, "id": "aa9603ee-25e6-471c-aa21-e613d3038d5d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ":WEMI a owl:Class ;\n", " owl:unionOf ( :Work :Expression :Manifestation :Item ) .\n", "\n", ":inst :instantiates :unknown1 .\n", "\n", ":instantiates a rdf:Property ;\n", " rdfs:domain :Item ;\n", " rdfs:range :WEM .\n", "\n", ":man :manifests :unknown2 .\n", "\n", ":manifests a rdf:Property ;\n", " rdfs:domain :Manifestation ;\n", " rdfs:range :WE .\n", "\n", ":WE a owl:Class ;\n", " owl:unionOf ( :Work :Expression ) .\n", "\n", ":WEM a owl:Class ;\n", " owl:unionOf ( :Work :Expression :Manifestation ) .\n" ] } ], "source": [ "interesting = [ \"inst\", \"man\", \"unknown\", \"WEM\", \"WE\" ]\n", "print(focus(interesting, g.serialize()))" ] }, { "cell_type": "code", "execution_count": 7, "id": "eb773749-1db0-4ae5-908c-0e56db44232c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "After inferencing g has 216 triples\n" ] } ], "source": [ "owlrl.DeductiveClosure(owlrl.OWLRL_Semantics,\n", " axiomatic_triples = False).expand(g)\n", "print(f\"After inferencing g has {len(g)} triples\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "60072f92-2e32-4253-9e16-f67c6938678e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ":inst a :Item,\n", " :WEMI,\n", " owl:Thing ;\n", " :instantiates :unknown1 ;\n", " owl:sameAs :inst .\n", "\n", ":man a :Manifestation,\n", " :WEM,\n", " :WEMI,\n", " owl:Thing ;\n", " :manifests :unknown2 ;\n", " owl:sameAs :man .\n", "\n", ":unknown1 a :WEM,\n", " owl:Thing ;\n", " owl:sameAs :unknown1 .\n", "\n", ":unknown2 a :WE,\n", " owl:Thing ;\n", " owl:sameAs :unknown2 .\n", "\n", ":instantiates a rdf:Property ;\n", " rdfs:domain :Item,\n", " :WEMI,\n", " owl:Thing ;\n", " rdfs:range :WEM,\n", " owl:Thing ;\n", " rdfs:subPropertyOf :instantiates ;\n", " owl:equivalentProperty :instantiates ;\n", " owl:sameAs :instantiates .\n", "\n", ":manifests a rdf:Property ;\n", " rdfs:domain :Manifestation,\n", " :WEM,\n", " :WEMI,\n", " owl:Thing ;\n", " rdfs:range :WE,\n", " owl:Thing ;\n", " rdfs:subPropertyOf :manifests ;\n", " owl:equivalentProperty :manifests ;\n", " owl:sameAs :manifests .\n", "\n", "owl:Nothing a owl:Class ;\n", " rdfs:subClassOf :WE,\n", " :WEM,\n", " :WEMI,\n", " owl:Nothing,\n", " owl:Thing ;\n", " owl:equivalentClass owl:Nothing ;\n", " owl:sameAs owl:Nothing .\n", "\n", ":Expression rdfs:subClassOf :WE,\n", " :WEM,\n", " :WEMI,\n", " owl:Thing ;\n", " owl:sameAs :Expression .\n", "\n", ":Item rdfs:subClassOf :WEMI,\n", " owl:Thing ;\n", " owl:sameAs :Item .\n", "\n", ":Work rdfs:subClassOf :WE,\n", " :WEM,\n", " :WEMI,\n", " owl:Thing ;\n", " owl:sameAs :Work .\n", "\n", ":Manifestation rdfs:subClassOf :WEM,\n", " :WEMI,\n", " owl:Thing ;\n", " owl:sameAs :Manifestation .\n", "\n", ":WE a owl:Class ;\n", " rdfs:subClassOf :WE,\n", " owl:Thing ;\n", " owl:equivalentClass :WE ;\n", " owl:sameAs :WE ;\n", " owl:unionOf _:n6dcfd01aaccb4b5cbdf98b83382bc750b8 .\n", "\n", ":WEM a owl:Class ;\n", " rdfs:subClassOf :WEM,\n", " owl:Thing ;\n", " owl:equivalentClass :WEM ;\n", " owl:sameAs :WEM ;\n", " owl:unionOf _:n6dcfd01aaccb4b5cbdf98b83382bc750b5 .\n", "\n", ":WEMI a owl:Class ;\n", " rdfs:subClassOf :WEMI,\n", " owl:Thing ;\n", " owl:equivalentClass :WEMI ;\n", " owl:sameAs :WEMI ;\n", " owl:unionOf _:n6dcfd01aaccb4b5cbdf98b83382bc750b1 .\n" ] } ], "source": [ "print(focus(interesting, g.serialize()))" ] }, { "cell_type": "code", "execution_count": null, "id": "0c723ab9-d2a7-4d21-a889-9ea924afc9d4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "5c30c825-461e-4602-b115-1f4ad3bec540", "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.12.4" } }, "nbformat": 4, "nbformat_minor": 5 }