Skip to content

Commit

Permalink
GRAD2-2023: refactored not to use map1996 and fixed the credits calcu…
Browse files Browse the repository at this point in the history
…lation for grad on minGrade12Credit courses with leftover credits that are used twice in elective rule.

GRAD2-2023: refactored not to use map1996 and fixed the credits calculation for grad on minGrade12Credit courses with leftover credits that are used twice in elective rule.
  • Loading branch information
infstar committed Dec 6, 2023
1 parent be27dc1 commit bfb368a
Show file tree
Hide file tree
Showing 35 changed files with 108 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class RuleProcessorData implements RuleData {
private List<ProgramAlgorithmRule> algorithmRules;
private List<ProgramRequirement> gradProgramRules;
private Map<String,OptionalProgramRuleProcessor> mapOptional;
private Map<String,Integer> map1996Crse;
private List<StudentCourse> studentCourses;
private List<StudentCourse> excludedCourses;
private List<StudentAssessment> excludedAssessments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ public static int processExtraCredits(boolean extraCreditsUsed, int extraCredits
}
} else {
if (totalCredits + sc.getCredits() <= requiredCredits) {
int credits = sc.isUsedInMinCreditRule() && (sc.getLeftOverCredits() != null && sc.getLeftOverCredits() > 0)? sc.getCredits() - sc.getLeftOverCredits() : sc.getCredits();
totalCredits += credits;
sc.setCreditsUsedForGrad(credits);
totalCredits += sc.getCredits();
sc.setCreditsUsedForGrad(sc.getCredits());
} else {
int extraCredits = totalCredits + sc.getCredits() - requiredCredits;
totalCredits = requiredCredits;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public RuleData fire() {

List<GradRequirement> requirementsMet = new ArrayList<>();
List<GradRequirement> requirementsNotMet = new ArrayList<>();
Map<String,Integer> map1996 = new HashMap<>();

List<StudentCourse> fineArtsCourseList = RuleProcessorRuleUtils
.getUniqueStudentCourses(ruleProcessorData.getStudentCourses(), ruleProcessorData.isProjected())
Expand Down Expand Up @@ -118,14 +117,13 @@ && isFineArtsOrAppliedSkillsRule(pr.getProgramRequirementCode().getProReqCode())
if (tempProgramRule != null
&& tempCourse.getCredits() > Integer.parseInt(tempProgramRule.getProgramRequirementCode().getRequiredCredits())) {
int extraCredits = tempCourse.getCredits() - Integer.parseInt(tempProgramRule.getProgramRequirementCode().getRequiredCredits());
map1996.put(tempCourse.getCourseCode(), extraCredits);
tempCourse.setLeftOverCredits(extraCredits);
}
}
}
}
logger.debug("Temp Program Rule: {}", tempProgramRule);
processCourse(tempCourse, tempCourseRequirements, tempProgramRule, requirementsMet, gradProgramRulesMatch, map1996);
processCourse(tempCourse, tempCourseRequirements, tempProgramRule, requirementsMet, gradProgramRulesMatch);

AlgorithmSupportRule.copyAndAddIntoStudentCoursesList(tempCourse, finalCourseList);
AlgorithmSupportRule.copyAndAddIntoProgramRulesList(tempProgramRule, finalProgramRulesList);
Expand Down Expand Up @@ -166,27 +164,25 @@ && isFineArtsOrAppliedSkillsRule(pr.getProgramRequirementCode().getProReqCode())

if (tempProgramRule != null && tempCourse.getCredits() > Integer.parseInt(tempProgramRule.getProgramRequirementCode().getRequiredCredits())) {
int extraCredits = tempCourse.getCredits() - Integer.parseInt(tempProgramRule.getProgramRequirementCode().getRequiredCredits());
map1996.put(tempCourse.getCourseCode(), extraCredits);
tempCourse.setLeftOverCredits(extraCredits);
}
}
}
}
logger.debug("Temp Program Rule: {}", tempProgramRule);
processCourse(tempCourse, tempCourseRequirements, tempProgramRule, requirementsMet, gradProgramRulesMatch, map1996);
processCourse(tempCourse, tempCourseRequirements, tempProgramRule, requirementsMet, gradProgramRulesMatch);

AlgorithmSupportRule.copyAndAddIntoStudentCoursesList(tempCourse, finalCourseList);
AlgorithmSupportRule.copyAndAddIntoProgramRulesList(tempProgramRule, finalProgramRulesList);
}

