/src/example/java/example/BasicExample.java

https://bitbucket.org/sgla/aiml-core · Java · 94 lines · 73 code · 21 blank · 0 comment · 5 complexity · c2089255be8b41834b78c00f91d787cb MD5 · raw file

  1. package example;
  2. import java.io.File;
  3. import java.net.URL;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import javax.inject.Inject;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import com.google.inject.Guice;
  10. import com.google.inject.Injector;
  11. import ai.saxatus.aiml.api.AIMLHandler;
  12. import ai.saxatus.aiml.api.AIMLHandlerBuilder;
  13. import ai.saxatus.aiml.api.AIMLResponse;
  14. import ai.saxatus.aiml.api.io.AIMLCreationException;
  15. import ai.saxatus.aiml.api.io.AIMLFileReader;
  16. import ai.saxatus.aiml.module.AIMLModule;
  17. public class BasicExample
  18. {
  19. @Inject
  20. AIMLHandlerBuilder aimlHandlerBuilder;
  21. private static final Log log = LogFactory.getLog(BasicExample.class);
  22. private Map<String, String> botMem = new HashMap<>();
  23. private AIMLHandler getAIMLHandler() throws AIMLCreationException
  24. {
  25. URL r = getClass().getResource("/example.aiml");
  26. File file = new File(r.getFile());
  27. botMem.put("name", "TestBot");
  28. return aimlHandlerBuilder.nonStaticMemory(new HashMap<>())
  29. .withBotMemory(botMem)
  30. .withAimlProvider(new AIMLFileReader(file))
  31. .build();
  32. }
  33. public void getAnswer()
  34. {
  35. AIMLResponse response;
  36. try
  37. {
  38. AIMLHandler handler = getAIMLHandler();
  39. response = handler.getAnswer("Hello TestBot");
  40. log.info(response);
  41. if (!"Hello User.".equals(response.getAnswer()))
  42. System.exit(1);
  43. response = handler.getAnswer("Condition test");
  44. log.info(response);
  45. if (!"No topic set.".equals(response.getAnswer()))
  46. System.exit(1);
  47. response = handler.getAnswer("What's your name?");
  48. log.info(response);
  49. if (!"My name is TestBot".equals(response.getAnswer()))
  50. System.exit(1);
  51. response = handler.getAnswer("My name is User");
  52. log.info(response);
  53. if (!"Random Name Sentence".equals(response.getAnswer()))
  54. System.exit(1);
  55. response = handler.getAnswer("Condition test");
  56. log.info(response);
  57. if (!"Topic is names.".equals(response.getAnswer()))
  58. System.exit(1);
  59. log.info("Basic Example successful");
  60. }
  61. catch(AIMLCreationException e)
  62. {
  63. log.error(e);
  64. }
  65. }
  66. public static void main(String[] args)
  67. {
  68. Injector injector = Guice.createInjector(new AIMLModule());
  69. BasicExample example = new BasicExample();
  70. injector.injectMembers(example);
  71. example.getAnswer();
  72. }
  73. }