{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dbac9fd1-6de0-4b42-bc80-9e0c98d7515a",
   "metadata": {},
   "source": [
    "# Characteristics\n",
    "\n",
    "MobsPy can also add characteristics to species and construct reactions based on these characteristics by a querying mechanism. In this example, we describe the characteristic mechanism in detail \n",
    "\n",
    "First we define 2 BaseSpecies:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5ed4381b-a079-44be-8183-21ca30e0adb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from mobspy import *\n",
    "\n",
    "Age, Color = BaseSpecies(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08d2ba60-b2e3-406f-91ef-5c212c0e0cd9",
   "metadata": {},
   "source": [
    "Characteristics can be added using the dot operator to a meta-species in the first time it is called. They can be added implicitly inside a reaction and explicitly outside a reaction. When added explicitly, the characteristics will automatically partake in the reaction. An example follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8a97e372-3f01-40f2-b3fc-c2309ba74efa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mobspy.modules.meta_class.Reactions at 0x7ff66a42be20>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Color.blue, Color.red, Color.yellow\n",
    "Age.young >> Age.old [1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbf5bfc3-cd90-492c-a5a2-a0f47d22f689",
   "metadata": {},
   "source": [
    "We next multiply Age and Color to create a Dummy meta-species. The new meta-species inherit the characteristics of Color and Age. It inherits them as a two-dimensional vector space with orthogonal characteristic sets.\n",
    "The Dummy meta-species represents all the possible combinations of characteristics from the meta-species it inherits from, separated by a dot. As an example, Dummy can be in the following states:\n",
    "\n",
    "Dummy.blue.young, \\\n",
    "Dummy.blue.old, \\\n",
    "Dummy.red.young, \\\n",
    "Dummy.red.old, \\\n",
    "Dummy.yellow.young, \\\n",
    "Dummy.yellow.old"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cf2fc33f-e028-4090-9f35-c3a8f6a6fc33",
   "metadata": {},
   "outputs": [],
   "source": [
    "Dummy = Age*Color"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71980e63-98e4-4190-a615-a243ec772079",
   "metadata": {},
   "source": [
    "One can query the meta-species that Dummy represents using characteristics.\n",
    "If the query is present on a reactant, it filters through the states that do not possess the requested characteristic.\n",
    "As an example, the meta-reaction:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "21118e41-f9f2-478b-bd73-9cf6ce5d4646",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mobspy.modules.meta_class.Reactions at 0x7ff66a42bb80>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dummy.old >> Zero [1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e019470-03f4-47c6-82e8-67f27501ab22",
   "metadata": {},
   "source": [
    "Defines the following reactions:\n",
    "\n",
    "Dummy.red.old -> \\\n",
    "Dummy.yellow.old -> \\\n",
    "Dummy.blue.old ->"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "366c3b80-97d9-44d3-8c07-0e7e1f4fb7a5",
   "metadata": {},
   "source": [
    "One can also perform a query in the products. When performed in the products, the query specifies in which space one wishes the transformation to occur. For instance, the meta-reaction: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "96ceda1e-c397-4e01-a815-bc7a262d2619",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mobspy.modules.meta_class.Reactions at 0x7ff66a42b9a0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dummy >> Dummy.blue [1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f294a057-6ab7-4b44-8b0f-7901c3f4deae",
   "metadata": {},
   "source": [
    "Defines the following reactions:\n",
    "\n",
    "Dummy.red.young -> Dummy.blue.young \\\n",
    "Dummy.red.old -> Dummy.blue.old \\\n",
    "Dummy.blue.young -> Dummy.blue.young \\\n",
    "Dummy.blue.old -> Dummy.blue.old \\\n",
    "Dummy.yellow.young -> Dummy.blue.young \\\n",
    "Dummy.yellow.old -> Dummy.blue.old\n",
    "\n",
    "Thus, by querying the blue characteristic in the products, one has specified that he wishes that the color characteristic transforms into blue.\n",
    "\n",
    "It is also possible to query over multiple characteristics at the same time, and the order is irrelevant.\n",
    "\n",
    "We finish by compiling the model to visualize all the reactions in detail:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4e3d95b8-ec1a-4969-87a0-93b39a421160",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Species\n",
      "Dummy.blue.old,0\n",
      "Dummy.blue.young,0\n",
      "Dummy.red.old,0\n",
      "Dummy.red.young,0\n",
      "Dummy.yellow.old,0\n",
      "Dummy.yellow.young,0\n",
      "\n",
      "Mappings\n",
      "Dummy :\n",
      "Dummy.blue.old\n",
      "Dummy.blue.young\n",
      "Dummy.red.old\n",
      "Dummy.red.young\n",
      "Dummy.yellow.old\n",
      "Dummy.yellow.young\n",
      "\n",
      "Parameters\n",
      "volume,1\n",
      "\n",
      "Reactions\n",
      "reaction_0,{'re': [(1, 'Dummy.blue.old')], 'pr': [(1, 'Dummy.blue.old')], 'kin': 'Dummy.blue.old * 1'}\n",
      "reaction_1,{'re': [(1, 'Dummy.blue.old')], 'pr': [], 'kin': 'Dummy.blue.old * 1'}\n",
      "reaction_2,{'re': [(1, 'Dummy.blue.young')], 'pr': [(1, 'Dummy.blue.old')], 'kin': 'Dummy.blue.young * 1'}\n",
      "reaction_3,{'re': [(1, 'Dummy.blue.young')], 'pr': [(1, 'Dummy.blue.young')], 'kin': 'Dummy.blue.young * 1'}\n",
      "reaction_4,{'re': [(1, 'Dummy.red.old')], 'pr': [(1, 'Dummy.blue.old')], 'kin': 'Dummy.red.old * 1'}\n",
      "reaction_5,{'re': [(1, 'Dummy.red.old')], 'pr': [], 'kin': 'Dummy.red.old * 1'}\n",
      "reaction_6,{'re': [(1, 'Dummy.red.young')], 'pr': [(1, 'Dummy.blue.young')], 'kin': 'Dummy.red.young * 1'}\n",
      "reaction_7,{'re': [(1, 'Dummy.red.young')], 'pr': [(1, 'Dummy.red.old')], 'kin': 'Dummy.red.young * 1'}\n",
      "reaction_8,{'re': [(1, 'Dummy.yellow.old')], 'pr': [(1, 'Dummy.blue.old')], 'kin': 'Dummy.yellow.old * 1'}\n",
      "reaction_9,{'re': [(1, 'Dummy.yellow.old')], 'pr': [], 'kin': 'Dummy.yellow.old * 1'}\n",
      "reaction_10,{'re': [(1, 'Dummy.yellow.young')], 'pr': [(1, 'Dummy.blue.young')], 'kin': 'Dummy.yellow.young * 1'}\n",
      "reaction_11,{'re': [(1, 'Dummy.yellow.young')], 'pr': [(1, 'Dummy.yellow.old')], 'kin': 'Dummy.yellow.young * 1'}\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n",
      "\u001b[91mWARNING: Automatic data-saving setup failed. Please save manually\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "S = Simulation(Dummy)\n",
    "print(S.compile())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f67b1bd-e81c-4f51-af2e-2319ea43bef6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
