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

aradzinski 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 3cfc23c  WIP
3cfc23c is described below

commit 3cfc23c7084ae79de5ab1adcdeed7e34c700b2f9
Author: Aaron Radzinski <[email protected]>
AuthorDate: Fri Jan 28 11:14:25 2022 -0800

    WIP
---
 .../internal/intent/compiler/NCIDLCompiler.scala   | 38 ++++++++++++++++++++--
 ...NCIDLCompilerGlobal.scala => NCIDLGlobal.scala} |  2 +-
 .../intent/compiler/NCIDLCompilerSpec.scala        |  4 +--
 .../intent/compiler/functions/NCIDLFunctions.scala |  2 +-
 4 files changed, 39 insertions(+), 7 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLCompiler.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLCompiler.scala
index e4e6c2c..f7671ec 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLCompiler.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLCompiler.scala
@@ -181,12 +181,12 @@ object NCIDLCompiler extends LazyLogging:
 
         override def exitFragId(ctx: IDP.FragIdContext): Unit =
             fragId = ctx.id().getText
-            if NCIDLCompilerGlobal.getFragment(mdlCfg.getId, fragId).isDefined 
then SE(s"Duplicate fragment ID: $fragId")(ctx.id())
+            if NCIDLGlobal.getFragment(mdlCfg.getId, fragId).isDefined then 
SE(s"Duplicate fragment ID: $fragId")(ctx.id())
 
         override def exitFragRef(ctx: IDP.FragRefContext): Unit =
             val id = ctx.id().getText
 
-            NCIDLCompilerGlobal.getFragment(mdlCfg.getId, id) match
+            NCIDLGlobal.getFragment(mdlCfg.getId, id) match
                 case Some(frag) =>
                     val meta = if fragMeta == null then Map.empty[String, Any] 
else fragMeta
                     for (fragTerm <- frag.terms)
@@ -256,7 +256,7 @@ object NCIDLCompiler extends LazyLogging:
             }
 
         override def exitFrag(ctx: IDP.FragContext): Unit =
-            NCIDLCompilerGlobal.addFragment(mdlCfg.getId, 
NCIDLFragment(fragId, terms.toList))
+            NCIDLGlobal.addFragment(mdlCfg.getId, NCIDLFragment(fragId, 
terms.toList))
             terms.clear()
             fragId = null
 
@@ -287,6 +287,38 @@ object NCIDLCompiler extends LazyLogging:
             intentOpts = new NCIDLIntentOptions()
             terms.clear()
 
+        override def exitImprt(ctx: IDP.ImprtContext): Unit =
+                val x = NCUtils.trimQuotes(ctx.qstring().getText)
+
+                if NCIDLGlobal.hasImport(x) then logger.warn(s"Ignoring 
already processed IDL import '$x' in: $origin")
+                else
+                    NCIDLGlobal.addImport(x)
+
+                    var imports: Set[NCIDLIntent] = null
+                    val file = new File(x)
+
+                    // First, try absolute path.
+                    if file.exists() then
+                        val idl = NCUtils.readFile(file).mkString("\n")
+                        imports = NCIDLCompiler.compile(idl, mdlCfg, x)
+
+                    // Second, try as a classloader resource.
+                    if imports == null then
+                        val in = 
mdlCfg.getClass.getClassLoader.getResourceAsStream(x)
+                        if (in != null)
+                            val idl = NCUtils.readStream(in).mkString("\n")
+                            imports = NCIDLCompiler.compile(idl, mdlCfg, x)
+
+                    // Finally, try as URL resource.
+                    if imports == null then
+                        try
+                            val idl = NCUtils.readStream(new 
URL(x).openStream()).mkString("\n")
+                            imports = NCIDLCompiler.compile(idl, mdlCfg, x )
+                        catch case _: Exception => throw 
newRuntimeError(s"Invalid or unknown import location: $x")(ctx.qstring())
+
+                    require(imports != null)
+                    imports.foreach(addIntent(_)(ctx.qstring()))
+
         override def syntaxError(errMsg: String, srcName: String, line: Int, 
pos: Int): NCException =
             throw new NCException(mkSyntaxError(errMsg, srcName, line, pos, 
idl, origin, mdlCfg))
 
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLCompilerGlobal.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLGlobal.scala
similarity index 98%
rename from 
nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLCompilerGlobal.scala
rename to 
nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLGlobal.scala
index 2191172..125dfb4 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLCompilerGlobal.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLGlobal.scala
@@ -23,7 +23,7 @@ import scala.collection.mutable
 /**
   * Global IDL compiler state.
   */
-object NCIDLCompilerGlobal:
+object NCIDLGlobal:
     private final val fragCache = TrieMap.empty[String /* Model ID. */ , 
mutable.Map[String, NCIDLFragment]]
     private final val importCache = mutable.HashSet.empty[String]
 
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 0d16593..869d51a 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
@@ -53,7 +53,7 @@ class NCIDLCompilerSpec:
     @Test
     @throws[NCException]
     def testInlineCompileOk(): Unit =
-        NCIDLCompilerGlobal.clearCache(MODEL_ID)
+        NCIDLGlobal.clearCache(MODEL_ID)
     
         checkCompileOk(
             """
@@ -120,7 +120,7 @@ class NCIDLCompilerSpec:
     @Test
     @throws[NCException]
     def testInlineCompileFail(): Unit =
-        NCIDLCompilerGlobal.clearCache(MODEL_ID)
+        NCIDLGlobal.clearCache(MODEL_ID)
 
         checkCompileError(
             """
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/functions/NCIDLFunctions.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/functions/NCIDLFunctions.scala
index 2df2308..4cfe8eb 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/functions/NCIDLFunctions.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/functions/NCIDLFunctions.scala
@@ -166,7 +166,7 @@ import 
org.apache.nlpcraft.internal.intent.compiler.functions.NCIDLFunctions.*
   */
 private[functions] trait NCIDLFunctions:
     @BeforeEach
-    def before(): Unit = NCIDLCompilerGlobal.clearCache(MODEL_ID)
+    def before(): Unit = NCIDLGlobal.clearCache(MODEL_ID)
 
     /**
       *

Reply via email to