From 604a38152c3e5739d4d3f73eb4ab9808ebf0d312 Mon Sep 17 00:00:00 2001
From: Tom Vahlman <tom@dsv.su.se>
Date: Mon, 27 Feb 2012 21:33:06 +0100
Subject: [PATCH] We must sort on the highest (individual availability) when
 the project classes are the same.

---
 .../scipro/match/GreedyMatchingAlgorithm.java |  4 +-
 .../scipro/match/dataobject/Availability.java | 13 ++-
 .../match/TestGreedyMatchingAlgorithm.java    | 89 +++++++------------
 3 files changed, 45 insertions(+), 61 deletions(-)

diff --git a/src/main/java/se/su/dsv/scipro/match/GreedyMatchingAlgorithm.java b/src/main/java/se/su/dsv/scipro/match/GreedyMatchingAlgorithm.java
index 9ffe337766..1a7a45b8f2 100644
--- a/src/main/java/se/su/dsv/scipro/match/GreedyMatchingAlgorithm.java
+++ b/src/main/java/se/su/dsv/scipro/match/GreedyMatchingAlgorithm.java
@@ -280,8 +280,8 @@ public class GreedyMatchingAlgorithm implements MatchingAlgorithm {
                     } else  if(!match.getProjectIdea().getProjectClass().equals(availability.getProjectClass()) &&
                             otherPair.getMatch().getProjectIdea().getProjectClass().equals(otherPair.getAvailability().getProjectClass())) {
                         return 1;
-                    } else {   // the supervisor can only one type of project class
-                        return 0;
+                    } else {   // the project class is the same sort on the highest availability
+                        return availability.compareTo(otherPair.getAvailability());
                     }
                 }
             }
diff --git a/src/main/java/se/su/dsv/scipro/match/dataobject/Availability.java b/src/main/java/se/su/dsv/scipro/match/dataobject/Availability.java
index ac83863365..3f06503a3f 100644
--- a/src/main/java/se/su/dsv/scipro/match/dataobject/Availability.java
+++ b/src/main/java/se/su/dsv/scipro/match/dataobject/Availability.java
@@ -8,7 +8,7 @@ import se.su.dsv.scipro.data.dataobjects.ProjectClass;
 /**
  * A class that specifies how available a supervisor is(in terms of thesis supervision)
  */
