View Javadoc
1   /*
2    * Copyright (c) 2002-2018, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.easyrulesbot.service.bot;
35  
36  import fr.paris.lutece.plugins.chatbot.business.BotPost;
37  import fr.paris.lutece.plugins.chatbot.service.bot.AbstractChatBot;
38  import fr.paris.lutece.plugins.easyrulesbot.service.bot.rules.BotRule;
39  import fr.paris.lutece.plugins.easyrulesbot.service.response.filters.ResponseFilter;
40  import fr.paris.lutece.plugins.easyrulesbot.service.response.exceptions.ResponseProcessingException;
41  import fr.paris.lutece.portal.web.l10n.LocaleService;
42  import java.util.ArrayList;
43  import java.util.HashMap;
44  import java.util.List;
45  import java.util.Locale;
46  import java.util.Map;
47  import org.easyrules.api.Rule;
48  import org.easyrules.api.RulesEngine;
49  
50  /**
51   * EasyRulesBot
52   */
53  public class EasyRulesBot extends AbstractChatBot
54  {
55      private static final String PROPERTY_LAST_MESSAGE = "easyrulesbot.bot.lastMessage";
56  
57      private static Map<String, BotExecutor> _mapBotExecutor = new HashMap<>( );
58      private RulesEngine _engine;
59      private List<ResponseFilter> _listResponseFilters;
60  
61      /**
62       * {@inheritDoc }
63       */
64      @Override
65      public List<BotPost> processUserMessage( String strMessage, String strConversationId, Locale locale )
66      {
67          List<BotPost> listBotPost;
68          BotExecutor executor = _mapBotExecutor.get( strConversationId );
69          if ( executor == null )
70          {
71              executor = new BotExecutor( this, locale );
72              _mapBotExecutor.put( strConversationId, executor );
73          }
74  
75          try
76          {
77              listBotPost = executor.processResponse( strMessage );
78          }
79          catch( ResponseProcessingException ex )
80          {
81              listBotPost = new ArrayList<>( );
82              listBotPost.add( new BotPost( ex.getMessage( ), BotPost.CONTENT_TYPE_TEXT ) );
83          }
84  
85          executor.fireRules( );
86  
87          String strQuestion = executor.getQuestion( );
88          
89          if( strQuestion != null )
90          {
91              listBotPost.add( new BotPost( strQuestion, BotPost.CONTENT_TYPE_TEXT ) );
92          }
93  
94          executor.traceData( );
95  
96          return listBotPost;
97      }
98  
99      /**
100      * Returns the RulesEngine
101      *
102      * @return The RulesEngine
103      */
104     public RulesEngine getRulesEngine( )
105     {
106         return _engine;
107     }
108 
109     /**
110      * Sets the RulesEngine
111      *
112      * @param rulesEngine
113      *            The RulesEngine
114      */
115     public void setRulesEngine( RulesEngine rulesEngine )
116     {
117         _engine = rulesEngine;
118     }
119 
120     /**
121      * Set the list of response filters
122      *
123      * @param list
124      *            The list
125      *
126      */
127     public void setListResponseFilters( List<ResponseFilter> list )
128     {
129         _listResponseFilters = list;
130     }
131 
132     /**
133      * The list of response filters
134      *
135      * @return The list
136      */
137     public List<ResponseFilter> getResponseFilters( )
138     {
139         return _listResponseFilters;
140     }
141     
142     /**
143      * {@inheritDoc }
144      */
145     @Override
146     public void reset( String strConversationId )
147     {
148         super.reset( strConversationId );
149         
150         _mapBotExecutor.remove( strConversationId );
151     }
152     
153     /**
154      * {@inheritDoc }
155      */
156     @Override
157     public String toString()
158     {
159         StringBuilder sbOutput = new StringBuilder( "\nEASYRULE BOT\n");
160         sbOutput.append( "\n key : " ).append( getKey() );
161         sbOutput.append( "\n name : " ).append( getName( LocaleService.getDefault()) );
162         sbOutput.append( "\n description : " ).append( getDescription( LocaleService.getDefault() ) );
163         sbOutput.append( "\n avatar URL : " ).append( getAvatarUrl() );
164         sbOutput.append( "\n welcome message : " ).append( getWelcomeMessage() );
165         sbOutput.append( "\n Rules : " );
166         for( Rule rule : getRulesEngine().getRules() )
167         {
168             BotRule br = (BotRule) rule;
169             sbOutput.append( br );
170         }
171         return sbOutput.toString();
172     }
173 
174     
175 }