1 | package dk.deepthought.sidious.goalhandler; |
2 | |
3 | import java.util.PriorityQueue; |
4 | |
5 | import org.apache.commons.logging.Log; |
6 | import org.apache.commons.logging.LogFactory; |
7 | |
8 | import dk.deepthought.sidious.ruleengine.RuleEngine; |
9 | import dk.deepthought.sidious.supportsystem.Repository; |
10 | import dk.deepthought.sidious.supportsystem.SuperLinkID; |
11 | import dk.deepthought.sidious.supportsystem.SystemSettings; |
12 | import dk.deepthought.sidious.util.SidiousQueue; |
13 | |
14 | /** |
15 | * This class prioritizes goals and chooses which are to be sent to the |
16 | * <code>BlackBoard</code>. |
17 | * <p> |
18 | * The class extends the sidious queue and process goals asynchronously. |
19 | * |
20 | * @author Deepthought |
21 | * |
22 | */ |
23 | public class GoalHandlerEngine extends SidiousQueue<SuperLinkID> implements |
24 | GoalHandler { |
25 | /** |
26 | * Logger for this class |
27 | */ |
28 | private static final Log logger = LogFactory |
29 | .getLog(GoalHandlerEngine.class); |
30 | |
31 | /** |
32 | * Private Singleton instance. |
33 | */ |
34 | private static final GoalHandler INSTANCE = new GoalHandlerEngine(); |
35 | |
36 | /** |
37 | * Private constructor to facilitate singleton property. |
38 | */ |
39 | private GoalHandlerEngine() { |
40 | super(GoalHandlerEngine.class.getSimpleName()); |
41 | } |
42 | |
43 | /* |
44 | * (non-Javadoc) |
45 | * |
46 | * @see dk.deepthought.sidious.goalhandler.GoalHandler#getTopPriorityGoal(dk.deepthought.sidious.supportsystem.SuperLinkID) |
47 | */ |
48 | public Goal getTopPriorityGoal(SuperLinkID id) { |
49 | if (logger.isDebugEnabled()) { |
50 | logger.debug("getTopPriorityGoal(SuperLinkID id=" + id |
51 | + ") - start"); |
52 | } |
53 | RuleEngine ruleEngine = Repository.getRuleEngine(); |
54 | PriorityQueue<Goal> queue = new PriorityQueue<Goal>(ruleEngine |
55 | .extractGoals(id)); |
56 | if (queue == null) { |
57 | if (logger.isDebugEnabled()) { |
58 | logger.debug("getTopPriorityGoal(SuperLinkID id=" + id |
59 | + ") - end - return value=null"); |
60 | } |
61 | return null; |
62 | } |
63 | Goal returnGoal = queue.peek(); |
64 | if (logger.isDebugEnabled()) { |
65 | logger.debug("getTopPriorityGoal(SuperLinkID id=" + id |
66 | + ") - end - return value=" + returnGoal); |
67 | } |
68 | return returnGoal; |
69 | } |
70 | |
71 | /** |
72 | * Returns the singleton instance of this goal handler. |
73 | * |
74 | * @return the singleton instance |
75 | */ |
76 | public static GoalHandler getInstance() { |
77 | return INSTANCE; |
78 | } |
79 | |
80 | /* |
81 | * (non-Javadoc) |
82 | * |
83 | * @see dk.deepthought.sidious.goalhandler.GoalHandler#request(dk.deepthought.sidious.supportsystem.SuperLinkID) |
84 | */ |
85 | public final void request(SuperLinkID id) { |
86 | enqueue(id); |
87 | } |
88 | |
89 | /* |
90 | * (non-Javadoc) |
91 | * |
92 | * @see dk.deepthought.sidious.util.SidiousQueue#process(java.lang.Object) |
93 | */ |
94 | @Override |
95 | protected void process(SuperLinkID item) { |
96 | Goal goal = getTopPriorityGoal(item); |
97 | if (goal != null && !SystemSettings.isTestMode()) { |
98 | Repository.getBlackboard().deliverResult(goal); |
99 | } |
100 | } |
101 | |
102 | } |