more tutorial

main
Ziyang Hu 2 years ago
parent cf887b549d
commit df8126590a

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -1,381 +1,225 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
" \u001b[31m×\u001b[0m invalid password\n"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%reload_ext pycozo.ipyext_direct\n",
"%cozo_auth tutorial x"
"# Transaction and index"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
" \u001b[31m×\u001b[0m invalid password\n"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"1"
"%reload_ext pycozo.ipyext_direct\n",
"%cozo_auth tutorial *******"
]
},
{
"cell_type": "code",
"execution_count": 2,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"isinstance(1.0, float)"
"It is true that stored relations and triples look like diametrically opposed concepts in Cozo. With triples you have to set up elaborate schema before use. With stored relations you just store your results, without even needing to pre-declare anything. Yet they are actually designed to work together. The intended way that they work together is through transactions."
]
},
{
"cell_type": "code",
"execution_count": 1,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
"We have already met transactions when we learned how to insert and mutate data in the triple store. But transactions can in fact contain many more things than `put` and `retract` requests. Let's first have the following schema in place:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame([[1, 2], [True, False], ['a', 'b'], [{}, None]])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"<style type=\"text/css\">\n",
"#T_da9d9_row0_col0, #T_da9d9_row1_col0, #T_da9d9_row2_col0, #T_da9d9_row3_col0 {\n",
" color: #307fc1;\n",
"}\n",
"#T_da9d9_row0_col1, #T_da9d9_row1_col1, #T_da9d9_row2_col1, #T_da9d9_row3_col1 {\n",
" color: black;\n",
"}\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
"<table id=\"T_da9d9\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" <tr>\n",
" <th class=\"blank level0\" >&nbsp;</th>\n",
" <th id=\"T_da9d9_level0_col0\" class=\"col_heading level0 col0\" >attr_id</th>\n",
" <th id=\"T_da9d9_level0_col1\" class=\"col_heading level0 col1\" >op</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <th id=\"T_da9d9_level0_row0\" class=\"row_heading level0 row0\" >0</th>\n",
" <td id=\"T_da9d9_row0_col0\" class=\"data row0 col0\" >10000030</td>\n",
" <td id=\"T_da9d9_row0_col1\" class=\"data row0 col1\" >assert</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" <th id=\"T_da9d9_level0_row1\" class=\"row_heading level0 row1\" >1</th>\n",
" <td id=\"T_da9d9_row1_col0\" class=\"data row1 col0\" >10000031</td>\n",
" <td id=\"T_da9d9_row1_col1\" class=\"data row1 col1\" >assert</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>a</td>\n",
" <td>b</td>\n",
" <th id=\"T_da9d9_level0_row2\" class=\"row_heading level0 row2\" >2</th>\n",
" <td id=\"T_da9d9_row2_col0\" class=\"data row2 col0\" >10000032</td>\n",
" <td id=\"T_da9d9_row2_col1\" class=\"data row2 col1\" >assert</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>{}</td>\n",
" <td>None</td>\n",
" <th id=\"T_da9d9_level0_row3\" class=\"row_heading level0 row3\" >3</th>\n",
" <td id=\"T_da9d9_row3_col0\" class=\"data row3 col0\" >10000033</td>\n",
" <td id=\"T_da9d9_row3_col1\" class=\"data row3 col1\" >assert</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
"</table>\n"
],
"text/plain": [
" 0 1\n",
"0 1 2\n",
"1 True False\n",
"2 a b\n",
"3 {} None"
"<pandas.io.formats.style.Styler at 0x11a036740>"
]
},
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
":schema\n",
"\n",
":put ap {\n",
" code: string unique\n",
"}\n",
"\n",
":put rt {\n",
" src: ref,\n",
" dst: ref,\n",
" distance: float\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's try to add some data, but with a check:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style type=\"text/css\">\n",
"</style>\n",
"<table id=\"T_4b4a0\">\n",
" <thead>\n",
" <tr>\n",
" <th class=\"blank level0\" >&nbsp;</th>\n",
" <th id=\"T_4b4a0_level0_col0\" class=\"col_heading level0 col0\" >0</th>\n",
" <th id=\"T_4b4a0_level0_col1\" class=\"col_heading level0 col1\" >1</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th id=\"T_4b4a0_level0_row0\" class=\"row_heading level0 row0\" >0</th>\n",
" <td id=\"T_4b4a0_row0_col0\" class=\"data row0 col0\" >1</td>\n",
" <td id=\"T_4b4a0_row0_col1\" class=\"data row0 col1\" >2</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_4b4a0_level0_row1\" class=\"row_heading level0 row1\" >1</th>\n",
" <td id=\"T_4b4a0_row1_col0\" class=\"data row1 col0\" >True</td>\n",
" <td id=\"T_4b4a0_row1_col1\" class=\"data row1 col1\" >False</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_4b4a0_level0_row2\" class=\"row_heading level0 row2\" >2</th>\n",
" <td id=\"T_4b4a0_row2_col0\" class=\"data row2 col0\" >a</td>\n",
" <td id=\"T_4b4a0_row2_col1\" class=\"data row2 col1\" >b</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_4b4a0_level0_row3\" class=\"row_heading level0 row3\" >3</th>\n",
" <td id=\"T_4b4a0_row3_col0\" class=\"data row3 col0\" >{}</td>\n",
" <td id=\"T_4b4a0_row3_col1\" class=\"data row3 col1\" >None</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n"
],
"text/plain": [
"<pandas.io.formats.style.Styler at 0x1270f8c40>"
"\u001b[31meval::assert_none_failure\u001b[0m\n",
"\n",
" \u001b[31m×\u001b[0m Triple store transaction failed as a post-condition failed\n",
"\u001b[31m ╰─▶ \u001b[0mThe query is asserted to return no result, but a tuple [-100.0] is found\n",
" ╭─[6:1]\n",
" \u001b[2m6\u001b[0m │ ?[dist] := [r rt.distance dist], dist < 0\n",
" \u001b[2m7\u001b[0m │ :assert none\n",
" · \u001b[35;1m ────────────\u001b[0m\n",
" \u001b[2m8\u001b[0m │ }\n",
" ╰────\n"
]
},
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.style"
":tx\n",
"\n",
"{rt.src: {ap.code: 'A'}, rt.dst: {ap.code: 'B'}, rt.distance: -100}\n",
"\n",
":after {\n",
" ?[dist] := [r rt.distance dist], dist < 0\n",
" :assert none\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 22,
"cell_type": "markdown",
"metadata": {},
"source": [
"We have used the transaction directive `:after` signifying we want the query inside its braces to be checked _after_ the main transaction is done. In the `:after` block itself, we used the directive `:assert none` to indicate the above query should return no results at all. The error message also shows us the offending tuple."
]
},
{
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"def colour_code_type(val):\n",
" if isinstance(val, int) or isinstance(val, float):\n",
" colour = '#307fc1'\n",
" elif isinstance(val, str):\n",
" colour = 'black'\n",
" else:\n",
" colour = '#bf5b3d'\n",
" return f'color: {colour}'\n"
"We can check that in this case, no triple is actually inserted, since the transaction failed:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style type=\"text/css\">\n",
"#T_64485_row0_col0, #T_64485_row0_col1, #T_64485_row1_col0, #T_64485_row1_col1 {\n",
" color: #307fc1;\n",
"}\n",
"#T_64485_row2_col0, #T_64485_row2_col1 {\n",
" color: black;\n",
"}\n",
"#T_64485_row3_col0, #T_64485_row3_col1 {\n",
" color: #bf5b3d;\n",
"}\n",
"</style>\n",
"<table id=\"T_64485\">\n",
"<table id=\"T_67120\">\n",
" <thead>\n",
" <tr>\n",
" <th class=\"blank level0\" >&nbsp;</th>\n",
" <th id=\"T_64485_level0_col0\" class=\"col_heading level0 col0\" >0</th>\n",
" <th id=\"T_64485_level0_col1\" class=\"col_heading level0 col1\" >1</th>\n",
" <th id=\"T_67120_level0_col0\" class=\"col_heading level0 col0\" >r</th>\n",
" <th id=\"T_67120_level0_col1\" class=\"col_heading level0 col1\" >dist</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th id=\"T_64485_level0_row0\" class=\"row_heading level0 row0\" >0</th>\n",
" <td id=\"T_64485_row0_col0\" class=\"data row0 col0\" >1</td>\n",
" <td id=\"T_64485_row0_col1\" class=\"data row0 col1\" >2</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_64485_level0_row1\" class=\"row_heading level0 row1\" >1</th>\n",
" <td id=\"T_64485_row1_col0\" class=\"data row1 col0\" >True</td>\n",
" <td id=\"T_64485_row1_col1\" class=\"data row1 col1\" >False</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_64485_level0_row2\" class=\"row_heading level0 row2\" >2</th>\n",
" <td id=\"T_64485_row2_col0\" class=\"data row2 col0\" >a</td>\n",
" <td id=\"T_64485_row2_col1\" class=\"data row2 col1\" >b</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_64485_level0_row3\" class=\"row_heading level0 row3\" >3</th>\n",
" <td id=\"T_64485_row3_col0\" class=\"data row3 col0\" >{}</td>\n",
" <td id=\"T_64485_row3_col1\" class=\"data row3 col1\" >None</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n"
],
"text/plain": [
"<pandas.io.formats.style.Styler at 0x156f77010>"
"<pandas.io.formats.style.Styler at 0x107af2f20>"
]
},
"execution_count": 23,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.style.applymap(colour_code_type)"
"?[r, dist] := [r rt.distance dist]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<bound method Styler.applymap of <pandas.io.formats.style.Styler object at 0x156f76290>>"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.style.applymap"
"Besides `:after`, there is also the `:before` directive, running before any triple transactions. In the block itself, the function `assert()` can also be very useful in the body of rules. See the manual for more information."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You may have any number of `:put`, `:retract`, `:before` or `:after`. They can be interleaved. When a transaction is run, all `:before` blocks are run in the order they are defined, then all `:put` and `:retract`, and finally all `:after` blocks are run."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The real power of the transaction is that you can put anything in your `:before` and `:after` blocks, including puts or retracts to stored relations!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>a</td>\n",
" <td>b</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>{}</td>\n",
" <td>None</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 0 1\n",
"0 1 2\n",
"1 True False\n",
"2 a b\n",
"3 {} None"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": []
}
],

Loading…
Cancel
Save