001 package dk.deepthought.sidious.goalhandler;
002 
003 import java.util.PriorityQueue;
004 
005 import org.apache.commons.logging.Log;
006 import org.apache.commons.logging.LogFactory;
007 
008 import dk.deepthought.sidious.ruleengine.RuleEngine;
009 import dk.deepthought.sidious.supportsystem.Repository;
010 import dk.deepthought.sidious.supportsystem.SuperLinkID;
011 import dk.deepthought.sidious.supportsystem.SystemSettings;
012 import dk.deepthought.sidious.util.SidiousQueue;
013 
014 /**
015  * This class prioritizes goals and chooses which are to be sent to the
016  <code>BlackBoard</code>.
017  <p>
018  * The class extends the sidious queue and process goals asynchronously.
019  
020  @author Deepthought
021  
022  */
023 public class GoalHandlerEngine extends SidiousQueue<SuperLinkID> implements
024         GoalHandler {
025     /**
026      * Logger for this class
027      */
028     private static final Log logger = LogFactory
029             .getLog(GoalHandlerEngine.class);
030 
031     /**
032      * Private Singleton instance.
033      */
034     private static final GoalHandler INSTANCE = new GoalHandlerEngine();
035 
036     /**
037      * Private constructor to facilitate singleton property.
038      */
039     private GoalHandlerEngine() {
040         super(GoalHandlerEngine.class.getSimpleName());
041     }
042 
043     /*
044      * (non-Javadoc)
045      
046      * @see dk.deepthought.sidious.goalhandler.GoalHandler#getTopPriorityGoal(dk.deepthought.sidious.supportsystem.SuperLinkID)
047      */
048     public Goal getTopPriorityGoal(SuperLinkID id) {
049         if (logger.isDebugEnabled()) {
050             logger.debug("getTopPriorityGoal(SuperLinkID id=" + id
051                     ") - start");
052         }
053         RuleEngine ruleEngine = Repository.getRuleEngine();
054         PriorityQueue<Goal> queue = new PriorityQueue<Goal>(ruleEngine
055                 .extractGoals(id));
056         if (queue == null) {
057             if (logger.isDebugEnabled()) {
058                 logger.debug("getTopPriorityGoal(SuperLinkID id=" + id
059                         ") - end - return value=null");
060             }
061             return null;
062         }
063         Goal returnGoal = queue.peek();
064         if (logger.isDebugEnabled()) {
065             logger.debug("getTopPriorityGoal(SuperLinkID id=" + id
066                     ") - end - return value=" + returnGoal);
067         }
068         return returnGoal;
069     }
070 
071     /**
072      * Returns the singleton instance of this goal handler.
073      
074      @return the singleton instance
075      */
076     public static GoalHandler getInstance() {
077         return INSTANCE;
078     }
079 
080     /*
081      * (non-Javadoc)
082      
083      * @see dk.deepthought.sidious.goalhandler.GoalHandler#request(dk.deepthought.sidious.supportsystem.SuperLinkID)
084      */
085     public final void request(SuperLinkID id) {
086         enqueue(id);
087     }
088 
089     /*
090      * (non-Javadoc)
091      
092      * @see dk.deepthought.sidious.util.SidiousQueue#process(java.lang.Object)
093      */
094     @Override
095     protected void process(SuperLinkID item) {
096         Goal goal = getTopPriorityGoal(item);
097         if (goal != null && !SystemSettings.isTestMode()) {
098             Repository.getBlackboard().deliverResult(goal);
099         }
100     }
101 
102 }