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

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


The following commit(s) were added to refs/heads/NLPCRAFT-283 by this push:
     new b2ca415  WIP.
b2ca415 is described below

commit b2ca4156c823fdc9949e80bca470ce71370aaae5
Author: Sergey Kamov <[email protected]>
AuthorDate: Fri Mar 26 13:43:27 2021 +0300

    WIP.
---
 .../org/apache/nlpcraft/common/util/NCUtils.scala  |  22 ++--
 .../model/intent/solver/NCIntentSolverEngine.scala |  15 ++-
 .../nlpcraft/model/dialog/NCDialogSpec.scala       |   2 +-
 .../nlpcraft/model/dialog/NCDialogSpec2.scala      | 114 +++++++++++++++++----
 .../idl/compiler/functions/NCIdlFunctions.scala    |  11 +-
 .../compiler/functions/NCIdlFunctionsCustom.scala  |   2 +-
 6 files changed, 126 insertions(+), 40 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
index f9a4eae..bfdc3c2 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
@@ -1952,9 +1952,9 @@ object NCUtils extends LazyLogging {
       * @return
       */
     @throws[NCE]
-    def callMethod[T: ClassTag, R: ClassTag](objFac: () ⇒ Object, mtdName: 
String, arg: T): R =
+    def callMethod[T: ClassTag, R: ClassTag](objFac: () ⇒ Any, mtdName: 
String, arg: T): R =
         try {
-            val obj = objFac()
+            val obj: Any = objFac()
 
             val argClazz = classTag[T].runtimeClass
             val retClazz = classTag[R].runtimeClass
@@ -1962,14 +1962,8 @@ object NCUtils extends LazyLogging {
             def mkErrors = s"[name=$mtdName, argType=$argClazz, 
retType=$retClazz]"
 
             val mtd =
-                try {
-                    val mtd = obj.getClass.getMethod(mtdName, argClazz)
-
-                    if (mtd.getAnnotatedReturnType.getType != retClazz)
-                        throw new NCE(s"Expected method not found $mkErrors")
-
-                    mtd
-                }
+                try
+                    obj.getClass.getMethod(mtdName, argClazz)
                 catch {
                     case e: NoSuchMethodException ⇒ throw new NCE(s"Expected 
method not found $mkErrors", e)
                 }
@@ -1987,7 +1981,7 @@ object NCUtils extends LazyLogging {
 
                 val res =
                     try
-                        mtd.invoke(obj, 
arg.asInstanceOf[Object]).asInstanceOf[R]
+                        mtd.invoke(obj, arg.asInstanceOf[Object])
                     catch {
                         case e: Throwable ⇒ throw new NCE(s"Method execution 
error $mkErrors", e)
                     }
@@ -1995,7 +1989,11 @@ object NCUtils extends LazyLogging {
                 if (res == null)
                     throw new NCE(s"Unexpected 'null' result for method 
execution $mkErrors")
 
-                res
+                try
+                    res.asInstanceOf[R]
+                catch {
+                    case e: ClassCastException ⇒ throw new NCE(s"Invalid 
method signature (result type) $mkErrors", e)
+                }
             }
             finally {
                 if (flag)
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolverEngine.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolverEngine.scala
index 9cba0d7..23fbb84 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolverEngine.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolverEngine.scala
@@ -18,15 +18,15 @@
 package org.apache.nlpcraft.model.intent.solver
 
 import com.typesafe.scalalogging.LazyLogging
+import org.apache.nlpcraft.common._
 import org.apache.nlpcraft.common.ascii.NCAsciiTable
 import org.apache.nlpcraft.common.debug.{NCLogGroupToken, NCLogHolder}
 import org.apache.nlpcraft.common.opencensus.NCOpenCensusTrace
-import org.apache.nlpcraft.common._
 import org.apache.nlpcraft.model.impl.NCTokenLogger
-import org.apache.nlpcraft.model.{NCContext, NCDialogFlowItem, NCIntentMatch, 
NCResult, NCToken}
-import org.apache.nlpcraft.probe.mgrs.dialogflow.NCDialogFlowManager
 import org.apache.nlpcraft.model.impl.NCTokenPimp._
 import org.apache.nlpcraft.model.intent.{NCIdlContext, NCIdlFunction, 
NCIdlIntent, NCIdlTerm, NCIdlStackItem ⇒ Z}
+import org.apache.nlpcraft.model.{NCContext, NCDialogFlowItem, NCIntentMatch, 
NCResult, NCToken}
+import org.apache.nlpcraft.probe.mgrs.dialogflow.NCDialogFlowManager
 
 import java.util.function.Function
 import scala.collection.JavaConverters._
@@ -455,11 +455,16 @@ object NCIntentSolverEngine extends LazyLogging with 
NCOpenCensusTrace {
             val mtdName = intent.flowMtdName.get
             
             val fqn = s"$clsName.$mtdName(java.util.List[NCDialogFlowItem])"
-            
+
             val res =
                 try
                     U.callMethod[java.util.List[NCDialogFlowItem], 
java.lang.Boolean](
-                        U.mkObject(clsName),
+                        () ⇒ {
+                            // Type must be defined.
+                            val any: Any = U.mkObject(clsName)
+
+                            any
+                        },
                         mtdName,
                         flow.toList.asJava
                     )
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/dialog/NCDialogSpec.scala 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/dialog/NCDialogSpec.scala
index a58cc00..9dd34ca 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/dialog/NCDialogSpec.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/dialog/NCDialogSpec.scala
@@ -28,7 +28,7 @@ import scala.collection.JavaConverters._
 /**
   * Test model.
   */
-class NCDialogSpecModelFlow  {
+class NCDialogSpecModelFlow {
     def trueAlways(flow: java.util.List[NCDialogFlowItem]): Boolean = true
     def falseAlways(flow: java.util.List[NCDialogFlowItem]): Boolean = false
     def trueAfterOnA7(flow: java.util.List[NCDialogFlowItem]): Boolean = 
flow.asScala.exists(_.getIntentId == "onA7")
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/dialog/NCDialogSpec2.scala 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/dialog/NCDialogSpec2.scala
index 76f5345..7e15ce8 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/dialog/NCDialogSpec2.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/dialog/NCDialogSpec2.scala
@@ -23,6 +23,13 @@ import org.junit.jupiter.api.Test
 
 import java.util
 
+object NCDialogSpecModelFlow2  {
+    var error = false
+}
+
+
+import NCDialogSpecModelFlow2._
+
 /**
   * Test model.
   */
@@ -31,43 +38,110 @@ class NCDialogSpecModelFlow2  {
     def invalidDef2(): Boolean = true
     def invalidDef3(flow: java.util.List[NCDialogFlowItem]): Boolean = throw 
new IllegalStateException()
     def invalidDef4(flow: java.util.List[NCDialogFlowItem]): java.lang.Boolean 
= null
-    def validDef(flow: java.util.List[NCDialogFlowItem]): Boolean = true
+    def validDef(flow: java.util.List[NCDialogFlowItem]): Boolean = {
+        if (error)
+            throw new IllegalStateException()
+
+        true
+    }
 }
 
-class NCDialogSpecModel2 extends NCModel {
+/**
+  *
+  */
+class NCDialogSpecModel21 extends NCModel {
     override def getId: String = this.getClass.getSimpleName
     override def getName: String = this.getClass.getSimpleName
     override def getVersion: String = "1.0.0"
 
-    override def getElements: util.Set[NCElement] = Set((for (i ← 1 to 9) 
yield NCTestElement(s"a$i")):_*)
+    override def getElements: util.Set[NCElement] = Set(NCTestElement("a"))
 
-    @NCIntent("intent=onA1 
flow=/org.apache.nlpcraft.model.dialog.NCDialogSpecModelFlow2#invalidDef1/ 
term~{id() == 'a1'}")
-    def onA1(): NCResult = NCResult.text("ok")
+    @NCIntent("intent=onA 
flow=/org.apache.nlpcraft.model.dialog.NCDialogSpecModelFlow2#invalidDef1/ 
term~{tok_id() == 'a'}")
+    def onA(): NCResult = NCResult.text("ok")
+}
 
-    @NCIntent("intent=onA2 
flow=/org.apache.nlpcraft.model.dialog.NCDialogSpecModelFlow2#invalidDef2/ 
term~{id() == 'a2'}")
-    def onA2(): NCResult = NCResult.text("ok")
+/**
+  *
+  */
+@NCTestEnvironment(model = classOf[NCDialogSpecModel21], startClient = true)
+class NCDialogSpec21 extends NCTestContext {
+    @Test
+    def test(): Unit = require(getClient.ask("a").isFailed)
+}
 
-    @NCIntent("intent=onA3 
flow=/org.apache.nlpcraft.model.dialog.NCDialogSpecModelFlow2#invalidDef3/ 
term~{id() == 'a3'}")
-    def onA3(): NCResult = NCResult.text("ok")
+/**
+  *
+  */
+class NCDialogSpecModel22 extends NCDialogSpecModel21 {
+    @NCIntent("intent=onA 
flow=/org.apache.nlpcraft.model.dialog.NCDialogSpecModelFlow2#invalidDef2/ 
term~{tok_id() == 'a'}")
+    override def onA(): NCResult = NCResult.text("ok")
+}
 
-    @NCIntent("intent=onA4 
flow=/org.apache.nlpcraft.model.dialog.NCDialogSpecModelFlow2#invalidDef4/ 
term~{id() == 'a4'}")
-    def onA4(): NCResult = NCResult.text("ok")
+/**
+  *
+  */
+@NCTestEnvironment(model = classOf[NCDialogSpecModel22], startClient = true)
+class NCDialogSpec22 extends NCDialogSpec21
 
-    @NCIntent("intent=onA5 
flow=/org.apache.nlpcraft.model.dialog.NCDialogSpecModelFlow2#validDef/ 
term~{id() == 'a5'}")
-    def onA5(): NCResult = NCResult.text("ok")
+/**
+  *
+  */
+class NCDialogSpecModel23 extends NCDialogSpecModel21 {
+    @NCIntent("intent=onA 
flow=/org.apache.nlpcraft.model.dialog.NCDialogSpecModelFlow2#invalidDef3/ 
term~{tok_id() == 'a'}")
+    override def onA(): NCResult = NCResult.text("ok")
 }
 
 /**
-  * @see NCDialogSpecModel
+  *
   */
-@NCTestEnvironment(model = classOf[NCDialogSpecModel], startClient = true)
-class NCDialogSpec2 extends NCTestContext {
+@NCTestEnvironment(model = classOf[NCDialogSpecModel23], startClient = true)
+class NCDialogSpec23 extends NCDialogSpec21
+
+/**
+  *
+  */
+class NCDialogSpecModel24 extends NCDialogSpecModel21 {
+    @NCIntent("intent=onA 
flow=/org.apache.nlpcraft.model.dialog.NCDialogSpecModelFlow2#invalidDef4/ 
term~{tok_id() == 'a'}")
+    override def onA(): NCResult = NCResult.text("ok")
+}
+
+/**
+  *
+  */
+@NCTestEnvironment(model = classOf[NCDialogSpecModel24], startClient = true)
+class NCDialogSpec24 extends NCDialogSpec21
+
+/**
+  *
+  */
+class NCDialogSpecModel25 extends NCDialogSpecModel21 {
+    @NCIntent("intent=onA 
flow=/org.apache.nlpcraft.model.dialog.NCDialogSpecModelFlow2#validDef/ 
term~{tok_id() == 'a'}")
+    override def onA(): NCResult = NCResult.text("ok")
+}
+
+/**
+  *
+  */
+@NCTestEnvironment(model = classOf[NCDialogSpecModel25], startClient = true)
+class NCDialogSpec25 extends NCTestContext {
     @Test
-    private[dialog] def test(): Unit = {
-        def fail(txts: String*): Unit = txts.foreach(txt ⇒ 
require(getClient.ask(txt).isFailed))
+    def test(): Unit = {
+        NCDialogSpecModelFlow2.error = true
+        require(getClient.ask("a").isFailed)
 
-        fail("a1", "a2", "a3", "a4")
+        NCDialogSpecModelFlow2.error = false
+        require(getClient.ask("a").isOk)
 
-        checkIntent("a5", "onA5")
+        NCDialogSpecModelFlow2.error = true
+        require(getClient.ask("a").isFailed)
     }
 }
+
+
+
+
+
+
+
+
+
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala
index c7d2c80..15d0180 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala
@@ -206,7 +206,16 @@ private[functions] trait NCIdlFunctions {
             require(false)
         }
         catch {
-            case e: Exception ⇒ println(s"Expected error: 
${e.getLocalizedMessage}")
+            case e: Exception ⇒
+                println(s"Expected error: ${e.getLocalizedMessage}")
+
+                var cause = e.getCause
+
+                while (cause != null) {
+                    println(s"  Cause: ${cause.getLocalizedMessage} 
(${cause.getClass.getName})")
+
+                    cause = cause.getCause
+                }
         }
 
     protected implicit def convert(pred: String): TestDesc = TestDesc(truth = 
pred)
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCustom.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCustom.scala
index 84abcbc..ec98903 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCustom.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCustom.scala
@@ -39,7 +39,7 @@ class NCIdlTokensTestWrapper {
   * Tests for 'custom' functions.
   */
 class NCIdlFunctionsCustom extends NCIdlFunctions {
-    private final val C = U.cleanClassName(classOf[NCIdlTokensTestWrapper])
+    private final val C = U.cleanClassName(classOf[NCIdlTokensTestWrapper], 
simpleName = false)
 
     @Test
     def testErrors(): Unit = {

Reply via email to