Replies: 3 comments 8 replies
-
One solution is to always return a set-like object that contains all the attribute values or all the CUDS linked when using the subscripting API. See examples below. In [6]: from osp.core.namespaces import city
...: paris = city.City(name='Paris')
...: marc = city.Citizen(name='Marc')
...: aimee = city.Citizen(name='Aimée')
...:
...: paris[city.hasInhabitant] += marc
...: paris[city.hasInhabitant] += aimee
In [7]: paris[city.hasInhabitant]
Out[7]: {<city.Citizen: 44529dab-b4bf-434b-a196-693d638a84c4, CoreSession: @0x7fee5562c1f0>, <city.Citizen: 4fa81a0a-1ab9-4801-931e-0b4665dec233, CoreSession: @0x7fee5562c1f0>} <city.hasInhabitant of CUDS city.City: 34bdf5b8-4757-4450-81f0-295a04aaa504>
In [8]: del paris[city.hasInhabitant]
In [9]: paris[city.hasInhabitant]
Out[9]: set() <city.hasInhabitant of CUDS city.City: 34bdf5b8-4757-4450-81f0-295a04aaa504>
In [10]: paris[city.hasInhabitant] = marc
In [11]: paris[city.hasInhabitant]
Out[11]: {<city.Citizen: 4fa81a0a-1ab9-4801-931e-0b4665dec233, CoreSession: @0x7fee5562c1f0>} <city.hasInhabitant of CUDS city.City: 34bdf5b8-4757-4450-81f0-295a04aaa504>
In [12]: paris[city.name]
Out[12]: {'Paris'} <city.name of CUDS city.City: 34bdf5b8-4757-4450-81f0-295a04aaa504>
In [13]: paris[city.hasInhabitant] = {marc, aimee}
In [14]: paris[city.hasInhabitant]
Out[14]: {<city.Citizen: 44529dab-b4bf-434b-a196-693d638a84c4, CoreSession: @0x7fee5562c1f0>, <city.Citizen: 4fa81a0a-1ab9-4801-931e-0b4665dec233, CoreSession: @0x7fee5562c1f0>} <city.hasInhabitant of CUDS city.City: 34bdf5b8-4757-4450-81f0-295a04aaa504> Advantages
Disadvantages
|
Beta Was this translation helpful? Give feedback.
-
Another solution is to use slices to decide whether to return a set-like object that contains all the attribute values or all the CUDS linked when using the subscripting API; or just one of the items (at random) in each case. See examples below. In [6]: from osp.core.namespaces import city
...: paris = city.City(name='Paris')
...: marc = city.Citizen(name='Marc')
...: aimee = city.Citizen(name='Aimée')
...:
...: paris[city.hasInhabitant, :] += marc
...: paris[city.hasInhabitant, :] += aimee
In [7]: paris[city.hasInhabitant, :]
Out[7]: {<city.Citizen: 44529dab-b4bf-434b-a196-693d638a84c4, CoreSession: @0x7fee5562c1f0>, <city.Citizen: 4fa81a0a-1ab9-4801-931e-0b4665dec233, CoreSession: @0x7fee5562c1f0>} <city.hasInhabitant of CUDS city.City: 34bdf5b8-4757-4450-81f0-295a04aaa504>
In [8]: del paris[city.hasInhabitant, :]
In [9]: paris[city.hasInhabitant, :]
Out[9]: set() <city.hasInhabitant of CUDS city.City: 34bdf5b8-4757-4450-81f0-295a04aaa504>
In [10]: paris[city.hasInhabitant, :] = marc
In [11]: paris[city.hasInhabitant, :]
Out[11]: {<city.Citizen: 4fa81a0a-1ab9-4801-931e-0b4665dec233, CoreSession: @0x7fee5562c1f0>} <city.hasInhabitant of CUDS city.City: 34bdf5b8-4757-4450-81f0-295a04aaa504>
In [12]: paris[city.name, :]
Out[12]: {'Paris'} <city.name of CUDS city.City: 34bdf5b8-4757-4450-81f0-295a04aaa504>
In [13]: paris[city.hasInhabitant, :] = {marc, aimee}
In [14]: paris[city.hasInhabitant, :]
Out[14]: {<city.Citizen: 44529dab-b4bf-434b-a196-693d638a84c4, CoreSession: @0x7fee5562c1f0>, <city.Citizen: 4fa81a0a-1ab9-4801-931e-0b4665dec233, CoreSession: @0x7fee5562c1f0>} <city.hasInhabitant of CUDS city.City: 34bdf5b8-4757-4450-81f0-295a04aaa504>
In [15]: paris[city.hasInhabitant]
Out[15]: <city.Citizen: 44529dab-b4bf-434b-a196-693d638a84c4, CoreSession: @0x7fee5562c1f0>
In [16]: paris[city.name]
Out[16]: 'Paris'
In [17]: paris[city.hasInhabitant] += marc # Expected not to work.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-17-c57c7d04cd70> in <module>
----> 1 paris[city.hasInhabitant] += marc # Expected not to work.
TypeError: unsupported operand type(s) for +=: 'Cuds' and 'Cuds'
In [26]: paris[city.hasInhabitant]
Out[26]: <city.Citizen: 4fa81a0a-1ab9-4801-931e-0b4665dec233, CoreSession: @0x7fee5562c1f0>
In [28]: paris[city.hasInhabitant] = None
In [29]: paris[city.hasInhabitant]
In [30]: paris[city.hasInhabitant] = marc
In [31]: paris[city.hasInhabitant]
Out[31]: <city.Citizen: 4fa81a0a-1ab9-4801-931e-0b4665dec233, CoreSession: @0x7fee5562c1f0>
In [32]: del paris[city.hasInhabitant]
In [33]: paris[city.hasInhabitant] (the del, None, and assignment of single item functionality would also be available with this solution, I just did not showcase them there) Advantages
Disadvantages
|
Beta Was this translation helpful? Give feedback.
-
Notes from the discussion on 24.11. from osp.core.namespaces import ns
paris = ns.City(name="Paris")
# Object properties
paris.get(rel=ns.hasInhabitant) # A (result)set (possibly empty)
paris.get(rel=ns.hasInhabitant).one() # Only if one, else exception
paris.get(rel=ns.hasInhabitant).any() # Return one, if empty None
paris.get(rel=ns.hasInhabitant).all() # A list/set of one or all
# Functional data properties for properties explicitly defined in the ontology, converted to datatype of the property
## Because it is functional, querying always returns the value (no set)
paris.name = "Paris"
paris.name == "Paris"
paris.name = {"Paris", "name2"}
paris.name == '{"Paris", "name2"}'
# Both for data and object properties, functional and non-functional
# [] will return a ResultSet
# ResultSet.one() return element only if one, else exception
# ResultSet.any() return one if one or multiple, if empty None
# ResultSet.all() return all (no change)
## Object properties
paris[ns.hasInhabitant]
paris[ns.hasInhabitant].one()
paris[ns.hasInhabitant].any()
paris[ns.hasInhabitant].all()
## Data properties
paris[ns.nickname] = "City of love"
paris[ns.nickname] == ResultSet("City of love")
paris[ns.nickname].one() == "City of love"
paris[ns.nickname].any() == "City of love"
paris[ns.nickname].all() == ResultSet("City of love")
paris[ns.nickname] = {"City of love", "French capital"}
paris[ns.nickname] == ResultSet("City of love", "French capital")
paris[ns.nickname].one() == Exception
paris[ns.nickname].any() == "City of love"
paris[ns.nickname].any() == "French capital"
paris[ns.nickname].all() == ResultSet("City of love", "French capital")
paris[ns.nickname] = 8 # Converted to datatype, "8" |
Beta Was this translation helpful? Give feedback.
-
Hi all!
Due to a issues #416, #642, a new API for working with cuds based on the Python subscripting feature
cuds[...]
has been developed. The inner workings are ready, but I wanted to open a discussion to decide together with the community what design for using it would work best from a user's perspective.To better introduce you to the topic, I would like to provide some background.
Adapted from #682:
Note that both object properties and datatype properties can be either functional or non-functional. In OSP-core, object properties are known as relationships and datatype properties are known as attributes.
The table below describes how to work with each type of property with the current CUDS API.
What has been developed is a new way to interact with CUDS, based on the subscripting syntax
cuds[...]
. So the future CUDS API will look like this instead.The goal of this discussion is to decide, what should be inside or around the brackets besides the
OntologyRelationship
orOntologyAttribute
that one wishes to modify.I will start by writing answers describing the solutions I have thought of. Please contribute your own ideas if you wish. You can upvote or downvote each solution using the buttons on the left. Please do not hesitate to use the reply buttons under the answers to comment on them or suggest amendments.
Beta Was this translation helpful? Give feedback.
All reactions