net.codescore.managers
Class CompetitionManager

java.lang.Object
  extended by net.codescore.managers.CompetitionManager
All Implemented Interfaces:
SubUpdateListener

public class CompetitionManager
extends Object
implements SubUpdateListener

Manages a competition, providing methods to get competitions from the database, and facilities for processing submissions, such as a grading queue and accessor methods to the competition settings.

Author:
Adam Cornett

Field Summary
private static CompetitionManager activeCompetition
          Deprecated.  
private  boolean compIsRunning
           
private  Competition currentComp
           
private static DataContext dc
           
private  ConcurrentHashMap<Submission,ExecutionResult> gradeResults
           
private  ConcurrentLinkedQueue<Submission> gradingQueue
           
private  List<GradingThread> gThreads
           
private  ConcurrentLinkedQueue<Submission> judgingQueue
           
private  org.apache.commons.logging.Log log
           
private static HashMap<Competition,CompetitionManager> managers
          A map of linking a competition to its associated manager, used to prevent a competition from having more than one associated manager
private  int numGradingThreads
           
private  ConcurrentLinkedQueue<Submission> practiceQueue
           
private  Map<Team,TeamHome> teamHomes
           
private  GradingThread testThread
           
 
Constructor Summary
CompetitionManager(Competition c)
          Create a new manager.
 
Method Summary
 void addGradeResult(Submission s, ExecutionResult er)
          When a submission has been graded, the result of the Grader is returned here so that it can be reviewed by a judge.
 void addSubmissionToGradeQueue(Submission s)
          Add a submission to the grading queue.
If the queue already contains s, it will not be added again.
 void addSubmissionToPracticeQueue(Submission s)
          Add a submission to the practice queue.
If the queue already contains s, it will not be added again.
 void checkGThreads()
          Removes dead graders from the list and ensures that the actual number of active grading threads matches the expected number.
private  void checkGThreads(boolean fix)
           
 void debug()
          Dump debugging information to the log
protected  void finalize()
          We need to make sure that we stop the grading thread for this competition when we clean up this object, also deregisters the listeners.
 int getAliveGThreads()
          Check for alive graders.
static List<Competition> getAllComps()
           
static List<Competition> getAllComps(ObjectContext oc)
           
 boolean getAutoCompileError()
           
 boolean getAutoCorrect()
          Check to see if the system will mark submissions correct when they match the expected output exactly.
 boolean getAutoRuntimeError()
           
 Competition getCompetition()
           
static Competition getCompetitionByName(ObjectContext c, String name)
           
static Competition getCompetitionByName(String name)
          Uses the internal context to search for a competition
 int getCompile_error_status()
           
 ProblemStatus getCompileError(ObjectContext oc)
           
 int getCorrect_status()
           
 ProblemStatus getCorrect(ObjectContext oc)
           
static CompetitionManager getCurrent()
          Deprecated.  
static Competition getCurrent(ObjectContext oc)
          Deprecated.  
static List<Competition> getCurrentComps()
          Get all currently running competitions using the internal context
static List<Competition> getCurrentComps(ObjectContext oc)
          A list of all currently running competitions using the supplied context
 int getExeTimeout()
           
static CompetitionManager getManager(Competition c)
          Get the associated manager for a competition.
 int getNumGraders()
           
 List<Submission> getPendingSubs()
           
 int getPresentation_error_status()
           
 ProblemStatus getPresError(ObjectContext oc)
           
 int getQueueLength()
           
 ExecutionResult getResult(TeamSubmission s)
          Returns the ExecutionResult for a given submission.
 int getRuntime_error_status()
           
 ProblemStatus getRuntimeError(ObjectContext oc)
           
 ScoringMode getScoringMode()
          Get the scoring mode for this competition.
Stored in the scoring_mode competition property
static TeamSubmission getSubmission(ObjectContext c, int sid)
          Lookup a submission for a given submission id (sid)
 int getTest_status()
           
 ProblemStatus getTestStatus(ObjectContext oc)
           
 int getTimelimit_error_status()
           
 ProblemStatus getTimelimitError(ObjectContext oc)
           
 boolean getUseSecurity()
          Tells the grading system if it should use the extra security precautions while executing a submission.
This will cause the grader to fail currently as the security system has not been implemented
 int getWrong_output_status()
           
 ProblemStatus getWrongOutput(ObjectContext oc)
           
private  void initGThreads()
           
 boolean isCompIsRunning()
          True if the competition is 'running' which means teams can login and submit solutions.
 void onSubAdd(TeamSubmission s)
          Callback for a new submission
 void onSubUpdate(TeamSubmission s)
          Callback for an updated submission
 Submission pollGradeQueue()
          Called by the grading thread to check if any submissions are waiting to be graded.
 Submission pollPracticeQueue()
          Called by the grading thread to check if any submissions are waiting to be run.
 void registerTeamHome(TeamHome th)
          Deprecated.  
 void reGradeAllSubs()
          Add ALL submissions for this competition to the grading queue.
