Practical 2 Solutions
Answers to questions in introductory notes
There are some questions in the comments of the examples.
- Example 1.
// Example: Is Humphrey Bear, a member of the Honey Party,
// Prime Minister?
// Question: The following code will only work as intended if there
// can only be one Prime Minister. Why?
// Answer: If there is more than one prime Minister,
// Humphrey Bear will not necessarily be the first match that will
// bind to name. The fact that name does not equal Humphrey Bear
// does not tell us whether or not there is another entry in the
// beliefset with Humphrey Bear as the Prime Minister.
logical String name;
mps.get(name,"Honey Party","Prime Minister");
if (name.getValue().equals("Humphrey Bear")
{
// Do something.
}
- Example 2.
// Example: Are there any elected members of the Honey Party
// whose first name is Rupert?
// Question: What happens in this example if the first match found is
// not Rupert?
// Answer: The agent does not fail the logical expression at that
// point. It tests it again with a new binding for name, until
// either it finds a binding with Rupert or it has checked all the
// beliefset relations and concluded that Rupert is not in the
// Honey Party.
// Refer to the section on Composite Logical Expressions in the
// Plans chapter in Agent Manual for a more detailed discussion.
logical String name;
logical String portfolio;
if (mps.get(name,"Honey Party",portfolio) &&
((name.getValue().indexOf("Rupert")==0 ))
{
// Do something.
}
Program solutions
The solutions to the programming exercises can be found in the practicals/jack_jde/solutions/practical2 subdirectory. There is a separate directory for each of the exercises.
Answers to questions
Exercise 2
- The event triggers the first applicable plan which successfully prints out the message. As it was successful, there is no reposting or triggering of alternative plans with different bindings.