From 3d275f52cf9bf37f32f9354bc18e0f665e6a220f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristina=20E=2E=20Gonz=C3=A1lez-Espinoza?= Date: Fri, 28 Jun 2024 13:55:22 +0200 Subject: [PATCH] Add missing parameter inside _register_one. Add notebook example of schema handling --- ...BP KG Creating and Modifying Schemas.ipynb | 254 ++++++++++++++++++ .../specializations/stores/bluebrain_nexus.py | 3 +- 2 files changed, 255 insertions(+), 2 deletions(-) create mode 100644 examples/notebooks/use-cases/BBP KG Creating and Modifying Schemas.ipynb diff --git a/examples/notebooks/use-cases/BBP KG Creating and Modifying Schemas.ipynb b/examples/notebooks/use-cases/BBP KG Creating and Modifying Schemas.ipynb new file mode 100644 index 00000000..3764c948 --- /dev/null +++ b/examples/notebooks/use-cases/BBP KG Creating and Modifying Schemas.ipynb @@ -0,0 +1,254 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "b14744df-3567-40f3-bb0c-06c6767ab695", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import json\n", + "import uuid\n", + "\n", + "from kgforge.core import KnowledgeGraphForge\n", + "from kgforge.specializations.resources import Dataset" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "72596414-c9ac-4312-a621-5bd4fdc3477b", + "metadata": {}, + "outputs": [], + "source": [ + "import getpass\n", + "TOKEN = getpass.getpass()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "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": "code", + "execution_count": 4, + "id": "29bceff6-d7b4-4fda-9cc3-fe1923c0f865", + "metadata": {}, + "outputs": [], + "source": [ + "def make_id(i):\n", + " return f\"{forge._store.endpoint}/schemas/{forge._store.bucket}/dummy_schema_{i}\"\n", + "\n", + "def create_schema(i):\n", + " payload = {\n", + " \"context\": \"https://incf.github.io/neuroshapes/contexts/schema.json\",\n", + " \"id\": make_id(i),\n", + " \"type\": \"Schema\",\n", + " \"imports\": [\n", + " \"https://neuroshapes.org/commons/entity\"\n", + " ],\n", + " \"shapes\": [\n", + " {\n", + " \"@id\": f\"{make_id(i)}/shapes/DummyShape\",\n", + " \"@type\": \"NodeShape\",\n", + " \"node\": \"https://neuroshapes.org/commons/entity/shapes/EntityShape\",\n", + " }\n", + " ]\n", + " }\n", + " resource = forge.from_json(payload)\n", + " forge.register(resource, schema_id=forge._store.service.SHACL_SCHEMA)\n", + " return resource" + ] + }, + { + "cell_type": "markdown", + "id": "e79f1807-dbfa-4cca-ab08-4809c9962517", + "metadata": {}, + "source": [ + "## 1. Create dummy schemas" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9a022c3d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " _register_one\n", + " True\n", + " _register_one\n", + " True\n", + " _register_one\n", + " True\n" + ] + } + ], + "source": [ + "dummies = [create_schema(uuid.uuid4()) for j in range(3)]" + ] + }, + { + "cell_type": "markdown", + "id": "d10b21d5-212b-48cc-b4a2-7ef2f739b2ed", + "metadata": {}, + "source": [ + "## 2. Retrieve the reciently created schemas using the ids and tag them" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "515b3e19", + "metadata": {}, + "outputs": [], + "source": [ + "retrieved = [forge.retrieve(dummy.id) for dummy in dummies]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "2c5594ee", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " context: https://incf.github.io/neuroshapes/contexts/schema.json\n", + " id: https://bbp.epfl.ch/nexus/v1/schemas/dke/kgforge/dummy_schema_7fab9433-af89-43eb-878a-607389240256\n", + " type: Schema\n", + " imports:\n", + " [\n", + " https://neuroshapes.org/commons/entity\n", + " ]\n", + " shapes:\n", + " [\n", + " {\n", + " id: https://bbp.epfl.ch/nexus/v1/schemas/dke/kgforge/dummy_schema_7fab9433-af89-43eb-878a-607389240256/shapes/DummyShape\n", + " type: NodeShape\n", + " node: https://neuroshapes.org/commons/entity/shapes/EntityShape\n", + " }\n", + " ]\n", + "}\n" + ] + } + ], + "source": [ + "print(retrieved[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "efb9cb41", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "https://bluebrain.github.io/nexus/schemas/shacl-20170720.ttl\n" + ] + } + ], + "source": [ + "print(retrieved[0]._store_metadata['_constrainedBy'])" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "a4f95633", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " _tag_one\n", + " True\n", + " _tag_one\n", + " True\n", + " _tag_one\n", + " True\n" + ] + } + ], + "source": [ + "for r in retrieved:\n", + " forge.tag(r, 't01')" + ] + }, + { + "cell_type": "markdown", + "id": "551f3fdc-4760-4c6b-861b-6ac085f27e00", + "metadata": {}, + "source": [ + "## 3. Deprecate the schemas" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "e6859730", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " _deprecate_one\n", + " True\n", + " _deprecate_one\n", + " True\n", + " _deprecate_one\n", + " True\n" + ] + } + ], + "source": [ + "for r in retrieved:\n", + " forge.deprecate(r)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "dev-forge", + "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.8.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/kgforge/specializations/stores/bluebrain_nexus.py b/kgforge/specializations/stores/bluebrain_nexus.py index 366b852f..19c5de74 100644 --- a/kgforge/specializations/stores/bluebrain_nexus.py +++ b/kgforge/specializations/stores/bluebrain_nexus.py @@ -164,9 +164,8 @@ def register_callback(task: Task): def _register_one(self, resource: Resource, schema_id: str) -> None: method, url, resource, exception_, headers, params, payload = ( - prepare_methods.prepare_create(service=self.service, resource=resource) + prepare_methods.prepare_create(service=self.service, resource=resource, schema_id=schema_id) ) - response = requests.request( method=method, url=url,