This method is used mainly for testing and has little real world use.
 void setAutoCompileError(boolean on)
          Sets the auto_compile_error flag
 void setAutoCorrect(boolean autoOn)
          Sets auto_correct flag for this competition.
 void setAutoRuntimeError(boolean on)
          Sets the value of the auto_runtime_error flag
 void setCompile_error_status(int compile_error_status)
           
 void setCompIsRunning(boolean compIsRunning)
           
 void setCorrect_status(int correct_status)
           
 void setCurrentComp(Competition currentComp)
          Set this manager's competition
 void setExeTimeout(int tout)
          Set the max time for an execution.
 void setNumGraders(int num)
          Sets the number of grading threads for this competition.
 void setPresentation_error_status(int presentation_error_status)
           
 void setRuntime_error_status(int runtime_error_status)
           
 void setScoringMode(ScoringMode mode)
          Set the scoring mode to be used by the competition
 void setTest_status(int test_status)
           
 void setTimelimit_error_status(int timelimit_error_status)
           
 void setUseSecurity(boolean on)
          Set the use_security property.
 void setWrong_output_status(int wrong_output_status)
           
 void unRegisterTeamHome(TeamHome th)
           
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

activeCompetition

private static CompetitionManager activeCompetition
Deprecated. 
The last competition to be created


dc

private static DataContext dc

managers

private static HashMap<Competition,CompetitionManager> managers
A map of linking a competition to its associated manager, used to prevent a competition from having more than one associated manager


compIsRunning

private volatile boolean compIsRunning

currentComp

private Competition currentComp

gradeResults

private ConcurrentHashMap<Submission,ExecutionResult> gradeResults

gradingQueue

private ConcurrentLinkedQueue<Submission> gradingQueue

practiceQueue

private ConcurrentLinkedQueue<Submission> practiceQueue

gThreads

private List<GradingThread> gThreads

judgingQueue

private ConcurrentLinkedQueue<Submission> judgingQueue

log

private org.apache.commons.logging.Log log

numGradingThreads

private int numGradingThreads

teamHomes

private Map<Team,TeamHome> teamHomes

testThread

private GradingThread testThread
Constructor Detail

CompetitionManager

public CompetitionManager(Competition c)
Create a new manager.

Parameters:
c - The competition the manager is associated with
Throws:
IllegalArgumentException - If a manager already exists for the competition. Use getManager(Competition) to safely get a manager for a competition
Method Detail

getAllComps

public static List<Competition> getAllComps()
Returns:
A list of all competitions, using the internal DataContext

getAllComps

public static List<Competition> getAllComps(ObjectContext oc)
Parameters:
oc - The context used to execute the query
Returns:
A List of all competitions

getCompetitionByName

public static Competition getCompetitionByName(ObjectContext c,
                                               String name)
Parameters:
c - The context used to search for the competition
name - The name of the competition
Returns:
The competition with the associated name

getCompetitionByName

public static Competition getCompetitionByName(String name)
Uses the internal context to search for a competition

Parameters:
name - The name of the competition to lookup
Returns:
The competition with the given name

getCurrent

public static CompetitionManager getCurrent()
Deprecated. 

Returns:
The last manager associated with the most recently created competition

getCurrent

public static Competition getCurrent(ObjectContext oc)
Deprecated. 

Parameters:
oc - The context used to lookup a competition
Returns:
The last registered competition

getCurrentComps

public static List<Competition> getCurrentComps()
Get all currently running competitions using the internal context

Returns:
A list of all competitions that are currently 'running': The competitions start time is before the current time, and the end time is after the current time.

getCurrentComps

public static List<Competition> getCurrentComps(ObjectContext oc)
A list of all currently running competitions using the supplied context

Parameters:
oc - The context to use when executing the query
Returns:
A list of all competitions where the competition's start time is before the current time and its end time is after the current time.

getManager

public static CompetitionManager getManager(Competition c)
Get the associated manager for a competition. A new manager will be created if one does not already exist for the given competition.

Parameters:
c - The competition
Returns:
The manager associated with the competition.

getSubmission

public static TeamSubmission getSubmission(ObjectContext c,
                                           int sid)
Lookup a submission for a given submission id (sid)

Parameters:
c - The context to use for executing the query
sid - The sid of the submission
Returns:
The Team Submission with the associated sid

addGradeResult

public void addGradeResult(Submission s,
                           ExecutionResult er)
When a submission has been graded, the result of the Grader is returned here so that it can be reviewed by a judge.

