protected void initPlan(int id, User user, Agent agent) throws Exception {
TaskEngine engine = agent.getEngine();
TaskModelSet modelSet = engine.getModelSet();
QName topName = new QName(modelSet.getDefaultModel().getURI(), top);
TaskPlan topPlan = engine.newTaskPlan(topName);
Task topTask = topPlan.getTask();
for (Map.Entry<String, String> e : topInputs.entrySet())
topTask.setSlotValueScript(e.getKey(), e.getValue(), "init agent");
if ( ! user.isGuest()) {
int userId = user.getId();
Slot userSlot = topTask.getType().getSlotIfExists("user");
if ((userSlot != null) && userSlot.isInput() && !topInputs.containsKey("user")) {
if (userSlot.getType().equals("string"))
topTask.setSlotValue("user", Integer.toString(userId));
else if (userSlot.getType().equals("number"))
topTask.setSlotValue("user", userId);
}
}
Slot lastConvSlot = topTask.getType().getSlotIfExists("lastConversation");
if ((lastConvSlot != null) &&
lastConvSlot.isInput() &&
lastConvSlot.getType().equals("number") &&
!topInputs.containsKey("lastConversation")) {
Conversation c = user.getLastConversation(id);
topTask.setSlotValue("lastConversation", c==null ? null : c.getStart());
}
if (Bool.isFalse(topTask.isApplicable()))
throw new IllegalArgumentException("task not applicable: " + topTask);
List<String> undefs = topTask.getUndefinedInputs();
if ( ! undefs.isEmpty())
throw new IllegalArgumentException("undefined inputs: " + undefs);
agent.setFocus(topPlan);
}