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 2021/03/23 11:13:23 UTC

[incubator-nlpcraft] 03/04: WIP.

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

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

commit d5eb9da623a3c4cf568adb0f6f4f5ff5691f6209
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Tue Mar 23 13:20:53 2021 +0300

    WIP.
---
 .../idl/compiler/functions/NCIdlFunctions.scala    | 45 ++++++++--------------
 .../compiler/functions/NCIdlFunctionsColl.scala    |  8 ++--
 .../compiler/functions/NCIdlFunctionsCompany.scala |  7 +++-
 .../compiler/functions/NCIdlFunctionsDate.scala    |  2 +-
 .../compiler/functions/NCIdlFunctionsMath.scala    |  6 +--
 .../compiler/functions/NCIdlFunctionsRequest.scala |  7 +++-
 .../compiler/functions/NCIdlFunctionsStat.scala    |  4 +-
 .../compiler/functions/NCIdlFunctionsStrings.scala | 12 +++---
 .../compiler/functions/NCIdlFunctionsToken.scala   | 11 ++++--
 .../compiler/functions/NCIdlFunctionsUser.scala    |  7 +++-
 10 files changed, 55 insertions(+), 54 deletions(-)

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 b65dc4c..b059f23 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
@@ -43,25 +43,27 @@ private[functions] trait NCIdlFunctions {
     @BeforeEach
     def before(): Unit = NCIdlCompilerGlobal.clearCache(MODEL_ID)
 
-    import BoolFunc._
+    import TrueFunc._
 
-    case class BoolFunc(
-        func: NCIdlFunction,
-        token: NCToken,
-        idlContext: NCIdlContext
+    case class TrueFunc(
+        truth: String,
+        token: NCToken = tkn(),
+        idlCtx: NCIdlContext = ctx()
     ) {
+        val function: NCIdlFunction = mkFunc(truth)
+
         override def toString: String =
             s"Boolean function [" +
                 s"token=${t2s(token)}, " +
-                s"function=$func" +
+                s"function=$truth" +
                 s"]"
     }
 
-    object BoolFunc {
+    object TrueFunc {
         private def t2s(t: NCToken) = s"${t.getOriginalText} (${t.getId})"
 
-        private def mkFunc(term: String): NCIdlFunction = {
-            val intents = NCIdlCompiler.compileIntents(s"intent=i term(t)={$term}", MODEL, MODEL_ID)
+        private def mkFunc(function: String): NCIdlFunction = {
+            val intents = NCIdlCompiler.compileIntents(s"intent=i term(t)={$function}", MODEL, MODEL_ID)
 
             require(intents.size == 1)
 
@@ -71,24 +73,12 @@ private[functions] trait NCIdlFunctions {
 
             new NCIdlFunction() {
                 override def apply(v1: NCToken, v2: NCIdlContext): NCIdlStackItem = intent.terms.head.pred.apply(v1, v2)
-                override def toString(): String = s"Function, based on term: $term"
+                override def toString(): String = s"Function, based on term: $function"
             }
         }
-
-        def apply(bool: String, tokenId: String): BoolFunc =
-            BoolFunc(func = mkFunc(bool), token = mkToken(tokenId), idlContext = mkIdlContext())
-
-        def apply(bool: String, token: NCToken): BoolFunc =
-            BoolFunc(func = mkFunc(bool), token, idlContext = mkIdlContext())
-
-        def apply(bool: String, idlContext: NCIdlContext): BoolFunc =
-            BoolFunc(func = mkFunc(bool), mkToken(), idlContext)
-
-        def apply(bool: String): BoolFunc =
-            BoolFunc(func = mkFunc(bool), mkToken(), idlContext = mkIdlContext())
     }
 
-    protected def mkIdlContext(
+    protected def ctx(
         usr: NCUser = null,
         comp: NCCompany = null,
         srvReqId: String = null,
@@ -97,7 +87,7 @@ private[functions] trait NCIdlFunctions {
         remAddress: String = null,
         clientAgent: String = null,
         reqData: Map[String, AnyRef] = Map.empty[String, AnyRef]
-    ): NCIdlContext = {
+    ): NCIdlContext =
         NCIdlContext(
             req =
                 new NCRequest() {
@@ -111,9 +101,8 @@ private[functions] trait NCIdlFunctions {
                     override def getRequestData: util.Map[String, AnyRef] = reqData.asJava
                 }
         )
-    }
 
-    protected def mkToken(
+    protected def tkn(
         id: String = null,
         srvReqId: String = null,
         parentId: String = null,
@@ -153,11 +142,11 @@ private[functions] trait NCIdlFunctions {
         }
     }
 
-    protected def test(funcs: BoolFunc*): Unit =
+    protected def test(funcs: TrueFunc*): Unit =
         for ((func, idx) ← funcs.zipWithIndex) {
             val res =
                 try
-                    func.func.apply(func.token, func.idlContext).value
+                    func.function.apply(func.token, func.idlCtx).value
                 catch {
                     case e: Exception ⇒ throw new Exception(s"Execution error [index=$idx, testFunc=$func]", e)
                 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsColl.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsColl.scala
index 0fe5523..ba253cd 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsColl.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsColl.scala
@@ -28,9 +28,9 @@ class NCIdlFunctionsColl extends NCIdlFunctions {
         test(
             // BoolFunc(boolCondition = "first(list(1, 2, 3)) == 1"),
             // BoolFunc(boolCondition = "last(list(1, 2, 3)) == 3")
-            BoolFunc(bool = "is_empty(list()) == true"),
-            BoolFunc(bool = "is_empty(list(1)) == false"),
-            BoolFunc(bool = "non_empty(list()) == false"),
-            BoolFunc(bool = "non_empty(list(1)) == true")
+            TrueFunc(truth = "is_empty(list()) == true"),
+            TrueFunc(truth = "is_empty(list(1)) == false"),
+            TrueFunc(truth = "non_empty(list()) == false"),
+            TrueFunc(truth = "non_empty(list(1)) == true")
         )
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCompany.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCompany.scala
index 35976ab..9554ad2 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCompany.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCompany.scala
@@ -67,10 +67,13 @@ class NCIdlFunctionsCompany extends NCRestSpec with NCIdlFunctions {
 
     @Test
     def test(): Unit = {
-        val ctx = mkIdlContext(comp = company)
+        val idlCtx = ctx(comp = company)
 
         test(
-            BoolFunc(s"comp_name() == '${company.getName}'", ctx)
+            TrueFunc(
+                truth = s"comp_name() == '${company.getName}'",
+                idlCtx = idlCtx
+            )
         )
     }
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsDate.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsDate.scala
index 34cbe19..cb8901e 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsDate.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsDate.scala
@@ -28,7 +28,7 @@ class NCIdlFunctionsDate extends NCIdlFunctions {
         val now = System.currentTimeMillis()
 
         test(
-            BoolFunc(bool = s"now() - $now < 1000")
+            TrueFunc(truth = s"now() - $now < 1000")
         )
     }
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsMath.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsMath.scala
index cb7da1b..1e9ffe0 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsMath.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsMath.scala
@@ -26,8 +26,8 @@ class NCIdlFunctionsMath extends NCIdlFunctions {
     @Test
     def test(): Unit =
         test(
-            BoolFunc(bool = "sin(90.0) == 0"),
-            BoolFunc(bool = "sin(90) == 0"),
-            BoolFunc(bool = "rand() < 1")
+            TrueFunc(truth = "sin(90.0) == 0"),
+            TrueFunc(truth = "sin(90) == 0"),
+            TrueFunc(truth = "rand() < 1")
         )
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsRequest.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsRequest.scala
index d60c2ca..aad7973 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsRequest.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsRequest.scala
@@ -25,10 +25,13 @@ import org.junit.jupiter.api.Test
 class NCIdlFunctionsRequest extends NCIdlFunctions {
     @Test
     def test(): Unit = {
-        val ctx = mkIdlContext(srvReqId = "req.id")
+        val idlCtx = ctx(srvReqId = "req.id")
 
         test(
-            BoolFunc(s"req_id() == '${ctx.req.getServerRequestId}'", ctx)
+            TrueFunc(
+                truth = s"req_id() == '${idlCtx.req.getServerRequestId}'",
+                idlCtx = idlCtx
+            )
         )
     }
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStat.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStat.scala
index d6aeabc..bed9c30 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStat.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStat.scala
@@ -26,7 +26,7 @@ class NCIdlFunctionsStat extends NCIdlFunctions {
     @Test
     def test(): Unit =
         test(
-             BoolFunc(bool = "max(list(1, 2, 3)) == 3"),
-             BoolFunc(bool = "min(list(1, 2, 3)) == 1")
+             TrueFunc(truth = "max(list(1, 2, 3)) == 3"),
+             TrueFunc(truth = "min(list(1, 2, 3)) == 1")
         )
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStrings.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStrings.scala
index c2fd3b7..c09dc3d 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStrings.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStrings.scala
@@ -26,11 +26,11 @@ class NCIdlFunctionsStrings extends NCIdlFunctions {
     @Test
     def test(): Unit =
         test(
-            BoolFunc(bool = "trim(' a b  ') == 'a b'"),
-            BoolFunc(bool = "strip(' a b  ') == 'a b'"),
-            BoolFunc(bool = "uppercase('aB') == 'AB'"),
-            BoolFunc(bool = "lowercase('aB') == 'ab'"),
-            BoolFunc(bool = "is_num('a') == false"),
-            BoolFunc(bool = "is_num('1') == true")
+            TrueFunc(truth = "trim(' a b  ') == 'a b'"),
+            TrueFunc(truth = "strip(' a b  ') == 'a b'"),
+            TrueFunc(truth = "uppercase('aB') == 'AB'"),
+            TrueFunc(truth = "lowercase('aB') == 'ab'"),
+            TrueFunc(truth = "is_num('a') == false"),
+            TrueFunc(truth = "is_num('1') == true")
         )
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsToken.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsToken.scala
index 1f5a823..272fac9 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsToken.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsToken.scala
@@ -26,10 +26,13 @@ class NCIdlFunctionsToken extends NCIdlFunctions {
     @Test
     def test(): Unit =
         test(
-            BoolFunc(bool = "id() == 'a'", "a"),
-            BoolFunc(
-                bool = "parent() == 'a'",
-                mkToken(parentId = "a")
+            TrueFunc(
+                truth = "id() == 'a'",
+                tkn(id = "a")
+            ),
+            TrueFunc(
+                truth = "parent() == 'a'",
+                tkn(parentId = "a")
             )
         )
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsUser.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsUser.scala
index 3c4560b..6d874c7 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsUser.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsUser.scala
@@ -63,10 +63,13 @@ class NCIdlFunctionsUser extends NCRestSpec with NCIdlFunctions {
 
     @Test
     def test(): Unit =  {
-        val ctx = mkIdlContext(usr = usr)
+        val idlCtx = ctx(usr = usr)
 
         test(
-            BoolFunc(s"user_email() == '${usr.getEmail.get()}'", ctx)
+            TrueFunc(
+                truth = s"user_email() == '${usr.getEmail.get()}'",
+                idlCtx = idlCtx
+            )
         )
     }
 }