1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
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
101
102
103
104 public RulesEngine getRulesEngine( )
105 {
106 return _engine;
107 }
108
109
110
111
112
113
114
115 public void setRulesEngine( RulesEngine rulesEngine )
116 {
117 _engine = rulesEngine;
118 }
119
120
121
122
123
124
125
126
127 public void setListResponseFilters( List<ResponseFilter> list )
128 {
129 _listResponseFilters = list;
130 }
131
132
133
134
135
136
137 public List<ResponseFilter> getResponseFilters( )
138 {
139 return _listResponseFilters;
140 }
141
142
143
144
145 @Override
146 public void reset( String strConversationId )
147 {
148 super.reset( strConversationId );
149
150 _mapBotExecutor.remove( strConversationId );
151 }
152
153
154
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 }