logger.debug("Final Program rules list: {}",finalProgramRulesList);
processReqMetAndNotMet(finalProgramRulesList,requirementsNotMet,finalCourseList,originalCourseRequirements,requirementsMet,gradProgramRulesMatch);
ruleProcessorData.setMap1996Crse(map1996);
checkAppliedScienceAndFineArtsCondition(ruleProcessorData.getStudentCourses(),ruleProcessorData.getRequirementsMet(),ruleProcessorData.getNonGradReasons(),ruleProcessorData.getMap1996Crse());
checkAppliedScienceAndFineArtsCondition(ruleProcessorData.getStudentCourses(),ruleProcessorData.getRequirementsMet(),ruleProcessorData.getNonGradReasons());
return ruleProcessorData;
}

private void checkAppliedScienceAndFineArtsCondition(List<StudentCourse> studentCourses, List<GradRequirement> requirementsMet, List<GradRequirement> nonGradReasons, Map<String, Integer> map1996Crse) {
private void checkAppliedScienceAndFineArtsCondition(List<StudentCourse> studentCourses, List<GradRequirement> requirementsMet, List<GradRequirement> nonGradReasons) {
boolean reqmtSatisfied = false;
int counter = 0; //counter to keep track of fine arts and applied science rule codes
for(GradRequirement gR:requirementsMet) {
Expand Down Expand Up @@ -215,15 +211,15 @@ private void checkAppliedScienceAndFineArtsCondition(List<StudentCourse> student
sc.setUsed(false);
sc.setUsedInMatchRule(false);

if(map1996Crse.get(sc.getCourseCode()) != null) {
map1996Crse.remove(sc.getCourseCode());
if (sc.getLeftOverCredits() != null && sc.getLeftOverCredits() > 0) {
sc.setLeftOverCredits(null);
}
}
}
}
}

private void processCourse(StudentCourse tempCourse, List<CourseRequirement> tempCourseRequirements, ProgramRequirement tempProgramRule, List<GradRequirement> requirementsMet, List<ProgramRequirement> gradProgramRulesMatch, Map<String,Integer> map1996) {
private void processCourse(StudentCourse tempCourse, List<CourseRequirement> tempCourseRequirements, ProgramRequirement tempProgramRule, List<GradRequirement> requirementsMet, List<ProgramRequirement> gradProgramRulesMatch) {
if (!tempCourseRequirements.isEmpty() && tempProgramRule != null) {
ProgramRequirement finalTempProgramRule = tempProgramRule;
if (requirementsMet.stream()
Expand Down Expand Up @@ -254,7 +250,6 @@ private void processCourse(StudentCourse tempCourse, List<CourseRequirement> tem
if (tempProgramRule != null) {
if(tempCourse.getCredits() > Integer.parseInt(tempProgramRule.getProgramRequirementCode().getRequiredCredits())) {
int extraCredits = tempCourse.getCredits() - Integer.parseInt(tempProgramRule.getProgramRequirementCode().getRequiredCredits());
map1996.put(tempCourse.getCourseCode(),extraCredits);
tempCourse.setLeftOverCredits(extraCredits);
}
setDetailsForCourses(tempCourse, tempProgramRule, requirementsMet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ public void processReqMet(StudentCourse sc, ProgramRequirement gradProgramRule,
sc.setUsed(true);
sc.setUsedInMinCreditRule(true);
if (totalCredits > requiredCredits) {
sc.setLeftOverCredits(totalCredits - requiredCredits);
int leftOverCredit = totalCredits - requiredCredits;
sc.setCreditsUsedForGrad(sc.getCredits() - leftOverCredit);
sc.setLeftOverCredits(leftOverCredit);
}
AlgorithmSupportRule.setGradReqMet(sc,gradProgramRule);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -13,7 +14,6 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
Expand All @@ -33,23 +33,22 @@ public RuleData fire() {

List<GradRequirement> requirementsNotMet = new ArrayList<>();

Map<String,Integer> map1996 = ruleProcessorData.getMap1996Crse();
int ldCourseCounter = 0;
List<StudentCourse> tempStudentCourseList = RuleProcessorRuleUtils
.getUniqueStudentCourses(ruleProcessorData.getStudentCourses(), ruleProcessorData.isProjected());
List<StudentCourse> minCreditGrade12Courses = tempStudentCourseList.stream().filter(StudentCourse::isUsedInMinCreditRule).collect(Collectors.toList());
List<StudentCourse> minCreditGrade12CoursesWithLeftOverCredits = minCreditGrade12Courses.stream()
.filter(sc -> sc.isUsedInMinCreditRule() && (sc.getLeftOverCredits() != null && sc.getLeftOverCredits() > 0)).collect(Collectors.toList());
tempStudentCourseList.removeAll(minCreditGrade12Courses);
tempStudentCourseList.addAll(minCreditGrade12CoursesWithLeftOverCredits);
tempStudentCourseList.sort(Comparator.comparing(StudentCourse::getCompletedCoursePercentage).reversed());

List<StudentCourse> studentCourses = new ArrayList<>(minCreditGrade12Courses);
studentCourses.addAll(tempStudentCourseList);

List<ProgramRequirement> gradProgramRules = ruleProcessorData
.getGradProgramRules().stream().filter(gpr -> "MCE".compareTo(gpr.getProgramRequirementCode().getRequirementTypeCode().getReqTypeCode()) == 0
&& "Y".compareTo(gpr.getProgramRequirementCode().getActiveRequirement()) == 0 && "C".compareTo(gpr.getProgramRequirementCode().getRequirementCategory()) == 0)
.distinct().toList();

if (studentCourses.isEmpty()) {
if (minCreditGrade12Courses.isEmpty() && tempStudentCourseList.isEmpty()) {
logger.warn("!!!Empty list sent to Min Elective Credits Rule for processing");
AlgorithmSupportRule.processEmptyCourseCondition(ruleProcessorData,ruleProcessorData.getGradProgramRules(),requirementsNotMet);
return ruleProcessorData;
Expand All @@ -59,52 +58,15 @@ public RuleData fire() {
if(gradProgramRule.getProgramRequirementCode().getRequiredLevel() == null) {
requiredCredits = Integer.parseInt(gradProgramRule.getProgramRequirementCode().getRequiredCredits().trim()); // list

for (StudentCourse sc : studentCourses) {
if (map1996.get(sc.getCourseCode()) != null) {
if (sc.getCourseCode().startsWith("X")) {
if (ldCourseCounter < 8) {
ldCourseCounter += map1996.get(sc.getCourseCode());
} else {
continue;
}
}
if (totalCredits + map1996.get(sc.getCourseCode()) <= requiredCredits) {
totalCredits += map1996.get(sc.getCourseCode());
sc.setCreditsUsedForGrad(sc.getCreditsUsedForGrad() + map1996.get(sc.getCourseCode()));
} else {
int extraCredits = totalCredits + map1996.get(sc.getCourseCode()) - requiredCredits;
totalCredits = requiredCredits;
sc.setCreditsUsedForGrad(sc.getCreditsUsedForGrad() + map1996.get(sc.getCourseCode()) - extraCredits);
}
AlgorithmSupportRule.setGradReqMet(sc,gradProgramRule);
sc.setUsed(true);
} else {
if (!sc.isUsedInMatchRule()) {
boolean extraCreditsUsed = false;
int extraCreditsLDcrses = 0;
if (sc.getCourseCode().startsWith("X")) {
if (ldCourseCounter < 8) {
if (ldCourseCounter + sc.getCredits() <= 8) {
ldCourseCounter += sc.getCredits();
} else {
int extraCredits = ldCourseCounter + sc.getCredits() - 8;
ldCourseCounter += extraCredits;
extraCreditsLDcrses = extraCredits;
extraCreditsUsed = true;
}
} else {
continue;
}
}
totalCredits = AlgorithmSupportRule.processExtraCredits(extraCreditsUsed,extraCreditsLDcrses,sc,totalCredits,requiredCredits);
AlgorithmSupportRule.setGradReqMet(sc,gradProgramRule);
sc.setUsed(true);
}
}
if (totalCredits == requiredCredits) {
break;
}
}
// 1st: minGrade12CreditCourses
Pair<Integer, Integer> counts = handleStudentCourses(gradProgramRule, requiredCredits, minCreditGrade12Courses, totalCredits, ldCourseCounter, true);
totalCredits = counts.getLeft();
ldCourseCounter = counts.getRight();

// 2nd: the rest of courses + (minGrade12CreditCourses with leftOverCredit > 0)
counts = handleStudentCourses(gradProgramRule, requiredCredits, tempStudentCourseList, totalCredits, ldCourseCounter, false);
totalCredits = counts.getLeft();

AlgorithmSupportRule.checkCredits1996(totalCredits,requiredCredits,gradProgramRule,ruleProcessorData);
totalCredits = 0;
}
Expand All @@ -115,7 +77,59 @@ public RuleData fire() {
return ruleProcessorData;
}


private Pair<Integer, Integer> handleStudentCourses(ProgramRequirement gradProgramRule, Integer requiredCredits, List<StudentCourse> studentCourses,
Integer totalCredits, Integer ldCourseCounter, boolean onlyMinGrade12CreditCourses) {
for (StudentCourse sc : studentCourses) {
if ((sc.isUsedInMatchRule() || sc.isUsedInMinCreditRule())
&& (sc.getLeftOverCredits() != null && sc.getLeftOverCredits() > 0)) {
if (sc.getCourseCode().startsWith("X")) {
if (ldCourseCounter < 8) {
ldCourseCounter += sc.getLeftOverCredits();
} else {
continue;
}
}
if (totalCredits + sc.getLeftOverCredits() <= requiredCredits) {
totalCredits += sc.getLeftOverCredits();
int credits = onlyMinGrade12CreditCourses? sc.getLeftOverCredits() : sc.getCreditsUsedForGrad() + sc.getLeftOverCredits();
sc.setCreditsUsedForGrad(credits > sc.getCredits() ? sc.getCredits() : credits);
} else {
int extraCredits = totalCredits + sc.getLeftOverCredits() - requiredCredits;
totalCredits = requiredCredits;
int credits = sc.getCreditsUsedForGrad() + sc.getLeftOverCredits() - extraCredits;
sc.setCreditsUsedForGrad(credits > sc.getCredits() ? sc.getCredits() : credits);
}
AlgorithmSupportRule.setGradReqMet(sc, gradProgramRule);
sc.setUsed(true);
} else {
if (!sc.isUsedInMatchRule()) {
boolean extraCreditsUsed = false;
int extraCreditsLDcrses = 0;
if (sc.getCourseCode().startsWith("X")) {
if (ldCourseCounter < 8) {
if (ldCourseCounter + sc.getCredits() <= 8) {
ldCourseCounter += sc.getCredits();
} else {
int extraCredits = ldCourseCounter + sc.getCredits() - 8;
ldCourseCounter += extraCredits;
extraCreditsLDcrses = extraCredits;
extraCreditsUsed = true;
}
} else {
continue;
}
}
totalCredits = AlgorithmSupportRule.processExtraCredits(extraCreditsUsed, extraCreditsLDcrses, sc, totalCredits, requiredCredits);
AlgorithmSupportRule.setGradReqMet(sc, gradProgramRule);
sc.setUsed(true);
}
}
if (totalCredits == requiredCredits) {
break;
}
}
return Pair.of(totalCredits, ldCourseCounter);
}

@Override
public void setInputData(RuleData inputData) {
Expand Down
1 change: 0 additions & 1 deletion api/src/test/resources/1950-122740988.json
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,6 @@
}
],
"mapOptional": {},
"map1996Crse": null,
"studentCourses": [
{
"pen": "122740988",
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/resources/1950-EN-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@
}
],
"mapOptional": {},
"map1996Crse": null,

"studentCourses": [
{
"pen": "120408505",
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/resources/1950-EN-3.json
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@
}
],
"mapOptional": {},
"map1996Crse": null,

"studentCourses": [
{
"pen": "120408505",
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/resources/1950-EN-4.json
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@
}
],
"mapOptional": {},
"map1996Crse": null,

"studentCourses": [
{
"pen": "120408505",
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/resources/1950-EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@
}
],
"mapOptional": {},
"map1996Crse": null,

"studentCourses": [
{
"pen": "124428368",
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/resources/1986-EN-105581557.json
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@
}
],
"mapOptional": {},
"map1996Crse": null,

"studentCourses": [
{
"pen": "105581557",
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/resources/1996-EN-101039378.json
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@
"optionalProgramGraduated": true
}
},
"map1996Crse": null,

"studentCourses": [
{
"pen": "101039378",
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/resources/1996-EN-101171718.json
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@
}
],
"mapOptional": {},
"map1996Crse": null,

"studentCourses": [
{
"pen": "101171718",
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/resources/1996-EN-101541068.json
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@
}
],
"mapOptional": {},
"map1996Crse": null,

"studentCourses": [
{
"pen": "101541068",
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/resources/1996-EN-101821056.json
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@
}
],
"mapOptional": {},
"map1996Crse": null,

"studentCourses": [
{
"pen": "101821056",
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/resources/1996-EN-104337712.json
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@
}
],
"mapOptional": {},
"map1996Crse": null,

"studentCourses": [
{
"pen": "104337712",
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/resources/1996-EN-106945306.json
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@
}
],
"mapOptional": {},
"map1996Crse": null,

"studentCourses": [
{
"pen": "106945306",
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/resources/1996-EN-109491597.json
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@
"optionalProgramGraduated": true
}
},
"map1996Crse": null,

"studentCourses": [
{
"pen": "109491597",
Expand Down
Loading

0 comments on commit bfb368a

Please sign in to comment.