- Added some source code

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@304 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
fpaparel 2006-02-09 18:51:26 +00:00
parent d56fd53f2d
commit e8b28cc2ec
15 changed files with 486 additions and 33 deletions

View File

@ -58,6 +58,7 @@ noinst_HEADERS = \
iomanager.hh \
main.hh \
mainwindow.hh \
observer.hh \
startgui.hh \
graphicalterminalio.hh \
parseopts.hh

View File

@ -47,13 +47,18 @@ libbackend_la_LDFLAGS = $(PYTHON_EXTRA_LDFLAGS) \
# Please keep this in sorted order:
libbackend_la_SOURCES = \
history.cc \
observedSubject.cc \
process.cc \
schedulable.cc \
schedulableStatus.cc \
simulationStatus.cc \
slice.cc
noinst_HEADERS = \
history.hh \
observedSubject.hh \
process.hh \
schedulable.hh \
schedulableStatus.hh \

127
src/backend/history.cc Normal file
View File

@ -0,0 +1,127 @@
// src/backend/history.cc - 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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "history.hh"
using namespace std;
using namespace sgpem;
using namespace memory;
//History::instance; //static object
History History::_instance(10); //dummy parameter
History::History(int) //private constructor. The parameter is discarded
:_total_time_elapsed(0)
{
}
History&
History::getInstance()
{
return _instance;
}
/**
Returns a pointer to a copy of the SchedulableStatus object relative to this instant.
It can be NULL if time is out of range or if the SimulationStatus object relative to
this instant has no running entities.
*/
smart_ptr<const SchedulableStatus>
History::getScheduledAt (int time)
{
if (time >= _total_time_elapsed || time < 0) //out of range
return smart_ptr<const SchedulableStatus>(NULL);
return getSimulationStatusAt(time)->getRunning();
}
/**
Returns a pointer to a copy of the SimulationStatus object relative to this instant or NULL
if time is out of range.
*/
smart_ptr<const SimulationStatus>
History::getSimulationStatusAt (int time)
{
if (time >= _total_time_elapsed || time < 0) //out of range
return smart_ptr<const SimulationStatus>(NULL);
int trascorso = 0;
for(vector<Slice>::iterator i=_slices.begin(); i < _slices.end(); i++)
if (time < trascorso + i->getDuration()) //FOUND!!
return smart_ptr<const SimulationStatus>(new SimulationStatus(i->getSimulationStatus()));
else //Go on...
trascorso += i->getDuration();
//never reached
return smart_ptr<const SimulationStatus>(NULL);
}
int
History::getCurrentTime()
{
return _total_time_elapsed;
}
/**
Appends to the history a SimulationStatus creating a Slice with duration of 1 instant.
Calls the method notify() in quality of ObservedSubject, updating all observers.
*/
void
History::enqueueSlice (SimulationStatus status)
{
if (_slices.size() == 0){
_slices.push_back(Slice(0, 1, status));
_total_time_elapsed = 1;
notify();
return;
}
//check the last slice
Slice& last = _slices[_slices.size()-1];
if (last.getSimulationStatus() == status) //increments the duration by ONE unit
{
last.setDuration(last.getDuration()+1);
}
else //insert a new slice CONTIGUOUS to the last one
{
_slices.push_back(Slice(last.getStartedAt() + last.getDuration(), 1, status));
}
_total_time_elapsed++; //one instant is passed...
notify();
}
/**
Removes all the informations about the simulation following the specified instant.
*/
void
History::truncateAt (int instant)
{
//reach the instant
vector<Slice>::iterator i = _slices.begin();
for (int k=0; k < instant; k++)
i++;
//replaces the current vector with the "trimmed" one.
_slices = vector<Slice>(_slices.begin(),i);
_total_time_elapsed = instant;
}

71
src/backend/history.hh Normal file
View File

@ -0,0 +1,71 @@
// src/backend/history.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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef HISTORY_HH
#define HISTORY_HH 1
#include "config.h"
#include <vector>
#include <iostream>
#include "slice.hh"
#include "observedSubject.hh"
#include "simulationStatus.hh"
#include "schedulableStatus.hh"
#include "../templates/smartp.hh"
namespace sgpem
{
/** \brief Manages the history of the simulation
Manages the history of the simulation from the instant 0 to the current time,
i.e. permits to know the state of each schedulable object inside this time interval.
In particoular it's possible to know which entity was running at a precise moment.
In a future iteration it will be possible to revert the entire simulation to a state
present in the history ("undo operation")
*/
class History;
class SG_DLLEXPORT History : public ObservedSubject {
public:
memory::smart_ptr<const sgpem::SchedulableStatus> getScheduledAt (int time);
memory::smart_ptr<const sgpem::SimulationStatus> getSimulationStatusAt (int time);
int getCurrentTime();
void enqueueSlice (sgpem::SimulationStatus status);
void truncateAt (int instant);
static History& getInstance();
private:
History(int); //private constructor. The parameter is discarded
static History _instance;
int _total_time_elapsed;
std::vector<sgpem::Slice> _slices;
};
}
#endif //HISTORY_H

View File

