11package net .b07z .sepia .sdk .main ;
22
33import java .util .ArrayList ;
4+ import java .util .Arrays ;
45import java .util .List ;
56import java .util .Map ;
67
78import org .slf4j .Logger ;
89import org .slf4j .LoggerFactory ;
910
1011import net .b07z .sepia .sdk .services .uid1007 .RestaurantDemo ;
12+ import net .b07z .sepia .sdk .substitutes .AnswerLoaderEmpty ;
13+ import net .b07z .sepia .server .assist .answers .AnswerLoaderFile ;
14+ import net .b07z .sepia .server .assist .answers .DefaultReplies ;
1115import net .b07z .sepia .server .assist .assistant .LANGUAGES ;
16+ import net .b07z .sepia .server .assist .data .Parameter ;
1217import net .b07z .sepia .server .assist .interpreters .NluInput ;
1318import net .b07z .sepia .server .assist .interpreters .NluKeywordAnalyzer ;
1419import net .b07z .sepia .server .assist .interpreters .NluResult ;
1520import net .b07z .sepia .server .assist .interpreters .Normalizer ;
21+ import net .b07z .sepia .server .assist .interviews .AbstractInterview ;
22+ import net .b07z .sepia .server .assist .interviews .Interview ;
23+ import net .b07z .sepia .server .assist .interviews .InterviewInterface ;
24+ import net .b07z .sepia .server .assist .interviews .InterviewResult ;
1625import net .b07z .sepia .server .assist .interviews .InterviewServicesMap ;
1726import net .b07z .sepia .server .assist .parameters .ParameterConfig ;
27+ import net .b07z .sepia .server .assist .parameters .ParameterHandler ;
1828import net .b07z .sepia .server .assist .server .Config ;
1929import net .b07z .sepia .server .assist .server .ConfigTestServer ;
2030import net .b07z .sepia .server .assist .services .ServiceInterface ;
31+ import net .b07z .sepia .server .assist .services .ServiceResult ;
32+ import net .b07z .sepia .server .core .assistant .PARAMETERS ;
2133import net .b07z .sepia .server .core .tools .JSONWriter ;
2234
2335/**
24- * Demonstrates how to test a service locally before using the upload function.
36+ * Demonstrates how to test a service locally before using the upload function and offers methods to help with testing .
2537 *
2638 * @author Florian Quirin
2739 *
@@ -41,25 +53,59 @@ public static void main(String[] args) {
4153 ServiceInterface service = new RestaurantDemo ();
4254 String lang = LANGUAGES .EN ;
4355
44- //Run
56+ //Get a result using the regular-expressions defined inside custom service
4557 setup ();
4658 NluResult result = testServiceWithRegExpTrigger (service , text , lang );
4759
4860 //Print result
4961 log .info (JSONWriter .getPrettyString (result .getBestResultJSON ()));
62+
63+ //Build a parameter - result is added to given NluResult
64+ /*
65+ Interview interviewApi = new Interview(result);
66+ Parameter p1 = buildParameter(interviewApi, PARAMETERS.NUMBER); //global name or ...
67+ Parameter p2 = buildParameter(interviewApi, PARAMETERS.TIME);
68+ Parameter p3 = buildParameter(interviewApi, new RestaurantDemo.ReservationName()); //... parameter handler
69+
70+ //Print individual results
71+ log.info(JSONWriter.getPrettyString(p1.getData()));
72+ log.info(JSONWriter.getPrettyString(p2.getData()));
73+ log.info(JSONWriter.getPrettyString(p3.getData()));
74+ */
75+
76+ //Get service result from NLU result
77+ ServiceResult sr = getServiceResultFromNluResult (service , result );
78+
79+ //Print result
80+ log .info (JSONWriter .getPrettyString (sr .getResultJSONObject ()));
5081 }
5182
5283 /**
53- * Setup some components of SEPIA.
84+ * Setup some default components of SEPIA.
5485 */
5586 public static void setup () {
87+ setup (false );
88+ }
89+ /**
90+ * Setup some default components of SEPIA and include the answers-file loader.<br>
91+ * NOTE: It will try to load answer files from "answers/" folder (may be useful if you want to include your custom files).
92+ */
93+ public static void setup (boolean includeAnswersFromFile ){
5694 //setup answers
57- //Config.setAnswerModule(new AnswerLoaderFile()); //choose txt-file answers-module
58- //DefaultReplies.setupDefaults(); //setup default question mapping for parameters and stuff
95+ if (includeAnswersFromFile ){
96+ Config .answersPath = "answers/" ; //new folder to copy answer files to
97+ Config .setAnswerModule (new AnswerLoaderFile ()); //choose txt-file answers-module
98+ }else {
99+ Config .setAnswerModule (new AnswerLoaderEmpty ());
100+ }
101+ DefaultReplies .setupDefaults (); //setup default question mapping for parameters and stuff
59102
60103 //setup commands and parameters
61104 InterviewServicesMap .load (); //services connected to interviews
62105 ParameterConfig .setup (); //connect parameter names to handlers and other stuff
106+
107+ //reduce DB access for default test user (we are not running a DB in this test-mode)
108+ ConfigTestServer .reduceDatabaseAccess (ConfigTestServer .getFakeUserId (null ));
63109 }
64110
65111 /**
@@ -95,7 +141,10 @@ public static NluResult testServiceWithRegExpTrigger(ServiceInterface service, S
95141
96142 int bestScoreIndex = 0 ; //for the test we assume that index 0 is our goal
97143 log .info ("Primary score: " + possibleScore .get (bestScoreIndex ));
144+
98145 NluResult result = new NluResult (possibleCMDs , possibleParameters , possibleScore , bestScoreIndex );
146+ result .input = input ; //TODO: is this enough for simple testing?
147+
99148 return result ;
100149 }
101150
@@ -108,4 +157,51 @@ private static String normalize(String text, String languageCode) {
108157 return normText ;
109158 }
110159
160+ /**
161+ * Very basic method to execute parameter 'build' process.
162+ * @param interview - {@link Interview} object initialized with given {@NluResult}
163+ * @param parameterName - name of a parameter, usually defined in {@link PARAMETERS}
164+ */
165+ public static Parameter buildParameter (Interview interview , String parameterName ){
166+ Parameter p = new Parameter (parameterName );
167+ return buildParameter (interview , p );
168+ }
169+ /**
170+ * Very basic method to execute parameter 'build' process.
171+ * @param interview - {@link Interview} object initialized with given {@NluResult}
172+ * @param parameterHandler - {@link ParameterHandler} e.g. your parameter from a custom service
173+ */
174+ public static Parameter buildParameter (Interview interview , ParameterHandler parameterHandler ){
175+ Parameter p = new Parameter (parameterHandler );
176+ return buildParameter (interview , p );
177+ }
178+ /**
179+ * Very basic method to execute parameter 'build' process.
180+ */
181+ private static Parameter buildParameter (Interview interview , Parameter p ){
182+ interview .getParameterInput (p );
183+ interview .buildParameterOrComment (p , null );
184+ return interview .nluResult .getOptionalParameter (p .getName (), null );
185+ }
186+
187+ /**
188+ * Build a service result for a given {@link ServiceInterface} from previously created {@link NluResult}.<br>
189+ * NOTE: Depending on how your answers-module is set up (default is empty) you will only get the answer-keys not the "real" answer.
190+ * @param service - your custom service, e.g. "new RestaurantDemo()"
191+ * @param nluResult - previously created {@link NluResult}, e.g. via {@link #testServiceWithRegExpTrigger}
192+ * @return
193+ */
194+ public static ServiceResult getServiceResultFromNluResult (ServiceInterface service , NluResult nluResult ){
195+ //ServiceResult answer;
196+ List <ServiceInterface > services = Arrays .asList (service );
197+ InterviewInterface interview = new AbstractInterview ();
198+ interview .setCommand (nluResult .getCommand ());
199+ interview .setServices (services );
200+ InterviewResult iResult = interview .getMissingParameters (nluResult );
201+ if (iResult .isComplete ()){
202+ return interview .getServiceResults (iResult );
203+ }else {
204+ return iResult .getApiComment ();
205+ }
206+ }
111207}
0 commit comments