{ "cells": [ { "cell_type": "markdown", "id": "boxed-professional", "metadata": {}, "source": [ "# Tutorial: Import and export" ] }, { "cell_type": "markdown", "id": "operational-honey", "metadata": {}, "source": [ "
\n", " \n", "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/simphony/docs/v3.9.0?filepath=docs%2Fsource%2Fjupyter%2Fimport_export.ipynb \"Click to run the tutorial yourself!\")\n", " \n", "
\n", "\n", "In this tutorial we will be covering the import and export capabilities of OSP-core. The utility functions that provide these functionalities are `import_cuds` and `export_cuds`, respectively." ] }, { "cell_type": "markdown", "id": "qualified-works", "metadata": {}, "source": [ "
\n", "
Tip
\n", " \n", "The full API specifications of the import and export functions can be found in the\n", "[utilities API reference page](../api_ref.md#osp.core.utils.export_cuds).\n", " \n", "
" ] }, { "cell_type": "markdown", "id": "driving-injury", "metadata": {}, "source": [ "For our running example, we'll be using the *city ontology* that was already introduces in the [cuds API tutorial](./cuds_api.ipynb). First, make sure the city ontology is installed. If not, run the following command:" ] }, { "cell_type": "code", "execution_count": 1, "id": "dying-accreditation", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO 2021-05-31 10:55:51,839 [osp.core.ontology.installation]: Will install the following namespaces: ['city']\n", "INFO 2021-05-31 10:55:51,879 [osp.core.ontology.yml.yml_parser]: Parsing YAML ontology file c:\\users\\yoav\\anaconda3\\envs\\osp\\lib\\site-packages\\osp\\core\\ontology\\docs\\city.ontology.yml\n", "INFO 2021-05-31 10:55:51,995 [osp.core.ontology.yml.yml_parser]: You can now use `from osp.core.namespaces import city`.\n", "INFO 2021-05-31 10:55:51,996 [osp.core.ontology.parser]: Loaded 7396 ontology triples in total\n", "INFO 2021-05-31 10:55:52,437 [osp.core.ontology.installation]: Installation successful\n" ] } ], "source": [ "!pico install city" ] }, { "cell_type": "markdown", "id": "lonely-listening", "metadata": {}, "source": [ "Next we create a few CUDS objects:" ] }, { "cell_type": "code", "execution_count": 2, "id": "considered-leonard", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from osp.core.namespaces import city\n", "\n", "c = city.City(name=\"Freiburg\", coordinates=[47, 7])\n", "p1 = city.Citizen(name=\"Peter\")\n", "p2 = city.Citizen(name=\"Anne\")\n", "c.add(p1, rel=city.hasInhabitant)\n", "c.add(p2, rel=city.hasInhabitant)" ] }, { "cell_type": "markdown", "id": "worth-province", "metadata": {}, "source": [ "Now we can use the `export_cuds` methods to export the data into a file:" ] }, { "cell_type": "code", "execution_count": 3, "id": "monthly-anxiety", "metadata": {}, "outputs": [], "source": [ "from osp.core.utils import export_cuds\n", "\n", "export_cuds(c, file='./data.ttl', format='turtle')" ] }, { "cell_type": "markdown", "id": "determined-nursing", "metadata": {}, "source": [ "This will create the file `data.ttl` with the following content:" ] }, { "cell_type": "code", "execution_count": 4, "id": "outstanding-wound", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "@prefix city: .\n", "@prefix cuba: .\n", "@prefix rdf: .\n", "@prefix xsd: .\n", "\n", "cuba:_serialization rdf:first \"47398674-720b-4765-9047-b5351ed175c0\" .\n", "\n", " a city:Citizen ;\n", " city:INVERSE_OF_hasInhabitant ;\n", " city:age 25 ;\n", " city:name \"Peter\" .\n", "\n", " a city:Citizen ;\n", " city:INVERSE_OF_hasInhabitant ;\n", " city:age 25 ;\n", " city:name \"Anne\" .\n", "\n", " a city:City ;\n", " city:coordinates \"[47, 7]\"^^ ;\n", " city:hasInhabitant ,\n", " ;\n", " city:name \"Freiburg\" .\n", "\n" ] } ], "source": [ "from sys import platform\n", "\n", "if platform == 'win32':\n", " !more data.ttl\n", "else:\n", " !cat data.ttl" ] }, { "cell_type": "markdown", "id": "offshore-cotton", "metadata": {}, "source": [ "You can change the format by entering a different value for the parameter `format`. The supported formats are “xml”, “n3”, “turtle”, “nt”, “pretty-xml”, “trix”, “trig” and “nquads”." ] }, { "cell_type": "markdown", "id": "derived-advancement", "metadata": {}, "source": [ "To import data, we can use the `import` method. Let's assume we wish to import data into an SQLite session. The following code will help us to achieve our aim:" ] }, { "cell_type": "code", "execution_count": 5, "id": "stable-session", "metadata": {}, "outputs": [], "source": [ "from osp.wrappers.sqlite import SqliteSession\n", "from osp.core.utils import import_cuds\n", "\n", "with SqliteSession(\"test.db\") as session:\n", " wrapper = city.CityWrapper(session=session)\n", " c = import_cuds('./data.ttl')\n", " wrapper.add(c)\n", " session.commit()" ] }, { "cell_type": "markdown", "id": "higher-short", "metadata": {}, "source": [ "Now we can verify the data was indeed imported:" ] }, { "cell_type": "code", "execution_count": 6, "id": "suspended-albert", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "- Cuds object:\n", " uid: 03015cb9-f88c-4ab1-9df9-bb52743b99de\n", " type: city.CityWrapper\n", " superclasses: city.CityWrapper, cuba.Entity, cuba.Wrapper\n", " description: \n", " To Be Determined\n", "\n", " |_Relationship city.hasPart:\n", " - city.City cuds object named :\n", " . uid: 72595bc4-1b68-46a3-97e9-8f3de2650f2c\n", " . coordinates: [47 7]\n", " . |_Relationship city.hasInhabitant:\n", " . - city.Citizen cuds object named :\n", " . . uid: 92a00459-0927-438c-a305-a26512ac7f03\n", " . . age: 25\n", " . - city.Citizen cuds object named :\n", " . uid: 27d1e83b-4ee9-4f4f-adb5-0b01a3cc2c1b\n", " . age: 25\n", " - city.City cuds object named :\n", " . uid: 47398674-720b-4765-9047-b5351ed175c0\n", " . coordinates: [47 7]\n", " . |_Relationship city.hasInhabitant:\n", " . - city.Citizen cuds object named :\n", " . . uid: 98f8ac8c-713d-4406-bc36-e0152f9e2ea3\n", " . . age: 25\n", " . - city.Citizen cuds object named :\n", " . uid: 8a90e2b3-7cca-4103-9eba-aab55e5903b1\n", " . age: 25\n", " - city.City cuds object named :\n", " uid: d886f8ce-1326-40f5-a98b-c4c893b8c085\n", " coordinates: [47 7]\n", " |_Relationship city.hasInhabitant:\n", " - city.Citizen cuds object named :\n", " . uid: 2b5d0a3f-81a5-4746-aab9-40adcb65e71f\n", " . age: 25\n", " - city.Citizen cuds object named :\n", " uid: 766b320a-7e9a-43ec-a696-96b4f9ee494d\n", " age: 25\n" ] } ], "source": [ "from osp.core.utils import pretty_print\n", "\n", "with SqliteSession(\"test.db\") as session:\n", " wrapper = city.CityWrapper(session=session)\n", " pretty_print(wrapper) " ] }, { "cell_type": "markdown", "id": "virgin-river", "metadata": {}, "source": [ "
\n", "
Notes
\n", " \n", "1. The format is automatically inferred from the file extension. To specify it explicitly, you can add the `format` parameter, like so: `import_cuds('./data.ttl', format='turtle')`.\n", "1. The `session` parameter is optional and inferred automatically from the context that created by the `with` statement (see the [tutorial on multiple wrappers](./multiple_wrappers.ipynb) for more information). You can specify the session explicitly like so: `import_cuds('./data.ttl', session=session)`.\n", " \n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.7.4 64-bit", "language": "python", "name": "python37464bit7e5bfc198a4544d1be12f13215aed90d" }, "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.8.5" }, "metadata": { "interpreter": { "hash": "301cd6007de04cbbf15bca26f0bc1cb48004d089278091d760363de622bdd0c8" } } }, "nbformat": 4, "nbformat_minor": 5 }