edu.wpi.prc.server.transitioner
Class LocalTransitioner

java.lang.Object
  extended by edu.wpi.prc.server.transitioner.LocalTransitioner
All Implemented Interfaces:
Transitioner

public class LocalTransitioner
extends java.lang.Object
implements Transitioner

The LocalTransitioner is used when the Transitioner component is executed on the same machine as the PRCServer. If the Transitioner will be run on a separate machine, use the RemoteTransitionerProxy instead.

Author:
James Baldassari
See Also:
Transitioner, RemoteTransitionerProxy

Field Summary
private  TransitionerQueue<WorkUnit> canonicalWorkUnits
           
private  TransitionerQueue<Result> ingressResults
           
private  TransitionerQueue<WorkUnit> ingressWorkUnits
           
private  int minNumResults
           
private  int minNumSpotChecks
           
private  TransitionerQueue<Result> pendingResults
           
private  TransitionerQueue<WorkUnit> pendingWorkUnits
           
private  SpotCheckThread spotChecker
           
private  float spotCheckProbability
           
private  TransitionerQueue<Result> spotCheckResults
           
private  TransitionerQueue<WorkUnit> spotCheckWorkUnits
           
 
Constructor Summary
LocalTransitioner()
          Private constructor ensures that only one instance of LocalTransitioner exists.
 
Method Summary
 void addEgressResult(Result r)
          NOTE: This method is for testing only! It should never be used.
 void addEgressWorkUnit(WorkUnit wu)
          NOTE: This method is for testing only! It should never be used.
 void addIngressResult(Result r)
          Enqueues a result in the ingress result queue.
 void addIngressWorkUnit(WorkUnit wu)
          Enqueues a work unit in the ingress work queue.
 void addPendingResult(Result r)
          NOTE: This method is for testing only! It should never be used.
 void addPendingWorkUnit(WorkUnit wu)
          NOTE: This method is for testing only! It should never be used.
 Result[] getAssociatedIngressResults(WorkUnit wu)
          Finds all ingress Results associated with a given WorkUnit.
 Result[] getAssociatedPendingResults(WorkUnit wu)
          Finds all pending Results associated with a given WorkUnit.
private  Result[] getAssociatedResults(java.lang.String hqlQuery)
          Finds all Results generated by the given HQL query.
private  WorkUnit[] getAssociatedWorkUnits(java.lang.String hqlQuery)
          Finds all WorkUnits generated by the given HQL query.
 TransitionerQueue<WorkUnit> getCanonicalWorkQueue()
           
 WorkUnit getCanonicalWorkUnit()
          Gets the first work unit that has enough Results.
 TransitionerQueue<Result> getIngressResultQueue()
           
 TransitionerQueue<WorkUnit> getIngressWorkQueue()
           
 TransitionerQueue<Result> getPendingResultQueue()
           
 TransitionerQueue<WorkUnit> getPendingWorkQueue()
           
 Result getResult()
          Gets the first result that needs to be validated.
private  Result[] getResultsFromUser(Volunteer v)
          Gets all results returned by the specified user.
 TransitionerQueue<WorkUnit> getRetiredWorkQueue()
           
 TransitionerQueue<Result> getValidResultQueue()
           
 WorkUnit getWorkUnit(Volunteer v)
          Gets the next work unit to be processed.
private  WorkUnit[] getWorkUnitsWithEnoughResults()
          Finds all pending WorkUnits for which enough valid Results have been returned.
 void markResultInvalid(Result invalidResult)
          Marks a result as invalid.
 void markResultValid(Result validResult)
          Marks a Result as valid.
private  void moveToPendingResultQueue(TransitionerQueue<Result> originQueue, Result r)
          Moves a Result from a result queue to the pending result queue.
private  void moveToPendingWorkQueue(TransitionerQueue<WorkUnit> originQueue, WorkUnit wu)
          Moves a WorkUnit from a work queue to the pending work queue.
private  void moveToRetiredWorkQueue(WorkUnit wu)
          Moves a WorkUnit from the pending work queue to the retired work queue.
private  void moveToValidResultQueue(Result r)
          Moves a Result from the pending result queue to the valid result queue.
private  void restoreFromDatabase()
          Restores all queues from the database.
 void selectCanonicalResult(Result canonicalResult)
          Designates a canonical Result by removing all related results from the ingress and pending queues, adding the canonical result to the valid queue, and retiring the canonical result's work unit.
 void shutdown()
          Performs any necessary tasks to safely shut down the Transitioner.
private  void verifySpotChecks()
          Checks whether sufficient spot checks have been computed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ingressWorkUnits

private TransitionerQueue<WorkUnit> ingressWorkUnits

