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

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


The following commit(s) were added to refs/heads/NLPCRAFT-495 by this push:
     new 4dae2138 API refactored.
4dae2138 is described below

commit 4dae213829060ae261139b851817b7051b38720b
Author: Sergey Kamov <[email protected]>
AuthorDate: Tue Jul 5 13:43:11 2022 +0300

    API refactored.
---
 .../examples/lightswitch/LightSwitchFrModel.scala  |  2 ++
 .../lightswitch/NCModelValidationSpec.scala        |  0
 .../examples/lightswitch/LightSwitchRuModel.scala  |  2 ++
 .../lightswitch/NCModelValidationSpec.scala        |  0
 .../lightswitch/LightSwitchScalaModel.scala        |  2 ++
 .../lightswitch/NCModelValidationSpec.scala        |  0
 .../apache/nlpcraft/examples/time/TimeModel.scala  |  6 ++--
 .../examples/time/NCModelValidationSpec.scala      |  0
 .../scala/org/apache/nlpcraft/NCModelClient.scala  | 32 ++++++++++++++++++----
 .../internal/conversation/NCConversationSpec.scala |  6 ++--
 .../conversation/NCConversationTimeoutSpec.scala   |  2 +-
 .../internal/impl/NCModelCallbacksSpec.scala       |  2 +-
 .../nlpcraft/internal/impl/NCModelClientSpec.scala |  4 +--
 .../internal/impl/NCModelClientSpec2.scala         |  4 +--
 .../internal/impl/NCModelClientSpec3.scala         |  2 +-
 .../internal/impl/NCModelPingPongSpec.scala        |  2 +-
 .../apache/nlpcraft/nlp/NCEntityMapperSpec.scala   |  2 +-
 .../org/apache/nlpcraft/nlp/util/NCTestUtils.scala |  2 +-
 18 files changed, 48 insertions(+), 22 deletions(-)

diff --git 
a/nlpcraft-examples/lightswitch-fr/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchFrModel.scala
 
b/nlpcraft-examples/lightswitch-fr/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchFrModel.scala
index 991f0a1c..eb0d8533 100644
--- 
a/nlpcraft-examples/lightswitch-fr/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchFrModel.scala
+++ 
b/nlpcraft-examples/lightswitch-fr/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchFrModel.scala
@@ -83,6 +83,8 @@ class LightSwitchFrModel extends NCModelAdapter(
         "Tuez l'illumination maintenant."
     ))
     def onMatch(
+        ctx: NCContext,
+        im: NCIntentMatch,
         @NCIntentTerm("act") actEnt: NCEntity,
         @NCIntentTerm("loc") locEnts: List[NCEntity]
     ): NCResult =
diff --git 
a/nlpcraft-examples/lightswitch-fr/src/test/java/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
 
b/nlpcraft-examples/lightswitch-fr/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
similarity index 100%
rename from 
nlpcraft-examples/lightswitch-fr/src/test/java/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
rename to 
nlpcraft-examples/lightswitch-fr/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
diff --git 
a/nlpcraft-examples/lightswitch-ru/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchRuModel.scala
 
b/nlpcraft-examples/lightswitch-ru/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchRuModel.scala
index 630fd555..60e7399c 100644
--- 
a/nlpcraft-examples/lightswitch-ru/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchRuModel.scala
+++ 
b/nlpcraft-examples/lightswitch-ru/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchRuModel.scala
@@ -67,6 +67,8 @@ class LightSwitchRuModel extends NCModelAdapter(
         "Зажги лампу на кухне"
     ))
     def onMatch(
+        ctx: NCContext, 
+        im: NCIntentMatch,
         @NCIntentTerm("act") actEnt: NCEntity,
         @NCIntentTerm("loc") locEnts: List[NCEntity]
     ): NCResult =
diff --git 
a/nlpcraft-examples/lightswitch-ru/src/test/java/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
 
b/nlpcraft-examples/lightswitch-ru/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
similarity index 100%
rename from 
nlpcraft-examples/lightswitch-ru/src/test/java/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
rename to 
nlpcraft-examples/lightswitch-ru/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
diff --git 
a/nlpcraft-examples/lightswitch/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchScalaModel.scala
 
