Queries: using the session object#

Binder

SimPhoNy’s session object implements methods that enable the retrieval of the ontology individuals it contains. The queries that can be run using such methods are extremely basic. If you need to run more advanced queries, head to the sections dealing with the search module and SPARQL.

Consider a similar example from the city ontology again. Remember that by default, newly created individuals created are stored in the Core Session.

[1]:
from simphony_osp.namespaces import city, rdfs
from simphony_osp.session import core_session

freiburg = city.City(name="Freiburg", coordinates=[47.997791, 7.842609])

neighborhoods = {
    city.Neighborhood(name=name, coordinates=coordinates)
    for name, coordinates in [
        ('Altstadt', [47.99525, 7.84726]),
        ('Stühlinger', [47.99888, 7.83774]),
        ('Neuburg', [48.00021, 7.86084]),
        ('Herdern', [48.00779, 7.86268]),
        ('Brühl', [48.01684, 7.843]),
    ]
}

citizen_1 = city.Citizen(name='Nikola', age=35,
                         iri='https://example.org/entities#Nikola')
citizen_2 = city.Citizen(name='Lena', age=70,
                         iri='https://example.org/entities#Lena')
citizen_2[rdfs.label] = 'Helena'
citizen_3 = city.Citizen(name='Marco', age=36,
                         iri='https://example.org/entities#Marco')
citizen_3[rdfs.label] = 'Marco'

freiburg[city.hasPart] |= neighborhoods
freiburg[city.hasInhabitant] += citizen_1, citizen_2

As shown in a previous section, the session object is iterable. This means that a simple method (althought very inefficient) to retrieve the individuals in the session is just iterating over the session object. It is also possible to know how many individuals are contained in a session using the Python built-in len.

[8]:
list(core_session)  # iterates over the session, very slow with big data
[8]:
[<OntologyIndividual: bd967f58-048b-45c2-bfba-d0ad7a1d4dc2>,
 <OntologyIndividual: f2f1dfb2-4fb9-4255-8059-9d9a56029721>,
 <OntologyIndividual: b706791a-93be-4192-9b2a-bf9e027ba47b>,
 <OntologyIndividual: 795c9172-cd38-4998-9f52-d6244e39c699>,
 <OntologyIndividual: 6087bd19-c53d-4b47-ad24-ec29b5585902>,
 <OntologyIndividual: a7700db4-5d6f-4eea-9a4c-488aab13b4be>,
 <OntologyIndividual: https://example.org/entities#Nikola>,
 <OntologyIndividual: Helena https://example.org/entities#Lena>]
[3]:
len(core_session)
[3]:
9

The session object also features a get method that is similar to the get method from ontology individuals. It returns, in fact, a set-like object that manages the individuals contained in the session. Consequently, it is even possible to add and remove individuals from the session using in-place modifcations (although not very practical). The most common use case of the method is just retrieving ontology individuals.

[4]:
core_session.get()

Just like for the get method from ontology individuals, it is possible to filter the results to specific individuals (given their identifier is known or they are already available as objects) and classes, as well as any combination of both.

[5]:
core_session.get('https://example.org/entities#Lena')
[5]:
<OntologyIndividual: Helena https://example.org/entities#Lena>
[6]:
core_session.get(oclass=city.Citizen)
[6]:
{<OntologyIndividual: https://example.org/entities#Nikola>, <OntologyIndividual: Helena https://example.org/entities#Lena>} <class Citizen of session default session>

Finally, if a label has been given to the individual, then it is also possible to use it as retrieval method.

[7]:
core_session.from_label('Helena')
[7]:
frozenset({<OntologyIndividual: Helena https://example.org/entities#Lena>})