{ "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.5.3" }, "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import spot\n", "spot.setup()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's build a small automaton to use as example." ] }, { "cell_type": "code", "collapsed": false, "input": [ "aut = spot.translate('G(Fa <-> XXb)'); aut" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "svg": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "0->1\n", "\n", "\n", "a\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "0->2\n", "\n", "\n", "!a\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "1->3\n", "\n", "\n", "a\n", "\n", "\n", "4\n", "\n", "4\n", "\n", "\n", "1->4\n", "\n", "\n", "!a\n", "\n", "\n", "5\n", "\n", "5\n", "\n", "\n", "1->5\n", "\n", "\n", "!a\n", "\n", "\n", "2->3\n", "\n", "\n", "a\n", "\n", "\n", "2->4\n", "\n", "\n", "!a\n", "\n", "\n", "6\n", "\n", "6\n", "\n", "\n", "2->6\n", "\n", "\n", "!a\n", "\n", "\n", "3->3\n", "\n", "\n", "a & b\n", "\u24ff\n", "\n", "\n", "3->4\n", "\n", "\n", "!a & b\n", "\n", "\n", "3->5\n", "\n", "\n", "!a & b\n", "\n", "\n", "4->3\n", "\n", "\n", "a & b\n", "\u24ff\n", "\n", "\n", "4->4\n", "\n", "\n", "!a & b\n", "\n", "\n", "5->6\n", "\n", "\n", "!a & b\n", "\n", "\n", "6->6\n", "\n", "\n", "!a & !b\n", "\u24ff\n", "\n", "\n", "\n" ], "text": [ " *' at 0x7fdf783f0510> >" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Build an accepting run:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "run = aut.accepting_run(); run" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "Prefix:\n", " 0\n", " | a\n", " 1\n", " | a\n", "Cycle:\n", " 3\n", " | a & b\t{0}" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accessing the contents of the run can be done via the `prefix` and `cycle` lists." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(spot.bdd_format_formula(aut.get_dict(), run.prefix[0].label))\n", "print(run.cycle[0].acc)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a\n", "{0}\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To convert the run into a word, using `spot.twa_word()`. Note that our runs are labeled by Boolean formulas that are not necessarily a conjunction of all involved litterals. The word is just the projection of the run on its labels." ] }, { "cell_type": "code", "collapsed": false, "input": [ "word = spot.twa_word(run); word" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "a; a; cycle{a & b}" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accessing the different formulas (stored as BDDs) can be done again via the `prefix` and `cycle` lists." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(spot.bdd_format_formula(aut.get_dict(), word.prefix[0]))\n", "print(spot.bdd_format_formula(aut.get_dict(), word.prefix[1]))\n", "print(spot.bdd_format_formula(aut.get_dict(), word.cycle[0]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a\n", "a\n", "a & b\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calling `simplifify()` will produce a shorter word that is compatible with the original word. For instance in the above word, the initial `a` is compatible with both `a & b` and `a & !b`. The word obtained by restricting `a` to `a & b` is therefore still accepted, allowing us to remove the prefix." ] }, { "cell_type": "code", "collapsed": false, "input": [ "word.simplify()\n", "word" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "cycle{a & b}" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Such a simplified word can be created directly from the automaton:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "aut.accepting_word()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "cycle{a & b}" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Words can be created using the `parse_word` function:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(spot.parse_word('a; b; cycle{a&b}'))\n", "print(spot.parse_word('cycle{a&bb|bac&(aaa|bbb)}'))\n", "print(spot.parse_word('a; b;b; qiwuei;\"a;b&c;a\" ;cycle{a}'))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a; b; cycle{a & b}\n", "cycle{(a & bb) | (aaa & bac) | (bac & bbb)}\n", "a; b; b; qiwuei; \"a;b&c;a\"; cycle{a}\n" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "# make sure that we can parse a word back after it has been printed\n", "spot.parse_word(str(spot.parse_word('a;b&a;cycle{!a&!b}')))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "a; a & b; cycle{!a & !b}" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Words can be easily converted as automata" ] }, { "cell_type": "code", "collapsed": false, "input": [ "w1 = spot.parse_word('a; !a; cycle{a; !a; a}')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "w1.as_automaton()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "svg": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "0->1\n", "\n", "\n", "a\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "1->2\n", "\n", "\n", "!a\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "2->3\n", "\n", "\n", "a\n", "\n", "\n", "4\n", "\n", "4\n", "\n", "\n", "3->4\n", "\n", "\n", "!a\n", "\n", "\n", "4->2\n", "\n", "\n", "a\n", "\n", "\n", "\n" ], "text": [ " *' at 0x7fdf783f0e40> >" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The rest of this pages tests some syntax errors, you (humans) may skip it, but the test suite will not." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(spot.parse_word('a; b&!a; b'))" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "\n>>> a; b&!a; b\n ^\nA twa_word must contain a cycle\n ()", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32munknown\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m \n>>> a; b&!a; b\n ^\nA twa_word must contain a cycle\n\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "print(spot.parse_word('a; b; c}'))" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "\n>>> a; b; c}\n ^\nExpected ';' delimiter: '}' stands for ending a cycle\n ()", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32munknown\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m \n>>> a; b; c}\n ^\nExpected ';' delimiter: '}' stands for ending a cycle\n\n" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "print(spot.parse_word('a; cycle{}'))" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "\n>>> a; cycle{}\n ^\nempty input\n\n ()", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32munknown\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m \n>>> a; cycle{}\n ^\nempty input\n\n\n" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "print(spot.parse_word('a; cycle{!a}; a'))" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "\n>>> a; cycle{!a}; a\n ^\nInput should be finished after cycle\n ()", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32munknown\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m \n>>> a; cycle{!a}; a\n ^\nInput should be finished after cycle\n\n" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "# Creating an empty word is OK...\n", "w = spot.twa_word(spot._bdd_dict)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "# ... as long as this word is not printed.\n", "print(w)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "RuntimeError", "evalue": "a twa_word may not have an empty cycle", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# ... as long as this word is not printed.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mw\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/home/adl/git/spot/python/spot/impl.py\u001b[0m in \u001b[0;36m__str__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 4160\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4161\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__str__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0;34m\"std::string\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 4162\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_impl\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtwa_word___str__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4163\u001b[0m \u001b[0mtwa_word_swigregister\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_impl\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtwa_word_swigregister\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4164\u001b[0m \u001b[0mtwa_word_swigregister\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtwa_word\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mRuntimeError\u001b[0m: a twa_word may not have an empty cycle" ] } ], "prompt_number": 18 } ], "metadata": {} } ] }