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

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


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

commit 5a04f73a7b3e94e643857f90526f4693c8e7b930
Author: Sergey Kamov <[email protected]>
AuthorDate: Fri Jan 28 23:20:31 2022 +0300

    WIP.
---
 .../nlpcraft/internal/impl/NCModelScanner.scala    | 38 +++++++++++++---------
 .../impl/scan/NCModelIntentsNestedSpec.scala       | 35 +++++++++++++++++---
 .../internal/impl/scan/NCTestModelJava.java        |  3 +-
 .../internal/impl/scan/NCTestModelScala.scala      |  6 ++--
 .../intent/compiler/NCIDLCompilerSpec.scala        |  2 +-
 .../nlpcraft/internal/intent/compiler/test_ok.idl  |  8 ++---
 6 files changed, 60 insertions(+), 32 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/NCModelScanner.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/NCModelScanner.scala
index c17612c..62b6b16 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/NCModelScanner.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/NCModelScanner.scala
@@ -246,8 +246,6 @@ object NCModelScanner extends LazyLogging:
 
         res
 
-
-
     /**
       *
       * @param mtd
@@ -425,10 +423,7 @@ class NCModelScanner(mdl: NCModel) extends LazyLogging:
         val anns = mtd.getAnnotationsByType(CLS_INTENT)
 
         if anns.nonEmpty then
-            lazy val mtdStr = method2Str(mtd)
-
-            if anns.exists(a => a == null || a.value().strip().isEmpty) then
-                E(s"Unexpected empty annotation definition @NCIntentRef in 
$mtdStr") // TODO: text
+            val mtdStr = method2Str(mtd)
 
             for (ann <- anns; intent <- NCIDLCompiler.compile(ann.value, cfg, 
mtdStr))
                 if intentDecls.exists(_.id == intent.id && 
existsForOtherMethod(intent.id)) then
@@ -444,17 +439,12 @@ class NCModelScanner(mdl: NCModel) extends LazyLogging:
     private def processMethodRefs(mtd: Method, obj: Object): Unit =
         val anns = mtd.getAnnotationsByType(CLS_INTENT_REF)
 
-        if anns.nonEmpty then
-            lazy val mtdStr = method2Str(mtd)
-            if anns.exists(a => a == null || a.value().strip().isEmpty) then
-                E(s"Unexpected empty annotation definition @NCIntent in 
$mtdStr") // TODO: text
-
-            for (ann <- anns)
-                val refId = ann.value.trim
+        for (ann <- anns)
+            val refId = ann.value.strip
 
-                intentDecls.find(_.id == refId) match
-                    case Some(intent) => bindIntent(intent, 
prepareCallback(mtd, obj, intent), mtd)
-                    case None => E(s"@NCIntentRef(\"$refId\") references 
unknown intent ID [mdlId=$mdlId, origin=$origin, refId=$refId, 
callback=$mtdStr]")
+            intentDecls.find(_.id == refId) match
+                case Some(intent) => bindIntent(intent, prepareCallback(mtd, 
obj, intent), mtd)
+                case None => E(s"@NCIntentRef(\"$refId\") references unknown 
intent ID [mdlId=$mdlId, origin=$origin, refId=$refId, 
callback=${method2Str(mtd)}]")
 
     /**
       *
@@ -595,12 +585,28 @@ class NCModelScanner(mdl: NCModel) extends LazyLogging:
       * @return
       */
     def scan(): Seq[NCModelIntent] =
+        def processClassHierarchy(claxx: Class[_]): Unit =
+            if claxx != null then
+                val anns = claxx.getAnnotationsByType(CLS_INTENT)
+
+                if anns.nonEmpty then
+                    val origin = getClassName(claxx)
+
+                    for (ann <- anns; intent <- 
NCIDLCompiler.compile(ann.value, cfg, origin))
+                        // TODO: duplicate
+                        intentDecls += intent
+
+                processClassHierarchy(claxx.getSuperclass)
+                claxx.getInterfaces.foreach(processClassHierarchy)
+
         // 1. First phase scan.
         //  - For given object finds references via fields (NCIntentObject). 
