\n",
"
\n",
"\n",
"The interface of the [session object](../api_reference.md#simphony_osp.session.Session) has been designed to interact exclusively with ontology individuals, and therefore, this page only shows you how to use sessions to deal with assertional knowledge.\n",
" \n",
"However, there is no technical reason for such limitation. Sessions can actually store any ontology entity (including terminological knowledge). As a curiosity, the entities from the ontologies that you install using [pico](ontologies/pico.md) are stored in a hidden session that is not meant to be directly accessed.\n",
"\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The default session can be temporarily changed using the `with` statement."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True True\n",
"False True\n"
]
}
],
"source": [
"session_B = Session()\n",
"\n",
"# this will be explained later\n",
"session_A.locked = True\n",
"session_B.locked = True\n",
"\n",
"with session_B:\n",
" london = city.City(name=\"London\", coordinates=[51.50737, -0.12767])\n",
" \n",
"print(paris in session_A, london in session_B)\n",
"\n",
"# Be careful when using the with statement with several session objects:\n",
"# keep in mind that the second will be the one set as default.\n",
"with session_A, session_B:\n",
" default_session = Session.get_default_session()\n",
" print(default_session is session_A, default_session is session_B)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sessions actually work in a way similar to databases. To start using them, one first has to \"open\" or \"connect\" to them. After that, changes can be performed on the data they contain, but such changes are not made permanent until a \"commit\" is performed. When one finishes working with them, the connection should be \"closed\". Unconfirmed changes are lost when the connection is \"closed\".\n",
"\n",
"In SimPhoNy, all sessions are automatically \"opened\" when they are created. The \"commit\" and \"close\" operations are controlled manually.\n",
"\n",
"In spite of that general rule, for sessions that are not connected to a wrapper, which are the ones being illustrated in this page, the \"commit\" command actually does nothing, as confirmed changes have nowhere else to go and be made permanent. You can think of commits being automatic in this case. These sessions also do not implement the \"close\" command.\n",
"\n",
"Therefore, this general rule has just been introduced in order to present a useful mental model for working with _all_ sessions, which includes [sessions connected to a wrapper](../wrappers/index.md)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Having said that, it is now simpler to understand the purpose of the [locked attribute of session objects](../../api_reference.md#simphony_osp.session.Session.locked) that appears in the last example. The `with` statement not only sets a session as the default, but also _closes_ it when leaving its context. _Locking a session_ with the [locked](../../api_reference.md#simphony_osp.session.Session.locked) attribute prevents the session from being closed if one intents to continue using it. To restore the original behavior, set it to `False`."
]
}
],
"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.10.7"
},
"toc-showcode": false,
"toc-showtags": true
},
"nbformat": 4,
"nbformat_minor": 4
}