-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing parameter inside _register_one. Add notebook example of s…
…chema handling
- Loading branch information
Showing
2 changed files
with
255 additions
and
2 deletions.
There are no files selected for viewing
254 changes: 254 additions & 0 deletions
254
examples/notebooks/use-cases/BBP KG Creating and Modifying Schemas.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": [ | ||
"<action> _register_one\n", | ||
"<succeeded> True\n", | ||
"<action> _register_one\n", | ||
"<succeeded> True\n", | ||
"<action> _register_one\n", | ||
"<succeeded> 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": [ | ||
"<action> _tag_one\n", | ||
"<succeeded> True\n", | ||
"<action> _tag_one\n", | ||
"<succeeded> True\n", | ||
"<action> _tag_one\n", | ||
"<succeeded> 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": [ | ||
"<action> _deprecate_one\n", | ||
"<succeeded> True\n", | ||
"<action> _deprecate_one\n", | ||
"<succeeded> True\n", | ||
"<action> _deprecate_one\n", | ||
"<succeeded> 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters