diff --git a/notebooks/updates.ipynb b/notebooks/updates.ipynb new file mode 100644 index 0000000..9525305 --- /dev/null +++ b/notebooks/updates.ipynb @@ -0,0 +1,440 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 7, + "id": "b7af0534-f924-45c0-bdd1-79a8ae0c1027", + "metadata": {}, + "outputs": [], + "source": [ + "from bigraph_viz import plot_bigraph\n", + "from bigraph_schema import TypeSystem" + ] + }, + { + "cell_type": "markdown", + "id": "b74a6c53-2e58-4fbf-8a39-2d9e7fcbc9f2", + "metadata": {}, + "source": [ + "### Initialize Type System" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "d9f8884a-cd9c-4f14-a662-25645fa76527", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "types = TypeSystem()" + ] + }, + { + "cell_type": "markdown", + "id": "2edffe2d-0624-4ae6-bc0a-083e5e2e85da", + "metadata": {}, + "source": [ + "## Updates\n", + "\n", + "- state update\n", + "- structure update" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "e343d08a-ea40-4e8b-8cec-2fb5c34e832f", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "schemaa = {\n", + " 'base': {\n", + " 'a': {'_type': 'int'},\n", + " },\n", + "\n", + " # cannot override existing keys unless it is of a subtype\n", + " 'second': {\n", + " 'b': {'_type': 'int'},\n", + " '_super': 'base',\n", + " },\n", + "}\n", + "\n", + "types.type_registry.register_multiple(schemaa, force=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "2aa57aeb-3b1a-4c04-b326-ad4f6e288285", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "bigraph\n", + "\n", + "\n", + "\n", + "('b',)\n", + "\n", + "b\n", + "13\n", + "\n", + "\n", + "\n", + "('a',)\n", + "\n", + "a\n", + "2\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "schema = {'_type': 'second'}\n", + "state = {\n", + " 'b': 13,\n", + " 'a': 2,\n", + "}\n", + "plot_bigraph(state, show_values=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "f7419fcb-df05-4068-9893-d3bd6cfa45e1", + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "bigraph\n", + "\n", + "\n", + "\n", + "('b',)\n", + "\n", + "b\n", + "8\n", + "\n", + "\n", + "\n", + "('a',)\n", + "\n", + "a\n", + "2\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "update = {\n", + " 'b': -5\n", + "}\n", + "new_state = types.apply(\n", + " schema,\n", + " state,\n", + " update\n", + ")\n", + "plot_bigraph(new_state, show_values=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "00fd55fb-f4bf-497b-a70c-45028c77ff6a", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "bigraph\n", + "\n", + "\n", + "\n", + "('b',)\n", + "\n", + "b\n", + "8\n", + "\n", + "\n", + "\n", + "('a',)\n", + "\n", + "a\n", + "7\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "update = {\n", + " 'a': 5\n", + "}\n", + "new_state = types.apply(\n", + " schema,\n", + " new_state,\n", + " update\n", + ")\n", + "plot_bigraph(new_state, show_values=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "fb8fad4e-87ab-4ac1-985f-621405c6c2b9", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# from bigraph_schema.registry import apply_registry\n", + "from bigraph_viz.dict_utils import deep_merge\n", + "def apply_merge(current, update, bindings=None, types=None):\n", + " assert isinstance(state, dict)\n", + " assert isinstance(update, dict)\n", + " updated = deep_merge(state, update)\n", + " return updated\n", + "types.apply_registry.register('merge', apply_merge, force=True)\n", + "\n", + "schemab = {\n", + " 'merge_branch': {\n", + " '_default': {'a1': 'any'},\n", + " '_apply': 'merge',},\n", + " # cannot override existing keys unless it is of a subtype\n", + " 'second': {\n", + " 'a1': {'a2': 'any'},\n", + " '_super': 'merge_branch'}}\n", + "types.type_registry.register_multiple(schemab, force=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "884749e9-659e-4fc0-afc6-0a6d0d3dbb12", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "bigraph\n", + "\n", + "\n", + "\n", + "('a1',)\n", + "\n", + "a1\n", + "\n", + "\n", + "\n", + "('a1', 'a2')\n", + "\n", + "a2\n", + "11\n", + "\n", + "\n", + "\n", + "('a1',)->('a1', 'a2')\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "schema = {'_type': 'merge_branch'}\n", + "state = {\n", + " 'a1': {'a2': 11},\n", + "}\n", + "plot_bigraph(state, show_values=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "0ad30d26-d33f-4d19-b4a9-f63a726f26e5", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "bigraph\n", + "\n", + "\n", + "\n", + "('a1',)\n", + "\n", + "a1\n", + "\n", + "\n", + "\n", + "('a1', 'a2')\n", + "\n", + "a2\n", + "11\n", + "\n", + "\n", + "\n", + "('a1',)->('a1', 'a2')\n", + "\n", + "\n", + "\n", + "\n", + "('a1', 'a3')\n", + "\n", + "a3\n", + "-5\n", + "\n", + "\n", + "\n", + "('a1',)->('a1', 'a3')\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "update = {\n", + " 'a1': {'a3': -5},\n", + "}\n", + "new_state = types.apply(\n", + " schema,\n", + " state,\n", + " update\n", + ")\n", + "# print(pf(new_state))\n", + "plot_bigraph(new_state, show_values=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ae2bc42e-1465-4531-8352-96d67b7a09f8", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}