b/nlpcraft-examples/lightswitch/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchScalaModel.scala
index c4ee5e07..119542cb 100644
--- 
a/nlpcraft-examples/lightswitch/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchScalaModel.scala
+++ 
b/nlpcraft-examples/lightswitch/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchScalaModel.scala
@@ -76,6 +76,8 @@ class LightSwitchScalaModel extends NCModelAdapter(
         "Kill the illumination now!"
     ))
     def onMatch(
+        ctx: NCContext,
+        im: NCIntentMatch,
         @NCIntentTerm("act") actEnt: NCEntity,
         @NCIntentTerm("loc") locEnts: List[NCEntity]
     ): NCResult =
diff --git 
a/nlpcraft-examples/lightswitch/src/test/java/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
 
b/nlpcraft-examples/lightswitch/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
similarity index 100%
rename from 
nlpcraft-examples/lightswitch/src/test/java/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
rename to 
nlpcraft-examples/lightswitch/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
diff --git 
a/nlpcraft-examples/time/src/main/scala/org/apache/nlpcraft/examples/time/TimeModel.scala
 
b/nlpcraft-examples/time/src/main/scala/org/apache/nlpcraft/examples/time/TimeModel.scala
index 75747999..4eea5984 100644
--- 
a/nlpcraft-examples/time/src/main/scala/org/apache/nlpcraft/examples/time/TimeModel.scala
+++ 
b/nlpcraft-examples/time/src/main/scala/org/apache/nlpcraft/examples/time/TimeModel.scala
@@ -89,7 +89,7 @@ class TimeModel extends NCModelAdapter(
         "Show me time of the day in London.",
         "Can you please give me the Tokyo's current date and time."
     ))
-    private def onRemoteMatch(@NCIntentTerm("city") cityEnt: NCEntity): 
NCResult =
+    private def onRemoteMatch(ctx: NCContext, im: NCIntentMatch, 
@NCIntentTerm("city") cityEnt: NCEntity): NCResult =
         val cityName: String = cityEnt.mkText
 
         // We don't have timezone mapping for parsed GEO location.
