{ "cells": [ { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:19.036357Z", "start_time": "2019-09-23T18:50:19.031896Z" } }, "source": [ "# Versioning\n", "\n", "The [Storing](03%20-%20Storing.ipynb) notebook showed that the store metadata can contain a version of a specific resource. This notebook demonstrates other functionalities that are related to [versioning](https://nexus-forge.readthedocs.io/en/latest/interaction.html#versioning) data such as tagging." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:20.068658Z", "start_time": "2019-09-23T18:50:19.054054Z" } }, "outputs": [], "source": [ "from kgforge.core import KnowledgeGraphForge" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A configuration file is needed in order to create a KnowledgeGraphForge session. A configuration can be generated using the notebook [00-Initialization.ipynb](00%20-%20Initialization.ipynb)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "forge = KnowledgeGraphForge(\"../../configurations/forge.yml\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from kgforge.core import Resource" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2019-09-23T15:42:21.191590Z", "start_time": "2019-09-23T15:42:21.184256Z" } }, "source": [ "## Tagging\n", "\n", "Tagging is used to provide a string identifier (tag) to a specific version of a resource." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "jane = Resource(type=\"Person\", name=\"Jane Doe\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " _register_one\n", " True\n" ] } ], "source": [ "forge.register(jane)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:21.235417Z", "start_time": "2019-09-23T18:50:21.229695Z" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "jane._synchronized" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:21.244576Z", "start_time": "2019-09-23T18:50:21.238277Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " _tag_one\n", " True\n" ] } ], "source": [ "forge.tag(jane, \"v1\")" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2019-09-23T15:56:05.768688Z", "start_time": "2019-09-23T15:56:05.760183Z" } }, "source": [ "### Error handling" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "john = Resource(type=\"Person\", name=\"John Smith\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:21.255080Z", "start_time": "2019-09-23T18:50:21.248873Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "john._synchronized" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:21.262122Z", "start_time": "2019-09-23T18:50:21.257592Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " _tag_one\n", " False\n", " TaggingError: resource should have an id\n" ] } ], "source": [ "forge.tag(john, \"v1\") # only _synchronized resources (i.e registered and without further changes) can be tagged" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Action(error='TaggingError', message='resource should have an id', operation='_tag_one', succeeded=False)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "john._last_action" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Freezing\n", "\n", "When linking to a resource it is possible to refer to a specific version by freezing it. In this demo example, the version is attached to the resource identifier, and this is specified in the `versioned_id_template` property on the Store section in the configuration file." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "jane = Resource(type=\"Person\", name=\"Jane Doe\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " _register_one\n", " True\n" ] } ], "source": [ "forge.register(jane)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "association = Resource(type=\"Association\", agent=jane)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " _register_one\n", " True\n" ] } ], "source": [ "forge.register(association)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "{'id': 'https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/3a112a8b-bc32-40c9-8503-c949a3e0dfb7',\n", " 'type': 'Association',\n", " 'agent': {'id': 'https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/ef908022-f24a-4fdf-8c23-c0f4261dce0c',\n", " 'type': 'Person',\n", " 'name': 'Jane Doe'}}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "forge.as_json(association)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "try:\n", " # DemoStore\n", " print(jane._store_metadata.version)\n", "except:\n", " # BlueBrainNexus\n", " print(jane._store_metadata._rev)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "try:\n", " # DemoStore\n", " print(association._store_metadata.version)\n", "except:\n", " # BlueBrainNexus\n", " print(association._store_metadata._rev)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " _freeze_one\n", " True\n" ] } ], "source": [ "forge.freeze(association)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "{'id': 'https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/3a112a8b-bc32-40c9-8503-c949a3e0dfb7?rev=1',\n", " 'type': 'Association',\n", " 'agent': {'id': 'https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/ef908022-f24a-4fdf-8c23-c0f4261dce0c?rev=1',\n", " 'type': 'Person',\n", " 'name': 'Jane Doe'}}" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "forge.as_json(association)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.7 (nexusforgelatest)", "language": "python", "name": "nexusforgelatest" }, "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.7.10" } }, "nbformat": 4, "nbformat_minor": 4 }