diff --git a/notebooks/manual/ExternalFunctions.ipynb b/notebooks/manual/ExternalFunctions.ipynb index 42260be5e612f232c912b5b55f90b1315bc961c3..670463d03d423323e56828a0d6e07925a5cc0441 100644 --- a/notebooks/manual/ExternalFunctions.ipynb +++ b/notebooks/manual/ExternalFunctions.ipynb @@ -2433,6 +2433,119 @@ "XML documents can either be stored in a file or in a B string." ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2018-05-14 10:43:17,767, T+2118174] \"Shell-0\" de.prob.cli.PrologProcessProvider.makeProcess(PrologProcessProvider.java:64): [INFO] Starting ProB's Prolog Core. Path is /Users/leuschel/.prob/prob2-3.2.10-SNAPSHOT/probcli.sh\n", + "[2018-05-14 10:43:19,119, T+2119526] \"Shell-0\" de.prob.cli.PortPattern.setValue(PortPattern.java:30): [INFO] Server has started and listens on port 60266\n", + "[2018-05-14 10:43:19,120, T+2119527] \"Shell-0\" de.prob.cli.InterruptRefPattern.setValue(InterruptRefPattern.java:29): [INFO] Server can receive user interrupts via reference 71649\n", + "[2018-05-14 10:43:19,132, T+2119539] \"ProB Output Logger for instance 2fda6747\" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] -- starting command loop --\u001b[0m\n", + "[2018-05-14 10:43:19,145, T+2119552] \"ProB Output Logger for instance 2fda6747\" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] Connected: 127.0.0.1\u001b[0m\n", + "[2018-05-14 10:43:19,271, T+2119678] \"ProB Output Logger for instance 2fda6747\" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] loading_classical_b(parser_version(2018-04-11 12:07:37.302),JupyterLibraryXML,[/Users/leuschel/git_root/JAVAPROB/prob2-jupyter-kernel/notebooks/manual])\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "Loaded machine: JupyterLibraryXML : []\n" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "::load\n", + "MACHINE JupyterLibraryXML\n", + "DEFINITIONS\n", + " XML_ELement_Type == \n", + " struct(\n", + " recId: NATURAL1,\n", + " pId:NATURAL,\n", + " element:STRING,\n", + " attributes: STRING +-> STRING,\n", + " meta: STRING +-> STRING\n", + " );\n", + " EXTERNAL_FUNCTION_READ_XML == (STRING * STRING) --> seq(XML_ELement_Type);\n", + " READ_XML(file,encoding) == {}; \n", + " // encoding can be \"auto\", \"ISO-8859-1\", \"ISO-8859-2\", \"ISO-8859-15\", \"UTF-8\", \"UTF-16\",\n", + " // \"UTF-16LE\",\"UTF-16BE\",\"UTF-32\",\"UTF-32LE\",\"UTF-32BE\", \"ANSI_X3.4-1968\", \"windows 1252\"\n", + " \n", + " EXTERNAL_FUNCTION_READ_XML_FROM_STRING == STRING --> seq(XML_ELement_Type);\n", + " READ_XML_FROM_STRING(contents) == {};\n", + " \n", + " EXTERNAL_FUNCTION_WRITE_XML_TO_STRING == seq(XML_ELement_Type) --> STRING;\n", + " WRITE_XML_TO_STRING(contents) == \"<?xml ...?>\";\n", + " \n", + " EXTERNAL_PREDICATE_WRITE_XML == seq(XML_ELement_Type) * STRING;\n", + " WRITE_XML(contents,file) == (1=1)\n", + " END" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### READ_XML_FROM_STRING\n", + "This external function takes an XML document string and converts into into the B format seq(XML_ELement_Type)}.\n", + "Note that all strings in ProB are encoded using UTF-8, so no encoding argument has to be provided." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2018-05-14 10:44:22,117, T+2182524] \"ProB Output Logger for instance 2fda6747\" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] % Walltime 10 ms to parse and convert XML\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "{(1↦rec(attributes∈{(\"version\"↦\"0.1\")},element∈\"Data\",meta∈{(\"xmlLineNumber\"↦\"3\")},pId∈0,recId∈1)),(2↦rec(attributes∈{(\"attr1\"↦\"value1\"),(\"elemID\"↦\"ID1\")},element∈\"Tag1\",meta∈{(\"xmlLineNumber\"↦\"4\")},pId∈1,recId∈2))}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "READ_XML_FROM_STRING('''\n", + "<?xml version=\"1.0\" encoding=\"ASCII\"?>\n", + " <Data version= \"0.1\">\n", + " <Tag1 elemID=\"ID1\" attr1=\"value1\" />\n", + " </Data>\n", + "''')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### READ_XML\n", + "\n", + "This external function can read in an XML document from file. In contrast to READ_XML_FROM_STRING\n", + "it also takes a second argument specifying the encoding used.\n", + "ProB cannot as of now detect the encoding from the XML header.\n", + "In future this argument may be removed.\n", + "Currently it can take these values:\n", + "\"auto\",\"ISO-8859-1\",\"ISO-8859-2\",\"ISO-8859-15\",\n", + " \"UTF-8\",\"UTF-16\",\"UTF-16LE\",\"UTF-16BE\",\"UTF-32\",\"UTF-32LE\",\"UTF-32BE\",\n", + " \"ANSI\\_X3.4-1968\", \"windows 1252\"." + ] + }, { "cell_type": "code", "execution_count": null,