pendingWorkUnits

private TransitionerQueue<WorkUnit> pendingWorkUnits

spotCheckWorkUnits

private TransitionerQueue<WorkUnit> spotCheckWorkUnits

canonicalWorkUnits

private TransitionerQueue<WorkUnit> canonicalWorkUnits

ingressResults

private TransitionerQueue<Result> ingressResults

pendingResults

private TransitionerQueue<Result> pendingResults

spotCheckResults

private TransitionerQueue<Result> spotCheckResults

spotChecker

private SpotCheckThread spotChecker

minNumResults

private final int minNumResults

spotCheckProbability

private final float spotCheckProbability

minNumSpotChecks

private final int minNumSpotChecks
Constructor Detail

LocalTransitioner

public LocalTransitioner()
Private constructor ensures that only one instance of LocalTransitioner exists. Use the getTransitioner() method to get access to the LocalTransitioner.

Method Detail

getIngressWorkQueue

public TransitionerQueue<WorkUnit> getIngressWorkQueue()
Specified by:
getIngressWorkQueue in interface Transitioner
Returns:
Returns the ingress work queue.

getPendingWorkQueue

public TransitionerQueue<WorkUnit> getPendingWorkQueue()
Specified by:
getPendingWorkQueue in interface Transitioner
Returns:
Returns the pending work queue.

getCanonicalWorkQueue

public TransitionerQueue<WorkUnit> getCanonicalWorkQueue()
Specified by:
getCanonicalWorkQueue in interface Transitioner
Returns:
Returns the work units that have enough results to select a canonical result.

getRetiredWorkQueue

public TransitionerQueue<WorkUnit> getRetiredWorkQueue()
Specified by:
getRetiredWorkQueue in interface Transitioner
Returns:
Returns the retired work queue.

getIngressResultQueue

public TransitionerQueue<Result> getIngressResultQueue()
Specified by:
getIngressResultQueue in interface Transitioner
Returns:
Returns the ingress result queue.

getPendingResultQueue

public TransitionerQueue<Result> getPendingResultQueue()
Specified by:
getPendingResultQueue in interface Transitioner
Returns:
Returns the pending result queue.

getValidResultQueue

public TransitionerQueue<Result> getValidResultQueue()
Specified by:
getValidResultQueue in interface Transitioner
Returns:
Returns the valid result queue, consisting of both valid results and accepted spot-check results.

moveToPendingWorkQueue

private void moveToPendingWorkQueue(TransitionerQueue<WorkUnit> originQueue,
                                    WorkUnit wu)
Moves a WorkUnit from a work queue to the pending work queue.

Parameters:
originQueue - The queue from which the WorkUnit will be moved.
wu - The WorkUnit to move.

moveToRetiredWorkQueue

private void moveToRetiredWorkQueue(WorkUnit wu)
Moves a WorkUnit from the pending work queue to the retired work queue. This actually just sets the work unit's state to Egress and updates it in the database. Retired work units are not stored in memory.

Parameters:
wu - The WorkUnit to move.

moveToPendingResultQueue

private void moveToPendingResultQueue(TransitionerQueue<Result> originQueue,
                                      Result r)
Moves a Result from a result queue to the pending result queue.

Parameters:
originQueue - The queue from which the Result will be moved.
r - The Result to move.

moveToValidResultQueue

private void moveToValidResultQueue(Result r)
Moves a Result from the pending result queue to the valid result queue. This actually just sets the result's state to Egress and updates it in the database. Valid results are not stored in memory.

Parameters:
r - The Result to move.

addIngressWorkUnit

public void addIngressWorkUnit(WorkUnit wu)
Enqueues a work unit in the ingress work queue. NOTE: wu.getState MUST be set to TaskState.Ingress before calling this method.

Specified by:
addIngressWorkUnit in interface Transitioner
Parameters:
wu - The work unit to add.

addPendingWorkUnit

public void addPendingWorkUnit(WorkUnit wu)
NOTE: This method is for testing only! It should never be used.

Parameters:
wu - The work unit to add.

addIngressResult

public void addIngressResult(Result r)
Enqueues a result in the ingress result queue. NOTE: r.getState MUST be set to TaskState.Ingress before calling this method.

Specified by:
addIngressResult in interface Transitioner
Parameters:
r - The result to add.

addPendingResult

public void addPendingResult(Result r)
NOTE: This method is for testing only! It should never be used.

Parameters:
r - The result to add.

getResultsFromUser

private Result[] getResultsFromUser(Volunteer v)
Gets all results returned by the specified user.

Parameters:
v - The user to search for.
Returns:
An array of all matching results or an array of lenght zero if none were found.

