Tutorial: Import and export

Binder

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.

Tip

The full API specifictions of the import and export functions can be found in the utilities API reference page.

For our running example, we’ll be using the city ontology that was already introduces in the cuds API tutorial. First, make sure the city ontology is installed. If not, run the following command:

[1]:
!pico install city
INFO 2021-05-31 10:55:51,839 [osp.core.ontology.installation]: Will install the following namespaces: ['city']
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
INFO 2021-05-31 10:55:51,995 [osp.core.ontology.yml.yml_parser]: You can now use `from osp.core.namespaces import city`.
INFO 2021-05-31 10:55:51,996 [osp.core.ontology.parser]: Loaded 7396 ontology triples in total
INFO 2021-05-31 10:55:52,437 [osp.core.ontology.installation]: Installation successful

Next we create a few CUDS objects:

[2]:
from osp.core.namespaces import city

c = city.City(name="Freiburg", coordinates=[47, 7])
p1 = city.Citizen(name="Peter")
p2 = city.Citizen(name="Anne")
c.add(p1, rel=city.hasInhabitant)
c.add(p2, rel=city.hasInhabitant)
[2]:
<city.Citizen: 98f8ac8c-713d-4406-bc36-e0152f9e2ea3,  CoreSession: @0x221b0a05250>

Now we can use the export_cuds methods to export the data into a file:

[3]:
from osp.core.utils import export_cuds

export_cuds(c, file='./data.ttl', format='turtle')

This will create the file data.ttl with the following content:

[4]:
from sys import platform

if platform == 'win32':
    !more data.ttl
else:
    !cat data.ttl
@prefix city: <http://www.osp-core.com/city#> .
@prefix cuba: <http://www.osp-core.com/cuba#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

cuba:_serialization rdf:first "47398674-720b-4765-9047-b5351ed175c0" .

<http://www.osp-core.com/cuds#8a90e2b3-7cca-4103-9eba-aab55e5903b1> a city:Citizen ;
    city:INVERSE_OF_hasInhabitant <http://www.osp-core.com/cuds#47398674-720b-4765-9047-b5351ed175c0> ;
    city:age 25 ;
    city:name "Peter" .

<http://www.osp-core.com/cuds#98f8ac8c-713d-4406-bc36-e0152f9e2ea3> a city:Citizen ;
    city:INVERSE_OF_hasInhabitant <http://www.osp-core.com/cuds#47398674-720b-4765-9047-b5351ed175c0> ;
    city:age 25 ;
    city:name "Anne" .

<http://www.osp-core.com/cuds#47398674-720b-4765-9047-b5351ed175c0> a city:City ;
    city:coordinates "[47, 7]"^^<http://www.osp-core.com/cuba#_datatypes/VECTOR-INT-2> ;
    city:hasInhabitant <http://www.osp-core.com/cuds#8a90e2b3-7cca-4103-9eba-aab55e5903b1>,
        <http://www.osp-core.com/cuds#98f8ac8c-713d-4406-bc36-e0152f9e2ea3> ;
    city:name "Freiburg" .

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”.

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:

[5]:
from osp.wrappers.sqlite import SqliteSession
from osp.core.utils import import_cuds

with SqliteSession("test.db") as session:
    wrapper = city.CityWrapper(session=session)
    c = import_cuds('./data.ttl')
    wrapper.add(c)
    session.commit()

Now we can verify the data was indeed imported:

[6]:
from osp.core.utils import pretty_print

with SqliteSession("test.db") as session:
    wrapper = city.CityWrapper(session=session)
    pretty_print(wrapper)
- Cuds object:
  uid: 03015cb9-f88c-4ab1-9df9-bb52743b99de
  type: city.CityWrapper
  superclasses: city.CityWrapper, cuba.Entity, cuba.Wrapper
  description:
    To Be Determined

   |_Relationship city.hasPart:
     -  city.City cuds object named <Freiburg>:
     .  uid: 72595bc4-1b68-46a3-97e9-8f3de2650f2c
     .  coordinates: [47  7]
     .   |_Relationship city.hasInhabitant:
     .     -  city.Citizen cuds object named <Anne>:
     .     .  uid: 92a00459-0927-438c-a305-a26512ac7f03
     .     .  age: 25
     .     -  city.Citizen cuds object named <Peter>:
     .        uid: 27d1e83b-4ee9-4f4f-adb5-0b01a3cc2c1b
     .        age: 25
     -  city.City cuds object named <Freiburg>:
     .  uid: 47398674-720b-4765-9047-b5351ed175c0
     .  coordinates: [47  7]
     .   |_Relationship city.hasInhabitant:
     .     -  city.Citizen cuds object named <Anne>:
     .     .  uid: 98f8ac8c-713d-4406-bc36-e0152f9e2ea3
     .     .  age: 25
     .     -  city.Citizen cuds object named <Peter>:
     .        uid: 8a90e2b3-7cca-4103-9eba-aab55e5903b1
     .        age: 25
     -  city.City cuds object named <Freiburg>:
        uid: d886f8ce-1326-40f5-a98b-c4c893b8c085
        coordinates: [47  7]
         |_Relationship city.hasInhabitant:
           -  city.Citizen cuds object named <Anne>:
           .  uid: 2b5d0a3f-81a5-4746-aab9-40adcb65e71f
           .  age: 25
           -  city.Citizen cuds object named <Peter>:
              uid: 766b320a-7e9a-43ec-a696-96b4f9ee494d
              age: 25

Notes

  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').

  2. The session parameter is optional and inferred automatically from the context that created by the with statement (see the tutorial on multiple wrappers for more information). You can specify the session explicitly like so: import_cuds('./data.ttl', session=session).