{ "cells": [ { "cell_type": "code", "execution_count": 10, "id": "b14744df-3567-40f3-bb0c-06c6767ab695", "metadata": {}, "outputs": [], "source": [ "import os\n", "import json\n", "\n", "from kgforge.core import KnowledgeGraphForge\n", "from kgforge.specializations.resources import Dataset" ] }, { "cell_type": "code", "execution_count": 11, "id": "72596414-c9ac-4312-a621-5bd4fdc3477b", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ " ········\n" ] } ], "source": [ "import getpass\n", "TOKEN = getpass.getpass()" ] }, { "cell_type": "code", "execution_count": 12, "id": "c124caae-b9ae-4eb4-8d8e-bc437d28d3e6", "metadata": {}, "outputs": [], "source": [ "BUCKET = \"dke/kgforge\"\n", "\n", "forge = KnowledgeGraphForge(\"../use-cases/prod-forge-nexus.yml\",\n", " bucket=BUCKET,\n", " token=TOKEN\n", ")" ] }, { "cell_type": "markdown", "id": "a185f986-e008-4f0a-9447-df10e4c30bb9", "metadata": {}, "source": [ "# Retrieve 10 unconstrained entities" ] }, { "cell_type": "code", "execution_count": 13, "id": "29bceff6-d7b4-4fda-9cc3-fe1923c0f865", "metadata": {}, "outputs": [], "source": [ "def make_id(i):\n", " return f\"{forge._store.endpoint}/resources/{forge._store.bucket}/_/dummy_resource_{i}\"\n", "\n", "resources = [forge.retrieve(make_id(i)) for i in range(10)]" ] }, { "cell_type": "code", "execution_count": 14, "id": "72c07da6-1f6b-4251-b552-9d5f12fa40a9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(resources)" ] }, { "cell_type": "code", "execution_count": 15, "id": "f3f2512d-4a1c-4c56-8cce-ea53a991d76d", "metadata": {}, "outputs": [], "source": [ "def cs(res): return [e._store_metadata._constrainedBy for e in res]" ] }, { "cell_type": "code", "execution_count": 16, "id": "fc675bd3-2e7c-4de1-b3dc-041215a1a0c3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json']" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cs(resources)" ] }, { "cell_type": "markdown", "id": "e79f1807-dbfa-4cca-ab08-4809c9962517", "metadata": {}, "source": [ "## 1.a. Constraint using _store.update_schema" ] }, { "cell_type": "markdown", "id": "cc361f70-c8c9-4ab8-8ed5-932c3a1bf717", "metadata": {}, "source": [ "### Multiple" ] }, { "cell_type": "code", "execution_count": 17, "id": "5ed305ef-16d2-42eb-8818-ec68674afceb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 10\n", " _update_schema_many\n", " True\n" ] } ], "source": [ "e = forge._store.update_schema(resources, schema_id=\"https://neuroshapes.org/dash/entity\")" ] }, { "cell_type": "code", "execution_count": 18, "id": "bf330be7-b5be-4ea5-9249-d2926f5481b3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity']" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cs(resources)" ] }, { "cell_type": "markdown", "id": "aad27794-ddbe-4e58-8c5f-76a799b1e15c", "metadata": {}, "source": [ "### One" ] }, { "cell_type": "code", "execution_count": 19, "id": "fcb99113-3a3e-4e8f-b98e-d127d04e4e6a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " _update_schema_one\n", " True\n" ] } ], "source": [ "e = forge._store.update_schema(resources[0], schema_id='https://bluebrain.github.io/nexus/schemas/unconstrained.json')" ] }, { "cell_type": "code", "execution_count": null, "id": "da104ef9-63cf-49d2-b296-6506b6bbf4de", "metadata": {}, "outputs": [], "source": [ "cs(resources)" ] }, { "cell_type": "markdown", "id": "d10b21d5-212b-48cc-b4a2-7ef2f739b2ed", "metadata": {}, "source": [ "## 1.b Delete schemas using _store.delete_schema (equivalent to constraining by unconstrained.json)" ] }, { "cell_type": "code", "execution_count": 20, "id": "410896b1-2997-4cdb-9af5-fdb4a95fb7de", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 10\n", " _update_schema_many\n", " True\n" ] } ], "source": [ "e = forge._store.delete_schema(resources)" ] }, { "cell_type": "code", "execution_count": 21, "id": "030a7cc6-e272-43b7-9401-c288684dbfb8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json']" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cs(resources)" ] }, { "cell_type": "markdown", "id": "551f3fdc-4760-4c6b-861b-6ac085f27e00", "metadata": {}, "source": [ "## 2.a Constraint using the update method" ] }, { "cell_type": "markdown", "id": "0a5b0ebd-3037-4302-a774-33c4cd6a1767", "metadata": {}, "source": [ "### Multiple" ] }, { "cell_type": "code", "execution_count": 22, "id": "c0c14efd-cc49-489c-81ca-d5614b25ca34", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 10\n", " _update_many\n", " True\n" ] } ], "source": [ "for r in resources:\n", " r._synchronized = False\n", "\n", "e = forge.update(resources, schema_id=\"https://neuroshapes.org/dash/entity\")" ] }, { "cell_type": "code", "execution_count": 23, "id": "e97e4261-2dcd-4a88-a189-a137ee7c207d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity',\n", " 'https://neuroshapes.org/dash/entity']" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cs(resources)" ] }, { "cell_type": "markdown", "id": "7e3b7dc4-e39f-4003-b974-1c877ad6a47c", "metadata": {}, "source": [ "## 2.b Delete schemas using update method" ] }, { "cell_type": "code", "execution_count": 24, "id": "211eeb24-4c6f-469c-85fc-04faa4bc77fc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 10\n", " _update_many\n", " True\n" ] } ], "source": [ "for r in resources:\n", " r._synchronized = False\n", "\n", "e = forge.update(resources, schema_id='https://bluebrain.github.io/nexus/schemas/unconstrained.json')" ] }, { "cell_type": "code", "execution_count": 25, "id": "7ce6cb5a-e6fc-4a6f-a707-3c1781354d3d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json']" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cs(resources)" ] }, { "cell_type": "markdown", "id": "796e572d-4577-44ab-8862-6145cb000124", "metadata": {}, "source": [ "### One" ] }, { "cell_type": "code", "execution_count": 26, "id": "90ffe938-dcdd-4b85-92eb-65362855cf94", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " _update_one\n", " True\n" ] } ], "source": [ "resources[0]._synchronized = False\n", "forge.update(resources[0], schema_id=\"https://neuroshapes.org/dash/entity\")" ] }, { "cell_type": "code", "execution_count": 27, "id": "93259b3c-9f84-4b01-bc5b-dafd7bc7c94d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['https://neuroshapes.org/dash/entity',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json']" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cs(resources)" ] }, { "cell_type": "code", "execution_count": 28, "id": "b55e6972-714c-4468-9195-544ec3af0213", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " _update_one\n", " True\n" ] } ], "source": [ "resources[0]._synchronized = False\n", "forge.update(resources[0], schema_id=\"https://bluebrain.github.io/nexus/schemas/unconstrained.json\")" ] }, { "cell_type": "code", "execution_count": 29, "id": "2c0bb710-e39d-480c-be19-f48c9aaff3cc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json',\n", " 'https://bluebrain.github.io/nexus/schemas/unconstrained.json']" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cs(resources)" ] } ], "metadata": { "kernelspec": { "display_name": "forge_venv", "language": "python", "name": "venv" }, "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.8.17" } }, "nbformat": 4, "nbformat_minor": 5 }