📄 rules.cpp
字号:
if(this->fixedDay[i]>=0 && this->sameDay[i]>=0){ bool t=this->fixedDay[this->sameDay[i]]==this->fixedDay[i] || this->fixedDay[this->sameDay[i]]==-1; if(!t){ QMessageBox::warning(NULL, QObject::tr("FET information"), QObject::tr("Activity with id=%1 has fixed day on %2 and must be scheduled on the same\n" " day with act. with id=%3, which is impossible. Please check your constraints").arg(this->internalActivitiesList[i].id) .arg(this->daysOfTheWeek[this->fixedDay[i]]).arg(this->internalActivitiesList[this->sameDay[i]].id)); this->internalStructureComputed=false; return false; } assert(this->fixedDay[this->sameDay[i]]==this->fixedDay[i] || this->fixedDay[this->sameDay[i]]==-1); this->fixedDay[this->sameDay[i]]=this->fixedDay[i]; } if(this->fixedHour[i]>=0 && this->sameHour[i]>=0){ bool t=this->fixedHour[this->sameHour[i]]==this->fixedHour[i] || this->fixedHour[this->sameHour[i]]==-1; if(!t){ QMessageBox::warning(NULL, QObject::tr("FET information"), QObject::tr("Activity with id=%1 has fixed hour on %2 and must be scheduled on the same\n" " hour with act. with id=%3, which is impossible. Please check your constraints").arg(this->internalActivitiesList[i].id) .arg(this->hoursOfTheDay[this->fixedHour[i]]).arg(this->internalActivitiesList[this->sameHour[i]].id)); this->internalStructureComputed=false; return false; } assert(this->fixedHour[this->sameHour[i]]==this->fixedHour[i] || this->fixedHour[this->sameHour[i]]==-1); this->fixedHour[this->sameHour[i]]=this->fixedHour[i]; } } //make all have the fixed day & hour for(int i=0; i<this->nInternalActivities; i++){ if(this->sameDay[i]>=0){ assert(this->fixedDay[i]==this->fixedDay[this->sameDay[i]] || this->fixedDay[i]==-1); this->fixedDay[i]=this->fixedDay[this->sameDay[i]]; } if(this->sameHour[i]>=0){ assert(this->fixedHour[i]==this->fixedHour[this->sameHour[i]] || this->fixedHour[i]==-1); this->fixedHour[i]=this->fixedHour[this->sameHour[i]]; } } this->nInternalTimeConstraints=i; assert(this->nInternalTimeConstraints<=MAX_TIME_CONSTRAINTS); //roomHasEquipment for(int rm=0; rm<this->nInternalRooms; rm++){ for(int eq=0; eq<this->nInternalEquipments; eq++){ this->roomHasEquipment[rm][eq] = (this->internalRoomsList[rm])->searchEquipment(this->internalEquipmentsList[eq]->name); } } //space constraints SpaceConstraint* sctr; assert(this->spaceConstraintsList.count()<=(uint)(MAX_SPACE_CONSTRAINTS)); for(int i=0; i<this->nInternalActivities; i++){ this->fixedRoom[i]=-1; this->sameRoom[i]=-1; } for(sctr=this->spaceConstraintsList.first(), i=0; sctr; sctr=this->spaceConstraintsList.next()){ if(!sctr->computeInternalStructure(*this)) continue; if(sctr->type==CONSTRAINT_TEACHER_REQUIRES_ROOM && sctr->compulsory==true){ //works by repairing ConstraintTeacherRequiresRoom* ctrr=(ConstraintTeacherRequiresRoom*) sctr; for(int i=0; i<ctrr->_nActivities; i++){ bool t=this->fixedRoom[ctrr->_activities[i]]==-1 || this->fixedRoom[ctrr->_activities[i]]==ctrr->_room; if(!t){ QMessageBox::warning(NULL, QObject::tr("FET information"), QObject::tr("ConstraintTeacherRequiresRoom: Teacher=%1,\n" "Activity with id=%2 has fixed room on %3 and %4 - impossible\n" "Please fix that") .arg(ctrr->teacherName) .arg(this->internalActivitiesList[ctrr->_activities[i]].id) .arg(this->roomsList.at(this->fixedRoom[ctrr->_activities[i]])->name) .arg(this->roomsList.at(ctrr->_room)->name)); this->internalStructureComputed=false; return false; } this->fixedRoom[ctrr->_activities[i]]=ctrr->_room; } } if(sctr->type==CONSTRAINT_TEACHER_SUBJECT_REQUIRE_ROOM && sctr->compulsory==true){ //works by repairing ConstraintTeacherSubjectRequireRoom* ctrr=(ConstraintTeacherSubjectRequireRoom*) sctr; for(int i=0; i<ctrr->_nActivities; i++){ bool t=this->fixedRoom[ctrr->_activities[i]]==-1 || this->fixedRoom[ctrr->_activities[i]]==ctrr->_room; if(!t){ QMessageBox::warning(NULL, QObject::tr("FET information"), QObject::tr("ConstraintTeacherSubjectRequireRoom: Teacher=%1, Subject=%2\n" "Activity with id=%3 has fixed room on %4 and %5 - impossible\n" "Please fix that") .arg(ctrr->teacherName) .arg(ctrr->subjectName) .arg(this->internalActivitiesList[ctrr->_activities[i]].id) .arg(this->roomsList.at(this->fixedRoom[ctrr->_activities[i]])->name) .arg(this->roomsList.at(ctrr->_room)->name)); this->internalStructureComputed=false; return false; } this->fixedRoom[ctrr->_activities[i]]=ctrr->_room; } } else if(sctr->type==CONSTRAINT_ACTIVITY_PREFERRED_ROOM && sctr->compulsory==true){ //works by repairing ConstraintActivityPreferredRoom* capr=(ConstraintActivityPreferredRoom*) sctr; bool t=this->fixedRoom[capr->_activity]==-1 || this->fixedRoom[capr->_activity]==capr->_room; if(!t){ QMessageBox::warning(NULL, QObject::tr("FET information"), QObject::tr("Activity with id=%1 has fixed room on %2 and %3 - impossible\n" "Please fix that") .arg(this->internalActivitiesList[capr->_activity].id) .arg(this->roomsList.at(this->fixedRoom[capr->_activity])->name) .arg(this->roomsList.at(capr->_room)->name)); this->internalStructureComputed=false; return false; } this->fixedRoom[capr->_activity]=capr->_room; } else if(sctr->type==CONSTRAINT_ACTIVITIES_SAME_ROOM && sctr->compulsory==true){ ConstraintActivitiesSameRoom* c=(ConstraintActivitiesSameRoom*)sctr; int ai1=c->_activities[0]; for(int i=1; i<c->_n_activities; i++){ int ai2=c->_activities[i]; int old=sameRoom[ai2]; if(old>=0){ bool visited[MAX_ACTIVITIES]; for(int i=0; i<this->nInternalActivities; i++) visited[i]=false; visited[old]=true; while(sameRoom[old]>=0 && !visited[sameRoom[old]]){ old=sameRoom[old]; visited[old]=true; } this->sameRoom[old]=ai1; } else this->sameRoom[ai2]=ai1; } } else if(sctr->type==CONSTRAINT_SUBJECT_SUBJECT_TAG_PREFERRED_ROOM && sctr->compulsory==true){ //works by repairing ConstraintSubjectSubjectTagPreferredRoom* c=(ConstraintSubjectSubjectTagPreferredRoom*) sctr; for(int i=0; i<c->_nActivities; i++){ int ai=c->_activities[i]; bool t=this->fixedRoom[ai]==-1 || this->fixedRoom[ai]==c->_room; if(!t){ QMessageBox::warning(NULL, QObject::tr("FET information"), QObject::tr("Because of space constraint\n%1\n" "Activity with id=%2 has fixed room on %3 and %4 - impossible\n" "Please fix that") .arg(c->getDetailedDescription(*this)) .arg(this->internalActivitiesList[ai].id) .arg(this->roomsList.at(this->fixedRoom[ai])->name) .arg(this->roomsList.at(c->_room)->name)); this->internalStructureComputed=false; return false; } this->fixedRoom[ai]=c->_room; } } else if(sctr->type==CONSTRAINT_SUBJECT_PREFERRED_ROOM && sctr->compulsory==true){ //works by repairing ConstraintSubjectPreferredRoom* c=(ConstraintSubjectPreferredRoom*) sctr; for(int i=0; i<c->_nActivities; i++){ int ai=c->_activities[i]; bool t=this->fixedRoom[ai]==-1 || this->fixedRoom[ai]==c->_room; if(!t){ QMessageBox::warning(NULL, QObject::tr("FET information"), QObject::tr("Because of space constraint\n%1\n" "Activity with id=%2 has fixed room on %3 and %4 - impossible\n" "Please fix that") .arg(c->getDetailedDescription(*this)) .arg(this->internalActivitiesList[ai].id) .arg(this->roomsList.at(this->fixedRoom[ai])->name) .arg(this->roomsList.at(c->_room)->name)); this->internalStructureComputed=false; return false; } this->fixedRoom[ai]=c->_room; } } else this->internalSpaceConstraintsList[i++]=sctr; } this->nInternalSpaceConstraints=i; assert(this->nInternalSpaceConstraints<=MAX_SPACE_CONSTRAINTS); //all activities will depend on only one "head" for(int i=0; i<this->nInternalActivities; i++){ for(int j=0; j<this->nInternalActivities; j++){ int k=this->sameRoom[j]; if(k>=0 && this->sameRoom[k]>=0) this->sameRoom[j]=this->sameRoom[k]; } } //make the head have the fixed room for(int i=0; i<this->nInternalActivities; i++){ if(this->fixedRoom[i]>=0 && this->sameRoom[i]>=0){ bool t=this->fixedRoom[this->sameRoom[i]]==this->fixedRoom[i] || this->fixedRoom[this->sameRoom[i]]==-1; if(!t){ QMessageBox::warning(NULL, QObject::tr("FET information"), QObject::tr("Activity with id=%1 has fixed room in %2 and must be scheduled in the same\n" " room with act. with id=%3, which is impossible. Please check your constraints").arg(this->internalActivitiesList[i].id) .arg(this->fixedRoom[i]).arg(this->internalActivitiesList[this->sameRoom[i]].id)); this->internalStructureComputed=false; return false; } assert(this->fixedRoom[this->sameRoom[i]]==this->fixedRoom[i] || this->fixedRoom[this->sameRoom[i]]==-1); this->fixedRoom[this->sameRoom[i]]=this->fixedRoom[i]; } } //make all have the fixed room for(int i=0; i<this->nInternalActivities; i++){ if(this->sameRoom[i]>=0){ assert(this->fixedRoom[i]==this->fixedRoom[this->sameRoom[i]] || this->fixedRoom[i]==-1); this->fixedRoom[i]=this->fixedRoom[this->sameRoom[i]]; } } //done. this->internalStructureComputed=true; return true;}void Rules::kill() //clears memory for the rules, destroyes them{ //Teachers assert(this->teachersList.autoDelete()==true); this->teachersList.clear(); //Subjects assert(this->subjectsList.autoDelete()==true); this->subjectsList.clear(); //Subject tags assert(this->subjectTagsList.autoDelete()==true); this->subjectTagsList.clear(); //Years assert(this->yearsList.autoDelete()==true); this->yearsList.clear(); //Activities assert(this->activitiesList.autoDelete()==true); this->activitiesList.clear(); //Time constraints assert(this->timeConstraintsList.autoDelete()==true); this->timeConstraintsList.clear(); //Space constraints assert(this->spaceConstraintsList.autoDelete()==true); this->spaceConstraintsList.clear(); //Equipments assert(this->equipmentsList.autoDelete()==true); this->equipmentsList.clear(); //Buildings assert(this->buildingsList.autoDelete()==true); this->buildingsList.clear(); //Rooms assert(this->roomsList.autoDelete()==true); this->roomsList.clear(); //done this->internalStructureComputed=false; this->initialized=false;}Rules::Rules(){ this->initialized=false;}Rules::~Rules(){ if(this->initialized) this->kill();}bool Rules::addTeacher(Teacher* teacher){ for(Teacher* tch=this->teachersList.first(); tch; tch=this->teachersList.next()) if(tch->name==teacher->name) return false; this->internalStructureComputed=false; this->teachersList.append(teacher); return true;}int Rules::searchTeacher(const QString& teacherName){ int i; Teacher* tch; for(tch=this->teachersList.first(), i=0; tch; tch=this->teachersList.next(), i++) if(tch->name==teacherName) return i; return -1;}bool Rules::removeTeacher(const QString& teacherName){ //TODO: improve this part for(Activity* act=this->activitiesList.first(); act; ){ act->removeTeacher(teacherName); if(act->teachersNames.count()==0){ this->removeActivity(act->id, act->activityGroupId); act=this->activitiesList.first(); //!!! please improve this!!! //(You have to be careful, there can be erased more activities here) } else act=this->activitiesList.next(); } for(TimeConstraint* ctr=this->timeConstraintsList.first(); ctr; ){ if(ctr->type==CONSTRAINT_TEACHER_NOT_AVAILABLE){ ConstraintTeacherNotAvailable* crt_constraint=(ConstraintTeacherNotAvailable*)ctr; if(teacherName == crt_constraint->teacherName){ this->removeTimeConstraint(ctr); //single constraint removal ctr=this->timeConstraintsList.current(); } else{ ctr=this->timeConstraintsList.next(); } } else ctr=this->timeConstraintsList.next(); } for(TimeConstraint* ctr=this->timeConstraintsList.first(); ctr; ){ if(ctr->type==CONSTRAINT_TEACHER_MAX_DAYS_PER_WEEK){ ConstraintTeacherMaxDaysPerWeek* crt_constraint=(ConstraintTeacherMaxDaysPerWeek*)ctr; if(teacherName == crt_constraint->teacher){ this->removeTimeConstraint(ctr); //single constraint removal ctr=this->timeConstraintsList.current(); } else{ ctr=this->timeConstraintsList.next(); } } else ctr=this->timeConstraintsList.next(); } for(TimeConstraint* ctr=this->timeConstraintsList.first(); ctr; ){ if(ctr->type==CONSTRAINT_ACTIVITIES_PREFERRED_TIMES){ ConstraintActivitiesPreferredTimes* crt_constraint=(ConstraintActivitiesPreferredTimes*)ctr; if(teacherName == crt_constraint->teacherName){ this->removeTimeConstraint(ctr); //single constraint removal ctr=this->timeConstraintsList.current(); } else{ ctr=this->timeConstraintsList.next(); } } else ctr=this->timeConstraintsList.next(); } for(SpaceConstraint* ctr=this->spaceConstraintsList.first(); ctr; ){ if(ctr->type==CONSTRAINT_TEACHER_REQUIRES_ROOM){ ConstraintTeacherRequiresRoom* crt_constraint=(ConstraintTeacherRequiresRoom*)ctr; if(teacherName == crt_constraint->teacherName){ this->removeSpaceConstraint(ctr); //single constraint removal ctr=this->spaceConstraintsList.current();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -