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 a9cdee05 API refactored.
a9cdee05 is described below

commit a9cdee05ed575a2185308ac6ab5cac0c31dc9be6
Author: Sergey Kamov <[email protected]>
AuthorDate: Tue Jul 5 15:24:28 2022 +0300

    API refactored.
---
 .../main/scala/org/apache/nlpcraft/NCModel.scala   |  8 +--
 .../main/scala/org/apache/nlpcraft/NCResult.scala  |  1 -
 .../intent/matcher/NCIntentSolverManager.scala     | 74 +++++++++++-----------
 .../internal/impl/NCModelCallbacksSpec.scala       |  8 +--
 .../nlpcraft/nlp/NCEntityValidatorSpec.scala       |  2 +-
 .../apache/nlpcraft/nlp/NCTokenValidatorSpec.scala |  2 +-
 .../org/apache/nlpcraft/nlp/util/NCTestToken.scala |  4 +-
 7 files changed, 49 insertions(+), 50 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModel.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModel.scala
index c663e891..a0244fe5 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModel.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModel.scala
@@ -105,7 +105,7 @@ trait NCModel:
       * returns a non-{@code null} result, it will be returned to the caller 
immediately overriding default behavior.
       * If the method returns {@code null} - the default processing flow will 
continue.
       * @throws NCRejection This callback can throw the rejection exception to 
abort input query processing. */
-    @throws[NCRejection] def onContext(ctx: NCContext): NCResult = null
+    @throws[NCRejection] def onContext(ctx: NCContext): Option[NCResult] = None
 
     /**
       * A callback that is called when intent was successfully matched but 
right before its callback is called. This
@@ -150,7 +150,7 @@ trait NCModel:
       * method returns a non-{@code null} result, it will be returned to the 
caller immediately overriding
       * default behavior and existing query result or error processing, if 
any. If the method returns {@code null} -
       * the default processing flow will continue. */
-    def onResult(ctx: NCContext, im: NCIntentMatch, res: NCResult): NCResult = 
null
+    def onResult(ctx: NCContext, im: NCIntentMatch, res: NCResult): 
Option[NCResult] = None
 
     /**
       * A callback that is called when intent callback threw NCRejection 
exception. This callback is called
@@ -168,7 +168,7 @@ trait NCModel:
       * returns a non-{@code null} result, it will be returned to the caller 
immediately overriding default behavior
       * and existing query result or error processing, if any. If the method 
returns {@code null} - the default
       * processing flow will continue. */
-    def onRejection(ctx: NCContext, im: NCIntentMatch, e: NCRejection): 
NCResult = null
+    def onRejection(ctx: NCContext, im: Option[NCIntentMatch], e: 
NCRejection): Option[NCResult] = None
 
     /**
       * A callback that is called when intent callback failed with unexpected 
exception. Note that this callback may
@@ -184,4 +184,4 @@ trait NCModel:
       * returns a non-{@code null} result, it will be returned to the caller 
immediately overriding default
       * behavior and existing query result or error processing, if any. If the 
method returns {@code null} - the
       * default processing flow will continue. */
-    def onError(ctx: NCContext, e: Throwable): NCResult = null
+    def onError(ctx: NCContext, e: Throwable): Option[NCResult] = None
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCResult.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCResult.scala
index d10fbe8f..90618e70 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCResult.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCResult.scala
@@ -26,7 +26,6 @@ object NCResult:
     def apply(body: Any, resultType: NCResultType, intentId: String): NCResult 
= new NCResult(body = body, resultType = resultType, Some(intentId))
     def apply(body: Any, resultType: NCResultType): NCResult = apply(body, 
resultType, intentId = None)
 
-
 /**
   *
   * @param body
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/matcher/NCIntentSolverManager.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/matcher/NCIntentSolverManager.scala
index d8b5208f..ed191ed1 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/matcher/NCIntentSolverManager.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/matcher/NCIntentSolverManager.scala
@@ -772,50 +772,50 @@ class NCIntentSolverManager(
         val key = UserModelKey(ctx.getRequest.getUserId, mdl.getConfig.id)
         reqIds.synchronized { reqIds.put(key, ctx.getRequest.getRequestId)}
 
-        val mdlCtxRes = mdl.onContext(ctx)
+        mdl.onContext(ctx) match
+            case Some(mdlCtxRes) =>
+                if typ != REGULAR then E("'onContext()' method is overridden, 
intents cannot be found.")
+                if intents.nonEmpty then logger.warn("'onContext()' method 
overrides existing intents - they are ignored.")
 
-        if mdlCtxRes != null then
-            if typ != REGULAR then E("'onContext()' method is overridden, 
intents cannot be found.")
-            if intents.nonEmpty then logger.warn("'onContext()' method 
overrides existing intents - they are ignored.")
+                Left(mdlCtxRes)
+            case None =>
+                if intents.isEmpty then
+                    throw NCRejection("There are no registered intents and 
model's 'onContext()' method returns 'null' result.")
 
-            Left(mdlCtxRes)
-        else
-            if intents.isEmpty then
-                throw NCRejection("There are no registered intents and model's 
'onContext()' method returns 'null' result.")
-
-            var loopRes: IterationResult = null
+                var loopRes: IterationResult = null
 
-            try
-                while (loopRes == null)
-                    solveIteration(mdl, ctx, typ, key) match
-                        case Some(iterRes) => loopRes = iterRes
-                        case None => // No-op.
-
-                typ match
-                    case REGULAR =>
-                        mdl.onResult(ctx, loopRes.intentMatch, 
loopRes.result.swap.toOption.get) match
-                            case null => loopRes.result
-                            case mdlRes => Left(mdlRes)
-                    case _ => loopRes.result
-            catch
-                case e: NCRejection =>
-                    typ match
-                        case REGULAR =>
-                            mdl.onRejection(ctx, if loopRes != null then 
loopRes.intentMatch else null, e) match
-                                case null => throw e
-                                case mdlRejRes => Left(mdlRejRes)
-                        case _ => throw e
+                try
+                    while (loopRes == null)
+                        solveIteration(mdl, ctx, typ, key) match
+                            case Some(iterRes) => loopRes = iterRes
+                            case None => // No-op.
 
-                case e: Throwable =>
                     typ match
                         case REGULAR =>
-                            mdl.onError(ctx, e) match
-                                case null => throw e
-                                case mdlErrRes =>
-                                    logger.warn("Error during execution.", e)
-                                    Left(mdlErrRes)
-                        case _ => throw e
+                            mdl.onResult(ctx, loopRes.intentMatch, 
loopRes.result.swap.toOption.get) match
+                                case Some(mdlRes) => Left(mdlRes)
+                                case None => loopRes.result
 
+                        case _ => loopRes.result
+                catch
+                    case e: NCRejection =>
+                        typ match
+                            case REGULAR =>
+                                mdl.onRejection(ctx, Option.when(loopRes != 
null)(loopRes.intentMatch), e) match
+                                    case Some(mdlRejRes) => Left(mdlRejRes)
+                                    case None => throw e
+
+                            case _ => throw e
+
+                    case e: Throwable =>
+                        typ match
+                            case REGULAR =>
+                                mdl.onError(ctx, e) match
+                                    case Some(mdlErrRes) =>
+                                        logger.warn("Error during execution.", 
e)
+                                        Left(mdlErrRes)
+                                    case None => throw e
+                            case _ => throw e
     /**
       *
       */
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 b6eb9037..02f07d66 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
@@ -58,10 +58,10 @@ class NCModelCallbacksSpec:
 
             override def onMatchedIntent(ctx: NCContext, im: NCIntentMatch): 
