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

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


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

commit 96d03582837754227e30f4e809e7d0c7ffa56a47
Author: Aaron Radzinski <[email protected]>
AuthorDate: Mon Feb 15 20:12:42 2021 -0800

    WIP.
---
 .../intent/impl/ver2/NCIntentDslCompiler.scala     | 77 +++++++++++++++++++---
 .../model/intent/utils/ver2/NCDslIntent.scala      | 10 +--
 .../model/intent/utils/ver2/NCDslTerm.scala        |  9 +--
 .../model/intent/dsl/NCDslCompilerSpec.scala       |  2 +-
 4 files changed, 71 insertions(+), 27 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala
index 45acce4..b2598a4 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala
@@ -31,8 +31,9 @@ import scala.collection.mutable
 import scala.collection.mutable.ArrayBuffer
 import java.lang.{Double ⇒ JDouble, IllegalArgumentException ⇒ IAE, Long ⇒ 
JLong}
 import java.time.LocalDate
+import java.util.regex.{Pattern, PatternSyntaxException}
 import java.util.{Collections, ArrayList ⇒ JArrayList, HashMap ⇒ JHashMap}
-import scala.language.implicitConversions
+import scala.language.{dynamics, implicitConversions}
 
 object NCIntentDslCompiler extends LazyLogging {
     // Compiler cache.
@@ -63,7 +64,7 @@ object NCIntentDslCompiler extends LazyLogging {
         type Instr = (NCToken, StackType,  NCDslTermContext) ⇒ Unit
 
         // Term's code, i.e. list of instructions.
-        private var termCode = mutable.Buffer.empty[Instr]
+        private var termInstrs = mutable.Buffer.empty[Instr]
 
         private def isJLong(v: AnyRef): Boolean = v.isInstanceOf[JLong]
         private def isJDouble(v: AnyRef): Boolean = v.isInstanceOf[JDouble]
@@ -140,7 +141,7 @@ object NCIntentDslCompiler extends LazyLogging {
         }
 
         override def exitUnaryExpr(ctx: NCIntentDslParser.UnaryExprContext): 
Unit = {
-            termCode += ((_, stack: StackType, _) ⇒ {
+            termInstrs += ((_, stack: StackType, _) ⇒ {
                 require(stack.nonEmpty)
 
                 implicit val s = stack
@@ -167,7 +168,7 @@ object NCIntentDslCompiler extends LazyLogging {
         }
 
         override def exitMultExpr(ctx: NCIntentDslParser.MultExprContext): 
Unit = {
-            termCode += ((_, stack: StackType, _) ⇒ {
+            termInstrs += ((_, stack: StackType, _) ⇒ {
                 require(stack.size >= 2)
 
                 implicit val s = stack
@@ -211,7 +212,7 @@ object NCIntentDslCompiler extends LazyLogging {
         }
 
         override def exitPlusExpr(ctx: NCIntentDslParser.PlusExprContext): 
Unit = {
-            termCode += ((_, stack: StackType, _) ⇒ {
+            termInstrs += ((_, stack: StackType, _) ⇒ {
                 require(stack.size >= 2)
 
                 implicit val s = stack
@@ -251,7 +252,7 @@ object NCIntentDslCompiler extends LazyLogging {
         }
 
         override def exitCompExpr(ctx: NCIntentDslParser.CompExprContext): 
Unit = {
-            termCode += ((_, stack: StackType, _) ⇒ {
+            termInstrs += ((_, stack: StackType, _) ⇒ {
                 implicit val s = stack
 
                 require(stack.size >= 2)
@@ -313,7 +314,7 @@ object NCIntentDslCompiler extends LazyLogging {
         }
 
         override def exitLogExpr(ctx: NCIntentDslParser.LogExprContext): Unit 
= {
-            termCode += ((_, stack: StackType, _) ⇒ {
+            termInstrs += ((_, stack: StackType, _) ⇒ {
                 implicit val s = stack
 
                 require(stack.size >= 2)
@@ -334,7 +335,7 @@ object NCIntentDslCompiler extends LazyLogging {
         }
 
         override def exitEqExpr(ctx: NCIntentDslParser.EqExprContext): Unit = {
-            termCode += ((_, stack: StackType, _) ⇒ {
+            termInstrs += ((_, stack: StackType, _) ⇒ {
                 implicit val s = stack
 
                 require(stack.size >= 2)
@@ -365,7 +366,7 @@ object NCIntentDslCompiler extends LazyLogging {
         override def exitCallExpr(ctx: NCIntentDslParser.CallExprContext): 
Unit = {
             val fun = ctx.ID().getText
 
-            termCode += ((tok: NCToken, stack: StackType, ctx: 
NCDslTermContext) ⇒ {
+            termInstrs += ((tok: NCToken, stack: StackType, ctx: 
NCDslTermContext) ⇒ {
                 implicit val evidence = stack
 
                 def ensureStack(min: Int): Unit =
@@ -637,6 +638,14 @@ object NCIntentDslCompiler extends LazyLogging {
 
                 flowRegex = if (regex.nonEmpty) Some(regex) else None
             }
+
+            if (flowRegex.isDefined) // Pre-check.
+                try
+                    Pattern.compile(flowRegex.get)
+                catch {
+                    case e: PatternSyntaxException ⇒
+                        throw new IAE(s"${e.getDescription} in DSL intent flow 
regex '${e.getPattern}' near index ${e.getIndex}.")
+                }
         }
 
         override def exitIntentId(ctx: NCIntentDslParser.IntentIdContext): 
Unit = {
@@ -675,7 +684,55 @@ object NCIntentDslCompiler extends LazyLogging {
                     }
                 }
 
-            termCode += ((_, stack, _) ⇒ pushAny(atom, false)(stack))
+            termInstrs += ((_, stack, _) ⇒ pushAny(atom, false)(stack))
+        }
+
+        override def exitTerm(ctx: NCIntentDslParser.TermContext): Unit = {
+            if (min < 0 || min > max)
+                throw new IAE(s"Invalid DSL intent term min quantifiers: $min 
(must be min >= 0 && min <= max).")
+            if (max < 1)
+                throw new IAE(s"Invalid DSL intent term max quantifiers: $max 
(must be max >= 1).")
+
+            val pred =
+                if (termMtdName != null) { // User-code defined term.
+                    (tok: NCToken, ctx: NCDslTermContext) ⇒ {
+                        (true, true)
+                    }
+                }
+                else { // DSL-defined term.
+                    val instrs = mutable.Buffer.empty[Instr]
+
+                    instrs ++= termInstrs
+
+                    (tok: NCToken, termCtx: NCDslTermContext) ⇒ {
+                        val stack = new mutable.ArrayStack[NCDslTermRetVal]()
+
+                        instrs.foreach(_(tok, stack, termCtx))
+
+                        val x = stack.pop()
+
+                        if (!isBoolean(x.retVal))
+                            throw new IAE(s"DSL intent term does not return 
boolean value: ${ctx.getText}")
+
+                        (asBoolean(x.retVal), x.usedTok)
+                    }
+
+                }
+
+            // Add term.
+            terms += NCDslTerm(
+                termId,
+                pred,
+                min,
+                max,
+                termConv
+            )
+
+            // Reset term vars.
+            setMinMax(1, 1)
+            termInstrs.clear()
+            termClsName = null
+            termMtdName = null
         }
 
         /**
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslIntent.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslIntent.scala
index a686453..512d5fc 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslIntent.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslIntent.scala
@@ -35,15 +35,9 @@ case class NCDslIntent(
     require(meta != null)
 
     // Flow regex as a compiled pattern.
+    // Regex validity check is already done during intent compilation.
     val flowRegex = flow match {
-        case Some(r) ⇒
-            try
-                Some(Pattern.compile(r))
-            catch {
-                case e: PatternSyntaxException ⇒
-                    throw new IllegalArgumentException(s"${e.getDescription} 
in flow regex '${e.getPattern}' near index ${e.getIndex}.")
-            }
-
+        case Some(r) ⇒ Some(Pattern.compile(r))
         case None ⇒ None
     }
 
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslTerm.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslTerm.scala
index 3edc892..3a7fa2a 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslTerm.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslTerm.scala
@@ -28,11 +28,4 @@ case class NCDslTerm(
     min: Int,
     max: Int,
     conv: Boolean
-) {
-    if (pred == null)
-        throw new IllegalArgumentException("Intent DSL term must be defined.")
-    if (min < 0 || min > max)
-        throw new IllegalArgumentException(s"Invalid intent DSL term min 
quantifiers: $min (must be min >= 0 && min <= max).")
-    if (max < 1)
-        throw new IllegalArgumentException(s"Invalid intent DSL term max 
quantifiers: $max (must be max >= 1).")
-}
+)
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslCompilerSpec.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslCompilerSpec.scala
index 7659856..021c2fb 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslCompilerSpec.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslCompilerSpec.scala
@@ -30,7 +30,7 @@ class NCDslCompilerSpec {
     def test(): Unit = {
         NCIntentDslCompiler.compile(
             """
-              |intent=i1 meta={'a': true} term={2 == 2}
+              |intent=i1 meta={'a': true} term={2 == 2 && size(id()) != -25}
               |""".stripMargin, "mdl.id",
             Map.empty[String, AnyRef],
             Map.empty[String, AnyRef],

Reply via email to