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 2022/08/25 10:28:49 UTC

[incubator-nlpcraft] 08/11: IDL fragments related fixes.

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

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

commit 9fa4af8b6d0a3304cb3516179b7f7ac3eee819fe
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Thu Aug 25 13:14:50 2022 +0300

    IDL fragments related fixes.
---
 .../internal/intent/compiler/NCIDLCompiler.scala   | 11 ++---
 .../compiler/NCIDLImportsOverridingSpec.scala      | 56 ++++++++++++++++++++++
 .../nlpcraft/internal/intent/compiler/imp1.idl     | 23 +++++++++
 .../nlpcraft/internal/intent/compiler/imp2.idl     | 24 ++++++++++
 4 files changed, 108 insertions(+), 6 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 b403545d..e0c48480 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
@@ -261,12 +261,11 @@ class NCIDLCompiler(cfg: NCModelConfig) extends LazyLogging with mutable.Cloneab
 
             fragCache.get(frag.id) match
                 case Some(exFrag) =>
-                    if frag.terms != exFrag.terms then
-                        // TODO: text
-                        if isMethodLevel then
-                            logger.warn(s"Fragment '${frag.id}' was overriden just for for origin: '${this.origin}'.")
-                        else
-                            logger.warn(s"Fragment '${frag.id}' was overriden in origin: '${this.origin}' permanently.")
+                    // TODO: text
+                    if isMethodLevel then
+                        logger.warn(s"Fragment '${frag.id}' was overriden just for for origin: '${this.origin}'.")
+                    else
+                        logger.warn(s"Fragment '${frag.id}' was overriden in origin: '${this.origin}' permanently.")
                 case None => // No-op.
 
             fragCache += frag.id -> frag
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLImportsOverridingSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLImportsOverridingSpec.scala
new file mode 100644
index 00000000..0d58e858
--- /dev/null
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLImportsOverridingSpec.scala
@@ -0,0 +1,56 @@
+/*
+ * 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
+ *
+ *      https://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.internal.intent.compiler
+
+import org.apache.nlpcraft.*
+import org.apache.nlpcraft.annotations.{NCIntent, NCIntentRef}
+import org.apache.nlpcraft.internal.impl.NCModelScanner
+import org.apache.nlpcraft.nlp.parsers.NCSemanticTestElement as TE
+import org.apache.nlpcraft.nlp.util.*
+import org.junit.jupiter.api.Test
+
+import scala.util.Using
+
+class NCIDLImportsOverridingSpec:
+    @NCIntent("import('org/apache/nlpcraft/internal/intent/compiler/imp2.idl')")
+    class M extends NCTestModelAdapter:
+        // Uses fragment defined on imp2.idl, which overrides imp1.idl.
+        @NCIntent("intent=i2 fragment(f)")
+        private def on2(ctx: NCContext, im: NCIntentMatch): NCResult = NCResult(2)
+
+        // Uses own fragment, which which overrides all.
+        @NCIntent("fragment=f term(z)~{# == 'x3'} intent=i3 fragment(f)")
+        private def on3(ctx: NCContext, im: NCIntentMatch): NCResult = NCResult(3)
+
+        // Uses initial fragment version, defined in imp1.idl.
+        @NCIntent("import('org/apache/nlpcraft/internal/intent/compiler/imp1.idl')")
+        @NCIntentRef("i1")
+        private def on1(ctx: NCContext, im: NCIntentMatch): NCResult = NCResult(1)
+
+        override val getPipeline: NCPipeline =
+            val pl = mkEnPipeline
+            pl.entParsers += NCTestUtils.mkEnSemanticParser(TE("x1"), TE("x2"), TE("x3"))
+            pl
+
+    @Test
+    def test(): Unit =
+        Using.resource(new NCModelClient(new M())) { client =>
+            require(client.ask("x1", "usr").getBody == 1)
+            require(client.ask("x2", "usr").getBody == 2)
+            require(client.ask("x3", "usr").getBody == 3)
+        }
\ No newline at end of file
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/imp1.idl b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/imp1.idl
new file mode 100644
index 00000000..e428c36c
--- /dev/null
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/imp1.idl
@@ -0,0 +1,23 @@
+ /*
+ * 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.
+ */
+
+// ============================
+// Test content.
+// ============================
+
+fragment=f term={# == 'x1'}
+intent=i1 fragment(f)
\ No newline at end of file
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/imp2.idl b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/imp2.idl
new file mode 100644
index 00000000..f92ba03f
--- /dev/null
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/imp2.idl
@@ -0,0 +1,24 @@
+ /*
+ * 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.
+ */
+
+// ============================
+// Test content.
+// ============================
+
+import('org/apache/nlpcraft/internal/intent/compiler/imp1.idl')
+// Overrides fragment.
+fragment=f term={# == 'x2'}