Scans also each reference recursively and collects them.
         //  - For all methods of processed object collects samples 
(NCIntentSample, NCIntentSampleRef) and intents (NCIntent)
         def scan(obj: Object): Unit =
             objs += obj
 
+            processClassHierarchy(obj.getClass)
+
             for (m <- getAllMethods(obj)) processMethod(m, obj)
             for (f <- getAllFields(obj) if 
f.isAnnotationPresent(CLS_INTENT_OBJ)) scan(getFieldObject(mdlId, f, obj))
 
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCModelIntentsNestedSpec.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCModelIntentsNestedSpec.scala
index 517e99b..c8e2a4c 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCModelIntentsNestedSpec.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCModelIntentsNestedSpec.scala
@@ -27,12 +27,11 @@ import org.junit.jupiter.api.Test
   * It tests imports and nested objects usage.
   */
 class NCModelIntentsNestedSpec:
-    private val MDL_VALID: NCModel = new NCTestModelAdapter:
+    private val MDL_VALID1: NCModel = new NCTestModelAdapter:
         @NCIntentObject
         val nested1: Object = new Object():
             @NCIntentObject
             val nested2: Object = new Object():
-                @NCIntent("import('scan/idl.idl')")
                 @NCIntent("intent=intent3 term(x)~{true}")
                 def intent1(@NCIntentTerm("x") x: NCEntity) = new NCResult()
 
@@ -42,8 +41,32 @@ class NCModelIntentsNestedSpec:
         @NCIntent("intent=intent1 term(x)~{true}")
         def intent1(@NCIntentTerm("x") x: NCEntity) = new NCResult()
 
