{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Modelverse Interfacing with external service (LoLA)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook, we will show how to use LoLA as an external service for Petri Net reachability analysis.\n",
    "LoLA will also return the path indicating how to reach the offending state, which we will also visualize as a model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initialization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we initialize the Modelverse as usual by importing the wrapper and logging in as some user (in our case the \"admin\" user)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append(\"../wrappers/\")\n",
    "from modelverse import *\n",
    "init()\n",
    "login(None, None)\n",
    "\n",
    "from IPython.display import SVG, display\n",
    "\n",
    "def view(model_name):\n",
    "    svg = SVG()\n",
    "    svg.data = show(model_name)\n",
    "    display(svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding metamodels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add the PetriNets metamodel and show it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg height=\"186\" width=\"989\"><marker id=\"triangle\" orient=\"auto\" refX=\"0.0\" refY=\"0.0\" style=\"overflow:visible;\"><path d=\"M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z \" id=\"path941\" style=\"fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1\" transform=\"scale(0.8) rotate(180) translate(12.5,0)\"/></marker><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"644\" x2=\"754.5\" y1=\"33\" y2=\"33.0\"/><line stroke=\"black\" x1=\"754.5\" x2=\"865\" y1=\"33.0\" y2=\"33\"/><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"865\" x2=\"754.5\" y1=\"33\" y2=\"33.0\"/><line stroke=\"black\" x1=\"754.5\" x2=\"644\" y1=\"33.0\" y2=\"33\"/><rect fill=\"white\" height=\"90\" stroke=\"black\" width=\"235\" x=\"3\" y=\"3\"/><rect fill=\"white\" height=\"90\" stroke=\"black\" width=\"226\" x=\"278\" y=\"3\"/><rect fill=\"white\" height=\"180\" stroke=\"black\" width=\"181\" x=\"544\" y=\"3\"/><rect fill=\"white\" height=\"165\" stroke=\"black\" width=\"181\" x=\"765\" y=\"3\"/><line stroke=\"black\" x1=\"3\" x2=\"238\" y1=\"33\" y2=\"33\"/><line stroke=\"black\" x1=\"3\" x2=\"238\" y1=\"23\" y2=\"23\"/><line stroke=\"black\" x1=\"278\" x2=\"504\" y1=\"33\" y2=\"33\"/><line stroke=\"black\" x1=\"278\" x2=\"504\" y1=\"23\" y2=\"23\"/><line stroke=\"black\" x1=\"544\" x2=\"725\" y1=\"78\" y2=\"78\"/><line stroke=\"black\" x1=\"544\" x2=\"725\" y1=\"23\" y2=\"23\"/><line stroke=\"black\" x1=\"765\" x2=\"946\" y1=\"63\" y2=\"63\"/><line stroke=\"black\" x1=\"765\" x2=\"946\" y1=\"23\" y2=\"23\"/><text fill=\"black\" x=\"13\" y=\"18\">Natural : SimpleAttribute</text><text fill=\"black\" x=\"13\" y=\"55.5\">(name)</text><text fill=\"black\" x=\"13\" y=\"70.5\">(constraint)</text><text fill=\"black\" x=\"288\" y=\"18\">String : SimpleAttribute</text><text fill=\"black\" x=\"288\" y=\"55.5\">(name)</text><text fill=\"black\" x=\"288\" y=\"70.5\">(constraint)</text><text fill=\"black\" x=\"554\" y=\"18\">Place : Class</text><text fill=\"black\" x=\"554\" y=\"36.75\">tokens : None</text><text fill=\"black\" x=\"554\" y=\"51.75\">name : None</text><text fill=\"black\" x=\"554\" y=\"100.5\">(lower_cardinality)</text><text fill=\"black\" x=\"554\" y=\"115.5\">(upper_cardinality)</text><text fill=\"black\" x=\"554\" y=\"130.5\">name = &quot;Place&quot;</text><text fill=\"black\" x=\"554\" y=\"145.5\">(abstract)</text><text fill=\"black\" x=\"554\" y=\"160.5\">(constraint)</text><text fill=\"black\" x=\"775\" y=\"18\">Transition : Class</text><text fill=\"black\" x=\"775\" y=\"36.75\">name : None</text><text fill=\"black\" x=\"775\" y=\"85.5\">(lower_cardinality)</text><text fill=\"black\" x=\"775\" y=\"100.5\">(upper_cardinality)</text><text fill=\"black\" x=\"775\" y=\"115.5\">name = &quot;Transition&quot;</text><text fill=\"black\" x=\"775\" y=\"130.5\">(abstract)</text><text fill=\"black\" x=\"775\" y=\"145.5\">(constraint)</text></svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "model_add(\"~/formalisms/PetriNets\", \"formalisms/SimpleClassDiagrams\", \"\"\"\n",
    "    SimpleAttribute Natural {}\n",
    "    SimpleAttribute String {}\n",
    "\n",
    "    Class Place {\n",
    "        name = \"Place\"\n",
    "        tokens : Natural\n",
    "        name : String\n",
    "    }\n",
    "    Class Transition {\n",
    "        name = \"Transition\"\n",
    "        name : String\n",
    "    }\n",
    "    Association P2T (Place, Transition) {\n",
    "        name = \"P2T\"\n",
    "        weight : Natural\n",
    "    }\n",
    "    Association T2P (Transition, Place) {\n",
    "        name = \"T2P\"\n",
    "        weight : Natural\n",
    "    }\n",
    "    \"\"\")\n",
    "view(\"~/formalisms/PetriNets\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add the Query metamodel and show it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg height=\"186\" width=\"786\"><marker id=\"triangle\" orient=\"auto\" refX=\"0.0\" refY=\"0.0\" style=\"overflow:visible;\"><path d=\"M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z \" id=\"path941\" style=\"fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1\" transform=\"scale(0.8) rotate(180) translate(12.5,0)\"/></marker><rect fill=\"white\" height=\"90\" stroke=\"black\" width=\"226\" x=\"3\" y=\"3\"/><rect fill=\"white\" height=\"90\" stroke=\"black\" width=\"235\" x=\"269\" y=\"3\"/><rect fill=\"white\" height=\"180\" stroke=\"black\" width=\"199\" x=\"544\" y=\"3\"/><line stroke=\"black\" x1=\"3\" x2=\"229\" y1=\"33\" y2=\"33\"/><line stroke=\"black\" x1=\"3\" x2=\"229\" y1=\"23\" y2=\"23\"/><line stroke=\"black\" x1=\"269\" x2=\"504\" y1=\"33\" y2=\"33\"/><line stroke=\"black\" x1=\"269\" x2=\"504\" y1=\"23\" y2=\"23\"/><line stroke=\"black\" x1=\"544\" x2=\"743\" y1=\"78\" y2=\"78\"/><line stroke=\"black\" x1=\"544\" x2=\"743\" y1=\"23\" y2=\"23\"/><text fill=\"black\" x=\"13\" y=\"18\">String : SimpleAttribute</text><text fill=\"black\" x=\"13\" y=\"55.5\">name = &quot;String&quot;</text><text fill=\"black\" x=\"13\" y=\"70.5\">(constraint)</text><text fill=\"black\" x=\"279\" y=\"18\">Natural : SimpleAttribute</text><text fill=\"black\" x=\"279\" y=\"55.5\">name = &quot;Natural&quot;</text><text fill=\"black\" x=\"279\" y=\"70.5\">(constraint)</text><text fill=\"black\" x=\"554\" y=\"18\">Place : Class</text><text fill=\"black\" x=\"554\" y=\"36.75\">name : String</text><text fill=\"black\" x=\"554\" y=\"51.75\">tokens : Natural</text><text fill=\"black\" x=\"554\" y=\"100.5\">lower_cardinality = 1</text><text fill=\"black\" x=\"554\" y=\"115.5\">upper_cardinality = 1</text><text fill=\"black\" x=\"554\" y=\"130.5\">name = &quot;Place&quot;</text><text fill=\"black\" x=\"554\" y=\"145.5\">(abstract)</text><text fill=\"black\" x=\"554\" y=\"160.5\">(constraint)</text></svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "model_add(\"~/formalisms/Query\", \"formalisms/SimpleClassDiagrams\", \"\"\"\n",
    "    include \"primitives.alh\"\n",
    "    include \"object_operations.alh\"\n",
    "    include \"modelling.alh\"\n",
    "\n",
    "    SimpleAttribute String {\n",
    "        name = \"String\"\n",
    "    }\n",
    "    SimpleAttribute Natural {\n",
    "        name = \"Natural\"\n",
    "    }\n",
    "\n",
    "    Class Place {\n",
    "        name = \"Place\"\n",
    "        name : String\n",
    "        tokens : Natural\n",
    "        lower_cardinality = 1\n",
    "        upper_cardinality = 1\n",
    "    }\n",
    "    \"\"\")\n",
    "view(\"~/formalisms/Query\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add the PetriNets execution path formalism and show it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg height=\"171\" width=\"732\"><marker id=\"triangle\" orient=\"auto\" refX=\"0.0\" refY=\"0.0\" style=\"overflow:visible;\"><path d=\"M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z \" id=\"path941\" style=\"fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1\" transform=\"scale(0.8) rotate(180) translate(12.5,0)\"/></marker><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"590\" x2=\"479.5\" y1=\"33\" y2=\"33.0\"/><line stroke=\"black\" x1=\"479.5\" x2=\"369\" y1=\"33.0\" y2=\"33\"/><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"369\" x2=\"369.0\" y1=\"33\" y2=\"33.0\"/><line stroke=\"black\" x1=\"369.0\" x2=\"369\" y1=\"33.0\" y2=\"33\"/><rect fill=\"white\" height=\"90\" stroke=\"black\" width=\"226\" x=\"3\" y=\"3\"/><rect fill=\"white\" height=\"165\" stroke=\"black\" width=\"181\" x=\"269\" y=\"3\"/><rect fill=\"white\" height=\"135\" stroke=\"black\" width=\"199\" x=\"490\" y=\"3\"/><line stroke=\"black\" x1=\"3\" x2=\"229\" y1=\"33\" y2=\"33\"/><line stroke=\"black\" x1=\"3\" x2=\"229\" y1=\"23\" y2=\"23\"/><line stroke=\"black\" x1=\"269\" x2=\"450\" y1=\"63\" y2=\"63\"/><line stroke=\"black\" x1=\"269\" x2=\"450\" y1=\"23\" y2=\"23\"/><line stroke=\"black\" x1=\"490\" x2=\"689\" y1=\"33\" y2=\"33\"/><line stroke=\"black\" x1=\"490\" x2=\"689\" y1=\"23\" y2=\"23\"/><text fill=\"black\" x=\"13\" y=\"18\">String : SimpleAttribute</text><text fill=\"black\" x=\"13\" y=\"55.5\">name = &quot;String&quot;</text><text fill=\"black\" x=\"13\" y=\"70.5\">(constraint)</text><text fill=\"black\" x=\"279\" y=\"18\">Entry : Class</text><text fill=\"black\" x=\"279\" y=\"36.75\">name : String</text><text fill=\"black\" x=\"279\" y=\"85.5\">(lower_cardinality)</text><text fill=\"black\" x=\"279\" y=\"100.5\">(upper_cardinality)</text><text fill=\"black\" x=\"279\" y=\"115.5\">name = &quot;Entry&quot;</text><text fill=\"black\" x=\"279\" y=\"130.5\">(abstract)</text><text fill=\"black\" x=\"279\" y=\"145.5\">(constraint)</text><text fill=\"black\" x=\"500\" y=\"18\">First : Class</text><text fill=\"black\" x=\"500\" y=\"55.5\">lower_cardinality = 1</text><text fill=\"black\" x=\"500\" y=\"70.5\">upper_cardinality = 1</text><text fill=\"black\" x=\"500\" y=\"85.5\">name = &quot;First&quot;</text><text fill=\"black\" x=\"500\" y=\"100.5\">(abstract)</text><text fill=\"black\" x=\"500\" y=\"115.5\">(constraint)</text></svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "model_add(\"~/formalisms/PNPath\", \"formalisms/SimpleClassDiagrams\", \"\"\"\n",
    "    SimpleAttribute String {\n",
    "        name = \"String\"\n",
    "    }\n",
    "    \n",
    "    Class Entry {\n",
    "        name = \"Entry\"\n",
    "        name : String\n",
    "    }\n",
    "    \n",
    "    Class First : Entry {\n",
    "        name = \"First\"\n",
    "        lower_cardinality = 1\n",
    "        upper_cardinality = 1\n",
    "    }\n",
    "    \n",
    "    Association Next (Entry, Entry){\n",
    "        source_upper_cardinality = 1\n",
    "        target_upper_cardinality = 1\n",
    "    }\n",
    "    \"\"\")\n",
    "view(\"~/formalisms/PNPath\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add the reachability analysis code, relying on the LoLA service.\n",
    "This code basically only has to serialize the PetriNets and Query model in LoLA format, and then forwards it.\n",
    "Upon receiving a response from the service, the reachable path is serialized to its own model, which can be used however the user wants."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "transformation_add_AL({\"PN\": \"~/formalisms/PetriNets\", \"Query\": \"~/formalisms/Query\"}, {\"Path\": \"~/formalisms/PNPath\"}, \"~/models/analyze_lola\", \"\"\"\n",
    "include \"primitives.alh\"\n",
    "include \"modelling.alh\"\n",
    "include \"object_operations.alh\"\n",
    "include \"services.alh\"\n",
    "\n",
    "Boolean function lola_format(model : Element):\n",
    "    Element all_places\n",
    "    String place\n",
    "    String name\n",
    "    Integer tokens\n",
    "    String transition\n",
    "    Element all_transitions\n",
    "    Element associations\n",
    "    Element association\n",
    "    String weight\n",
    "    String lola_net\n",
    "    String query\n",
    "    String place_output\n",
    "    String marking_output\n",
    "    String transition_output\n",
    "    String outp\n",
    "\n",
    "    all_places = allInstances(model, \"PN/Place\")\n",
    "    place_output = \"PLACE \"\n",
    "    marking_output = \"MARKING \"\n",
    "    while (set_len(all_places) > 0):\n",
    "        place = set_pop(all_places)\n",
    "        name = read_attribute(model, place, \"name\")\n",
    "        tokens = read_attribute(model, place, \"tokens\")\n",
    "\n",
    "        place_output = string_join(place_output, name)\n",
    "\n",
    "        if tokens > 0:\n",
    "            if marking_output != \"MARKING \":\n",
    "                marking_output = string_join(marking_output, \", \")\n",
    "            marking_output = string_join(marking_output, name)\n",
    "            marking_output = string_join(marking_output, \": \")\n",
    "            marking_output = string_join(marking_output, cast_string(tokens))\n",
    "    \n",
    "        if list_len(all_places) == 0:\n",
    "            place_output = string_join(place_output, \";\")\n",
    "            marking_output = string_join(marking_output, \";\")\n",
    "        else:\n",
    "            place_output = string_join(place_output, \", \")\n",
    "\n",
    "    lola_net = string_join(place_output, marking_output)\n",
    "\n",
    "    all_transitions = allInstances(model, \"PN/Transition\")\n",
    "    transition_output = \"\"\n",
    "    while (set_len(all_transitions) > 0):\n",
    "        transition = set_pop(all_transitions)\n",
    "        name = read_attribute(model, transition, \"name\")\n",
    "\n",
    "        transition_output = string_join(transition_output, \" TRANSITION \")\n",
    "        transition_output = string_join(transition_output, name)\n",
    "        transition_output = string_join(transition_output, \" CONSUME \")\n",
    "\n",
    "        associations = allIncomingAssociationInstances(model, transition, \"PN/P2T\")\n",
    "        while (set_len(associations) > 0):\n",
    "            association = set_pop(associations)\n",
    "            place = readAssociationSource(model, association)\n",
    "            weight = read_attribute(model, association, \"weight\")\n",
    "            if cast_integer(weight) > 0:\n",
    "                transition_output = string_join(transition_output, read_attribute(model, place, \"name\"))\n",
    "                transition_output = string_join(transition_output, \": \")\n",
    "                transition_output = string_join(transition_output, weight)\n",
    "\n",
    "            if list_len(associations) == 0:\n",
    "                transition_output = string_join(transition_output, \";\")\n",
    "            else:\n",
    "                transition_output = string_join(transition_output, \", \")\n",
    "\n",
    "        transition_output = string_join(transition_output, \" PRODUCE \")\n",
    "\n",
    "        associations = allOutgoingAssociationInstances(model, transition, \"PN/T2P\")\n",
    "        while (set_len(associations) > 0):\n",
    "            association = set_pop(associations)\n",
    "            place = readAssociationDestination(model, association)\n",
    "            weight = read_attribute(model, association, \"weight\")\n",
    "            if cast_integer(weight) > 0:\n",
    "                transition_output = string_join(transition_output, read_attribute(model, place, \"name\"))\n",
    "                transition_output = string_join(transition_output, \": \")\n",
    "                transition_output = string_join(transition_output, weight)\n",
    "\n",
    "            if list_len(associations) == 0:\n",
    "                transition_output = string_join(transition_output, \";\")\n",
    "            else:\n",
    "                transition_output = string_join(transition_output, \", \")\n",
    "\n",
    "    lola_net = string_join(lola_net, transition_output)\n",
    "\n",
    "    all_places = allInstances(model, \"Query/Place\")\n",
    "    if set_len(all_places) > 0:\n",
    "        place = set_pop(all_places)\n",
    "        name = read_attribute(model, place, \"name\")\n",
    "        tokens = read_attribute(model, place, \"tokens\")\n",
    "        query = string_join(name, ' = ')\n",
    "        query = string_join(query, cast_string(tokens))\n",
    "\n",
    "    outp = string_join(\"{\\\\\"petrinet\\\\\":\\\\\"\", lola_net)\n",
    "    outp = string_join(outp, \"\\\\\" , \\\\\"query\\\\\":\\\\\"\")\n",
    "    outp = string_join(outp, query)\n",
    "    outp = string_join(outp, \"\\\\\"}\")\n",
    "    \n",
    "    String port\n",
    "    port = comm_connect(\"lola\")\n",
    "    comm_set(port, outp)\n",
    "\n",
    "    String result\n",
    "    String path\n",
    "    result = comm_get(port)\n",
    "    path = comm_get(port)\n",
    "    \n",
    "    Element path_elements\n",
    "    String first\n",
    "    String elem\n",
    "    String prev\n",
    "    path_elements = string_split(path, \",\")\n",
    "    if (list_len(path_elements) > 0):\n",
    "        first = instantiate_node(model, \"Path/First\", \"\")\n",
    "        prev = first\n",
    "        instantiate_attribute(model, first, \"name\", list_pop(path_elements, 0))\n",
    "        while (list_len(path_elements) > 0):\n",
    "            elem = instantiate_node(model, \"Path/Entry\", \"\")\n",
    "            instantiate_attribute(model, elem, \"name\", list_pop(path_elements, 0))\n",
    "            instantiate_link(model, \"Path/Next\", \"\", prev, elem)\n",
    "            prev = elem\n",
    "    \n",
    "    comm_close(port)\n",
    "\n",
    "    return cast_boolean(result)!\n",
    "    \"\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Add models under test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add a simple Petri Nets model to test with."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg height=\"361\" width=\"1169\"><marker id=\"triangle\" orient=\"auto\" refX=\"0.0\" refY=\"0.0\" style=\"overflow:visible;\"><path d=\"M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z \" id=\"path941\" style=\"fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1\" transform=\"scale(0.8) rotate(180) translate(12.5,0)\"/></marker><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"103\" x2=\"524.5\" y1=\"33\" y2=\"33.0\"/><line stroke=\"black\" x1=\"524.5\" x2=\"946\" y1=\"33.0\" y2=\"33\"/><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"396\" x2=\"249.5\" y1=\"33\" y2=\"103.0\"/><line stroke=\"black\" x1=\"249.5\" x2=\"103\" y1=\"103.0\" y2=\"173\"/><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"689\" x2=\"556.0\" y1=\"33\" y2=\"103.0\"/><line stroke=\"black\" x1=\"556.0\" x2=\"423\" y1=\"103.0\" y2=\"173\"/><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"689\" x2=\"716.0\" y1=\"33\" y2=\"103.0\"/><line stroke=\"black\" x1=\"716.0\" x2=\"743\" y1=\"103.0\" y2=\"173\"/><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"946\" x2=\"817.5\" y1=\"33\" y2=\"33.0\"/><line stroke=\"black\" x1=\"817.5\" x2=\"689\" y1=\"33.0\" y2=\"33\"/><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"103\" x2=\"396.0\" y1=\"173\" y2=\"103.0\"/><line stroke=\"black\" x1=\"396.0\" x2=\"689\" y1=\"103.0\" y2=\"33\"/><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"423\" x2=\"263.0\" y1=\"173\" y2=\"103.0\"/><line stroke=\"black\" x1=\"263.0\" x2=\"103\" y1=\"103.0\" y2=\"33\"/><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"743\" x2=\"569.5\" y1=\"173\" y2=\"103.0\"/><line stroke=\"black\" x1=\"569.5\" x2=\"396\" y1=\"103.0\" y2=\"33\"/><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"103\" x2=\"103.0\" y1=\"33\" y2=\"165.5\"/><line stroke=\"black\" x1=\"103.0\" x2=\"103\" y1=\"165.5\" y2=\"298\"/><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"396\" x2=\"249.5\" y1=\"33\" y2=\"165.5\"/><line stroke=\"black\" x1=\"249.5\" x2=\"103\" y1=\"165.5\" y2=\"298\"/><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"103\" x2=\"583.0\" y1=\"298\" y2=\"235.5\"/><line stroke=\"black\" x1=\"583.0\" x2=\"1063\" y1=\"235.5\" y2=\"173\"/><rect fill=\"white\" height=\"90\" stroke=\"black\" width=\"253\" x=\"3\" y=\"3\"/><rect fill=\"white\" height=\"90\" stroke=\"black\" width=\"253\" x=\"296\" y=\"3\"/><rect fill=\"white\" height=\"90\" stroke=\"black\" width=\"217\" x=\"589\" y=\"3\"/><rect fill=\"white\" height=\"75\" stroke=\"black\" width=\"280\" x=\"846\" y=\"3\"/><rect fill=\"white\" height=\"75\" stroke=\"black\" width=\"280\" x=\"3\" y=\"143\"/><rect fill=\"white\" height=\"75\" stroke=\"black\" width=\"280\" x=\"323\" y=\"143\"/><rect fill=\"white\" height=\"75\" stroke=\"black\" width=\"280\" x=\"643\" y=\"143\"/><rect fill=\"white\" height=\"90\" stroke=\"black\" width=\"136\" x=\"963\" y=\"143\"/><rect fill=\"white\" height=\"75\" stroke=\"black\" width=\"199\" x=\"3\" y=\"268\"/><line stroke=\"black\" x1=\"3\" x2=\"256\" y1=\"33\" y2=\"33\"/><line stroke=\"black\" x1=\"3\" x2=\"256\" y1=\"23\" y2=\"23\"/><line stroke=\"black\" x1=\"296\" x2=\"549\" y1=\"33\" y2=\"33\"/><line stroke=\"black\" x1=\"296\" x2=\"549\" y1=\"23\" y2=\"23\"/><line stroke=\"black\" x1=\"589\" x2=\"806\" y1=\"33\" y2=\"33\"/><line stroke=\"black\" x1=\"589\" x2=\"806\" y1=\"23\" y2=\"23\"/><line stroke=\"black\" x1=\"846\" x2=\"1126\" y1=\"33\" y2=\"33\"/><line stroke=\"black\" x1=\"846\" x2=\"1126\" y1=\"23\" y2=\"23\"/><line stroke=\"black\" x1=\"3\" x2=\"283\" y1=\"173\" y2=\"173\"/><line stroke=\"black\" x1=\"3\" x2=\"283\" y1=\"163\" y2=\"163\"/><line stroke=\"black\" x1=\"323\" x2=\"603\" y1=\"173\" y2=\"173\"/><line stroke=\"black\" x1=\"323\" x2=\"603\" y1=\"163\" y2=\"163\"/><line stroke=\"black\" x1=\"643\" x2=\"923\" y1=\"173\" y2=\"173\"/><line stroke=\"black\" x1=\"643\" x2=\"923\" y1=\"163\" y2=\"163\"/><line stroke=\"black\" x1=\"963\" x2=\"1099\" y1=\"173\" y2=\"173\"/><line stroke=\"black\" x1=\"963\" x2=\"1099\" y1=\"163\" y2=\"163\"/><line stroke=\"black\" x1=\"3\" x2=\"202\" y1=\"298\" y2=\"298\"/><line stroke=\"black\" x1=\"3\" x2=\"202\" y1=\"288\" y2=\"288\"/><text fill=\"black\" x=\"13\" y=\"18\">critical_section_1 : Place</text><text fill=\"black\" x=\"13\" y=\"55.5\">tokens = 1</text><text fill=\"black\" x=\"13\" y=\"70.5\">name = &quot;critical_section_1&quot;</text><text fill=\"black\" x=\"306\" y=\"18\">critical_section_2 : Place</text><text fill=\"black\" x=\"306\" y=\"55.5\">tokens = 0</text><text fill=\"black\" x=\"306\" y=\"70.5\">name = &quot;critical_section_2&quot;</text><text fill=\"black\" x=\"599\" y=\"18\">lock_available : Place</text><text fill=\"black\" x=\"599\" y=\"55.5\">tokens = 1</text><text fill=\"black\" x=\"599\" y=\"70.5\">name = &quot;lock_available&quot;</text><text fill=\"black\" x=\"856\" y=\"18\">release_section_1 : Transition</text><text fill=\"black\" x=\"856\" y=\"55.5\">name = &quot;release_section_1&quot;</text><text fill=\"black\" x=\"13\" y=\"158\">release_section_2 : Transition</text><text fill=\"black\" x=\"13\" y=\"195.5\">name = &quot;release_section_2&quot;</text><text fill=\"black\" x=\"333\" y=\"158\">acquire_section_1 : Transition</text><text fill=\"black\" x=\"333\" y=\"195.5\">name = &quot;acquire_section_1&quot;</text><text fill=\"black\" x=\"653\" y=\"158\">acquire_section_2 : Transition</text><text fill=\"black\" x=\"653\" y=\"195.5\">name = &quot;acquire_section_2&quot;</text><text fill=\"black\" x=\"973\" y=\"158\">check : Place</text><text fill=\"black\" x=\"973\" y=\"195.5\">tokens = 0</text><text fill=\"black\" x=\"973\" y=\"210.5\">name = &quot;ERROR&quot;</text><text fill=\"black\" x=\"13\" y=\"283\">error : Transition</text><text fill=\"black\" x=\"13\" y=\"320.5\">name = &quot;both_enabled&quot;</text></svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "model_add(\"~/models/PN\", \"~/formalisms/PetriNets\", \"\"\"\n",
    "    Place critical_section_1 {\n",
    "        tokens = 1\n",
    "        name = \"critical_section_1\"\n",
    "    }\n",
    "    Place critical_section_2 {\n",
    "        tokens = 0\n",
    "        name = \"critical_section_2\"\n",
    "    }\n",
    "    Place lock_available {\n",
    "        tokens = 1\n",
    "        name = \"lock_available\"\n",
    "    }\n",
    "\n",
    "    Transition release_section_1 {\n",
    "        name = \"release_section_1\"\n",
    "    }\n",
    "    Transition release_section_2 {\n",
    "        name = \"release_section_2\"\n",
    "    }\n",
    "    Transition acquire_section_1 {\n",
    "        name = \"acquire_section_1\"\n",
    "    }\n",
    "    Transition acquire_section_2 {\n",
    "        name = \"acquire_section_2\"\n",
    "    }\n",
    "\n",
    "    P2T (critical_section_1, release_section_1) {\n",
    "        weight = 1\n",
    "    }\n",
    "\n",
    "    P2T (critical_section_2, release_section_2) {\n",
    "        weight = 1\n",
    "    }\n",
    "\n",
    "    P2T (lock_available, acquire_section_1) {\n",
    "        weight = 1\n",
    "    }\n",
    "\n",
    "    P2T (lock_available, acquire_section_2) {\n",
    "        weight = 1\n",
    "    }\n",
    "\n",
    "    T2P (release_section_1, lock_available) {\n",
    "        weight = 1\n",
    "    }\n",
    "\n",
    "    T2P (release_section_2, lock_available) {\n",
    "        weight = 1\n",
    "    }\n",
    "\n",
    "    T2P (acquire_section_1, critical_section_1) {\n",
    "        weight = 1\n",
    "    }\n",
    "\n",
    "    T2P (acquire_section_2, critical_section_2) {\n",
    "        weight = 1\n",
    "    }\n",
    "\n",
    "    Place check {\n",
    "        tokens = 0\n",
    "        name = \"ERROR\"\n",
    "    }\n",
    "    Transition error {\n",
    "        name = \"both_enabled\"\n",
    "    }\n",
    "    P2T (critical_section_1, error) {\n",
    "        weight = 1\n",
    "    }\n",
    "    P2T (critical_section_2, error) {\n",
    "        weight = 1\n",
    "    }\n",
    "    T2P (error, check) {\n",
    "        weight = 1\n",
    "    }\n",
    "    \"\"\")\n",
    "view(\"~/models/PN\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add a simple Query model to check whether it is reachable or not, and subsequently show it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg height=\"96\" width=\"182\"><marker id=\"triangle\" orient=\"auto\" refX=\"0.0\" refY=\"0.0\" style=\"overflow:visible;\"><path d=\"M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z \" id=\"path941\" style=\"fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1\" transform=\"scale(0.8) rotate(180) translate(12.5,0)\"/></marker><rect fill=\"white\" height=\"90\" stroke=\"black\" width=\"136\" x=\"3\" y=\"3\"/><line stroke=\"black\" x1=\"3\" x2=\"139\" y1=\"33\" y2=\"33\"/><line stroke=\"black\" x1=\"3\" x2=\"139\" y1=\"23\" y2=\"23\"/><text fill=\"black\" x=\"13\" y=\"18\">error : Place</text><text fill=\"black\" x=\"13\" y=\"55.5\">name = &quot;ERROR&quot;</text><text fill=\"black\" x=\"13\" y=\"70.5\">tokens = 1</text></svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "model_add(\"~/models/Query\", \"~/formalisms/Query\", \"\"\"\n",
    "    Place error {\n",
    "        name = \"ERROR\"\n",
    "        tokens = 1\n",
    "    }\n",
    "    \"\"\")\n",
    "view(\"~/models/Query\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Execution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we actually invoke the activity, thereby visualizing the error path if applicable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg height=\"81\" width=\"529\"><marker id=\"triangle\" orient=\"auto\" refX=\"0.0\" refY=\"0.0\" style=\"overflow:visible;\"><path d=\"M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z \" id=\"path941\" style=\"fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1\" transform=\"scale(0.8) rotate(180) translate(12.5,0)\"/></marker><line stroke=\"black\" style=\"marker-end:url(#triangle)\" x1=\"103\" x2=\"245.0\" y1=\"33\" y2=\"33.0\"/><line stroke=\"black\" x1=\"245.0\" x2=\"387\" y1=\"33.0\" y2=\"33\"/><rect fill=\"white\" height=\"75\" stroke=\"black\" width=\"244\" x=\"3\" y=\"3\"/><rect fill=\"white\" height=\"75\" stroke=\"black\" width=\"199\" x=\"287\" y=\"3\"/><line stroke=\"black\" x1=\"3\" x2=\"247\" y1=\"33\" y2=\"33\"/><line stroke=\"black\" x1=\"3\" x2=\"247\" y1=\"23\" y2=\"23\"/><line stroke=\"black\" x1=\"287\" x2=\"486\" y1=\"33\" y2=\"33\"/><line stroke=\"black\" x1=\"287\" x2=\"486\" y1=\"23\" y2=\"23\"/><text fill=\"black\" x=\"13\" y=\"18\">__2835739 : First</text><text fill=\"black\" x=\"13\" y=\"55.5\">name = &quot;acquire_section_2&quot;</text><text fill=\"black\" x=\"297\" y=\"18\">__2835810 : Entry</text><text fill=\"black\" x=\"297\" y=\"55.5\">name = &quot;both_enabled&quot;</text></svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "transformation_execute_AL(\"~/models/analyze_lola\", {\"PN\": \"~/models/PN\", \"Query\": \"~/models/Query\"}, {\"Path\": \"~/models/Path\"})\n",
    "view(\"~/models/Path\")"
   ]
  }
 ],
 "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
