{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import spot\n",
"import spot.ltsmin\n",
"# The following line causes the notebook to exit with 77 if spins is not \n",
"# installed, therefore skipping this test in the test suite.\n",
"spot.ltsmin.require('spins')\n",
"spot.setup()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are two ways to load a Promela model: from a file or from a cell. \n",
"\n",
"Loading from a cell\n",
"-------------------\n",
"\n",
"Using the `%%pml` magic will save the model in a temporary file, call `spins` to compile it, load the resulting shared library, and store the result into an object whose name is passed as an argument to `%%pml`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"SpinS Promela Compiler - version 1.1 (3-Feb-2015)\n",
"(C) University of Twente, Formal Methods and Tools group\n",
"\n",
"Parsing tmpfz39v5au.pml...\n",
"Parsing tmpfz39v5au.pml done (0.0 sec)\n",
"\n",
"Optimizing graphs...\n",
" StateMerging changed 0 states/transitions.\n",
" RemoveUselessActions changed 2 states/transitions.\n",
" RemoveUselessGotos changed 2 states/transitions.\n",
" RenumberAll changed 1 states/transitions.\n",
"Optimization done (0.0 sec)\n",
"\n",
"Generating next-state function ...\n",
" Instantiating processes\n",
" Statically binding references\n",
" Creating transitions\n",
"Generating next-state function done (0.0 sec)\n",
"\n",
"Creating state vector\n",
"Creating state labels\n",
"Generating transitions/state dependency matrices (2 / 3 slots) ... \n",
"\n",
" [.......... ]\n",
" [.................... ]\n",
" [.............................. ]\n",
" [........................................ ]\n",
" [..................................................]\n",
" Found 5 / 15 ( 33.3%) Guard/slot reads \n",
"\n",
" [......................... ]\n",
" [..................................................]\n",
" Found 6 / 6 (100.0%) Transition/slot tests \n",
"\n",
" [........ ]\n",
" [................ ]\n",
" [......................... ]\n",
" [................................. ]\n",
" [......................................... ]\n",
" [..................................................]\n",
" Found 2, 4, 4 / 18 ( 11.1%, 22.2%, 22.2%) Actions/slot r,W,w \n",
"\n",
" [......................... ]\n",
" [..................................................]\n",
" Found 2, 4, 4 / 6 ( 33.3%, 66.7%, 66.7%) Atomics/slot r,W,w \n",
"\n",
" [......................... ]\n",
" [..................................................]\n",
" Found 6, 4, 4 / 6 (100.0%, 66.7%, 66.7%) Transition/slot r,W,w \n",
"Generating transition/state dependency matrices done (0.0 sec)\n",
"\n",
"Generating guard dependency matrices (5 guards) ...\n",
"\n",
" [.... ]\n",
" [........ ]\n",
" [............ ]\n",
" [................ ]\n",
" [.................... ]\n",
" [......................... ]\n",
" [............................. ]\n",
" [................................. ]\n",
" [..................................... ]\n",
" [......................................... ]\n",
" Found 3 / 12 ( 25.0%) Guard/guard dependencies \n",
"\n",
" [..... ]\n",
" [.......... ]\n",
" [............... ]\n",
" [.................... ]\n",
" [......................... ]\n",
" [.............................. ]\n",
" [................................... ]\n",
" [........................................ ]\n",
" [............................................. ]\n",
" [..................................................]\n",
" Found 8 / 10 ( 80.0%) Transition/guard writes \n",
"\n",
" Found 4 / 4 (100.0%) Transition/transition writes \n",
"\n",
" [.... ]\n",
" [........ ]\n",
" [............ ]\n",
" [................ ]\n",
" [.................... ]\n",
" [......................... ]\n",
" [............................. ]\n",
" [................................. ]\n",
" [..................................... ]\n",
" [......................................... ]\n",
" Found 2 / 12 ( 16.7%) !MCE guards \n",
"\n",
" [......................... ]\n",
" Found 1 / 2 ( 50.0%) !MCE transitions \n",
"\n",
" [.. ]\n",
" [.... ]\n",
" [...... ]\n",
" [........ ]\n",
" [.......... ]\n",
" [............ ]\n",
" [.............. ]\n",
" [................ ]\n",
" [.................. ]\n",
" [.................... ]\n",
" [...................... ]\n",
" [........................ ]\n",
" [.......................... ]\n",
" [............................ ]\n",
" [.............................. ]\n",
" [................................ ]\n",
" [.................................. ]\n",
" [.................................... ]\n",
" [...................................... ]\n",
" [........................................ ]\n",
" [.......................................... ]\n",
" [............................................ ]\n",
" [.............................................. ]\n",
" [................................................ ]\n",
" [..................................................]\n",
" Found 7 / 25 ( 28.0%) !ICE guards \n",
"\n",
" [..... ]\n",
" [.......... ]\n",
" [............... ]\n",
" [.................... ]\n",
" [......................... ]\n",
" [.............................. ]\n",
" [................................... ]\n",
" [........................................ ]\n",
" [............................................. ]\n",
" [..................................................]\n",
" Found 10 / 10 (100.0%) !NES guards \n",
"\n",
" [............ ]\n",
" [......................... ]\n",
" [..................................... ]\n",
" [..................................................]\n",
" Found 4 / 4 (100.0%) !NES transitions \n",
"\n",
" [..... ]\n",
" [.......... ]\n",
" [............... ]\n",
" [.................... ]\n",
" [......................... ]\n",
" [.............................. ]\n",
" [................................... ]\n",
" [........................................ ]\n",
" [............................................. ]\n",
" [..................................................]\n",
" Found 8 / 10 ( 80.0%) !NDS guards \n",
"\n",
" [..... ]\n",
" [.......... ]\n",
" [............... ]\n",
" [.................... ]\n",
" [......................... ]\n",
" [.............................. ]\n",
" [................................... ]\n",
" [........................................ ]\n",
" [............................................. ]\n",
" [..................................................]\n",
" Found 0 / 10 ( 0.0%) MDS guards \n",
"\n",
" [..... ]\n",
" [.......... ]\n",
" [............... ]\n",
" [.................... ]\n",
" [......................... ]\n",
" [.............................. ]\n",
" [................................... ]\n",
" [........................................ ]\n",
" [............................................. ]\n",
" [..................................................]\n",
" Found 4 / 10 ( 40.0%) MES guards \n",
"\n",
" [............ ]\n",
" [......................... ]\n",
" [..................................... ]\n",
" [..................................................]\n",
" Found 0 / 4 ( 0.0%) !NDS transitions \n",
"\n",
" [......................... ]\n",
" Found 0 / 2 ( 0.0%) !DNA transitions \n",
"\n",
" [......................... ]\n",
" [..................................................]\n",
" [..................................................]\n",
" Found 2 / 2 (100.0%) Commuting actions \n",
"Generating guard dependency matrices done (0.0 sec)\n",
"\n",
"Written C code to /home/adl/git/spot/tests/python/tmpfz39v5au.pml.spins.c\n",
"Compiled C code to PINS library tmpfz39v5au.pml.spins\n",
"\n"
]
}
],
"source": [
"%%pml model\n",
"active proctype P() {\n",
"int a = 0;\n",
"int b = 0;\n",
"x: if\n",
" :: d_step {a < 3 && b < 3; a = a + 1; } goto x;\n",
" :: d_step {a < 3 && b < 3; b = b + 1; } goto x;\n",
"fi;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Yes, the `spins` compiler is quite verbose. Printing the resulting `model` object will show information about the variables in that model. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ltsmin model with the following variables:\n",
" P_0._pc: pc\n",
" P_0.a: int\n",
" P_0.b: int"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To instantiate a Kripke structure, one should provide a list of atomic proposition to observe."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
" *' at 0x7fb9a46cb540> >"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k = model.kripke(['P_0.a < 2', 'P_0.b > 1']); k"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And then from this Kripke structure you can do some model checking using the same functions as illustrated in `ltsmin-dve.ipynb`.\n",
"\n",
"Loading from a `*.pml` file\n",
"---------------------------\n",
"\n",
"Another option is to use `ltsmin.load()` to load a Promela file directly. In order for this test-case to be self-contained, we are going to write the Promela file first, but in practice you probably already have your model."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"!rm -rf test1.pml"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing test1.pml\n"
]
}
],
"source": [
"%%file test1.pml\n",
"active proctype P() {\n",
"int a = 0;\n",
"int b = 0;\n",
"x: if\n",
" :: d_step {a < 3 && b < 3; a = a + 1; } goto x;\n",
" :: d_step {a < 3 && b < 3; b = b + 1; } goto x;\n",
"fi;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now load it:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"model2 = spot.ltsmin.load('test1.pml')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ltsmin model with the following variables:\n",
" P_0._pc: pc\n",
" P_0.a: int\n",
" P_0.b: int"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model2"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"!rm -f test1.pml test1.pml.spins.c test1.pml.spins"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.4+"
}
},
"nbformat": 4,
"nbformat_minor": 2
}