You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by se...@apache.org on 2022/08/24 19:39:14 UTC

[incubator-nlpcraft] 01/02: IDL fragments related fixes.

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

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

commit 2cb43ff69e4508c63ea086ff373b198506caa8e6
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Wed Aug 24 12:17:19 2022 +0300

    IDL fragments related fixes.
---
 .../examples/pizzeria/PizzeriaModelSpec.scala      |  0
 .../pizzeria/cli/PizzeriaModelClientCli.scala      |  0
 .../pizzeria/cli/PizzeriaModelServer.scala         |  0
 .../nlpcraft/internal/impl/NCModelScanner.scala    | 37 ++++++++++++++++------
 4 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala b/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala
similarity index 100%
rename from nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala
rename to nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala
diff --git a/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelClientCli.scala b/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelClientCli.scala
similarity index 100%
rename from nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelClientCli.scala
rename to nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelClientCli.scala
diff --git a/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelServer.scala b/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelServer.scala
similarity index 100%
rename from nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelServer.scala
rename to nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelServer.scala
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 5312b372..563045e2 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
@@ -429,10 +429,31 @@ object NCModelScanner extends LazyLogging:
             if intentsMtds.contains(mtd) then E(s"The callback cannot have more one intent [$z, callback=${method2Str(mtd)}]")
             intentsMtds += mtd -> IntentHolder(cfg, intent, obj, mtd)
 
+        /**
+          *  It is done such way because intents can contain references to 'fragments',
+          *  but annotations can be received via java reflection in inordered way.
+          */
+        def addIntent2Phases(anns: scala.Array[NCIntent], origin: String): Iterable[NCIDLIntent] =
+            val errAnns = mutable.ArrayBuffer.empty[NCIntent]
+            val intents = mutable.ArrayBuffer.empty[NCIDLIntent]
+
+            def addIntents(ann: NCIntent) = intents ++= NCIDLCompiler.compile(ann.value, cfg, origin)
+
+            // 1. First pass.
+            for (ann <- anns) try addIntents(ann)
+            catch case _: NCException => errAnns += ann
+
+            // 2. Second pass.
+            for (ann <- errAnns) addIntents(ann)
+
+            // Process all compiled intents.
+            for (intent <- intents) addDecl(intent)
+
+            intents
+
         def processClassAnnotations(cls: Class[_]): Unit =
             if cls != null && processed.add(cls) then
-                for (ann <- cls.getAnnotationsByType(CLS_INTENT); intent <- NCIDLCompiler.compile(ann.value, cfg, class2Str(cls)))
-                    addDecl(intent)
+                addIntent2Phases(cls.getAnnotationsByType(CLS_INTENT), class2Str(cls))
 
                 processClassAnnotations(cls.getSuperclass)
                 cls.getInterfaces.foreach(processClassAnnotations)
@@ -446,13 +467,11 @@ object NCModelScanner extends LazyLogging:
             val methods = getAllMethods(obj)
 
             // // Collects intents for each method.
-            for (
-                mtd <- methods;
-                ann <- mtd.getAnnotationsByType(CLS_INTENT);
-                intent <- NCIDLCompiler.compile(ann.value, cfg, method2Str(mtd))
-            )
-                addDecl(intent)
-                addIntent(intent, mtd, obj)
+            for (mtd <- methods)
+                val anns = mtd.getAnnotationsByType(CLS_INTENT)
+                val intents = addIntent2Phases(anns, method2Str(mtd))
+
+                for (intent <- intents) addIntent(intent, mtd, obj)
 
             // Scans annotated fields.
             for (f <- getAllFields(obj) if f.isAnnotationPresent(CLS_INTENT_OBJ)) scan(getFieldObject(cfg, f, obj))