{ "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 notebook showed that the store metadata can contain a version of a specific resource. This notebook demonstrates other functionalities that are related to versions." ] }, { "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": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "forge = KnowledgeGraphForge(\"../../configurations/demo-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\")" ] }, { "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 resources it is possible to refer to 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": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: 47486895-afc0-4f6f-94ac-265a63556b30\n", " type: Association\n", " agent:\n", " {\n", " id: f33b9536-b01a-4010-818c-587172990b65\n", " type: Person\n", " name: Jane Doe\n", " }\n", "}\n" ] } ], "source": [ "print(association)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "jane._store_metadata.version" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "association._store_metadata.version" ] }, { "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": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: 47486895-afc0-4f6f-94ac-265a63556b30_version=1\n", " type: Association\n", " agent:\n", " {\n", " id: f33b9536-b01a-4010-818c-587172990b65_version=1\n", " type: Person\n", " name: Jane Doe\n", " }\n", "}\n" ] } ], "source": [ "print(association)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "kgforge(v2)", "language": "python", "name": "kgforge" }, "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.5" } }, "nbformat": 4, "nbformat_minor": 4 }