markResultValid

public void markResultValid(Result validResult)
Marks a Result as valid. If the minimum number of valid results for the Result's associated WorkUnit, the Result is moved from the ingress result queue to the pending result queue. If the minimum number of valid results for the Result's associated WorkUnit has been reached, all Results for that WorkUnit are removed from the pending result queue, a canonical result is chosen, and the canonical result is moved to the valid result queue.

Specified by:
markResultValid in interface Transitioner
Parameters:
validResult - The result from the ingress result queue to be marked valid.

markResultInvalid

public void markResultInvalid(Result invalidResult)
Marks a result as invalid. This will remove the result from the ingress queue and delete it from the database. If the result is a spot check result, additional steps may be taken depending on the project's spot check failure action.

Specified by:
markResultInvalid in interface Transitioner
Parameters:
invalidResult - The result to be marked invalid.

addEgressWorkUnit

public void addEgressWorkUnit(WorkUnit wu)
NOTE: This method is for testing only! It should never be used.

Parameters:
wu - The work unit to add.

addEgressResult

public void addEgressResult(Result r)
NOTE: This method is for testing only! It should never be used.

Parameters:
r - The result to add.

getAssociatedResults

private Result[] getAssociatedResults(java.lang.String hqlQuery)
Finds all Results generated by the given HQL query.

Parameters:
hqlQuery - The HQL query to execute.
Returns:
An array of all Results generated by the HQL query or an array of length 0 if none were found.

getAssociatedIngressResults

public Result[] getAssociatedIngressResults(WorkUnit wu)
Finds all ingress Results associated with a given WorkUnit.

Specified by:
getAssociatedIngressResults in interface Transitioner
Parameters:
wu - The WorkUnit for which to find all associated ingress Results.
Returns:
Array of ingress Results associated with the given WorkUnit or an array of length 0 if none were found.

getAssociatedPendingResults

public Result[] getAssociatedPendingResults(WorkUnit wu)
Finds all pending Results associated with a given WorkUnit.

Specified by:
getAssociatedPendingResults in interface Transitioner
Parameters:
wu - The WorkUnit for which to find all associated pending Results.
Returns:
Array of pending Results associated with the given WorkUnit or an array of length 0 if none were found.

getWorkUnitsWithEnoughResults

private WorkUnit[] getWorkUnitsWithEnoughResults()
Finds all pending WorkUnits for which enough valid Results have been returned.

Returns:
Array of ingress Results associated with the given WorkUnit or an array of length 0 if none were found.

getAssociatedWorkUnits

private WorkUnit[] getAssociatedWorkUnits(java.lang.String hqlQuery)
Finds all WorkUnits generated by the given HQL query.

Parameters:
hqlQuery - The HQL query to execute.
Returns:
An array of all WorkUnits generated by the HQL query or an array of length 0 if none were found.

selectCanonicalResult

public void selectCanonicalResult(Result canonicalResult)
Designates a canonical Result by removing all related results from the ingress and pending queues, adding the canonical result to the valid queue, and retiring the canonical result's work unit.

Specified by:
selectCanonicalResult in interface Transitioner
Parameters:
canonicalResult - The canonical result to select.

getWorkUnit

public WorkUnit getWorkUnit(Volunteer v)
                     throws EmptyQueueException
Gets the next work unit to be processed. First, it will try to find a work unit in the pending work queue that has not already been sent to the given volunteer. If there are no satisfiable work units in the pending work queue, it will try to return a work unit from the ingress work queue. If there are no work units in the ingress work queue, an EmptyQueueException will be thrown.

Specified by:
getWorkUnit in interface Transitioner
Parameters:
v - The volunteer who will receive it.
Returns:
The next work unit to be processed.
Throws:
EmptyQueueException

getCanonicalWorkUnit

public WorkUnit getCanonicalWorkUnit()
                              throws EmptyQueueException
Gets the first work unit that has enough Results.

Specified by:
getCanonicalWorkUnit in interface Transitioner
Returns:
The first work unit in the canonical work queue.
Throws:
EmptyQueueException

getResult

public Result getResult()
                 throws EmptyQueueException
Gets the first result that needs to be validated.

Specified by:
getResult in interface Transitioner
Returns:
The first result in the ingress queue.
Throws:
EmptyQueueException

verifySpotChecks

private void verifySpotChecks()
Checks whether sufficient spot checks have been computed. If not, it will start a new thread to compute them.


restoreFromDatabase

private void restoreFromDatabase()
Restores all queues from the database.


shutdown

public void shutdown()
Performs any necessary tasks to safely shut down the Transitioner.

Specified by:
shutdown in interface Transitioner