{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PythonPDEVS examples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import PythonPDEVS modelling elements for *AtomicDEVS* and *CoupledDEVS* models.\n",
    "Also import the concept of *INFINITY* to be used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pypdevs.DEVS import AtomicDEVS, CoupledDEVS\n",
    "from pypdevs.infinity import INFINITY"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create some simple *AtomicDEVS* models for a *Generator* and a *Queue*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Generator(AtomicDEVS):\n",
    "    def __init__(self):\n",
    "        AtomicDEVS.__init__(self, \"Generator\")\n",
    "        self.state = 0\n",
    "        self.outport = self.addOutPort(\"outport\")\n",
    "\n",
    "    def timeAdvance(self):\n",
    "        return 1.0\n",
    "\n",
    "    def outputFnc(self):\n",
    "        # Our message is simply the integer 5, though this could be anything\n",
    "        return {self.outport: 5}\n",
    "\n",
    "    def intTransition(self):\n",
    "        return self.state + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Queue(AtomicDEVS):\n",
    "    def __init__(self):\n",
    "        AtomicDEVS.__init__(self, \"Queue\")\n",
    "        self.state = None\n",
    "        self.processing_time = 1.0\n",
    "        self.inport = self.addInPort(\"input\")\n",
    "        self.outport = self.addOutPort(\"output\")\n",
    "\n",
    "    def timeAdvance(self):\n",
    "        if self.state is None:\n",
    "            return INFINITY\n",
    "        else:\n",
    "            return self.processing_time\n",
    "\n",
    "    def outputFnc(self):\n",
    "        return {self.outport: self.state}\n",
    "\n",
    "    def extTransition(self, inputs):\n",
    "        self.state = inputs[self.inport]\n",
    "        return self.state\n",
    "\n",
    "    def intTransition(self):\n",
    "        return None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add the Coupled DEVS model for a simple *CoupledQueue*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "class CQueue(CoupledDEVS):\n",
    "    def __init__(self):\n",
    "        CoupledDEVS.__init__(self, \"CQueue\")\n",
    "        self.generator = self.addSubModel(Generator())\n",
    "        self.queue = self.addSubModel(Queue())\n",
    "        self.connectPorts(self.generator.outport, self.queue.inport)\n",
    "    \n",
    "    def select(self, imm):\n",
    "        return self.queue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally run the simulation by setting up an experiment file as follows.\n",
    "First, however, we have to include the simulator concept."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pypdevs.simulator import Simulator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = CQueue()\n",
    "sim = Simulator(model)\n",
    "# Required to set Classic DEVS, as we simulate in Parallel DEVS otherwise\n",
    "sim.setClassicDEVS()\n",
    "sim.setTerminationTime(10.0)\n",
    "sim.simulate()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Simulation is now done, and the results can be seen in the changed states of the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.generator.state"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Another option is to use verbose simulation, as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "__  Current Time:       0.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tINITIAL CONDITIONS in model <CQueue.Generator>\n",
      "\t\tInitial State: 0\n",
      "\t\tNext scheduled internal transition at time 1.00\n",
      "\n",
      "\n",
      "\tINITIAL CONDITIONS in model <CQueue.Queue>\n",
      "\t\tInitial State: None\n",
      "\t\tNext scheduled internal transition at time inf\n",
      "\n",
      "\n",
      "__  Current Time:       1.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tEXTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>:\n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tNext scheduled internal transition at time 2.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 1\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 2.00\n",
      "\n",
      "\n",
      "__  Current Time:       2.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tNew State: None\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time inf\n",
      "\n",
      "\n",
      "__  Current Time:       2.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tEXTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>:\n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tNext scheduled internal transition at time 3.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 2\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 3.00\n",
      "\n",
      "\n",
      "__  Current Time:       3.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tNew State: None\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time inf\n",
      "\n",
      "\n",
      "__  Current Time:       3.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tEXTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>:\n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tNext scheduled internal transition at time 4.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 3\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 4.00\n",
      "\n",
      "\n",
      "__  Current Time:       4.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tNew State: None\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time inf\n",
      "\n",
      "\n",
      "__  Current Time:       4.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tEXTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>:\n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tNext scheduled internal transition at time 5.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 4\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 5.00\n",
      "\n",
      "\n",
      "__  Current Time:       5.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tNew State: None\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time inf\n",
      "\n",
      "\n",
      "__  Current Time:       5.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tEXTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>:\n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tNext scheduled internal transition at time 6.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 6.00\n",
      "\n",
      "\n",
      "__  Current Time:       6.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tNew State: None\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time inf\n",
      "\n",
      "\n",
      "__  Current Time:       6.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tEXTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>:\n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tNext scheduled internal transition at time 7.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 6\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 7.00\n",
      "\n",
      "\n",
      "__  Current Time:       7.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tNew State: None\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time inf\n",
      "\n",
      "\n",
      "__  Current Time:       7.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tEXTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>:\n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tNext scheduled internal transition at time 8.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 7\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 8.00\n",
      "\n",
      "\n",
      "__  Current Time:       8.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tNew State: None\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time inf\n",
      "\n",
      "\n",
      "__  Current Time:       8.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tEXTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>:\n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tNext scheduled internal transition at time 9.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 8\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 9.00\n",
      "\n",
      "\n",
      "__  Current Time:       9.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tNew State: None\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time inf\n",
      "\n",
      "\n",
      "__  Current Time:       9.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tEXTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>:\n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tNext scheduled internal transition at time 10.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 9\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 10.00\n",
      "\n",
      "\n",
      "__  Current Time:      10.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tNew State: None\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time inf\n",
      "\n",
      "\n",
      "__  Current Time:      10.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tEXTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>:\n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tNext scheduled internal transition at time 11.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 10\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 11.00\n",
      "\n"
     ]
    }
   ],
   "source": [
    "model = CQueue()\n",
    "sim = Simulator(model)\n",
    "sim.setVerbose()\n",
    "# Required to set Classic DEVS, as we simulate in Parallel DEVS otherwise\n",
    "sim.setClassicDEVS()\n",
    "sim.setTerminationTime(10.0)\n",
    "sim.simulate()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Apart from Classic DEVS simulation, Parallel DEVS simulation is also possible.bag\n",
    "For this, we add a slightly altered version of the models, to comply with bag semantics."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Generator(AtomicDEVS):\n",
    "    def __init__(self):\n",
    "        AtomicDEVS.__init__(self, \"Generator\")\n",
    "        self.state = 0\n",
    "        self.outport = self.addOutPort(\"outport\")\n",
    "\n",
    "    def timeAdvance(self):\n",
    "        return 1.0\n",
    "\n",
    "    def outputFnc(self):\n",
    "        # Our message is simply the integer 5, though this could be anything\n",
    "        return {self.outport: [5]}\n",
    "\n",
    "    def intTransition(self):\n",
    "        return self.state + 1\n",
    "    \n",
    "class Queue(AtomicDEVS):\n",
    "    def __init__(self):\n",
    "        AtomicDEVS.__init__(self, \"Queue\")\n",
    "        self.state = None\n",
    "        self.processing_time = 1.0\n",
    "        self.inport = self.addInPort(\"input\")\n",
    "        self.outport = self.addOutPort(\"output\")\n",
    "\n",
    "    def timeAdvance(self):\n",
    "        if self.state is None:\n",
    "            return INFINITY\n",
    "        else:\n",
    "            return self.processing_time\n",
    "\n",
    "    def outputFnc(self):\n",
    "        return {self.outport: [self.state]}\n",
    "\n",
    "    def extTransition(self, inputs):\n",
    "        self.state = inputs[self.inport][0]\n",
    "        return self.state\n",
    "\n",
    "    def intTransition(self):\n",
    "        self.state = None\n",
    "        return self.state"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And similar to before, we start the experiment, now not using the *setClassicDEVS* option."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "__  Current Time:       0.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tINITIAL CONDITIONS in model <CQueue.Generator>\n",
      "\t\tInitial State: 0\n",
      "\t\tNext scheduled internal transition at time 1.00\n",
      "\n",
      "\n",
      "\tINITIAL CONDITIONS in model <CQueue.Queue>\n",
      "\t\tInitial State: None\n",
      "\t\tNext scheduled internal transition at time inf\n",
      "\n",
      "\n",
      "__  Current Time:       1.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tEXTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>:\n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tNext scheduled internal transition at time 2.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 1\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 2.00\n",
      "\n",
      "\n",
      "__  Current Time:       2.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 3.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 2\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 3.00\n",
      "\n",
      "\n",
      "__  Current Time:       3.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 4.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 3\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 4.00\n",
      "\n",
      "\n",
      "__  Current Time:       4.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 5.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 4\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 5.00\n",
      "\n",
      "\n",
      "__  Current Time:       5.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 6.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 6.00\n",
      "\n",
      "\n",
      "__  Current Time:       6.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 7.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 6\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 7.00\n",
      "\n",
      "\n",
      "__  Current Time:       7.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 8.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 7\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 8.00\n",
      "\n",
      "\n",
      "__  Current Time:       8.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 9.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 8\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 9.00\n",
      "\n",
      "\n",
      "__  Current Time:       9.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 10.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 9\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 10.00\n",
      "\n",
      "\n",
      "__  Current Time:      10.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 11.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 10\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 11.00\n",
      "\n"
     ]
    }
   ],
   "source": [
    "model = CQueue()\n",
    "sim = Simulator(model)\n",
    "sim.setVerbose()\n",
    "sim.setTerminationTime(10.0)\n",
    "sim.simulate()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A termination condition can also be used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def termFunc(clock, model):\n",
    "    if model.generator.state > 5:\n",
    "        # The generator has generated more than 5 events\n",
    "        # So stop\n",
    "        return True\n",
    "    elif clock[0] > 10:\n",
    "        # Or if the clock has progressed past simulation time 10\n",
    "        return True\n",
    "    else:\n",
    "        # Otherwise, we simply continue\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "__  Current Time:       0.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tINITIAL CONDITIONS in model <CQueue.Generator>\n",
      "\t\tInitial State: 0\n",
      "\t\tNext scheduled internal transition at time 1.00\n",
      "\n",
      "\n",
      "\tINITIAL CONDITIONS in model <CQueue.Queue>\n",
      "\t\tInitial State: None\n",
      "\t\tNext scheduled internal transition at time inf\n",
      "\n",
      "\n",
      "__  Current Time:       1.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tEXTERNAL TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>:\n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tNext scheduled internal transition at time 2.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 1\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 2.00\n",
      "\n",
      "\n",
      "__  Current Time:       2.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 3.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 2\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 3.00\n",
      "\n",
      "\n",
      "__  Current Time:       3.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 4.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 3\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 4.00\n",
      "\n",
      "\n",
      "__  Current Time:       4.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 5.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 4\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 5.00\n",
      "\n",
      "\n",
      "__  Current Time:       5.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 6.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 6.00\n",
      "\n",
      "\n",
      "__  Current Time:       6.00 __________________________________________ \n",
      "\n",
      "\n",
      "\tCONFLUENT TRANSITION in model <CQueue.Queue>\n",
      "\t\tInput Port Configuration:\n",
      "\t\t\tport <input>: \n",
      "\t\t\t\t5\n",
      "\t\tNew State: 5\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <output>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 7.00\n",
      "\n",
      "\n",
      "\tINTERNAL TRANSITION in model <CQueue.Generator>\n",
      "\t\tNew State: 6\n",
      "\t\tOutput Port Configuration:\n",
      "\t\t\tport <outport>:\n",
      "\t\t\t\t5\n",
      "\t\tNext scheduled internal transition at time 7.00\n",
      "\n"
     ]
    }
   ],
   "source": [
    "model = CQueue()\n",
    "sim = Simulator(model)\n",
    "sim.setVerbose()\n",
    "sim.setTerminationCondition(termFunc)\n",
    "sim.simulate()"
   ]
  }
 ],
 "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
