Commit 7d89bfd1 authored by Alexis Lebis's avatar Alexis Lebis

Adding TMP builder for tmp object, as well as ID overflow protection.

Solve the #5 issue
parent 48b01b2e
......@@ -8,6 +8,7 @@
#include <model/problem.h>
#include <model/profession.h>
#include <model/magnitude.h>
#include <model/tools.h>
#include <model/ea/cursus.h>
#include <model/ea/initializer.h>
......
......@@ -2,11 +2,14 @@
#include "competency.h"
#include "magnitude.h"
#include "tools.h"
#include "exception/magnitudeException.h"
#include "exception/competencyEvolvingException.h"
#include "exception/idOverflowException.h"
int Competency::COMPETENCY_COUNTER = 0;
int Competency::COMPETENCY_TMP_COUNTER = ID_RANGE_FOR_OBJECT + 1;
// === FACTORY
......@@ -37,6 +40,24 @@ Competency Competency::build(double d = 0, std::string name)
}
}
Competency Competency::buildTMP(double d, std::string name)
{
int id = Competency::assignID4TMP();
if(name.empty())
name = "Competency#"+std::to_string(id);
try
{
Magnitude m = Magnitude::build(d);
return Competency(id, m, name);
}
catch(MagnitudeException & e)
{
e.getMagnitude().rebase();
throw CompetencyEvolvingException(new Competency(id, e.getMagnitude(), name));
}
}
// === CONSTRUCTOR
Competency::Competency(int id, Magnitude m, std::string s)
......@@ -49,9 +70,21 @@ Competency::Competency(int id, Magnitude m, std::string s)
int Competency::assignID()
{
if(Competency::COMPETENCY_COUNTER + 1 > ID_RANGE_FOR_OBJECT)
throw idOverflowException("assignID()@Competency.cpp");
return ++Competency::COMPETENCY_COUNTER;
}
int Competency::assignID4TMP()
{
if(Competency::COMPETENCY_TMP_COUNTER + 1 > ID_RANGE_FOR_TEMPORARY_OBJECT)
{
std::cout << "INFO: COMPETENCY_TMP_COUNTER was about to overflow: restored to ID_RANGE_OBJECT + 1" << std::endl;
COMPETENCY_TMP_COUNTER = ID_RANGE_FOR_OBJECT + 1;
}
return ++Competency::COMPETENCY_TMP_COUNTER;
}
// === FUNCTION
void Competency::evolveTowards(Magnitude & m)
......
......@@ -24,10 +24,15 @@ class Competency
//STATIC
static int COMPETENCY_COUNTER;
static int COMPETENCY_TMP_COUNTER;
static int assignID();
static int assignID4TMP();//Called by the tmp builder
public:
static Competency build(Magnitude &, std::string s = "");
static Competency build(double, std::string = "");
///This builder should be used for TMP element
static Competency buildTMP(double, std::string);
Competency() = default;
// === FUNCTION
......
......@@ -96,7 +96,7 @@ std::pair<bool, double> ConstraintsPrerequisites::integrityCheck(Cursus indiv)
}
else
{
compByTF.at(currentTF).push_back(Competency::build(currentCompetency.c_magnitude().value(), currentCompetency.c_name()));
compByTF.at(currentTF).push_back(Competency::buildTMP(currentCompetency.c_magnitude().value(), currentCompetency.c_name()));
}
}
}
......
......@@ -16,7 +16,7 @@ std::pair<bool, double> ConstraintsProfession::integrityCheck(Cursus indiv)
for(int i = 0 ; i < this->_job.prerequisites().size(); i++)
{
std::string name = this->_job.prerequisites().at(i).c_name();
compToAnswer.push_back(Competency::build(0, name)); //same name to exploit the Competency::operator== on name equality
compToAnswer.push_back(Competency::buildTMP(0, name)); //same name to exploit the Competency::operator== on name equality
}
Course current;
......
......@@ -11,8 +11,10 @@
#include "exception/courseECTSException.h"
#include "exception/courseTemporalFrameException.h"
#include "exception/notImplementedException.h"
#include "exception/idOverflowException.h"
int Course::COURSE_COUNTER = 0;
int Course::COURSE_TMP_COUNTER = ID_RANGE_FOR_OBJECT + 1;
// === FACTORY
Course Course::build(int ects, std::string name)
......@@ -27,6 +29,19 @@ Course Course::build(int ects, std::string name)
else
return Course(id, ects, name);
}
Course Course::buildTMP(int ects, std::string name)
{
int id = Course::assignID4TMP();
if(name.empty())
name = "Course#"+std::to_string(id);
if(ects < 0)
throw CourseECTSException(new Course(id, ects, name));
else
return Course(id, ects, name);
}
// === END FACTORY
// === CONSTRUCTOR
......@@ -269,6 +284,18 @@ bool Course::operator==(const Course & c) const
/// Course counter
int Course::assignID()
{
if(Course::COURSE_COUNTER + 1 > ID_RANGE_FOR_OBJECT)
throw idOverflowException("assignID()@Course.cpp");
return ++Course::COURSE_COUNTER;
}
int Course::assignID4TMP()
{
if(Course::COURSE_TMP_COUNTER + 1 > ID_RANGE_FOR_TEMPORARY_OBJECT)
{
std::cout << "INFO: COURSE_TMP_COUNTER was about to overflow: restored to ID_RANGE_OBJECT + 1" << std::endl;
COURSE_TMP_COUNTER = ID_RANGE_FOR_OBJECT + 1;
}
return ++COURSE_TMP_COUNTER;
}
// === END STATIC
\ No newline at end of file
......@@ -55,13 +55,17 @@ class Course
// Static
static int COURSE_COUNTER;
static int COURSE_TMP_COUNTER;
static int assignID();
static int assignID4TMP();
// Constructor
//Course();
Course(int id, int ects, std::string name);
public:
static Course build(int ects = 0, std::string name = "");
static Course buildTMP(int ects = 0, std::string name = "");
/// Default constructor. Use Course::build instead !
Course() = default;
......
#ifndef SRD_MODEL_EXCEPTION_ID_OVERFLOW_EXCEPTION_H_
#define SRD_MODEL_EXCEPTION_ID_OVERFLOW_EXCEPTION_H_
#include <exception>
#include <string>
class idOverflowException : public std::exception
{
private:
std::string _msg;
public:
idOverflowException(std::string buildInfo) throw()
{
this->_msg = "The id of an object has overflow the maximal value. Where: "+buildInfo;
}
virtual const char* what() const throw()
{
return this->_msg.c_str();
}
};
#endif //SRD_MODEL_EXCEPTION_ID_OVERFLOW_EXCEPTION_H_
\ No newline at end of file
......@@ -6,6 +6,10 @@
#include <algorithm>
#include <queue>
#include <random>
#include <limits>
static const int ID_RANGE_FOR_OBJECT = 10000;
static const int ID_RANGE_FOR_TEMPORARY_OBJECT = std::numeric_limits<int>::max();
template<typename T>
/** Searches into vec the element findMe. The class T must have T() defined ( T()=default; is OK) */
......
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