Boolean = getOrElse(MatchFalse, false, true)
             override def onVariant(vrn: NCVariant): Boolean = 
getOrElse(VariantFalse, false, true)
-            override def onContext(ctx: NCContext): NCResult = 
getOrElse(ContextNotNull, RESULT_CONTEXT, null)
-            override def onResult(ctx: NCContext, im: NCIntentMatch, res: 
NCResult): NCResult = getOrElse(ResultNotNull, RESULT_RESULT, null)
-            override def onRejection(ctx: NCContext, im: NCIntentMatch, e: 
NCRejection): NCResult = getOrElse(RejectionNotNull, RESULT_REJECTION, null)
-            override def onError(ctx: NCContext, e: Throwable): NCResult = 
getOrElse(ErrorNotNull, RESULT_ERROR, null)
+            override def onContext(ctx: NCContext): Option[NCResult] = 
getOrElse(ContextNotNull, Some(RESULT_CONTEXT), None)
+            override def onResult(ctx: NCContext, im: NCIntentMatch, res: 
NCResult): Option[NCResult] = getOrElse(ResultNotNull, Some(RESULT_RESULT), 
None)
+            override def onRejection(ctx: NCContext, im: 
Option[NCIntentMatch], e: NCRejection): Option[NCResult] = 
getOrElse(RejectionNotNull, Some(RESULT_REJECTION), None)
+            override def onError(ctx: NCContext, e: Throwable): 
Option[NCResult] = getOrElse(ErrorNotNull, Some(RESULT_ERROR), None)
 
     MDL.pipeline.entParsers += 
NCTestUtils.mkEnSemanticParser(List(NCSemanticTestElement("x")))
 
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityValidatorSpec.scala 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityValidatorSpec.scala
index 376b09fa..a09cb0b6 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityValidatorSpec.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityValidatorSpec.scala
@@ -32,7 +32,7 @@ import scala.util.Using
 class NCEntityValidatorSpec:
     private def test0(pipeline: NCPipeline, ok: Boolean): Unit =
         val mdl: NCModel = new NCModelAdapter(new NCModelConfig("test.id", 
"Test model", "1.0"), pipeline):
-            override def onContext(ctx: NCContext): NCResult = NCResult("OK", 
NCResultType.ASK_RESULT)
+            override def onContext(ctx: NCContext): Option[NCResult] = 
Option(NCResult("OK", NCResultType.ASK_RESULT))
 
         NCTestUtils.askSomething(mdl, ok)
 
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCTokenValidatorSpec.scala 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCTokenValidatorSpec.scala
index 3817f6fb..1b9fb256 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCTokenValidatorSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCTokenValidatorSpec.scala
@@ -32,7 +32,7 @@ import scala.util.Using
 class NCTokenValidatorSpec:
     private def test0(pipeline: NCPipeline, ok: Boolean): Unit =
         val mdl: NCModel = new NCModelAdapter(new NCModelConfig("test.id", 
"Test model", "1.0"), pipeline):
-            override def onContext(ctx: NCContext): NCResult = NCResult("OK", 
NCResultType.ASK_RESULT)
+            override def onContext(ctx: NCContext): Option[NCResult] = 
Option(NCResult("OK", NCResultType.ASK_RESULT))
 
         NCTestUtils.askSomething(mdl, ok)
 
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestToken.scala 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestToken.scala
index 6a0cbe5b..e4d00cef 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestToken.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestToken.scala
@@ -39,9 +39,9 @@ case class NCTestToken(
     end: Int = -1,
     lemma: String = null,
     pos: String = null,
-    data: Map[String, AnyRef] = null
+    data: Map[String, AnyRef] = Map.empty
 ) extends NCPropertyMapAdapter with NCToken:
-    if data != null then data.foreach { (k, v) => put(k, v)}
+    data.foreach { (k, v) => put(k, v)}
 
     override def getText: String = txt
     override def getIndex: Int = idx

Reply via email to