001package org.cpsolver.exam.model; 002 003import java.util.HashMap; 004import java.util.HashSet; 005import java.util.Map; 006import java.util.Set; 007 008import org.cpsolver.ifs.assignment.Assignment; 009import org.cpsolver.ifs.assignment.context.AssignmentConstraintContext; 010 011 012public class ExamContext implements AssignmentConstraintContext<Exam, ExamPlacement> { 013 private Map<ExamStudent, Set<Exam>>[] iStudentTable; 014 private Map<ExamStudent, Set<Exam>>[] iStudentDayTable; 015 private Map<ExamInstructor, Set<Exam>>[] iInstructorTable; 016 private Map<ExamInstructor, Set<Exam>>[] iInstructorDayTable; 017 018 @SuppressWarnings("unchecked") 019 public ExamContext(ExamModel model, Assignment<Exam, ExamPlacement> assignment) { 020 iStudentTable = new Map[model.getNrPeriods()]; 021 for (int i = 0; i < iStudentTable.length; i++) 022 iStudentTable[i] = new HashMap<ExamStudent, Set<Exam>>(); 023 iStudentDayTable = new Map[model.getNrDays()]; 024 for (int i = 0; i < iStudentDayTable.length; i++) 025 iStudentDayTable[i] = new HashMap<ExamStudent, Set<Exam>>(); 026 iInstructorTable = new Map[model.getNrPeriods()]; 027 for (int i = 0; i < iInstructorTable.length; i++) 028 iInstructorTable[i] = new HashMap<ExamInstructor, Set<Exam>>(); 029 iInstructorDayTable = new Map[model.getNrDays()]; 030 for (int i = 0; i < iInstructorDayTable.length; i++) 031 iInstructorDayTable[i] = new HashMap<ExamInstructor, Set<Exam>>(); 032 for (Exam exam: model.variables()) { 033 ExamPlacement placement = assignment.getValue(exam); 034 if (placement != null) 035 assigned(assignment, placement); 036 } 037 } 038 039 @Override 040 public void assigned(Assignment<Exam, ExamPlacement> assignment, ExamPlacement placement) { 041 int period = placement.getPeriod().getIndex(); 042 int day = placement.getPeriod().getDay(); 043 for (ExamStudent student: placement.variable().getStudents()) { 044 Set<Exam> examsThisPeriod = iStudentTable[period].get(student); 045 if (examsThisPeriod == null) { 046 examsThisPeriod = new HashSet<Exam>(); 047 iStudentTable[period].put(student, examsThisPeriod); 048 } 049 examsThisPeriod.add(placement.variable()); 050 Set<Exam> examsThisDay = iStudentDayTable[day].get(student); 051 if (examsThisDay == null) { 052 examsThisDay = new HashSet<Exam>(); 053 iStudentDayTable[day].put(student, examsThisDay); 054 } 055 examsThisDay.add(placement.variable()); 056 } 057 for (ExamInstructor instructor: placement.variable().getInstructors()) { 058 Set<Exam> examsThisPeriod = iInstructorTable[period].get(instructor); 059 if (examsThisPeriod == null) { 060 examsThisPeriod = new HashSet<Exam>(); 061 iInstructorTable[period].put(instructor, examsThisPeriod); 062 } 063 examsThisPeriod.add(placement.variable()); 064 Set<Exam> examsThisDay = iInstructorDayTable[day].get(instructor); 065 if (examsThisDay == null) { 066 examsThisDay = new HashSet<Exam>(); 067 iInstructorDayTable[day].put(instructor, examsThisDay); 068 } 069 examsThisDay.add(placement.variable()); 070 } 071 } 072 073 @Override 074 public void unassigned(Assignment<Exam, ExamPlacement> assignment, ExamPlacement placement) { 075 int period = placement.getPeriod().getIndex(); 076 int day = placement.getPeriod().getDay(); 077 for (ExamStudent student: placement.variable().getStudents()) { 078 Set<Exam> examsThisPeriod = iStudentTable[period].get(student); 079 examsThisPeriod.remove(placement.variable()); 080 if (examsThisPeriod.isEmpty()) 081 iStudentTable[period].remove(student); 082 Set<Exam> examsThisDay = iStudentDayTable[day].get(student); 083 examsThisDay.remove(placement.variable()); 084 if (examsThisDay.isEmpty()) 085 iStudentDayTable[day].remove(student); 086 }for (ExamInstructor instructor: placement.variable().getInstructors()) { 087 Set<Exam> examsThisPeriod = iInstructorTable[period].get(instructor); 088 examsThisPeriod.remove(placement.variable()); 089 if (examsThisPeriod.isEmpty()) 090 iInstructorTable[period].remove(instructor); 091 Set<Exam> examsThisDay = iInstructorDayTable[day].get(instructor); 092 examsThisDay.remove(placement.variable()); 093 if (examsThisDay.isEmpty()) 094 iInstructorDayTable[day].remove(instructor); 095 } 096 } 097 098 public Map<ExamStudent, Set<Exam>> getStudentsOfPeriod(int period) { return iStudentTable[period]; } 099 100 public Map<ExamStudent, Set<Exam>> getStudentsOfDay(int day) { return iStudentDayTable[day]; } 101 102 public Map<ExamInstructor, Set<Exam>> getInstructorsOfPeriod(int period) { return iInstructorTable[period]; } 103 104 public Map<ExamInstructor, Set<Exam>> getInstructorsOfDay(int day) { return iInstructorDayTable[day]; } 105}