@ -0,0 +1,63 @@
// src/backend/observedSubject.cc - 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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "observedSubject.hh"
using namespace std;
using namespace sgpem;
ObservedSubject::~ObservedSubject()
{
}
/**
Calls update() in each Observer
*/
void
ObservedSubject::notify()
{
for(vector<Observer*>::iterator i = _attached.begin(); i < _attached.end(); i++)
(*i)->update();
}
/**
Attachs an Observer to this ObservedSubject.
*/
void
ObservedSubject::attach(Observer* o)
{
_attached.push_back(o);
}
/**
Detachs the observer from this ObservedSubject.
*/
bool
ObservedSubject::detach(Observer* o)
{
vector<Observer*>::iterator i = find(_attached.begin(), _attached.end(), o);
if (i == _attached.end())
return false;
_attached.erase(i); // FOUND and POPPED
return true;
}

View File

@ -0,0 +1,54 @@
// src/backend/observedSubject.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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef OBSERVEDSUBJECT_HH
#define OBSERVEDSUBJECT_HH 1
#include "config.h"
#include <vector>
#include <algorithm>
#include "../observer.hh"
namespace sgpem
{
/**
Abstract class which represents an observed entity who calls Update() in all Observer objects.
See "Observer Pattern" for more information.
*/
class ObservedSubject;
class SG_DLLEXPORT ObservedSubject
{
public:
virtual ~ObservedSubject() =0;
void notify();
void attach(sgpem::Observer*);
bool detach(sgpem::Observer*);
private:
std::vector<Observer*> _attached;
};
}
#endif

View File

@ -34,5 +34,5 @@ Process::~Process()
Glib::ustring
Process::getType() const
{
return _("Process");
return "Process";
}

View File

@ -71,6 +71,10 @@ SchedulableStatus::getSchedulable() const
return _ref;
}
bool
SchedulableStatus::operator==(const SchedulableStatus& dx) const
{
return (_ref==dx._ref)&&(_last==dx._last)&&(_timeLeft==dx._timeLeft)&&(_myState==dx._myState);
}

View File

@ -51,7 +51,7 @@ class SG_DLLEXPORT SchedulableStatus
SchedulableStatus(const Schedulable& obj);
//SchedulableStatus(const SchedulableStatus& obj); //copy constructor
//SchedulableStatus& operator=(const SchedulableStatus&);
bool operator==(const SchedulableStatus&) const;
int getCpuTimeLeft() const;
void giveCpuTime(const int& time);

View File

@ -21,7 +21,11 @@
#include "simulationStatus.hh"
using namespace sgpem;
using namespace std;
using namespace memory;
/** \brief Creates the SimulationStatus object with no SchedulableStatus object
*/
SimulationStatus::SimulationStatus()
{
}
@ -52,14 +56,41 @@ SimulationStatus::setRunning(SchedulableStatus entity)
the NULL pointer.
*/
auto_ptr<SchedulableStatus>
SimulationStatus::getRunning() const
smart_ptr<SchedulableStatus>
SimulationStatus::getRunning()
{
for (uint i=0; i < _set.size(); i++)
if (_set[i].getState() == SchedulableStatus::state_running){ //there can be only ONE running schedulable objext
SchedulableStatus* s = const_cast<SchedulableStatus*>( &_set[i]);
return auto_ptr<SchedulableStatus>(s);
for (list<SchedulableStatus>::iterator f=_set.begin(); f != _set.end(); f++)
if (f->getState() == SchedulableStatus::state_running){ //there can be only ONE running schedulable objext
return smart_ptr<SchedulableStatus>(new SchedulableStatus(*f));
}
return auto_ptr<SchedulableStatus>(NULL);
return smart_ptr<SchedulableStatus>(NULL);
}
smart_ptr<const SchedulableStatus>
SimulationStatus::getRunning() const
{
for (list<SchedulableStatus>::const_iterator f=_set.begin(); f != _set.end(); f++)
if (f->getState() == SchedulableStatus::state_running){ //there can be only ONE running schedulable objext
return smart_ptr<const SchedulableStatus>(new SchedulableStatus(*f));
}
return smart_ptr<const SchedulableStatus>(NULL); //NOT FOUND RUNNING ENTITIES
}
/**
\brief Returns TRUE if the two objects have the same SchedulableStatus objects
*/
bool
SimulationStatus::operator==(const SimulationStatus& dx) const
{
if (_set.size() != dx._set.size())
return false;
//check if dx has ALL and ONLY the elements holded by _set with no order importance
for(list<SchedulableStatus>::const_iterator f=_set.begin(); f != _set.end(); f++)
if (find(dx._set.begin(), dx._set.end(), *f) == dx._set.end()) //element NOT found!!
return false;
return true;
}

View File

@ -22,10 +22,10 @@
#define SIMULATIONSTATUS_HH 1
#include "config.h"
#include <stdlib.h>
#include <vector>
#include <list>
#include "schedulableStatus.hh"
#include "../templates/smartp.hh"
namespace sgpem
{
@ -43,11 +43,15 @@ namespace sgpem
SimulationStatus();
SimulationStatus(const SimulationStatus&);
std::auto_ptr<SchedulableStatus> getRunning() const;
bool operator==(const SimulationStatus&) const;
memory::smart_ptr<SchedulableStatus> getRunning(); //this method isn't const because it returns a pointer which CAN alter the object!
memory::smart_ptr<const SchedulableStatus> getRunning() const;
void setRunning(SchedulableStatus);
private:
std::vector<SchedulableStatus> _set;
//I used a list instead of a vector because this one could reallocate the internal elements
//making pointers returned by getRunning() point to nothing! SEGMENTATION-FAULT :-O
std::list<SchedulableStatus> _set;
};
}

View File

@ -35,13 +35,19 @@ Slice::getSimulationStatus()
}
int
Slice::getStartedAt()
Slice::getStartedAt() const
{
return _started_at;
}
int
Slice::getDuration()
Slice::getDuration() const
{
return _duration;
}
void
Slice::setDuration(const int& i)
{
_duration = i;
}

View File

@ -42,8 +42,9 @@ namespace sgpem
Slice(const int& start, const int& duration, const SimulationStatus& status);
SimulationStatus& getSimulationStatus();
int getStartedAt();
int getDuration();
int getStartedAt() const;
int getDuration() const;
void setDuration(const int&);
private:
SimulationStatus _ref;

View File

@ -25,6 +25,8 @@
#include "parseopts.hh"
#include "startgui.hh"
#include "templates/smartp.hh"
#include "backend/history.hh"
#include "backend/slice.hh"
#include "backend/schedulable.hh"
#include "backend/schedulableStatus.hh"
@ -32,12 +34,12 @@
#include "backend/process.hh"
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
using namespace sgpem;
using namespace memory;
int
main(int argc, char* argv[])
@ -48,7 +50,7 @@ main(int argc, char* argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
/*
// Parses options and prepares vector with
// filenames of documents to be opened
vector<string> filenames;
@ -58,22 +60,59 @@ main(int argc, char* argv[])
parse_options(a_count, a_ptr);
filenames.insert(filenames.begin(), a_ptr, a_ptr+a_count);
}
start_gui(argc, argv);
*/
//start_gui(argc, argv);
//SMOKE-TEST for backend classes
cout << "\n\n********************************";
Process p("P1", 0,10,5);
SchedulableStatus ss(p);
SchedulableStatus ss2(ss);
SimulationStatus sim;
sim.setRunning(ss);
Process p1("P1", 0,10,1);
Process p2("P2", 0,30,2);
Process p3("P3", 5,15,3);
Slice sl(0,5, sim);
SchedulableStatus ss1(p1);
SchedulableStatus ss2(p2);
SchedulableStatus ss3(p3);
cout << "\n\n";
SimulationStatus sim1; sim1.setRunning(p1);
SimulationStatus sim2; sim2.setRunning(p2);
SimulationStatus sim3; sim3.setRunning(p3);
History h(History::getInstance());
h.enqueueSlice(sim1);
h.enqueueSlice(sim1);
h.enqueueSlice(sim2);
h.enqueueSlice(sim1);
h.enqueueSlice(sim2);
h.enqueueSlice(sim1);
h.enqueueSlice(sim2);
h.enqueueSlice(sim3);
h.enqueueSlice(sim3);
h.enqueueSlice(sim1);
h.enqueueSlice(sim3);
h.enqueueSlice(sim1);
h.truncateAt(3);
smart_ptr<const sgpem::SimulationStatus> quale;
quale = h.getSimulationStatusAt(0);
if (quale) cout << "\n" << quale->getRunning()->getSchedulable()->getName(); else cout << "NO";
quale = h.getSimulationStatusAt(1);
if (quale) cout << "\n" << quale->getRunning()->getSchedulable()->getName(); else cout << "NO";
quale = h.getSimulationStatusAt(2);
if (quale) cout << "\n" << quale->getRunning()->getSchedulable()->getName(); else cout << "NO";
h.truncateAt(2);
smart_ptr<const sgpem::SchedulableStatus> quale2;
quale2 = h.getScheduledAt(0);
if (quale2) cout << "\n" << quale2->getSchedulable()->getName(); else cout << "NO";
quale2 = h.getScheduledAt(1);
if (quale2) cout << "\n" << quale2->getSchedulable()->getName(); else cout << "NO";
quale2 = h.getScheduledAt(2);
if (quale2) cout << "\n" << quale2->getSchedulable()->getName(); else cout << "NO";
cout << "AAA " <<sl.getSimulationStatus().getRunning()->getSchedulable()->getName();
cout << "\n\n";
return 0;

47
src/observer.hh Normal file
View File

@ -0,0 +1,47 @@
// src/frontend/observer.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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef OBSERVER_HH
#define OBSERVER_HH 1
#include "config.h"
namespace sgpem
{
/**
Abstract class which represents an observed entity who calls Update() in all Observer objects.
See "Observer Pattern" for more information.
*/
class Observer;
class SG_DLLEXPORT Observer
{
public:
virtual ~Observer()=0;
virtual void update();
private:
};
}
#endif