Parameters:
s - The submission which was graded
er - The result of the grading

addSubmissionToGradeQueue

public void addSubmissionToGradeQueue(Submission s)
Add a submission to the grading queue.
If the queue already contains s, it will not be added again. The queue is a FIFO queue.

Parameters:
s - The submission to be added.
See Also:
ConcurrentLinkedQueue

addSubmissionToPracticeQueue

public void addSubmissionToPracticeQueue(Submission s)
Add a submission to the practice queue.
If the queue already contains s, it will not be added again. The queue is a FIFO queue.

Parameters:
s - The submission to be added.
See Also:
ConcurrentLinkedQueue

checkGThreads

public void checkGThreads()
Removes dead graders from the list and ensures that the actual number of active grading threads matches the expected number.


debug

public void debug()
Dump debugging information to the log


getAliveGThreads

public int getAliveGThreads()
Check for alive graders.

Returns:
The number of alive grading threads

getAutoCompileError

public boolean getAutoCompileError()
Returns:
true if the system should mark submissions as having a compile error when the compiler returns a non-zero value.

getAutoCorrect

public boolean getAutoCorrect()
Check to see if the system will mark submissions correct when they match the expected output exactly.

Returns:
true if the system should mark submissions correct if they have a perfect diff.

getAutoRuntimeError

public boolean getAutoRuntimeError()
Returns:
true if the system should mark submissions as having a runtime error if they do not exit with a exit code of 0;

getCompetition

public Competition getCompetition()
Returns:
The associated competition

getCompile_error_status

public int getCompile_error_status()
Returns:
The compile_error_status property of the competition.
See Also:
getCompileError(ObjectContext)

getCompileError

public ProblemStatus getCompileError(ObjectContext oc)
Parameters:
oc - The context used to get the ProblemStatus.
Returns:
The problem status object signifying a compiler error for a submission.
See Also:
getCompile_error_status()

getCorrect

public ProblemStatus getCorrect(ObjectContext oc)
Parameters:
oc - The context used to get the ProblemStatus.
Returns:
The problem status object signifying a correct submission.
See Also:
getCorrect_status()

getCorrect_status

public int getCorrect_status()
Returns:
The correct_status property of the competition
See Also:
getCorrect(ObjectContext)

getExeTimeout

public int getExeTimeout()
Returns:
The maximum execution time for a submission in ms.

getNumGraders

public int getNumGraders()
Returns:
The number of grading threads this competition should maintain

getPendingSubs

public List<Submission> getPendingSubs()
Returns:
A list of submissions waiting to be graded

getPresentation_error_status

public int getPresentation_error_status()
Returns:
the presentation_error_status property of the competition
See Also:
getPresError(ObjectContext)

getPresError

public ProblemStatus getPresError(ObjectContext oc)
Parameters:
oc - The context used to get the ProblemStatus.
Returns:
The problem status object signifying a presentational error in a submission
See Also:
getPresentation_error_status()

getQueueLength

public int getQueueLength()
Returns:
The length of the grading queue
See Also:
ConcurrentLinkedQueue.size()

getResult

public ExecutionResult getResult(TeamSubmission s)
Returns the ExecutionResult for a given submission.

Parameters:
s - The submission you're looking for.
Returns:
The ExecutionResult from the grading of s.

getRuntime_error_status

public int getRuntime_error_status()
Returns:
The runtime_error_status property of the competition.
See Also:
getRuntimeError(ObjectContext)

getRuntimeError

public ProblemStatus getRuntimeError(ObjectContext oc)
Parameters:
oc - The context used to get the ProblemStatus.
Returns:
The problem status object signifying a runtime error in a submission.
See Also:
getRuntime_error_status()

getScoringMode

public ScoringMode getScoringMode()
Get the scoring mode for this competition.
Stored in the scoring_mode competition property

Returns:
The scoring mode for the associated competition

getTest_status

public int getTest_status()
Returns:
The test_status property of the competition.
See Also:
getTestStatus(ObjectContext)

getTestStatus

public ProblemStatus getTestStatus(ObjectContext oc)
Parameters:
oc - The context used to get the ProblemStatus.
Returns:
The problem status signifying a problem should not be graded normally, but instead is a test submission.
See Also:
getTest_status()

getTimelimit_error_status

public int getTimelimit_error_status()
Returns:
The timelimit_error_status property of the competition.
See Also:
getTimelimitError(ObjectContext)

getTimelimitError

public ProblemStatus getTimelimitError(ObjectContext oc)
Parameters:
oc - The context used to get the ProblemStatus.
Returns:
The problem status signifying that a submission has exceeded the maximum time limit.
See Also:
getTimelimit_error_status(), getExeTimeout()

getUseSecurity

public boolean getUseSecurity()
Tells the grading system if it should use the extra security precautions while executing a submission.
This will cause the grader to fail currently as the security system has not been implemented

Returns:
The use_security property of the competition

getWrong_output_status

public int getWrong_output_status()
Returns:
The wrong_output_status property of the competition
See Also:
getWrongOutput(ObjectContext)

getWrongOutput

public ProblemStatus getWrongOutput(ObjectContext oc)
Parameters:
oc - The context used to get the ProblemStatus.
Returns:
The problem status object signifying that a submission has the wrong output.
See Also:
getWrong_output_status()

isCompIsRunning

public boolean isCompIsRunning()
True if the competition is 'running' which means teams can login and submit solutions.

Returns:
the compIsRunning

onSubAdd

public void onSubAdd(TeamSubmission s)
Description copied from interface: SubUpdateListener
Callback for a new submission

Specified by:
onSubAdd in interface SubUpdateListener
Parameters:
s - The new submission

onSubUpdate

public void onSubUpdate(TeamSubmission s)
Description copied from interface: SubUpdateListener
Callback for an updated submission

Specified by:
onSubUpdate in interface SubUpdateListener
Parameters:
s - The updated submission

pollGradeQueue

public Submission pollGradeQueue()
Called by the grading thread to check if any submissions are waiting to be graded.

Returns:
A submission, or null if the queue is empty.

pollPracticeQueue

public Submission pollPracticeQueue()
Called by the grading thread to check if any submissions are waiting to be run. This polls the practice queue, which is for submissions that are not scored and are only fed the sample input.

Returns:
A submission, or null if the queue is empty.

registerTeamHome

public void registerTeamHome(TeamHome th)
Deprecated. 

Registers a team home with the competition manager

Parameters:
th - The team home to register

reGradeAllSubs

public void reGradeAllSubs()
Add ALL submissions for this competition to the grading queue.
This method is used mainly for testing and has little real world use.


setAutoCompileError

public void setAutoCompileError(boolean on)
Sets the auto_compile_error flag

Parameters:
on - the new value

setAutoCorrect

public void setAutoCorrect(boolean autoOn)
Sets auto_correct flag for this competition.

Parameters:
autoOn - true if the system should mark submissions correct when they match the expected output exactly.

setAutoRuntimeError

public void setAutoRuntimeError(boolean on)
Sets the value of the auto_runtime_error flag

Parameters:
on - the new value

setCompile_error_status

public void setCompile_error_status(int compile_error_status)
Parameters:
compile_error_status - the compile_error_status to set

setCompIsRunning

public void setCompIsRunning(boolean compIsRunning)
Parameters:
compIsRunning - The compIsRunning to set
See Also:
compIsRunning

setCorrect_status

public void setCorrect_status(int correct_status)
Parameters:
correct_status - the correct_status to set

setCurrentComp

public void setCurrentComp(Competition currentComp)
Set this manager's competition

Parameters:
currentComp - The new competition

setExeTimeout

public void setExeTimeout(int tout)
Set the max time for an execution.

Parameters:
tout -

setNumGraders

public void setNumGraders(int num)
Sets the number of grading threads for this competition.

Parameters:
num - if num is greater than the current number of threads, new threads are started. If num is less than the current number of threads, the excess graders are stopped.

setPresentation_error_status

public void setPresentation_error_status(int presentation_error_status)
Parameters:
presentation_error_status - the presentation_error_status to set

setRuntime_error_status

public void setRuntime_error_status(int runtime_error_status)
Parameters:
runtime_error_status - the runtime_error_status to set

setScoringMode

public void setScoringMode(ScoringMode mode)
Set the scoring mode to be used by the competition

Parameters:
mode - The new scoring mode

setTest_status

public void setTest_status(int test_status)
Parameters:
test_status - the test_status to set

setTimelimit_error_status

public void setTimelimit_error_status(int timelimit_error_status)
Parameters:
timelimit_error_status - the timelimit_error_status to set

setUseSecurity

public void setUseSecurity(boolean on)
Set the use_security property.

Parameters:
on -
See Also:
getUseSecurity()

setWrong_output_status

public void setWrong_output_status(int wrong_output_status)
Parameters:
wrong_output_status - the wrong_output_status to set

unRegisterTeamHome

public void unRegisterTeamHome(TeamHome th)

checkGThreads

private void checkGThreads(boolean fix)

initGThreads

private void initGThreads()

finalize

protected void finalize()
                 throws Throwable
We need to make sure that we stop the grading thread for this competition when we clean up this object, also deregisters the listeners.

Overrides:
finalize in class Object
Throws:
Throwable


Copyright 2008 © Adam Cornett, Andrew Conner All Rights Reserved.