KeywordRouter implements Router { @Override public Agent[] route(Request request) { String requestText = request.getText().toLowerCase(); String[] tokens = tokenizer.tokenize(requestText); if (tokens.length == 0) { return new Agent[0]; } Set<Agent> agents = new HashSet<Agent>(); for (String token : tokens) { if (agentMap.containsKey(token)) { agents.add(agentMap.get(token)); } } return agents.toArray(new Agent[agents.size()]); } KeywordRouter(RouterAgentConfig agentsConfig); @Override Agent[] route(Request request); @Override Agent[] getAgents(); static final String KEYWORDS; }
@Test public void testRouting() { Agent agent1 = mock(Agent.class); MultiValuedMap params1 = new MultiValuedMap(); params1.addString(KeywordRouter.KEYWORDS, "time"); params1.addString(KeywordRouter.KEYWORDS, "date"); Agent agent2 = mock(Agent.class); MultiValuedMap params2 = new MultiValuedMap(); params2.addString(KeywordRouter.KEYWORDS, "twitter"); RouterAgentConfig config = RouterAgentConfig.create(); config.add(agent1, params1); config.add(agent2, params2); Router router = new KeywordRouter(config); Agent agents[] = router.route(new Request("What time is it")); Agent expected[] = new Agent[]{agent1}; assertArrayEquals(expected, agents); Agent agents2[] = router.route(new Request("Post this on twitter")); Agent expected2[] = new Agent[]{agent2}; assertArrayEquals(expected2, agents2); }
@Test public void testRoutingWithNoMatch() { Agent agent1 = mock(Agent.class); MultiValuedMap params1 = new MultiValuedMap(); params1.addString(KeywordRouter.KEYWORDS, "time"); params1.addString(KeywordRouter.KEYWORDS, "date"); Router router = new KeywordRouter(RouterAgentConfig.create().add(agent1, params1)); Agent agents[] = router.route(new Request("What is the temperature")); Agent expected[] = new Agent[]{}; assertArrayEquals(expected, agents); }
@Test public void testRoutingWithMatchSameAgentTwice() { Agent agent = mock(Agent.class); MultiValuedMap params = new MultiValuedMap(); params.addString(KeywordRouter.KEYWORDS, "time"); params.addString(KeywordRouter.KEYWORDS, "date"); Router router = new KeywordRouter(RouterAgentConfig.create().add(agent, params)); Agent agents[] = router.route(new Request("What is the time and date")); Agent expected[] = new Agent[]{agent}; assertArrayEquals(expected, agents); }
@Test public void testRoutingWithSubwordMatch() { Agent agent = mock(Agent.class); MultiValuedMap params = new MultiValuedMap(); params.addString(KeywordRouter.KEYWORDS, "time"); params.addString(KeywordRouter.KEYWORDS, "date"); Router router = new KeywordRouter(RouterAgentConfig.create().add(agent, params)); Agent agents[] = router.route(new Request("Do you have a timer")); Agent expected[] = new Agent[]{}; assertArrayEquals(expected, agents); }