// src/backend/concrete_simulation.hh - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2. If not, see http://www.gnu.org/licenses/.


#ifndef CONCRETE_SIMULATION_HH
#define CONCRETE_SIMULATION_HH 1


#include <sgpemv2/simulation.hh>
#include "concrete_history.hh"

#include <map>
#include <stdexcept>
#include <utility>
#include <vector>

namespace sgpem
{
  class ConcreteSimulation;

  class SG_DLLLOCAL ConcreteSimulation : public Simulation
  {
  public:
    ConcreteSimulation();

    /**
     * \brief Runs the simulation.
     *
     * Advances the simulation by one or more steps, depending on the
     * actual state and on the value set with set_mode().
     */
    void  run();

    /**
      \brief Pauses a running simulation.

      It is obviously useful only when the advancement mode is continue.
      Calling again run() will cause the simulation to start from the current
      simulation step.
     */
    void  pause();

    /**
      \brief Jumps the simulation to the specified instant
      
      Pauses the simulation and jumps to the specified instant
      \throw UserInterruptException, NullPolicyException, MalformedPolicyException
     */
		void jump_to(History::position p);

    /**
      \brief Stops the simulation.

      Behaves in the same way as pause(), except that the next call to run() 
      will cause the simulation to start from the beginning.
     */
    void  stop();

    /**
      \brief This methods allows to change the way the simulation progresses.

      If the input value is 0 (false), the simulation will advance a single time 
      step for each call to run().
      If the input value is 1 (true), the simulation will advance contiuosly, 
      waiting the time defined with set_timer() between each step, until all 
      processes have terminated, or some error happens. 
     */
    void  set_mode(mode new_mode);

    /**
      \return The simulation advancement mode: 0 if step-to-step, 1 if
      continue.
     */
    mode  get_mode() const;

    /** 
      \return The curent simulation state.
      \see Simulation::state
     */
    state get_state() const;

    /**
     \throw An instance of CPUPolicyException, \b not a derived class!!!
     */
    void set_policy(CPUPolicy*);

    /**
      \brief Setup the resource policy to be used by the system.
     */
    void set_resource_policy(ResourcePolicy*);

    /**
      \return A reference to the ConcreteHistory associated with this simulation.
     */
    ConcreteHistory& get_history();

    /**
      \return A const reference to the ConcreteHistory associated with this simulation.
     */
    const ConcreteHistory& get_history() const;

    /**
      \return The CPU policy currently in use.
     */
    CPUPolicy* get_policy();

    /**
      \return The resource policy currently in use.
     */
    ResourcePolicy * get_resource_policy();


  private:
    state  _state;
    mode   _mode;
    ConcreteHistory _history;
    CPUPolicy* _policy;
    ResourcePolicy* _resource_policy;

    bool step();
  };

}

#endif

