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/05/17 17:02:29 UTC

[incubator-nlpcraft] branch NLPCRAFT-319 updated: WIP.

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

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


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

commit 68bd96e100da4832139538bff5b65322df923d9c
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Mon May 17 20:02:17 2021 +0300

    WIP.
---
 .../model/tools/test/NCTestClientBuilder.java      | 27 ++++++--
 .../nlpcraft/model/tools/test/NCTestResult.java    |  6 +-
 .../nlpcraft/model/meta/NCMetaResultSpec.scala     | 74 ++++++++++++++++++++++
 3 files changed, 102 insertions(+), 5 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/NCTestClientBuilder.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/NCTestClientBuilder.java
index 550d0a3..ef09c1f 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/NCTestClientBuilder.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/NCTestClientBuilder.java
@@ -192,7 +192,7 @@ public class NCTestClientBuilder {
         @SerializedName("usrId") private long userId;
         @SerializedName("resType") private String resType;
         @SerializedName("resBody") private Object resBody;
-        @SerializedName("resMeta") private Object resMeta;
+        @SerializedName("resMeta") private Map<String, Object>  resMeta;
         @SerializedName("status") private String status;
         @SerializedName("error") private String error;
         @SerializedName("createTstamp") private long createTstamp;
@@ -287,6 +287,16 @@ public class NCTestClientBuilder {
             return resBody;
         }
 
+        // TODO:
+        public Map<String, Object> getResultMeta() {
+            return resMeta;
+        }
+
+        // TODO:
+        public void setResultMeta(Map<String, Object> resMeta) {
+            this.resMeta = resMeta;
+        }
+
         /**
          *
          * @param resBody
@@ -558,7 +568,7 @@ public class NCTestClientBuilder {
                     e.getLocalizedMessage()
                 );
 
-                return mkResult(txt, mdlId, null, null, e.getLocalizedMessage(), null, 0);
+                return mkResult(txt, mdlId, null, null, null, e.getLocalizedMessage(), null, 0);
             }
 
             NCRequestStateJson state = resJs.getState();
@@ -574,6 +584,7 @@ public class NCTestClientBuilder {
                             gson.toJson(state.getResultBody()) :
                             (String)state.getResultBody() :
                         null,
+                    state.getResultMeta(),
                     state.getError(),
                     state.getIntentId(),
                     System.currentTimeMillis() - now
@@ -601,7 +612,7 @@ public class NCTestClientBuilder {
                     e.getLocalizedMessage()
                 );
 
-                return mkResult(txt, mdlId, null, null, e.getLocalizedMessage(), null,0);
+                return mkResult(txt, mdlId, null, null, null, e.getLocalizedMessage(), null,0);
             }
 
             long maxTime = System.currentTimeMillis() + DFLT_MAX_WAIT_TIME;
@@ -638,6 +649,7 @@ public class NCTestClientBuilder {
                     res.getBody() != null ?
                         "json".equals(res.getType()) ? gson.toJson(res.getBody()) : res.getBody() :
                         null,
+                    res.getMeta(),
                     res.getErrorMessage(),
                     res.getIntentId(),
                     System.currentTimeMillis() - now
@@ -1008,6 +1020,7 @@ public class NCTestClientBuilder {
          * @param mdlId Model ID.
          * @param resType
          * @param resBody
+         * @param resMeta
          * @param errMsg
          * @param intentId
          * @param time
@@ -1018,6 +1031,7 @@ public class NCTestClientBuilder {
             String mdlId,
             String resType,
             String resBody,
+            Map<String, Object> resMeta,
             String errMsg,
             String intentId,
             long time
@@ -1027,7 +1041,7 @@ public class NCTestClientBuilder {
             assert (resType != null && resBody != null) ^ errMsg != null;
 
             return new NCTestResult() {
-                private Optional<String> convert(String s) {
+                private<T> Optional<T> convert(T s) {
                     return s == null ? Optional.empty() : Optional.of(s);
                 }
 
@@ -1065,6 +1079,11 @@ public class NCTestClientBuilder {
                 public String getIntentId() {
                     return intentId;
                 }
+
+                @Override
+                public Optional<Map<String, Object>> getResultMeta() {
+                    return convert(resMeta);
+                }
             };
         }
     }
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/NCTestResult.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/NCTestResult.java
index 44157be..8dd8570 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/NCTestResult.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/NCTestResult.java
@@ -17,6 +17,7 @@
 
 package org.apache.nlpcraft.model.tools.test;
 
+import java.util.Map;
 import java.util.Optional;
 
 /**
@@ -61,7 +62,10 @@ public interface NCTestResult {
      * @see #isOk()
      */
     Optional<String> getResultType();
-    
+
+    // TODO:
+    Optional<Map<String, Object>> getResultMeta();
+
     /**
      * Gets optional execution error. Only provided if processing failed.
      *
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/meta/NCMetaResultSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/meta/NCMetaResultSpec.scala
new file mode 100644
index 0000000..c31f6fe
--- /dev/null
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/meta/NCMetaResultSpec.scala
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nlpcraft.model.meta
+
+import org.apache.nlpcraft.common.JavaMeta
+import org.apache.nlpcraft.model.`abstract`.NCAbstractTokensModel
+import org.apache.nlpcraft.model.{NCElement, NCIntent, NCIntentMatch, NCResult}
+import org.apache.nlpcraft.{NCTestElement, NCTestEnvironment}
+import org.junit.jupiter.api.Test
+
+import java.util
+
+/**
+  * Test model.
+  */
+object NCMetaResultSpecModel {
+    final val V1 = "v1"
+    final val V2 = 2.2.asInstanceOf[AnyRef]
+    final val V3 = new util.HashMap[String, AnyRef]()
+
+    V3.put("k1", V1)
+    V3.put("k2", V2)
+}
+
+import org.apache.nlpcraft.model.meta.NCMetaResultSpecModel._
+
+class NCMetaResultSpecModel extends NCAbstractTokensModel {
+    override def getElements: util.Set[NCElement] = Set(NCTestElement("a"))
+
+    @NCIntent("intent=i term(t)={tok_id() == 'a'}")
+    def onIntent(): NCResult = {
+        val res = NCResult.text("OK")
+
+        res.getMetadata.put("k1", V1)
+        res.getMetadata.put("k2", V2)
+        res.getMetadata.put("k3", V3)
+
+        res
+    }
+}
+
+@NCTestEnvironment(model = classOf[NCMetaResultSpecModel], startClient = true)
+class NCMetaResultSpec extends NCMetaSpecAdapter {
+    @Test
+    def test(): Unit = {
+        val res = getClient.ask("a")
+
+        require(res.isOk)
+        require(res.getResultMeta.isPresent)
+
+        val meta = res.getResultMeta.get()
+
+        println(s"Meta received: $meta")
+
+        require(meta.get("k1") == V1)
+        require(meta.get("k2") == V2)
+        require(meta.get("k3") == V3)
+    }
+}