Commit 1265d8f1 authored by Alexis Lebis's avatar Alexis Lebis

Big commit. Magnitude and ExcepMag ok. Competency and CompetencyExcep ok.

This commit improve the stability of Magnituce. It solve the issue #1 about shallow/direct copy and updating.
It also implements Competency class, and introduce ceao_competency.cpp (and associate cmake modif) to test it.
parent f48dc69a
......@@ -5,12 +5,16 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
# 2) Define targets: executable and dependencies
ADD_EXECUTABLE(ceao main.cpp)
ADD_EXECUTABLE(ceao_competency ceao_competency.cpp)
ADD_EXECUTABLE(tryInit tryInit.cpp)
ADD_EXECUTABLE(tryMutation tryMutation.cpp)
ADD_EXECUTABLE(tryCrossover tryCrossover.cpp)
ADD_EXECUTABLE(tryEval tryEval.cpp)
ADD_DEPENDENCIES(ceao lQueen lModel)
ADD_DEPENDENCIES(ceao_competency lQueen lModel)
ADD_DEPENDENCIES(tryInit lQueen)
ADD_DEPENDENCIES(tryMutation lQueen)
ADD_DEPENDENCIES(tryCrossover lQueen)
......@@ -19,6 +23,8 @@ ADD_DEPENDENCIES(tryEval lQueen)
# 3) Link the librairies for your executable
TARGET_LINK_LIBRARIES(ceao ${PARADISEO_LIBRARIES} lQueen lModel)
TARGET_LINK_LIBRARIES(ceao_competency ${PARADISEO_LIBRARIES} lQueen lModel)
TARGET_LINK_LIBRARIES(tryInit ${PARADISEO_LIBRARIES} lQueen)
TARGET_LINK_LIBRARIES(tryMutation ${PARADISEO_LIBRARIES} lQueen)
TARGET_LINK_LIBRARIES(tryCrossover ${PARADISEO_LIBRARIES} lQueen)
......
#include <eo>
#include <iostream>
#include <string>
#include "model/magnitude.h"
#include "model/competency.h"
#include "model/exception/magnitudeException.h"
#include "model/exception/competencyEvolvingException.h"
int main(int argc, char* argv[])
{
Magnitude m = Magnitude::build(0.7);
Competency c = Competency::build(0.5, "");
Competency c2 = Competency::build(m,"");
std::cout << std::to_string(c.c_magnitude().value()) << std::endl ;
std::cout << c2.competencyValue() << std::endl;
c.evolveTowards(0.2);
std::cout << c << std::endl;
c.evolveTowards(-0.2);
std::cout << c << std::endl;
c.evolveTowards(-0.3);
std::cout << c << std::endl;
c.evolveTowards(m);
std::cout << c << std::endl;
try
{
c.evolveTowards(2);
}
catch(CompetencyEvolvingException & e)
{
std::cout << e.what() << std::endl;
e.getCompetency().setName("Oeuf!");
std::cout << e.getCompetency() << std::endl;
std::cout << c << std::endl;
}
c.evolveTowards(-0.5);
std::cout << c << std::endl;
return EXIT_SUCCESS;
}
\ No newline at end of file
......@@ -8,11 +8,18 @@
#include <model/magnitude.h>
#include <model/competency.h>
#include <model/exception/magnitudeException.h>
#include <model/exception/competencyEvolvingException.h>
int main(int argc, char* argv[]){
// ================================ TEST ZONE ===========================================
Magnitude m = Magnitude::build(0.5);
Magnitude n = Magnitude::build(0.3);
Magnitude o = Magnitude::build(0.2);
o = m;
std::cout << "Magnitude" << o.value() << std::endl;
std::cout << "Magnitude" << m.value() << std::endl;
......@@ -23,15 +30,22 @@ int main(int argc, char* argv[]){
try{
std::cout << "After addition mag is : " << (m + m).value() << std::endl;
}
catch(MagnitudeException e)
catch(MagnitudeException & e)
{
std::cout << "\nEXCEPTION CATCH !\n";
std::cout << "Memory adr of m is : " << &m << std::endl;
std::cout << "Memory adr of e is : " << &e.getMagnitude() << std::endl;
e.getMagnitude().rebase();
std::cout << "REBASE! New magnitude value is" << e.getMagnitude().rebase() << std::endl;
std::cout << "Accessing magnitude value : " << e.getMagnitude().value() << std::endl;
std::cout << "magnitude value of M : " << m.value() << std::endl;
m = e.getMagnitude();
std::cout << "before end catch" << m.value() << std::endl;
}
std::cout << "Inspect m value:" << m.value() << std::endl;
// ================================= END TEST ZONE =====================================
//Define a QUEEN -> 1 line
QUEEN s1;
......
......@@ -5,6 +5,7 @@ SET(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
SET (EXERCICE_SOURCES
magnitude.cpp
competency.cpp
)
ADD_LIBRARY(lModel STATIC ${EXERCICE_SOURCES})
......
#include <iostream>
#include "competency.h"
#include "magnitude.h"
#include "exception/magnitudeException.h"
#include "exception/competencyEvolvingException.h"
int Competency::COMPETENCY_COUNTER = 0;
// === FACTORY
......@@ -8,7 +14,7 @@ Competency Competency::build(Magnitude & m, std::string name = "")
{
int id = Competency::assignID();
if(name.empty())
name = "Competency#"+id;
name = "Competency#"+std::to_string(id);
return Competency(id, m, name);
}
......@@ -17,7 +23,7 @@ Competency Competency::build(double d = 0, std::string name = "")
{
int id = Competency::assignID();
if(name.empty())
name = "Competency#"+id;
name = "Competency#"+std::to_string(id);
try
{
......@@ -27,8 +33,7 @@ Competency Competency::build(double d = 0, std::string name = "")
catch(MagnitudeException & e)
{
e.getMagnitude().rebase();
Magnitude m(e.getMagnitude());
return Competency(id, m, name);
throw CompetencyEvolvingException(new Competency(id, e.getMagnitude(), name));
}
}
......@@ -49,14 +54,43 @@ int Competency::assignID()
// === FUNCTION
/// @TODO
void Competency::evolveTowards(Magnitude & m)
{
try
{
this->_m+= m;
}
catch(MagnitudeException & e)
{
e.getMagnitude().rebase();
this->_m = e.getMagnitude();
throw CompetencyEvolvingException(this);
}
}
/// @TODO
void Competency::evolveTowards(double d)
{
try
{
this->_m += d;
}
catch(MagnitudeException & e)
{
e.getMagnitude().rebase();
this->_m = e.getMagnitude();
throw CompetencyEvolvingException(this);
}
}
const double Competency::competencyValue() const
{
return this->_m.value();
}
// === OPERATOR
std::ostream& operator<<(std::ostream& Stream, const Competency & c)
{
std::string s = "Competency\n\tid:"+std::to_string(c.id())+"\n\tname:"+c.c_name()+"\n\tvalue:"+std::to_string(c.competencyValue());
Stream << s ;
return Stream;
}
\ No newline at end of file
......@@ -15,7 +15,7 @@
class Competency
{
private:
Magnitude & _m;
Magnitude _m;
std::string _name;
int _id;
......@@ -33,13 +33,13 @@ class Competency
/** evolveTowards allows the competency to increase or decrease according to the value passed in parameter (either in Magnitude._value or double). It represents the competency evolving towards a specfic state, here represented by this->_m.value() + m.value().
* Use it instead of manipuling directly a Magnitude, since it'll handle MagnitudeException
*
* @throw CompetencyEvolvingException
* @throw CompetencyEvolvingException Exception containing this
*/
void evolveTowards(Magnitude & m);
/** evolveTowards allows the competency to increase or decrease according to the value passed in parameter (either in Magnitude._value or double). It represents the competency evolving towards a specfic state, here represented by this->_m.value() + d.
* Use it instead of manipuling directly a Magnitude, since it'll handle MagnitudeException
*
* @throw CompetencyEvolvingException
* @throw CompetencyEvolvingException Exception containing this
*/
void evolveTowards(double d);
......@@ -56,8 +56,11 @@ class Competency
const int id() const {return this->_id;}
// === SETTER
void setMagnitude(Magnitude & m){this->_m = m;};
void setMagnitude(Magnitude & m){this->_m = m;}
void setName(std::string s){this->_name = s;}
};
int Competency::COMPETENCY_COUNTER = 0;
// === OPERATOR
std::ostream & operator<<(std::ostream& Stream, const Competency & c);
#endif // SRC_COMPETENCY_H_
\ No newline at end of file
#ifndef SRC_MODEL_EXCEPTION_COMPETENCY_EVOLVING_EXCEPTION_H_
#define SRC_MODEL_EXCEPTION_COMPETENCY_EVOLVING_EXCEPTION_H_
#include <exception>
#include <iostream>
#include <string>
#include "../competency.h"
class CompetencyEvolvingException : public std::exception
{
public:
CompetencyEvolvingException(Competency * c)
: _c(c)
{
this->_phrase = "Exception while evolving the competency. Magnitude value was OOB and has been auto-corrected.";
}
virtual const char* what() const throw()
{
return _phrase.c_str();
}
virtual ~CompetencyEvolvingException() throw()
{}
Competency & getCompetency() const {return *(this->_c);}
private:
Competency * _c;
std::string _phrase;
};
#endif // SRC_MODEL_EXCEPTION_COMPETENCY_EVOLVING_EXCEPTION_H_
\ No newline at end of file
......@@ -10,14 +10,14 @@
class MagnitudeException : public std::exception
{
private:
Magnitude & _triedValue;
Magnitude * _triedValue;
std::string _phrase;
public:
MagnitudeException(Magnitude & m) throw()
MagnitudeException(Magnitude * m) throw()
: _triedValue(m)
{
this->_phrase = "Magnitude Exception: value (" + std::to_string(m.value()) + ") is out of bound [0;1].";
this->_phrase = "Magnitude Exception: value (" + std::to_string(m->value()) + ") is out of bound [0;1].";
}
virtual const char* what() const throw()
......@@ -26,9 +26,12 @@ class MagnitudeException : public std::exception
}
virtual ~MagnitudeException() throw()
{}
{
//this->_triedValue must not be freed by ~this !
}
Magnitude & getMagnitude() const {return *(this->_triedValue);}
Magnitude & getMagnitude() const {return this->_triedValue;}
};
#endif // SRC_EXCEPTION_MAGNITUDE_EXCEPTION_H_
\ No newline at end of file
#include <iostream>
#include "magnitude.h"
#include "exception/magnitudeException.h"
......@@ -8,8 +10,8 @@ Magnitude Magnitude::build(double value = 0)
if( !isInRange )
{
Magnitude mag(value);
throw MagnitudeException(mag);
//Magnitude mag(value);
throw MagnitudeException(new Magnitude(value));
}
else
return Magnitude(value);
......@@ -26,10 +28,10 @@ Magnitude::Magnitude(double d)
this->_value=d;
}
Magnitude::Magnitude(const Magnitude & m)
/* Magnitude::Magnitude(const Magnitude & m)
{
this->_value = m._value;
}
} */
// === OPERATOR
......@@ -40,7 +42,10 @@ Magnitude & Magnitude::operator+=(const double d)
{
this->_value+=d;
if( Magnitude::_inRange(this->_value) )
throw MagnitudeException(*this);
{
Magnitude *mag = new Magnitude(*this);
throw MagnitudeException(mag);
}
return *this;
}
......@@ -51,7 +56,10 @@ Magnitude & Magnitude::operator+=(const Magnitude & m)
{
this->_value += m._value;
if( Magnitude::_inRange(this->_value) )
throw MagnitudeException(*this);
{
Magnitude *mag = new Magnitude(*this);
throw MagnitudeException(mag);
}
return *this;
}
......@@ -61,7 +69,7 @@ Magnitude & Magnitude::operator+=(const Magnitude & m)
Magnitude operator+(const Magnitude & m, const Magnitude & n)
{
Magnitude mag(m);
mag+= n.value();
mag+= n;
return mag;
}
......@@ -112,8 +120,12 @@ bool Magnitude::set(double v)
double Magnitude::rebase()
{
if(this->_value < 0)
{
this->_value = 0;
}
else if(this->_value > 1)
{
this->_value = 1;
}
return this->_value;
}
\ No newline at end of file
......@@ -20,7 +20,7 @@ class Magnitude
public:
static Magnitude build(double); //factory
Magnitude(const Magnitude & m);
/* Magnitude(const Magnitude & m); */
double rebase(); /// if value is out of range, assign it the nearest value. Usefull when a MagnitudeException is caught
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment