{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# OpenBugger Example"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/LAION-AI/Open-Assistant/blob/main/notebooks/openbugger/openbugger_example.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# First, we'll import the necessary libraries\n",
    "import os\n",
    "import subprocess"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## only run from OpenAssistant not if you alread cloned the github\n",
    "# Next, we'll define the function that will clone the OpenBugger repository and install it.\n",
    "def install_openbugger():\n",
    "    # First, we'll get the current working directory. This is where the repository will be cloned to.\n",
    "    cwd = os.getcwd()\n",
    "\n",
    "    # Next, we'll use Git to clone the repository.\n",
    "    subprocess.run([\"git\", \"clone\", \"https://github.com/furlat/OpenBugger\", cwd + \"/OpenBugger\"])\n",
    "\n",
    "    # Now, we'll use pip to install the package from the local repository.\n",
    "    subprocess.run([\"python3\", \"-m\", \"pip\", \"install\", \"--editable\", cwd + \"/OpenBugger\"])\n",
    "\n",
    "\n",
    "# Now, we'll call the function to install OpenBugger.\n",
    "install_openbugger()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Finally, we'll import SyntaxBug and LogicBug.\n",
    "from syntax.syntax_injector import SyntaxBug\n",
    "from logic.logic_injector import LogicBug"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# setup syntax bug\n",
    "syntax_bug = SyntaxBug()\n",
    "\n",
    "# Simple script\n",
    "simple_script = \"\"\"\n",
    "def greet(name):\n",
    "    print(\"Hello, \" + name)\n",
    "\n",
    "greet(\"Bob\")\n",
    "\"\"\"\n",
    "\n",
    "# The simple script can be modified using the \"easy\" injection method because it only contains simple syntax and does not have any nested code blocks. This means that there are fewer characters (e.g. quotes, brackets, braces, parenthesis) that could be the target of syntax errors, and the \"easy\" injection method, which only injects errors that involve replacing or removing a single character, is sufficient to modify the script.\n",
    "print(simple_script)\n",
    "# Inject easy syntax errors into the simple script\n",
    "\n",
    "modified_simple_script, errors, counter = syntax_bug.inject(simple_script, \"easy\", 1)\n",
    "print(\"Modified version Easy\", errors, counter)\n",
    "print(modified_simple_script)\n",
    "print(\"are they the same?\", simple_script == modified_simple_script)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Medium script\n",
    "medium_script = \"\"\"\n",
    "def greet(name):\n",
    "    print(\"Hello, \" + name)\n",
    "    \n",
    "def greet_all(names):\n",
    "    for name in names:\n",
    "        greet(name)\n",
    "        \n",
    "greet_all([\"Bob\", \"Alice\", \"Eve\"])\n",
    "\"\"\"\n",
    "# The medium script can be modified using the \"medium\" injection method because it contains a nested code block (the for loop in the `greet_all` function). This means that there are more characters (e.g. quotes, brackets, braces, parenthesis) that could be the target of syntax errors, and the \"medium\" injection method, which injects errors that involve replacing, removing, or adding a single character, is sufficient to modify the script.\n",
    "print(medium_script)\n",
    "# Inject medium syntax errors into the medium script\n",
    "modified_medium_script, errors, counter = syntax_bug.inject(medium_script, \"medium\", 3)\n",
    "print(\"Modified version Medium\", errors, counter)\n",
    "print(modified_medium_script)\n",
    "print(\"are they the same?\", medium_script == modified_medium_script)\n",
    "# Hard script\n",
    "hard_script = \"\"\"\n",
    "class Greeting:\n",
    "    def __init__(self, greeting):\n",
    "        self.greeting = greeting\n",
    "        \n",
    "    def greet(self, name):\n",
    "        print(self.greeting + \", \" + name)\n",
    "        \n",
    "greeting = Greeting(\"Hello\")\n",
    "greeting.greet(\"Bob\")\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Hard script\n",
    "hard_script = \"\"\"\n",
    "class Greeting:\n",
    "    def __init__(self, greeting):\n",
    "        self.greeting = greeting\n",
    "        \n",
    "    def greet(self, name):\n",
    "        print(self.greeting + \", \" + name)\n",
    "        \n",
    "greeting = Greeting(\"Hello\")\n",
    "greeting.greet(\"Bob\")\n",
    "\"\"\"\n",
    "\n",
    "# The hard script can be modified using the \"hard\" injection method because it contains multiple nested code blocks (the `__init__` and `greet` methods in the `Greeting` class). This means that there are even more characters (e.g. quotes, brackets, braces, parenthesis) that could be the target of syntax errors, and the \"hard\" injection method, which injects errors that involve replacing, removing, adding, or swapping characters, is sufficient to modify the script.\n",
    "print(hard_script)\n",
    "# Inject hard syntax errors into the hard script\n",
    "modified_hard_script, errors, counter = syntax_bug.inject(hard_script, \"hard\", 3)\n",
    "print(\"Modified version Hard\", errors, counter)\n",
    "print(modified_hard_script)\n",
    "print(\"are they the same?\", hard_script == modified_hard_script)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we switch to testing the LogicBugs and show how to bug a function that is defined in the script without already having the string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import inspect\n",
    "import random\n",
    "\n",
    "# Simple example script\n",
    "\n",
    "\n",
    "def simple_script():\n",
    "    # Choose two random integers\n",
    "    num1 = random.randint(0, 10)\n",
    "    num2 = random.randint(0, 10)\n",
    "\n",
    "    # Compare the two numbers and print a message based on their relation\n",
    "    if num1 > num2:\n",
    "        print(\"num1 is greater than num2\")\n",
    "    elif num1 < num2:\n",
    "        print(\"num1 is less than num2\")\n",
    "    else:\n",
    "        print(\"num1 is equal to num2\")\n",
    "\n",
    "\n",
    "# Medium example script\n",
    "def medium_script():\n",
    "    # Choose a random integer and assign it to a variable\n",
    "    num = random.randint(0, 10)\n",
    "\n",
    "    # Use a loop to print all numbers from 0 to the chosen integer\n",
    "    for i in range(num):\n",
    "        print(i)\n",
    "\n",
    "\n",
    "# Hard example script\n",
    "def hard_script():\n",
    "    # Choose a random integer and assign it to a variable\n",
    "    num = random.randint(0, 10)\n",
    "\n",
    "    # Use a loop to print the square of all numbers from 0 to the chosen integer\n",
    "    for i in range(num):\n",
    "        print(i**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create an instance of the LogicBug class\n",
    "logic_bug = LogicBug()\n",
    "\n",
    "# get the source code of the simple_script function as a string\n",
    "simple_script_str = inspect.getsource(simple_script)\n",
    "print(\"Simple\", simple_script_str)\n",
    "# inject a logic error into the simple_script function\n",
    "modified_simple_script, error, counter = logic_bug.inject(simple_script_str, \"easy\", num_errors=3)\n",
    "print(\"Modified version Simple\", error, counter)\n",
    "# print the modified simple_script function\n",
    "print(modified_simple_script)\n",
    "print(\"are they the same?\", simple_script_str == modified_simple_script)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get the source code of the medium_script function as a string\n",
    "medium_script_str = inspect.getsource(medium_script)\n",
    "print(\"Medium\", medium_script_str)\n",
    "# inject a logic error into the medium_script function\n",
    "modified_medium_script, error, counter = logic_bug.inject(medium_script_str, \"medium\", num_errors=3)\n",
    "\n",
    "# print the modified medium_script function\n",
    "print(\"Modified version Medium\", error, counter)\n",
    "print(modified_medium_script)\n",
    "print(\"are they the same?\", medium_script_str == modified_medium_script)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get the source code of the hard_script function as a string\n",
    "hard_script_str = inspect.getsource(hard_script)\n",
    "print(\"Hard\", hard_script_str)\n",
    "# inject a logic error into the hard_script function\n",
    "modified_hard_script, error, counter = logic_bug.inject(hard_script_str, \"hard\", num_errors=1)\n",
    "print(\"Modified version Hard\", error, counter)\n",
    "# print the modified hard_script function\n",
    "print(modified_hard_script)\n",
    "print(\"are they the same?\", hard_script_str == modified_hard_script)"
   ]
  }
 ],
 "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.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "25d5c2324055587ceaeef27650c79ce8358ea61d7689f2e0b8ada5d53f85bce4"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