@@ -107,7 +107,7 @@ class TimeModel extends NCModelAdapter(
       * @return Query result. */
     @NCIntentRef("intent1")
     @NCIntentSample(Array("What's the local time?"))
-    private def onLocalMatch(ctx: NCIntentMatch): NCResult =  // NOTE:
+    private def onLocalMatch(ctx: NCContext, im: NCIntentMatch): NCResult =  
// NOTE:
         // We need to have two intents vs. one intent with an optional GEO. 
The reason is that
         // first intent isn't using the conversation to make sure we can 
always ask
         // for local time **no matter** what was asked before... Note that 
non-conversational
@@ -117,7 +117,7 @@ class TimeModel extends NCModelAdapter(
         // That's an indication of asking for local time only.
         // Get local GEO data from sentence metadata defaulting to
         // Silicon Valley location in case we are missing that info.
-        val geo = 
GeoManager.get(ctx.getContext.getRequest).getOrElse(GeoManager.getSiliconValley)
+        val geo = 
GeoManager.get(ctx.getRequest).getOrElse(GeoManager.getSiliconValley)
 
         mkResult(geo.city, geo.country_name, geo.timezone, geo.latitude, 
geo.longitude)
 
diff --git 
a/nlpcraft-examples/time/src/test/java/org/apache/nlpcraft/examples/time/NCModelValidationSpec.scala
 
b/nlpcraft-examples/time/src/test/scala/org/apache/nlpcraft/examples/time/NCModelValidationSpec.scala
similarity index 100%
rename from 
nlpcraft-examples/time/src/test/java/org/apache/nlpcraft/examples/time/NCModelValidationSpec.scala
rename to 
nlpcraft-examples/time/src/test/scala/org/apache/nlpcraft/examples/time/NCModelValidationSpec.scala
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala
index 68d53092..e7ed8411 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala
@@ -113,32 +113,47 @@ class NCModelClient(mdl: NCModel) extends LazyLogging, 
AutoCloseable:
       * @return
       */
     def ask(txt: String, data: Map[String, AnyRef], usrId: String): NCResult =
+        require(txt != null, "Input text cannot be null.")
+        require(data != null, "Data cannot be null.")
+        require(usrId != null, "User id cannot be null.")
         ask0(txt, data, usrId, NCIntentSolveType.REGULAR).swap.toOption.get
 
+    def ask(txt: String, usrId: String): NCResult = ask(txt, Map.empty, usrId)
+
     /**
       *
       * @param usrId
       */
-    def clearStm(usrId: String): Unit = convMgr.getConversation(usrId).clear(_ 
=> true)
+    def clearStm(usrId: String): Unit =
+        require(usrId != null, "User id cannot be null.")
+        convMgr.getConversation(usrId).clear(_ => true)
 
     /**
       *
       * @param usrId
       * @param filter
       */
-    def clearStm(usrId: String, filter: NCEntity => Boolean): Unit = 
convMgr.getConversation(usrId).clear(filter)
+    def clearStm(usrId: String, filter: NCEntity => Boolean): Unit =
+        require(usrId != null, "User id cannot be null.")
+        require(filter != null, "Filter cannot be null.")
+        convMgr.getConversation(usrId).clear(filter)
 
     /**
       *
       * @param usrId
       */
-    def clearDialog(usrId: String): Unit = dlgMgr.clear(usrId)
+    def clearDialog(usrId: String): Unit =
+        require(usrId != null, "User id cannot be null.")
+        dlgMgr.clear(usrId)
 
     /**
       *
       * @param usrId
       */
-    def clearDialog(usrId: String, filter: NCDialogFlowItem => Boolean): Unit 
= dlgMgr.clear(usrId, (i: NCDialogFlowItem) => filter(i))
+    def clearDialog(usrId: String, filter: NCDialogFlowItem => Boolean): Unit =
+        require(usrId != null, "User id cannot be null.")
+        require(usrId != null, "Filter cannot be null.")
+        dlgMgr.clear(usrId, (i: NCDialogFlowItem) => filter(i))
 
     /**
       *
@@ -157,7 +172,7 @@ class NCModelClient(mdl: NCModel) extends LazyLogging, 
AutoCloseable:
 
                 val err: Option[String] =
                     try
-                        val r = ask(sample, null, userId)
+                        val r = ask(sample, Map.empty, userId)
 
                         Option.when(r.intentId != i.intent.id)(s"Unexpected 
intent ID: '${r.intentId}'")
                     catch case e: Throwable =>
@@ -206,5 +221,10 @@ class NCModelClient(mdl: NCModel) extends LazyLogging, 
AutoCloseable:
       * @return
       */
     def debugAsk(txt: String, data: Map[String, AnyRef], usrId: String, 
saveHist: Boolean): NCCallbackData =
+        require(txt != null, "Input text cannot be null.")
+        require(data != null, "Data cannot be null.")
+        require(usrId != null, "User id cannot be null.")
         import NCIntentSolveType.*
-        ask0(txt, data, usrId, if saveHist then SEARCH else 
SEARCH_NO_HISTORY).toOption.get
\ No newline at end of file
+        ask0(txt, data, usrId, if saveHist then SEARCH else 
SEARCH_NO_HISTORY).toOption.get
+
+    def debugAsk(txt: String, usrId: String, saveHist: Boolean): 
NCCallbackData = debugAsk(txt, Map.empty, usrId, saveHist)
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationSpec.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationSpec.scala
index b2ff04e5..18084985 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationSpec.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationSpec.scala
@@ -51,10 +51,10 @@ class NCConversationSpec:
                 def onMatch(ctx: NCContext, im: NCIntentMatch, 
@NCIntentTerm("t1") t1: NCEntity, @NCIntentTerm("t2") t2: Option[NCEntity]): 
NCResult = NCTestResult()
 
         Using.resource(new NCModelClient(mdl)) { cli =>
-            def execOk(txt: String): Unit = cli.ask(txt, null, usrId)
+            def execOk(txt: String): Unit = cli.ask(txt, usrId)
             def execReject(txt: String): Unit =
                 try
-                    cli.ask(txt, null, usrId)
+                    cli.ask(txt, usrId)
                     require(false)
                 catch
                     case e: NCRejection => // OK.
@@ -95,7 +95,7 @@ class NCConversationSpec:
                     NCTestResult()
 
         Using.resource(new NCModelClient(mdl)) { client =>
-            client.ask("e1", null, "userId")
+            client.ask("e1", "userId")
             client.clearDialog("userId1", _ => true)
             client.clearDialog("userId2")
             client.clearStm("userId3", _ => true)
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationTimeoutSpec.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationTimeoutSpec.scala
index a91d3ac7..61442ad6 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationTimeoutSpec.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationTimeoutSpec.scala
@@ -64,7 +64,7 @@ class NCConversationTimeoutSpec:
 
         Using.resource(new NCModelClient(mdl)) { cli =>
             def check(hasValue: Boolean): Unit =
-                require(cli.ask("test", null, "userId").body.toString == (if 
hasValue then VALUE else EMPTY))
+                require(cli.ask("test", "userId").body.toString == (if 
hasValue then VALUE else EMPTY))
 
             check(false)
             check(true)
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelCallbacksSpec.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelCallbacksSpec.scala
index bb947844..b6eb9037 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelCallbacksSpec.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelCallbacksSpec.scala
@@ -100,7 +100,7 @@ class NCModelCallbacksSpec:
       */
     private def testOk(client: NCModelClient, exp: NCResult, states: State*): 
Unit =
         set(states*)
-        require(client.ask("x", null, "userId").body == exp.body)
+        require(client.ask("x", "userId").body == exp.body)
 
     /**
       *
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala
index fc8426c3..a9f7c5b2 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala
@@ -47,14 +47,14 @@ class NCModelClientSpec:
         mdl.pipeline.entParsers += 
NCTestUtils.mkEnSemanticParser("models/lightswitch_model.yaml")
 
         Using.resource(new NCModelClient(mdl)) { client =>
-            val res = client.ask("Lights on at second floor kitchen", null, 
"userId")
+            val res = client.ask("Lights on at second floor kitchen", "userId")
 
             println(s"Intent: ${res.intentId}")
             println(s"Body: ${res.body}")
 
             client.validateSamples()
 
-            val winner = client.debugAsk("Lights on at second floor kitchen", 
null, "userId", true)
+            val winner = client.debugAsk("Lights on at second floor kitchen", 
"userId", true)
             println(s"Winner intent: ${winner.getIntentId}")
             println("Entities: \n" + winner.getCallbackArguments.map(p => 
p.map(s).mkString(", ")).mkString("\n"))
         }
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec2.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec2.scala
index 1df8fae1..4333fa2b 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec2.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec2.scala
@@ -49,7 +49,7 @@ class NCModelClientSpec2:
 
         Using.resource(new NCModelClient(mdl)) { client =>
             case class Result(txt: String):
-                private val wi = client.debugAsk(txt, null, "userId", true)
+                private val wi = client.debugAsk(txt, "userId", true)
                 private val allArgs: List[List[NCEntity]] = 
wi.getCallbackArguments
 
                 val intentId: String = wi.getIntentId
@@ -89,7 +89,7 @@ class NCModelClientSpec2:
 
             // 3. No winners.
             try
-                client.debugAsk("x", null, "userId", false)
+                client.debugAsk("x", "userId", false)
 
                 require(false)
             catch
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec3.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec3.scala
index 54331930..f98408be 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec3.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec3.scala
@@ -47,7 +47,7 @@ class NCModelClientSpec3:
             def onMatch(ctx: NCContext, im: NCIntentMatch, @NCIntentTerm("t1") 
t1: NCEntity): NCResult = NCResult("Data", NCResultType.ASK_RESULT)
 
         Using.resource(new NCModelClient(mdl)) { client =>
-            def ask(): NCCallbackData = client.debugAsk("e1", null, "userId", 
true)
+            def ask(): NCCallbackData = client.debugAsk("e1", "userId", true)
             def execCallback(cb: NCCallbackData): NCResult = 
cb.getCallback.apply(cb.getCallbackArguments)
             def execCallbackOk(cb: NCCallbackData): Unit = println(s"Result: 
${execCallback(cb).body}")
             def execCallbackFail(cb: NCCallbackData): Unit =
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala
index 66553c86..ba5115a3 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala
@@ -71,7 +71,7 @@ class NCModelPingPongSpec:
     def tearDown(): Unit = client.close()
 
     private def ask(txt: String, typ: NCResultType): Unit =
-        val res = client.ask(txt, null, "userId")
+        val res = client.ask(txt, "userId")
         println(s"Request [text=$txt, result=$res]")
         require(res.resultType == typ)
 
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala
index 018ed8f1..28727124 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala
@@ -72,5 +72,5 @@ class NCEntityMapperSpec:
 
     @Test
     def test(): Unit = Using.resource(new NCModelClient(mdl)) { client =>
-        require(client.ask("a b c d", null, "userId").intentId == "abcd")
+        require(client.ask("a b c d", "userId").intentId == "abcd")
     }
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestUtils.scala 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestUtils.scala
index 95339821..7e4da7e9 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestUtils.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestUtils.scala
@@ -110,7 +110,7 @@ object NCTestUtils:
       */
     def askSomething(mdl: NCModel, expectedOk: Boolean): Unit =
         Using.resource(new NCModelClient(mdl)) { client =>
-            def ask(): NCResult = client.ask("test", null, "userId")
+            def ask(): NCResult = client.ask("test", "userId")
 
             if expectedOk then
                 println(ask().body)

Reply via email to