Package edu.neu.ccs.task.web

Source Code of edu.neu.ccs.task.web.DTaskManager

package edu.neu.ccs.task.web;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;

import webframe.Configuration;
import webframe.Conversation;
import webframe.DialogueManager;
import webframe.DialogueSession;
import webframe.User;
import edu.neu.ccs.task.Slot;
import edu.neu.ccs.task.Task;
import edu.neu.ccs.task.TaskEngine;
import edu.neu.ccs.task.TaskModel;
import edu.neu.ccs.task.TaskModelSet;
import edu.neu.ccs.task.TaskPlan;
import edu.neu.ccs.task.agent.Agent;
import edu.neu.ccs.task.agent.AgentFactory;
import edu.neu.ccs.task.util.Bool;
import edu.neu.ccs.task.util.XMLUtil;

public class DTaskManager implements DialogueManager {
  private final AgentFactory factory;
  private final Persistence persistence;
  private final TaskModelSet models;
  protected final String top;
  protected final Map<String, String> topInputs;
 
  public DTaskManager(Configuration c) throws
      InstantiationException,
      IllegalAccessException,
      ClassNotFoundException,
      InvocationTargetException,
      XMLStreamException,
      IOException {
    factory = c.getInstance(AgentFactory.class, "dtask.agent-factory", AgentFactory.DEFAULT);
    persistence = c.getInstance(Persistence.class, "dtask.persistence", Persistence.DEFAULT);
    models = factory.createModelSet();
    top = c.getString("dtask.top-task", "Top");
   
    topInputs = new HashMap<String, String>();
    for (String name : c.parameters())
      if (name.startsWith("dtask.top-input."))
        topInputs.put(name.substring(16), c.getString(name));
   
    for (String model : c.getStrings("dtask.models")) {
      TaskModel tm;
      if (XMLUtil.isURI(model))
        tm = models.load(model, null);
      else
        tm = models.load(c.findResource(model), null);
      System.out.println("loaded " + tm.getURI());
    }
  }
 
  public DialogueSession create(int id, User user, Map<String,String[]> params) throws Exception {
    int userId = user.getId();
    System.out.println("creating DTask session for " + userId);
   
    Agent agent = factory.createAgent(models);
    initAgent(id, user, agent);
    initPlan(id, user, agent);
    return new DTaskSession(agent, userId, persistence);
  }

  protected void initAgent(int id, User user, Agent agent) throws Exception {
    agent.getEngine().evalInitScripts();
    persistence.load(agent, user.getId());
  }
 
  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);
  }

  public List<String> statusTypes() {
    return factory.agentStatusTypes();
  }
 
  public List<String> actionTypes() {
    return Collections.emptyList();
  }
}
TOP

Related Classes of edu.neu.ccs.task.web.DTaskManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.