use of org.cpsolver.studentsct.reservation.CourseReservation in project cpsolver by UniTime.
the class StudentSectioningXMLLoader method loadReservation.
/**
* Load reservation
* @param reservationEl reservation element
* @param offering parent offering
* @param configTable config table (of the offering)
* @param sectionTable section table (of the offering)
* @return loaded reservation
*/
protected Reservation loadReservation(Element reservationEl, Offering offering, HashMap<Long, Config> configTable, HashMap<Long, Section> sectionTable) {
Reservation r = null;
if ("individual".equals(reservationEl.attributeValue("type"))) {
Set<Long> studentIds = new HashSet<Long>();
for (Iterator<?> k = reservationEl.elementIterator("student"); k.hasNext(); ) {
Element studentEl = (Element) k.next();
studentIds.add(Long.parseLong(studentEl.attributeValue("id")));
}
r = new IndividualReservation(Long.valueOf(reservationEl.attributeValue("id")), offering, studentIds);
} else if ("group".equals(reservationEl.attributeValue("type"))) {
Set<Long> studentIds = new HashSet<Long>();
for (Iterator<?> k = reservationEl.elementIterator("student"); k.hasNext(); ) {
Element studentEl = (Element) k.next();
studentIds.add(Long.parseLong(studentEl.attributeValue("id")));
}
r = new GroupReservation(Long.valueOf(reservationEl.attributeValue("id")), Double.parseDouble(reservationEl.attributeValue("limit", "-1")), offering, studentIds);
} else if ("curriculum".equals(reservationEl.attributeValue("type"))) {
List<String> classifications = new ArrayList<String>();
for (Iterator<?> k = reservationEl.elementIterator("classification"); k.hasNext(); ) {
Element clasfEl = (Element) k.next();
classifications.add(clasfEl.attributeValue("code"));
}
List<String> majors = new ArrayList<String>();
for (Iterator<?> k = reservationEl.elementIterator("major"); k.hasNext(); ) {
Element majorEl = (Element) k.next();
majors.add(majorEl.attributeValue("code"));
}
r = new CurriculumReservation(Long.valueOf(reservationEl.attributeValue("id")), Double.parseDouble(reservationEl.attributeValue("limit", "-1")), offering, reservationEl.attributeValue("area"), classifications, majors);
} else if ("course".equals(reservationEl.attributeValue("type"))) {
long courseId = Long.parseLong(reservationEl.attributeValue("course"));
for (Course course : offering.getCourses()) {
if (course.getId() == courseId)
r = new CourseReservation(Long.valueOf(reservationEl.attributeValue("id")), course);
}
} else if ("dummy".equals(reservationEl.attributeValue("type"))) {
r = new DummyReservation(offering);
} else if ("override".equals(reservationEl.attributeValue("type"))) {
Set<Long> studentIds = new HashSet<Long>();
for (Iterator<?> k = reservationEl.elementIterator("student"); k.hasNext(); ) {
Element studentEl = (Element) k.next();
studentIds.add(Long.parseLong(studentEl.attributeValue("id")));
}
r = new ReservationOverride(Long.valueOf(reservationEl.attributeValue("id")), offering, studentIds);
}
if (r == null) {
sLogger.error("Unknown reservation type " + reservationEl.attributeValue("type"));
return null;
}
r.setExpired("true".equals(reservationEl.attributeValue("expired", "false")));
for (Iterator<?> k = reservationEl.elementIterator("config"); k.hasNext(); ) {
Element configEl = (Element) k.next();
r.addConfig(configTable.get(Long.parseLong(configEl.attributeValue("id"))));
}
for (Iterator<?> k = reservationEl.elementIterator("section"); k.hasNext(); ) {
Element sectionEl = (Element) k.next();
r.addSection(sectionTable.get(Long.parseLong(sectionEl.attributeValue("id"))));
}
r.setPriority(Integer.parseInt(reservationEl.attributeValue("priority", String.valueOf(r.getPriority()))));
r.setMustBeUsed("true".equals(reservationEl.attributeValue("mustBeUsed", r.mustBeUsed() ? "true" : "false")));
r.setAllowOverlap("true".equals(reservationEl.attributeValue("allowOverlap", r.isAllowOverlap() ? "true" : "false")));
r.setCanAssignOverLimit("true".equals(reservationEl.attributeValue("canAssignOverLimit", r.canAssignOverLimit() ? "true" : "false")));
return r;
}
use of org.cpsolver.studentsct.reservation.CourseReservation in project cpsolver by UniTime.
the class StudentSectioningXMLSaver method saveReservation.
/**
* Save reservation
* @param reservationEl reservation element to be populated
* @param reservation reservation to be saved
*/
protected void saveReservation(Element reservationEl, Reservation reservation) {
reservationEl.addAttribute("id", getId("reservation", reservation.getId()));
reservationEl.addAttribute("expired", reservation.isExpired() ? "true" : "false");
if (reservation instanceof GroupReservation) {
GroupReservation gr = (GroupReservation) reservation;
reservationEl.addAttribute("type", "group");
for (Long studentId : gr.getStudentIds()) reservationEl.addElement("student").addAttribute("id", getId("student", studentId));
if (gr.getReservationLimit() >= 0.0)
reservationEl.addAttribute("limit", String.valueOf(gr.getReservationLimit()));
} else if (reservation instanceof ReservationOverride) {
reservationEl.addAttribute("type", "override");
ReservationOverride o = (ReservationOverride) reservation;
for (Long studentId : o.getStudentIds()) reservationEl.addElement("student").addAttribute("id", getId("student", studentId));
} else if (reservation instanceof IndividualReservation) {
reservationEl.addAttribute("type", "individual");
for (Long studentId : ((IndividualReservation) reservation).getStudentIds()) reservationEl.addElement("student").addAttribute("id", getId("student", studentId));
} else if (reservation instanceof CurriculumReservation) {
reservationEl.addAttribute("type", "curriculum");
CurriculumReservation cr = (CurriculumReservation) reservation;
if (cr.getReservationLimit() >= 0.0)
reservationEl.addAttribute("limit", String.valueOf(cr.getReservationLimit()));
reservationEl.addAttribute("area", cr.getAcademicArea());
for (String clasf : cr.getClassifications()) reservationEl.addElement("classification").addAttribute("code", clasf);
for (String major : cr.getMajors()) reservationEl.addElement("major").addAttribute("code", major);
} else if (reservation instanceof CourseReservation) {
reservationEl.addAttribute("type", "course");
CourseReservation cr = (CourseReservation) reservation;
reservationEl.addAttribute("course", getId("course", cr.getCourse().getId()));
} else if (reservation instanceof DummyReservation) {
reservationEl.addAttribute("type", "dummy");
}
reservationEl.addAttribute("priority", String.valueOf(reservation.getPriority()));
reservationEl.addAttribute("mustBeUsed", reservation.mustBeUsed() ? "true" : "false");
reservationEl.addAttribute("allowOverlap", reservation.isAllowOverlap() ? "true" : "false");
reservationEl.addAttribute("canAssignOverLimit", reservation.canAssignOverLimit() ? "true" : "false");
for (Config config : reservation.getConfigs()) reservationEl.addElement("config").addAttribute("id", getId("config", config.getId()));
for (Map.Entry<Subpart, Set<Section>> entry : reservation.getSections().entrySet()) {
for (Section section : entry.getValue()) {
reservationEl.addElement("section").addAttribute("id", getId("section", section.getId()));
}
}
}
use of org.cpsolver.studentsct.reservation.CourseReservation in project cpsolver by UniTime.
the class Test method clone.
protected Course clone(Course course, long studentId, Student originalStudent, Map<Long, Section> classTable, StudentSectioningModel model) {
Offering clonedOffering = new Offering(course.getOffering().getId(), course.getOffering().getName());
clonedOffering.setModel(model);
int courseLimit = course.getLimit();
if (courseLimit >= 0) {
courseLimit -= course.getEnrollments(assignment()).size();
if (courseLimit < 0)
courseLimit = 0;
for (Iterator<Enrollment> i = course.getEnrollments(assignment()).iterator(); i.hasNext(); ) {
Enrollment enrollment = i.next();
if (enrollment.getStudent().getId() == studentId) {
courseLimit++;
break;
}
}
}
Course clonedCourse = new Course(course.getId(), course.getSubjectArea(), course.getCourseNumber(), clonedOffering, courseLimit, course.getProjected());
clonedCourse.setNote(course.getNote());
Hashtable<Config, Config> configs = new Hashtable<Config, Config>();
Hashtable<Subpart, Subpart> subparts = new Hashtable<Subpart, Subpart>();
Hashtable<Section, Section> sections = new Hashtable<Section, Section>();
for (Iterator<Config> e = course.getOffering().getConfigs().iterator(); e.hasNext(); ) {
Config config = e.next();
int configLimit = config.getLimit();
int configEnrollment = config.getEnrollments(assignment()).size();
if (configLimit >= 0) {
configLimit -= config.getEnrollments(assignment()).size();
if (configLimit < 0)
configLimit = 0;
for (Iterator<Enrollment> i = config.getEnrollments(assignment()).iterator(); i.hasNext(); ) {
Enrollment enrollment = i.next();
if (enrollment.getStudent().getId() == studentId) {
configLimit++;
configEnrollment--;
break;
}
}
}
OnlineConfig clonedConfig = new OnlineConfig(config.getId(), configLimit, config.getName(), clonedOffering);
clonedConfig.setInstructionalMethodId(config.getInstructionalMethodId());
clonedConfig.setInstructionalMethodName(config.getInstructionalMethodName());
clonedConfig.setEnrollment(configEnrollment);
configs.put(config, clonedConfig);
for (Iterator<Subpart> f = config.getSubparts().iterator(); f.hasNext(); ) {
Subpart subpart = f.next();
Subpart clonedSubpart = new Subpart(subpart.getId(), subpart.getInstructionalType(), subpart.getName(), clonedConfig, (subpart.getParent() == null ? null : subparts.get(subpart.getParent())));
clonedSubpart.setAllowOverlap(subpart.isAllowOverlap());
clonedSubpart.setCredit(subpart.getCredit());
subparts.put(subpart, clonedSubpart);
for (Iterator<Section> g = subpart.getSections().iterator(); g.hasNext(); ) {
Section section = g.next();
int limit = section.getLimit();
int enrl = section.getEnrollments(assignment()).size();
if (limit >= 0) {
// limited section, deduct enrollments
limit -= section.getEnrollments(assignment()).size();
if (limit < 0)
// over-enrolled, but not unlimited
limit = 0;
if (studentId >= 0)
for (Enrollment enrollment : section.getEnrollments(assignment())) if (enrollment.getStudent().getId() == studentId) {
limit++;
enrl--;
break;
}
}
OnlineSection clonedSection = new OnlineSection(section.getId(), limit, section.getName(course.getId()), clonedSubpart, section.getPlacement(), section.getInstructors(), (section.getParent() == null ? null : sections.get(section.getParent())));
clonedSection.setName(-1l, section.getName(-1l));
clonedSection.setNote(section.getNote());
clonedSection.setSpaceExpected(section.getSpaceExpected());
clonedSection.setSpaceHeld(section.getSpaceHeld());
clonedSection.setEnrollment(enrl);
clonedSection.setCancelled(section.isCancelled());
if (section.getIgnoreConflictWithSectionIds() != null)
for (Long id : section.getIgnoreConflictWithSectionIds()) clonedSection.addIgnoreConflictWith(id);
if (limit > 0) {
double available = Math.round(section.getSpaceExpected() - limit);
clonedSection.setPenalty(available / section.getLimit());
}
sections.put(section, clonedSection);
classTable.put(section.getId(), clonedSection);
}
}
}
if (course.getOffering().hasReservations()) {
for (Reservation reservation : course.getOffering().getReservations()) {
int reservationLimit = (int) Math.round(reservation.getLimit());
if (reservationLimit >= 0) {
reservationLimit -= reservation.getEnrollments(assignment()).size();
if (reservationLimit < 0)
reservationLimit = 0;
for (Iterator<Enrollment> i = reservation.getEnrollments(assignment()).iterator(); i.hasNext(); ) {
Enrollment enrollment = i.next();
if (enrollment.getStudent().getId() == studentId) {
reservationLimit++;
break;
}
}
if (reservationLimit <= 0 && !reservation.mustBeUsed())
continue;
}
boolean applicable = originalStudent != null && reservation.isApplicable(originalStudent);
if (reservation instanceof CourseReservation)
applicable = (course.getId() == ((CourseReservation) reservation).getCourse().getId());
if (reservation instanceof org.cpsolver.studentsct.reservation.DummyReservation) {
// the student is already enrolled in the course
for (Enrollment enrollment : course.getEnrollments(assignment())) if (enrollment.getStudent().getId() == studentId) {
applicable = true;
break;
}
}
Reservation clonedReservation = new OnlineReservation(0, reservation.getId(), clonedOffering, reservation.getPriority(), reservation.canAssignOverLimit(), reservationLimit, applicable, reservation.mustBeUsed(), reservation.isAllowOverlap(), reservation.isExpired());
for (Config config : reservation.getConfigs()) clonedReservation.addConfig(configs.get(config));
for (Map.Entry<Subpart, Set<Section>> entry : reservation.getSections().entrySet()) {
Set<Section> clonedSections = new HashSet<Section>();
for (Section section : entry.getValue()) clonedSections.add(sections.get(section));
clonedReservation.getSections().put(subparts.get(entry.getKey()), clonedSections);
}
}
}
return clonedCourse;
}
Aggregations