1+ using aima . core . agent ;
2+ using aima . core . agent . impl ;
3+ using aima . core . logic . propositional . kb ;
4+ using aima . core . logic . propositional . parsing . ast ;
5+
6+ namespace aima . core . logic . propositional . agent
7+ {
8+ /**
9+ * Artificial Intelligence A Modern Approach (3rd Edition): Figure 7.1, page
10+ * 236.<br>
11+ * <br>
12+ *
13+ * <pre>
14+ * function KB-AGENT(percept) returns an action
15+ * persistent: KB, a knowledge base
16+ * t, a counter, initially 0, indicating time
17+ *
18+ * TELL(KB, MAKE-PERCEPT-SENTENCE(percept, t))
19+ * action <- ASK(KB, MAKE-ACTION-QUERY(t))
20+ * TELL(KB, MAKE-ACTION-SENTENCE(action, t))
21+ * t <- t + 1
22+ * return action
23+ *
24+ * </pre>
25+ *
26+ * Figure 7.1 A generic knowledge-based agent. Given a percept, the agent adds
27+ * the percept to its knowledge base, asks the knowledge base for the best
28+ * action, and tells the knowledge base that it has in fact taken that action.
29+ *
30+ * @author Ciaran O'Reilly
31+ */
32+ public abstract class KBAgent : AbstractAgent
33+ {
34+ // persistent: KB, a knowledge base
35+ protected KnowledgeBase KB ;
36+ // t, a counter, initially 0, indicating time
37+ private int t = 0 ;
38+
39+ public KBAgent ( KnowledgeBase KB )
40+ {
41+ this . KB = KB ;
42+ }
43+
44+ // function KB-AGENT(percept) returns an action
45+ public override Action execute ( Percept percept )
46+ {
47+ // TELL(KB, MAKE-PERCEPT-SENTENCE(percept, t))
48+ KB . tell ( makePerceptSentence ( percept , t ) ) ;
49+ // action <- ASK(KB, MAKE-ACTION-QUERY(t))
50+ Action action = ask ( KB , makeActionQuery ( t ) ) ;
51+
52+ // TELL(KB, MAKE-ACTION-SENTENCE(action, t))
53+ KB . tell ( makeActionSentence ( action , t ) ) ;
54+ // t <- t + 1
55+ t = t + 1 ;
56+ // return action
57+ return action ;
58+ }
59+
60+ /**
61+ * MAKE-PERCEPT-SENTENCE constructs a sentence asserting that the agent
62+ * perceived the given percent at the given time.
63+ *
64+ * @param percept
65+ * the given percept
66+ * @param t
67+ * the given time
68+ * @return a sentence asserting that the agent perceived the given percept
69+ * at the given time.
70+ */
71+ // MAKE-PERCEPT-SENTENCE(percept, t)
72+ public abstract Sentence makePerceptSentence ( Percept percept , int t ) ;
73+
74+ /**
75+ * MAKE-ACTION-QUERY constructs a sentence that asks what action should be
76+ * done at the current time.
77+ *
78+ * @param t
79+ * the current time.
80+ * @return a sentence that asks what action should be done at the current
81+ * time.
82+ */
83+ // MAKE-ACTION-QUERY(t)
84+ public abstract Sentence makeActionQuery ( int t ) ;
85+
86+ /**
87+ * MAKE-ACTION-SENTENCE constructs a sentence asserting that the chosen action was executed.
88+ * @param action
89+ * the chose action.
90+ * @param t
91+ * the time at which the action was executed.
92+ * @return a sentence asserting that the chosen action was executed.
93+ */
94+ // MAKE-ACTION-SENTENCE(action, t)
95+ public abstract Sentence makeActionSentence ( Action action , int t ) ;
96+
97+ /**
98+ * A wrapper around the KB's ask() method which translates the action (in the form of
99+ * a sentence) determined by the KB into an allowed 'Action' object from the current
100+ * environment in which the KB-AGENT resides.
101+ *
102+ * @param KB
103+ * the KB to ask.
104+ * @param actionQuery
105+ * an action query.
106+ * @return the Action to be performed in response to the given query.
107+ */
108+ // ASK(KB, MAKE-ACTION-QUERY(t))
109+ public abstract Action ask ( KnowledgeBase KB , Sentence actionQuery ) ;
110+ }
111+ }
0 commit comments