This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/master by this push:
     new 4f462e3  Weather example fixes.
4f462e3 is described below

commit 4f462e34789fd9d23bd69b4939008fc1ce9c3c2c
Author: Sergey Kamov <[email protected]>
AuthorDate: Fri Jun 25 09:49:07 2021 +0300

    Weather example fixes.
---
 .../nlpcraft/examples/weather/WeatherModel.java    |  7 +++++--
 .../openweathermap/OpenWeatherMapService.java      | 23 +++++++++++-----------
 .../examples/weather/NCModelValidationSpec.scala   | 12 +++++++++++
 3 files changed, 28 insertions(+), 14 deletions(-)

diff --git 
a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/WeatherModel.java
 
b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/WeatherModel.java
index 5b8b8b8..f8380b6 100644
--- 
a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/WeatherModel.java
+++ 
b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/WeatherModel.java
@@ -47,13 +47,16 @@ import static java.time.temporal.ChronoUnit.DAYS;
  * <p>
  * This is a relatively complete weather service with JSON output and a 
non-trivial
  * intent matching logic. It uses OpenWeather API weather provider REST 
service for the actual
- * weather information (https://openweathermap.org/api/one-call-api)
+ * weather information (https://openweathermap.org/api/one-call-api).
+ * <p>
+ * NOTE: you must provide OpenWorldMap API key in 'OWM_API_KEY' system 
property.
+ * See  https://openweathermap.org/api for more information.
  * <p>
  * See 'README.md' file in the same folder for running and testing 
instructions.
  */
 @SuppressWarnings("OptionalUsedAsFieldOrParameterType")
 public class WeatherModel extends NCModelFileAdapter {
-    /* System property for OpenWeatherMap API key. */
+    // System property for OpenWeatherMap API key.
     public final String OWM_API_KEY = "OWM_API_KEY";
 
     // Please register your own account at https://openweathermap.org/api and
diff --git 
a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/openweathermap/OpenWeatherMapService.java
 
b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/openweathermap/OpenWeatherMapService.java
index 2faa068..1d43507 100644
--- 
a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/openweathermap/OpenWeatherMapService.java
+++ 
b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/openweathermap/OpenWeatherMapService.java
@@ -50,7 +50,8 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
  */
 public class OpenWeatherMapService {
     // GSON response type.
-    private static final Type TYPE_RESP = new TypeToken<HashMap<String, 
Object>>() {}.getType();
+    private static final Type TYPE_RESP = new TypeToken<HashMap<String, 
Object>>() {
+    }.getType();
 
     // Access key.
     private final String key;
@@ -69,8 +70,8 @@ public class OpenWeatherMapService {
 
     // Can be configured.
     private final ExecutorService pool = NCUtils.mkThreadPool(
-    "openweather",
-    Runtime.getRuntime().availableProcessors() * 8
+        "openweather",
+        Runtime.getRuntime().availableProcessors() * 8
     );
 
     /**
@@ -98,8 +99,8 @@ public class OpenWeatherMapService {
     /**
      * Constructor.
      *
-     * @param key  Service key.
-     * @param maxDaysBack Max days (looking back) configuration value.
+     * @param key            Service key.
+     * @param maxDaysBack    Max days (looking back) configuration value.
      * @param maxDaysForward Max days (looking forward) configuration value.
      */
     public OpenWeatherMapService(String key, int maxDaysBack, int 
maxDaysForward) {
@@ -125,10 +126,9 @@ public class OpenWeatherMapService {
     }
 
     /**
-     *
      * @param lat Latitude.
      * @param lon Longitude.
-     * @param d Date.
+     * @param d   Date.
      * @return REST call result.
      */
     private Map<String, Object> get(double lat, double lon, long d) {
@@ -141,7 +141,6 @@ public class OpenWeatherMapService {
     }
 
     /**
-     *
      * @param url REST endpoint URL.
      * @return REST call result.
      */
@@ -167,10 +166,10 @@ public class OpenWeatherMapService {
     /**
      * See https://openweathermap.org/api/one-call-api#hist_parameter to 
extract fields.
      *
-     * @param lat Latitude.
-     * @param lon Longitude.
+     * @param lat  Latitude.
+     * @param lon  Longitude.
      * @param from From date.
-     * @param to To date.
+     * @param to   To date.
      * @return List of REST call results.
      * @throws OpenWeatherMapException Thrown in case of any provider errors.
      */
@@ -218,6 +217,6 @@ public class OpenWeatherMapService {
      * @throws OpenWeatherMapException Thrown in case of any provider errors.
      */
     public Map<String, Object> getCurrent(double lat, double lon) throws 
OpenWeatherMapException {
-        return get("https://api.openweathermap.org/data/2.5/forecast?lat="; + 
lat + "&lon=" + lon +"&appid="+ key);
+        return get("https://api.openweathermap.org/data/2.5/forecast?lat="; + 
lat + "&lon=" + lon + "&appid=" + key);
     }
 }
diff --git 
a/nlpcraft-examples/weather/src/test/java/org/apache/nlpcraft/examples/weather/NCModelValidationSpec.scala
 
b/nlpcraft-examples/weather/src/test/java/org/apache/nlpcraft/examples/weather/NCModelValidationSpec.scala
index 6258fe6..136d713 100644
--- 
a/nlpcraft-examples/weather/src/test/java/org/apache/nlpcraft/examples/weather/NCModelValidationSpec.scala
+++ 
b/nlpcraft-examples/weather/src/test/java/org/apache/nlpcraft/examples/weather/NCModelValidationSpec.scala
@@ -24,8 +24,20 @@ import org.junit.jupiter.api.{Assertions, Test}
   * JUnit model validation.
   */
 class NCModelValidationSpec {
+    private final val propName = "OWM_API_KEY"
+
     @Test
     def test(): Unit = {
+        // Set your own API key here.
+        var apiKey: String = System.getProperty(propName)
+
+        if (apiKey == null)
+            apiKey = System.getenv(propName)
+
+        // Default value, used for tests.
+        if (apiKey == null)
+            System.setProperty(propName, "8a51a2eb343bf87dc55ffd352f5641ea")
+
         // Instruct auto-validator what models to test.
         System.setProperty("NLPCRAFT_TEST_MODELS", 
"org.apache.nlpcraft.examples.weather.WeatherModel")
 

Reply via email to