-        // Imported in `nested2` (scan/idl.idl)
-        @NCIntentRef("impIntId")
+        @NCIntent("import('scan/idl.idl')")
+        def intent4(
+            @NCIntentTerm("single") single: NCEntity,
+            @NCIntentTerm("list") list: List[NCEntity],
+            @NCIntentTerm("opt") opt: Option[NCEntity]
+        ): NCResult = new NCResult()
+
+    private val MDL_VALID2: NCModel = new NCTestModelAdapter:
+        @NCIntent("import('scan/idl.idl')")
+        class RefClass:
+            ()
+
+        @NCIntentObject
+        val nested1: Object = new Object():
+            @NCIntentObject
+            val nested2 = new RefClass():
+                @NCIntent("intent=intent3 term(x)~{true}")
+                def intent1(@NCIntentTerm("x") x: NCEntity) = new NCResult()
+
+            @NCIntent("intent=intent2 term(x)~{true}")
+            def intent1(@NCIntentTerm("x") x: NCEntity) = new NCResult()
+
+        @NCIntent("intent=intent1 term(x)~{true}")
+        def intent1(@NCIntentTerm("x") x: NCEntity) = new NCResult()
+
+        @NCIntentRef("impIntId") // Reference via nested2 (RefClass)
         def intent4(
             @NCIntentTerm("single") single: NCEntity,
             @NCIntentTerm("list") list: List[NCEntity],
@@ -57,7 +80,9 @@ class NCModelIntentsNestedSpec:
             val nested2: Object = null
 
     @Test
-    def test(): Unit = require(new NCModelScanner(MDL_VALID).scan().sizeIs == 
4)
+    def test(): Unit =
+        require(new NCModelScanner(MDL_VALID1).scan().sizeIs == 4)
+        require(new NCModelScanner(MDL_VALID2).scan().sizeIs == 4)
 
     @Test
     def testNull(): Unit =
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCTestModelJava.java
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCTestModelJava.java
index 046e7bc..96560a6 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCTestModelJava.java
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCTestModelJava.java
@@ -42,7 +42,6 @@ public class NCTestModelJava {
     public static NCModel mkModel() {
         return
             new NCModelAdapter(NCTestConfigJava.CFG, 
NCTestConfigJava.EN_PIPELINE) {
-                @NCIntent("import('scan/idl.idl')")
                 @NCIntent(
                     "intent=locInt term(single)~{# == 'id1'} term(list)~{# == 
'id2'}[0,10] term(opt)~{# == 'id3'}?"
                 )
@@ -55,7 +54,7 @@ public class NCTestModelJava {
                     return new NCResult();
                 }
 
-                @NCIntentRef("impIntId")
+                @NCIntent("import('scan/idl.idl')")
                 @NCIntentSampleRef("scan/samples.txt")
                 NCResult intentImport(
                     @NCIntentTerm("single") NCEntity single,
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCTestModelScala.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCTestModelScala.scala
index 5ec7a2b..8adaa10 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCTestModelScala.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/scan/NCTestModelScala.scala
@@ -25,7 +25,6 @@ import org.apache.nlpcraft.nlp.util.opennlp.*
   *
   */
 object NCTestModelScala:
-    @NCIntent("import('scan/idl.idl')")
     object NCTestModelScalaObj extends NCTestModelAdapter :
         @NCIntent("intent=locInt term(single)~{# == 'id1'} term(list)~{# == 
'id2'}[0,10] term(opt)~{# == 'id3'}?")
         @NCIntentSample(Array("What are the least performing categories for 
the last quarter?"))
@@ -35,7 +34,7 @@ object NCTestModelScala:
             @NCIntentTerm("opt") opt: Option[NCEntity]
         ): NCResult = new NCResult()
 
-        @NCIntentRef("impIntId")
+        @NCIntent("import('scan/idl.idl')")
         @NCIntentSampleRef("scan/samples.txt")
         def intentImport(
             @NCIntentTerm("single") single: NCEntity,
@@ -66,7 +65,6 @@ object NCTestModelScala:
       * @return
       */
     def mkModel: NCModel = new NCTestModelAdapter() :
-        @NCIntent("import('scan/idl.idl')")
         @NCIntent("intent=locInt term(single)~{# == 'id1'} term(list)~{# == 
'id2'}[0,10] term(opt)~{# == 'id3'}?")
         @NCIntentSample(Array("What are the least performing categories for 
the last quarter?"))
         def intent(
@@ -75,7 +73,7 @@ object NCTestModelScala:
             @NCIntentTerm("opt") opt: Option[NCEntity]
         ): NCResult = new NCResult()
 
-        @NCIntentRef("impIntId")
+        @NCIntent("import('scan/idl.idl')")
         @NCIntentSampleRef("scan/samples.txt")
         def intentImport(
             @NCIntentTerm("single") single: NCEntity,
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLCompilerSpec.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLCompilerSpec.scala
index 869d51a..4471ef4 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLCompilerSpec.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLCompilerSpec.scala
@@ -57,7 +57,7 @@ class NCIDLCompilerSpec:
     
         checkCompileOk(
             """
-              
|import('org/apache/nlpcraft/model/intent/idl/compiler/test_ok.idl')
+              
|import('org/apache/nlpcraft/internal/intent/compiler/test_ok.idl')
               |""".stripMargin
         )
         checkCompileOk(
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/test_ok.idl
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/test_ok.idl
index a033a88..41afdb3 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/test_ok.idl
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/test_ok.idl
@@ -35,9 +35,9 @@ intent=i1
 intent=i2
     flow="a[^0-9]b"
     meta={'a': 42, 'b': {'Москва': [1, 2, 3]}}
-    term(t1)={2 == 2 && !# != -25 && meta_model('a') == 42}
+    term(t1)={2 == 2 && !# != -25 && meta_req('a') == 42}
     term(t2)={
-        @a = meta_model('a')
+        @a = meta_req('a')
         @list = list(1, 2, 3, 4)
 
         @a == 42 && has_all(@list, list(3, 2))
@@ -58,9 +58,9 @@ intent=i3
 intent=i5
     flow="a[^0-9]b"
     meta={'a': 42, 'b': {'Москва': [1, 2, 3]}}
-    term(t1)={month >= 6 && !(#) != -25 && meta_model('a') == 42}
+    term(t1)={month >= 6 && !(#) != -25 && meta_req('a') == 42}
     term(t2)={
-        @a = meta_model('a')
+        @a = meta_req('a')
         @list = list(1, 2, 3, 4)
 
         @a == 42 && has_all(@list, list(3, 2))

Reply via email to