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 2020/12/22 21:20:48 UTC

[incubator-nlpcraft] branch master updated: Predicate error fixed, related test added.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 88d13b9  Predicate error fixed, related test added.
88d13b9 is described below

commit 88d13b98d84edfc2f7ed5a1ebdfe3419c1e0b063
Author: Sergey Kamov <se...@apache.org>
AuthorDate: Wed Dec 23 00:19:09 2020 +0300

    Predicate error fixed, related test added.
---
 .../model/intent/utils/NCDslTokenPredicate.java    | 20 ++++---
 .../apache/nlpcraft/model/NCIntentDslSpec.scala    | 61 ++++++++++++++++++++++
 2 files changed, 70 insertions(+), 11 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/NCDslTokenPredicate.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/NCDslTokenPredicate.java
index c717833..99270a1 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/NCDslTokenPredicate.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/NCDslTokenPredicate.java
@@ -54,13 +54,13 @@ public class NCDslTokenPredicate implements Function<NCToken, Boolean> {
         "endidx"
     );
 
-    private List<String> parts;
-    private String param;
-    private String paramFunc;
-    private String op;
-    private Object value;
-    private String valueStr;
-    private Function<NCToken, NCToken> qualFunc;
+    private final List<String> parts;
+    private final String param;
+    private final String paramFunc;
+    private final String op;
+    private final Object value;
+    private final String valueStr;
+    private final Function<NCToken, NCToken> qualFunc;
 
     private boolean validated = false;
 
@@ -93,8 +93,6 @@ public class NCDslTokenPredicate implements Function<NCToken, Boolean> {
         this.param = param;
         this.op = op;
 
-        valueStr = null;
-
         if (value == null)
             valueStr = "null";
         else if (value instanceof Collection)
@@ -401,11 +399,11 @@ public class NCDslTokenPredicate implements Function<NCToken, Boolean> {
                         }
                     }
                     else if (obj instanceof Map)
-                        lval = ((Map<String, Object>)obj).get(strIdx);
+                        lval = ((Map<String, Object>)obj).get(stripQuotes(strIdx));
                     else
                         throw new IllegalArgumentException(String.format(
                             "Invalid token predicate DSL meta parameter value " +
-                                "for indexed access (java.util.List or java.util.Map only): %s %s %s",
+                            "for indexed access (java.util.List or java.util.Map only): %s %s %s",
                             param, op, valueStr));
                 }
             }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/NCIntentDslSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/NCIntentDslSpec.scala
new file mode 100644
index 0000000..19913b2
--- /dev/null
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/NCIntentDslSpec.scala
@@ -0,0 +1,61 @@
+/*
+ * 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
+
+import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
+import org.junit.jupiter.api.Assertions.{assertEquals, assertTrue}
+import org.junit.jupiter.api.Test
+
+import scala.language.implicitConversions
+
+/**
+  * Intents DSL test model.
+  */
+class NCIntentDslSpecModel extends NCModelAdapter(
+    "nlpcraft.intents.dsl.test", "Intents DSL Test Model", "1.0"
+) {
+    private implicit def convert(s: String): NCResult = NCResult.text(s)
+
+    // Moscow population filter.
+    @NCIntent("intent=bigCity term(city)~{id == 'nlpcraft:city' && ~nlpcraft:city:citymeta['population'] >= 10381222}")
+    private def onBigCity(ctx: NCIntentMatch): NCResult = "OK"
+
+    @NCIntent("intent=otherCity term(city)~{id == 'nlpcraft:city' }")
+    private def onOtherCity(ctx: NCIntentMatch): NCResult = "OK"
+}
+
+/**
+  * Intents DSL test.
+  */
+@NCTestEnvironment(model = classOf[NCIntentDslSpecModel], startClient = true)
+class NCIntentDslSpec extends NCTestContext {
+    private def check(txt: String, intent:  String): Unit = {
+        val res = getClient.ask(txt)
+
+        assertTrue(res.isOk, s"Checked: $txt")
+        assertTrue(res.getResult.isPresent, s"Checked: $txt")
+        assertEquals(intent, res.getIntentId, s"Checked: $txt")
+    }
+
+    @Test
+    def testBigCity(): Unit = check("Moscow", "bigCity")
+
+    @Test
+    def testOtherCity(): Unit = check("San Francisco", "otherCity")
+}
+