Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve information exported to bullet software FIST-868 #resolve #743

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public static Stream<BulletCharacteristic> all(final DumpContext context) {
* because some characteristics represent undesirable/erroneous rooms (Antecâmara, Secretariado...)
* Once sure these rooms are not included then simply return the remaining map values */
Set<String> present = new HashSet<>();
Set<String> remaining = new HashSet<>(patterns.values());
// Set<String> remaining = new HashSet<>(patterns.values());
Iterator<BulletRoom> rooms = context.all(BulletRoom.class).iterator();
while (!remaining.isEmpty() || rooms.hasNext()) {
while (/*!remaining.isEmpty() || */ rooms.hasNext()) {
Set<String> c = getCharacteristics(rooms.next().space).collect(Collectors.toSet());
remaining.removeAll(c);
// remaining.removeAll(c);
present.addAll(c);
}
return present.stream().map(BulletCharacteristic::new);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package pt.ist.fenixedu.bullet.domain;

import org.fenixedu.academic.domain.*;

import java.util.LinkedHashMap;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.fenixedu.academic.domain.ExecutionCourse;

public class BulletClass extends BulletObject {
private BulletPlan plan;
private String id;
Expand All @@ -19,19 +16,50 @@ private BulletClass(BulletPlan plan, String id, int students) {
this.students = students;
}

private static ExecutionSemester getExecutionSemester(final DumpContext context) {
final ExecutionSemester previous = context.baseSemester;
return previous == null ? null : previous.getPreviousExecutionPeriod();
}

private static int numberOfStudentsInClass(final SchoolClass schoolClass) {
final int numbberOfStudents = schoolClass.getAssociatedShiftsSet().stream()
.filter(shift -> !shift.getTypes().contains(ShiftType.TEORICA))
.mapToInt(shift -> Math.round(factorFor(schoolClass, shift) * Math.abs(shift.getLotacao().intValue())))
.filter(capacity -> capacity > 0)
.min().orElse(0);
return numbberOfStudents;
}

private static float factorFor(final SchoolClass schoolClass, final Shift shift) {
final int otherStudentCount = shift.getAssociatedClassesSet().stream()
.map(someClass -> someClass.getExecutionDegree())
.distinct()
.filter(executionDegree -> executionDegree != schoolClass.getExecutionDegree())
.mapToInt(executionDegree -> studentCount(executionDegree))
.sum();
final int studentCount = studentCount(schoolClass.getExecutionDegree());
return otherStudentCount == 0 ? 1 : studentCount == 0 ? 0 : (studentCount / (studentCount + otherStudentCount));
}

private static int studentCount(final ExecutionDegree executionDegree) {
final DegreeCurricularPlan dcp = executionDegree.getDegreeCurricularPlan();
return Math.toIntExact(dcp.getStudentCurricularPlansSet().stream()
.map(scp -> scp.getRegistration())
.filter(r -> r.isActive())
.count());
}

public static Stream<BulletClass> all(final DumpContext context) {
//XXX we're estimating based on number of shifts however we should also regard number of enrolments with respect to the class's students limit
return context.all(BulletPlan.class).stream().flatMap(plan -> {
Set<ExecutionCourse> courses = plan.getTargetExecutions(context).collect(Collectors.toSet());
int totalClasses = courses.stream().flatMap(ec -> ec.getShiftTypes().stream().map(ec::getNumberOfShifts)).mapToInt(Integer::intValue).max().orElse(0);
if (totalClasses == 0) {
//XXX usually happens for master's last semester and third cycle plans and these shouldn't have any loads either, so it should be ok to have no classes at generation time
return Stream.of();
}
double totalStudents = courses.stream().map(ExecutionCourse::getEnrolmentCount).mapToInt(Integer::intValue).max().getAsInt();
int expectedPerClass = new Double(Math.ceil(totalStudents / totalClasses)).intValue();
return IntStream.range(1, totalClasses + 1).mapToObj(id -> new BulletClass(plan, String.valueOf(id), expectedPerClass));
});
final ExecutionSemester targetSemester = getExecutionSemester(context);
if (targetSemester == null) {
return Stream.empty();
}
return context.all(BulletPlan.class).stream().flatMap(plan ->
plan.getDegreeCurricularPlan().getExecutionDegreesSet().stream()
.filter(ed -> ed.getExecutionYear().getExecutionPeriodsSet().contains(targetSemester))
.flatMap(ed -> ed.getSchoolClassesSet().stream())
.filter(schoolClass -> schoolClass.getExecutionPeriod() == targetSemester)
.map(schoolClass -> new BulletClass(plan, schoolClass.getNome(), numberOfStudentsInClass(schoolClass))));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.fenixedu.academic.domain.ExecutionCourse;
import org.fenixedu.academic.domain.Professorship;
import org.fenixedu.academic.domain.Teacher;
import org.fenixedu.academic.domain.exceptions.DomainException;

public class BulletCourse extends BulletObject {
private ExecutionCourse course;
Expand Down Expand Up @@ -40,5 +41,14 @@ public void populateSlots(final DumpContext context, final LinkedHashMap<String,
departments.retainAll(teacherDepartments);
}
slots.put(BulletObjectTag.AREA.unit(), departments.isEmpty() ? "" : BulletArea.key(departments.iterator().next()));
slots.put(BulletObjectTag.ECTS.unit(), creditsFor(course));
}

private String creditsFor(final ExecutionCourse course) {
try {
return course.getEctsCredits() == null ? "0" : course.getEctsCredits().toString();
} catch (final DomainException ex) {
return "Data is not consistent";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public enum BulletObjectTag {
COURSE("Disciplina", "Disciplinas", BulletCourse.class, BulletObjectType.CURRICULUM),
LOAD("CargaSemanal", "CargasSemanais", BulletLoad.class, BulletObjectType.SCHEDULE),
CLASS("Turma", "Turmas", BulletClass.class, BulletObjectType.ENTITY),
NEWCLASS("Turma", "Turmas", NewBulletClass.class, BulletObjectType.ENTITY),
//NEWCLASS("Turma", "Turmas", NewBulletClass.class, BulletObjectType.ENTITY),

NAME("Nome"),
DESCRIPTION("Descricao"),
Expand All @@ -42,7 +42,8 @@ public enum BulletObjectTag {
PLAN_CODE("CodigoPlanoCurricular"),
NUMBER_STUDENTS("NumeroAlunos"),
MAX_LIMIT("LimiteMaximo"),
CONSECUTIVE_LIMIT("LimiteConsecutivo");
CONSECUTIVE_LIMIT("LimiteConsecutivo"),
ECTS("ECTS");

private String unit, group = null;
private Class<? extends BulletObject> entity = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,126 @@
package pt.ist.fenixedu.bullet.domain;

import java.util.LinkedHashMap;
import org.fenixedu.academic.domain.*;
import org.fenixedu.academic.domain.degreeStructure.RegimeType;
import org.fenixedu.academic.domain.organizationalStructure.ScientificAreaUnit;
import org.fenixedu.academic.domain.student.Registration;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.fenixedu.academic.domain.CurricularSemester;
import org.fenixedu.academic.domain.DegreeCurricularPlan;
import org.fenixedu.academic.domain.ExecutionCourse;
import org.fenixedu.academic.domain.degreeStructure.RegimeType;

public class BulletPlan extends BulletObject {
protected static final String CODE_DELIM = "/", NAME_DELIM = " - ";
private DegreeCurricularPlan plan;
private CurricularSemester semester;
private String courseGroupName;
private Set<CurricularCourse> courseGroup;

private BulletPlan(DegreeCurricularPlan plan, CurricularSemester semester) {
private BulletPlan(DegreeCurricularPlan plan, CurricularSemester semester, Set<CurricularCourse> courseGroup, String courseGroupName) {
//XXX Plans are not divided by specialization
this.plan = plan;
this.semester = semester;
this.courseGroup = courseGroup;
this.courseGroupName = courseGroupName;
}

public static Stream<BulletPlan> all(final DumpContext context) {
return context.plans.stream()
.flatMap(plan -> context.semesters.stream()
.filter(semester -> plan.getDegreeModuleScopes().stream()
.map(scope -> CurricularSemester.readBySemesterAndYear(scope.getCurricularSemester(), scope.getCurricularYear()))
.collect(Collectors.toSet()).contains(semester))
.map(semester -> new BulletPlan(plan, semester)));
return context.plans.stream().flatMap(dcp -> toPlansFor(context.baseSemester, dcp));
}

private static Stream<BulletPlan> toPlansFor(final ExecutionSemester executionSemester, final DegreeCurricularPlan degreeCurricularPlan) {
final Map<CurricularSemester, Set<CurricularCourse>> coursesByYear = new HashMap<>();
/*
final int numberOfYears = degreeCurricularPlan.getDurationInYears();
for (int curricularYear = 1; curricularYear <= numberOfYears; curricularYear++) {
coursesByYear.put(curricularYear, new HashSet<>());
}
*/
final Map<CurricularCourse, Set<Registration>> studentsByCourse = new HashMap<>();

for (final CurricularCourse curricularCourse : degreeCurricularPlan.getCurricularCoursesSet()) {
for (final DegreeModuleScope degreeModuleScope : curricularCourse.getDegreeModuleScopes()) {
final CurricularSemester curricularSemester = CurricularSemester.readBySemesterAndYear(
degreeModuleScope.getCurricularSemester(), degreeModuleScope.getCurricularYear());
if (curricularSemester.getSemester().intValue() == executionSemester.getSemester().intValue()) {
if (!coursesByYear.containsKey(curricularSemester)) {
coursesByYear.put(curricularSemester, new HashSet<>());
}
coursesByYear.get(curricularSemester).add(curricularCourse);
}
}
final Set<Registration> registrations = curricularCourse.getCurriculumModulesSet().stream()
.filter(cm -> cm.isEnrolment())
.map(cm -> (Enrolment) cm)
.filter(e -> e.getExecutionYear().isCurrent())
.map(e -> e.getRegistration())
.collect(Collectors.toSet());
studentsByCourse.put(curricularCourse, registrations);
}

final Map<String, Set<CurricularCourse>> ccMap = new HashMap<>();
studentsByCourse.forEach((cc, set) -> {
final Set<CurricularCourse> courses = new TreeSet<>((cc1, cc2) -> cc1.getExternalId().compareTo(cc2.getExternalId()));
courses.add(cc);
studentsByCourse.forEach((occ, oset) -> {
if (cc != occ) {
final Set<Registration> intersection = new HashSet<>(set);
intersection.removeAll(oset);
if (intersection.size() >= (set.size() / 2)) {
courses.add(occ);
}
}
});
if (courses.size() >= 5) {
final String key = courses.stream()
.map(scc -> cc.getExternalId())
.collect(Collectors.joining(", "));
ccMap.put(key, courses);
}
});

final Set<BulletPlan> result = new HashSet<>();
for (final Set<CurricularCourse> set : ccMap.values()) {
final CurricularSemester curricularSemester = curricularYearFor(set, coursesByYear);
if (curricularSemester != null) {
result.add(new BulletPlan(degreeCurricularPlan, curricularSemester, set, "T" + (result.size() + 1)));
}
}
return result.stream();
}

private static CurricularSemester curricularYearFor(final Set<CurricularCourse> set, final Map<CurricularSemester, Set<CurricularCourse>> coursesByYear) {
for (final CurricularCourse curricularCourse : set) {
for (final Map.Entry<CurricularSemester, Set<CurricularCourse>> e : coursesByYear.entrySet()) {
if (e.getValue().contains(curricularCourse)) {
return e.getKey();
}
}
}
return null;
}

protected String key() {
return String.join(CODE_DELIM, plan.getName(), semester.getCurricularYear().getYear().toString(), semester.getSemester().toString());
return courseGroupName == null ?
String.join(CODE_DELIM, plan.getName(), semester.getCurricularYear().getYear().toString(), semester.getSemester().toString()) :
String.join(CODE_DELIM, plan.getName(), semester.getCurricularYear().getYear().toString(), semester.getSemester().toString(), courseGroupName);
}

protected String getName() {
return String.join(" - ", plan.getPresentationName(), semester.getCurricularYear().getYear() + "º Ano", semester.getSemester() + "º Semestre");
return courseGroupName == null ? String.join(" - ", plan.getName(),
semester.getCurricularYear().getYear().toString(),
semester.getSemester().toString()) :
String.join(" - ", plan.getName(),
courseGroupName,
semester.getCurricularYear().getYear().toString(),
semester.getSemester().toString());
}

public Stream<ExecutionCourse> getTargetExecutions(final DumpContext context) {
//XXX It's safe to assume that multiple executions per semester and curricular are a thing of the past
return plan.getExecutionCoursesByExecutionPeriod(context.baseSemester).stream()
.filter(ec -> ec.getCurricularCourseFor(plan).getParentContextsSet().stream().anyMatch(c -> c.isOpen(context.baseSemester)
&& c.containsSemesterAndCurricularYear(semester.getSemester(), semester.getCurricularYear().getYear(), RegimeType.SEMESTRIAL)));
return courseGroup.stream()
.flatMap(cc -> cc.getAssociatedExecutionCoursesSet().stream())
.filter(ec -> ec.getExecutionPeriod() == context.baseSemester);
}

@Override
Expand All @@ -53,4 +132,8 @@ public void populateSlots(final DumpContext context, final LinkedHashMap<String,
slots.put(BulletObjectTag.MANDATORY_COURSE_CODE.unit(), getTargetExecutions(context).map(BulletCourse::key).collect(Collectors.joining(",")));
slots.put(BulletObjectTag.OPTIONAL_COURSE_CODE.unit(), "");
}

public DegreeCurricularPlan getDegreeCurricularPlan() {
return plan;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package pt.ist.fenixedu.bullet.domain;

import java.util.LinkedHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.fenixedu.academic.domain.space.SpaceUtils;
import org.fenixedu.academic.domain.Lesson;
import org.fenixedu.bennu.core.domain.User;
import org.fenixedu.bennu.core.security.Authenticate;
import org.fenixedu.spaces.domain.Space;
import org.fenixedu.spaces.domain.SpaceClassification;

import java.util.LinkedHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class BulletRoom extends BulletSpace {

Expand All @@ -17,13 +18,39 @@ public BulletRoom(Space space) {

public static Stream<BulletRoom> all(final DumpContext context) {
//XXX SpaceUtils forEducation method is user relative
User GOPUser = User.findByUsername("ist22986"); // Suzana Visenjou
final User GOPUser = User.findByUsername("ist22986"); // Suzana Visenjou
Authenticate.mock(GOPUser, "Script");
Stream<BulletRoom> rooms = SpaceUtils.allocatableSpacesForEducation().filter(s -> s.getAllocatableCapacity() != 0).map(BulletRoom::new);
/*
Stream<BulletRoom> rooms = SpaceUtils.allocatableSpacesForEducation()
.filter(s -> s.getAllocatableCapacity() != 0)
.filter(s -> isForEducation(s))
.map(BulletRoom::new);
*/

Stream<BulletRoom> rooms = Stream.of(context.baseSemester.getPreviousExecutionPeriod(), context.baseSemester)
.flatMap(es -> es.getAssociatedExecutionCoursesSet().stream())
.flatMap(ec -> ec.getCourseLoadsSet().stream())
.flatMap(cl -> cl.getShiftsSet().stream())
.flatMap(s -> s.getAssociatedLessonsSet().stream())
.flatMap(l -> roomStream(l))
.distinct()
.map(BulletRoom::new);

Authenticate.unmock();
return rooms;
}

private static Stream<Space> roomStream(final Lesson lesson) {
return Stream.concat(lesson.getLessonInstancesSet().stream().map(i -> i.getRoom()).filter(r -> r != null),
Stream.of(lesson).map(l -> l.getRoomOccupation()).filter(o -> o != null).map(o -> o.getRoom()));
}

private static boolean isForEducation(final Space space) {
final SpaceClassification spaceClassification = space.getClassification();
final String code = spaceClassification == null ? null : spaceClassification.getAbsoluteCode();
return code != null && (code.startsWith("1.") || code.startsWith("2."));
}

@Override
public void populateSlots(final DumpContext context, final LinkedHashMap<String, String> slots) {
super.populateSlots(context, slots);
Expand Down