-public class Availability implements Serializable {
+public class Availability implements Serializable, Comparable<Availability> {
 
 	private static final long serialVersionUID = 1L;
 	
@@ -54,6 +54,17 @@ public class Availability implements Serializable {
     	numMatched = num;
     }
     
+    @Override
+    public int compareTo(Availability availability) {
+        if(getAvailability() > availability.getAvailability()) {
+            return -1;
+        } else if(availability.getAvailability() > getAvailability()) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+    
     @Override
     public String toString() {
         return "Availability [supervisorId=" + supervisor + ", numMatched="
diff --git a/src/test/java/se/su/dsv/scipro/match/TestGreedyMatchingAlgorithm.java b/src/test/java/se/su/dsv/scipro/match/TestGreedyMatchingAlgorithm.java
index 998d5fd839..9e66082e26 100644
--- a/src/test/java/se/su/dsv/scipro/match/TestGreedyMatchingAlgorithm.java
+++ b/src/test/java/se/su/dsv/scipro/match/TestGreedyMatchingAlgorithm.java
@@ -578,6 +578,7 @@ public class TestGreedyMatchingAlgorithm {
         unmatchedProjectIdeas.add(bachelorProjectIdea);
         Result result = new GreedyMatchingAlgorithm().match(supervisorAvailability, unmatchedProjectIdeas, weights);
         assertTrue(result.matches.size() > 0);
+
         assertTrue(result.matches.get(0).getProjectIdea().equals(bachelorProjectIdea));
         assertTrue(result.matches.get(0).getSupervisor().equals(masterSupervisor));
         assertTrue(result.unmatched.size() == 0);
@@ -586,78 +587,50 @@ public class TestGreedyMatchingAlgorithm {
     @Test
     @Transactional
     @Rollback
-    /*  test that a master which has filled up his slot for bachelor but has slots left for master can supervise a bachelor idea */
+    /*  test that the supervisor who has the highest total slot available will be chosen */
     public void testIncreaseSlotForMasterSupervisor() {
-        Employee bachelorSupervisor2 = createEmployee("David", "Hallberg", languages);
-        supervisorAvailability.add(new Availability(masterSupervisor, 3L, 3, bachelorProjectClass));
-        supervisorAvailability.add(new Availability(masterSupervisor, 3L, 4, masterProjectClass));
-        supervisorAvailability.add(new Availability(bachelorSupervisor2, 0L, 1, bachelorProjectClass));
-        unmatchedProjectIdeas.add(bachelorProjectIdea);
-        bachelorProjectIdea.setPreferredSupervisor(masterSupervisor);
-        Result result = new GreedyMatchingAlgorithm().match(supervisorAvailability, unmatchedProjectIdeas, weights);
-        assertTrue(result.matches.size() > 0);
-        assertTrue(result.matches.get(0).getProjectIdea().equals(bachelorProjectIdea));
-        assertTrue(result.matches.get(0).getSupervisor().equals(masterSupervisor));
-        assertTrue(result.unmatched.size() == 0);
-// increase num capable for the supervisor
+        Employee davidH = createEmployee("David", "Hallberg", languages);
+        Employee henrikH = createEmployee("Henrik", "Hansson", languages);
+//ProjectIdea firstBachelorIdea = createProjectIdea(bachelorProjectClass, applicationPeriod);
+        supervisorAvailability.add(new Availability(henrikH, 2L, 3, bachelorProjectClass)); // total slot = 2
+        supervisorAvailability.add(new Availability(henrikH, 3L, 4, masterProjectClass));
+        supervisorAvailability.add(new Availability(davidH, 1L, 1, bachelorProjectClass));          // total slot = 1
+        supervisorAvailability.add(new Availability(davidH, 0L, 1, masterProjectClass));
         unmatchedProjectIdeas.add(masterProjectIdea);
-        for (Availability availability : supervisorAvailability) {
-            if(availability.getProjectClass().equals(masterProjectClass)) {
-                availability.setNumCapable(availability.getNumCapable() + 1);
-            }
-        }
-        result = new GreedyMatchingAlgorithm().match(supervisorAvailability, unmatchedProjectIdeas, weights);
+        unmatchedProjectIdeas.add(bachelorProjectIdea);
+
+        Result result = new GreedyMatchingAlgorithm().match(supervisorAvailability, unmatchedProjectIdeas, weights);
         assertTrue(result.matches.size() > 0);
         assertTrue(result.matches.get(0).getProjectIdea().equals(masterProjectIdea));
-        assertTrue(result.matches.get(0).getSupervisor().equals(masterSupervisor));
+        assertTrue(result.matches.get(0).getSupervisor().equals(henrikH));
+        assertTrue(result.matches.get(1).getProjectIdea().equals(bachelorProjectIdea));
+        assertTrue(result.matches.get(1).getSupervisor().equals(henrikH));
         assertTrue(result.unmatched.size() == 0);
     }
 
     @Test
     @Transactional
     @Rollback
-    /*  test that a master which has filled up his slot for bachelor but has slots left for master can supervise a bachelor idea */
+    /*  test that a master supervisor is chosen for a master project idea which has the same points as a bachelor project idea */
     public void testIncreaseSlotForMasterSupervisor_v2() {
-    /*
-        supervisorAvailability.clear();
-        unmatchedProjectIdeas.clear();
-        Employee bachelorSupervisor2 = createEmployee("David", "Hallberg", languages);
-        supervisorAvailability.add(new Availability(masterSupervisor, 3L, 3, bachelorProjectClass));
-        supervisorAvailability.add(new Availability(masterSupervisor, 3L, 4, masterProjectClass));
-        supervisorAvailability.add(new Availability(bachelorSupervisor2, 0L, 1, bachelorProjectClass));
+        Employee davidH = createEmployee("David", "Hallberg", languages);
+        Employee henrikH = createEmployee("Henrik", "Hansson", languages);
+        supervisorAvailability.add(new Availability(henrikH, 3L, 3, bachelorProjectClass));
+        supervisorAvailability.add(new Availability(henrikH, 3L, 4, masterProjectClass));
+        supervisorAvailability.add(new Availability(davidH, 0L, 1, bachelorProjectClass));
         unmatchedProjectIdeas.add(bachelorProjectIdea);
         unmatchedProjectIdeas.add(masterProjectIdea);
-        bachelorProjectIdea.setPreferredSupervisor(masterSupervisor);
-        masterProjectIdea.setPreferredSupervisor(masterSupervisor);
-        List<ProjectIdea> projectIdeaList = new ArrayList<ProjectIdea>();
-        projectIdeaList.add(bachelorProjectIdea);
-        List<Employee> supervisorList = new ArrayList<Employee>();
-        supervisorList.add(masterSupervisor);
-        addKeyWords(supervisorList, projectIdeaList, createKeyword(keywordTypeWord, "UML", false));
+        bachelorProjectIdea.setPreferredSupervisor(henrikH);
+        masterProjectIdea.setPreferredSupervisor(henrikH);
         Result result = new GreedyMatchingAlgorithm().match(supervisorAvailability, unmatchedProjectIdeas, weights);
-        assertTrue(result.matches.size() > 0);            
-        boolean foundMasterProjIdea = false;
-        boolean foundMasterSuperVisor= false;
-
-        boolean foundBachelorProjIdea = false;
-        boolean foundBachelorSuperVisor= false;
-
-        for(Match match : result.matches) {
-            if(match.getProjectIdea().equals(masterProjectIdea) && match.getSupervisor().equals(masterSupervisor)) {
-                foundMasterProjIdea = true;
-                foundMasterSuperVisor = true;
-            }
-
-            if(match.getProjectIdea().equals(bachelorProjectIdea) && match.getSupervisor().equals(bachelorSupervisor2)) {
-                foundBachelorProjIdea = true;
-                foundBachelorSuperVisor = true;
-            }
-        }
-        assertTrue(foundMasterProjIdea);
-        assertTrue(foundBachelorProjIdea);
-        assertTrue(foundBachelorSuperVisor);
-        assertTrue(foundMasterSuperVisor);
-        assertTrue(result.unmatched.size() == 0);   */
+        assertTrue(result.matches.size() > 0);
+        assertTrue(result.matches.get(0).getPoints() == 10);
+        assertTrue(result.matches.get(1).getPoints() == 0);
+        assertTrue(result.matches.get(0).getSupervisor().equals(henrikH));
+        assertTrue(result.matches.get(1).getSupervisor().equals(davidH));
+        assertTrue(result.matches.get(0).getProjectIdea().equals(masterProjectIdea));
+        assertTrue(result.matches.get(1).getProjectIdea().equals(bachelorProjectIdea));
+        assertTrue(result.unmatched.size() == 0);
     }
 
     @Test