This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch NLPCRAFT-509 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit e7836144dfd6b8c0794ac716c4c481a393da8602 Author: Sergey Kamov <[email protected]> AuthorDate: Sat Aug 6 16:12:11 2022 +0300 Result trait refactoring. --- .../examples/pizzeria/PizzeriaModelSpec.scala | 6 ++-- .../pizzeria/cli/PizzeriaModelServer.scala | 4 +-- .../scala/org/apache/nlpcraft/NCModelClient.scala | 2 +- .../main/scala/org/apache/nlpcraft/NCResult.scala | 35 ---------------------- .../intent/matcher/NCIntentSolverManager.scala | 2 +- .../conversation/NCConversationTimeoutSpec.scala | 2 +- .../internal/impl/NCModelCallbacksSpec.scala | 2 +- .../nlpcraft/internal/impl/NCModelClientSpec.scala | 4 +-- .../internal/impl/NCModelClientSpec3.scala | 2 +- .../internal/impl/NCModelPingPongSpec.scala | 16 ++++++---- .../apache/nlpcraft/nlp/NCEntityMapperSpec.scala | 2 +- .../apache/nlpcraft/nlp/util/NCTestResult.scala | 4 +-- .../org/apache/nlpcraft/nlp/util/NCTestUtils.scala | 2 +- 13 files changed, 26 insertions(+), 57 deletions(-) diff --git a/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala b/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala index f0f80a75..8d5c3723 100644 --- a/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala +++ b/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala @@ -87,10 +87,10 @@ class PizzeriaModelSpec: val resp = client.ask(txt, "userId") testMsgs += s">> Request: $txt" - testMsgs += s">> Response: '${resp.resultType}': ${resp.body}" + testMsgs += s">> Response: '${resp.getType}': ${resp.getBody}" - if expType != resp.resultType then - errs += testNum -> new Exception(s"Unexpected result type [num=$testNum, txt=$txt, expected=$expType, type=${resp.resultType}]") + if expType != resp.getType then + errs += testNum -> new Exception(s"Unexpected result type [num=$testNum, txt=$txt, expected=$expType, type=${resp.getType}]") // Check execution result on last request. if idx == reqs.size - 1 then diff --git a/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelServer.scala b/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelServer.scala index 056d071a..aa8fd12a 100644 --- a/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelServer.scala +++ b/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelServer.scala @@ -64,8 +64,8 @@ object PizzeriaModelServer: if req == null || req.isEmpty then Exception(s"Empty request.") val resp = nlpClient.ask(req, null, "userId") - val prompt = if resp.resultType == ASK_DIALOG then "(Your should answer on the model's question below)\n" else "" - doResponse(s"$prompt${resp.body}") + val prompt = if resp.getType == ASK_DIALOG then "(Your should answer on the model's question below)\n" else "" + doResponse(s"$prompt${resp.getBody}") catch case e: NCRejection => doResponse(s"Request rejected: ${e.getMessage}") case e: Throwable => diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala index 07c626d1..4b5553e1 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala @@ -174,7 +174,7 @@ class NCModelClient(mdl: NCModel) extends LazyLogging, AutoCloseable: try val r = ask(sample, Map.empty, userId) - Option.when(r.intentId.isEmpty || r.intentId.get!= i.intent.id)(s"Unexpected intent ID: '${r.intentId.getOrElse("(not set)")}'") + Option.when(r.getIntentId.isEmpty || r.getIntentId.get != i.intent.id)(s"Unexpected intent ID: '${r.getIntentId.getOrElse("(not set)")}'") catch case e: Throwable => logger.warn("Unexpected error.", e) Option(e.getLocalizedMessage) diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCResult.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCResult.scala deleted file mode 100644 index 63c5cd60..00000000 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCResult.scala +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 - -import org.apache.nlpcraft.NCResultType.* - -/** - * - */ -object NCResult: - def apply(body: Any, resultType: NCResultType, intentId: String): NCResult = new NCResult(body = body, resultType = resultType, Option(intentId)) - def apply(body: Any, resultType: NCResultType): NCResult = apply(body, resultType, intentId = None) - -/** - * - * @param body - * @param resultType - * @param intentId - */ -case class NCResult(body: Any, resultType: NCResultType, intentId: Option[String] = None) diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/matcher/NCIntentSolverManager.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/matcher/NCIntentSolverManager.scala index 4ce87943..1f04f9f1 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/matcher/NCIntentSolverManager.scala +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/matcher/NCIntentSolverManager.scala @@ -711,7 +711,7 @@ class NCIntentSolverManager( def executeCallback(in: NCCallbackInput): NCResult = var cbRes = intentRes.fn(in) // Store winning intent match in the input. - if cbRes.intentId.isEmpty then cbRes = NCResult(cbRes.body, cbRes.resultType, intentRes.intentId) + if cbRes.getIntentId.isEmpty then cbRes = NCResult(cbRes.getBody, cbRes.getType, intentRes.intentId) cbRes def finishSearch(): Unit = diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationTimeoutSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationTimeoutSpec.scala index ee8537c8..62e5119b 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationTimeoutSpec.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationTimeoutSpec.scala @@ -64,7 +64,7 @@ class NCConversationTimeoutSpec: Using.resource(new NCModelClient(mdl)) { cli => def check(hasValue: Boolean): Unit = - require(cli.ask("test", "userId").body.toString == (if hasValue then VALUE else EMPTY)) + require(cli.ask("test", "userId").getBody.toString == (if hasValue then VALUE else EMPTY)) check(false) check(true) diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelCallbacksSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelCallbacksSpec.scala index 02f07d66..706c0641 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelCallbacksSpec.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelCallbacksSpec.scala @@ -100,7 +100,7 @@ class NCModelCallbacksSpec: */ private def testOk(client: NCModelClient, exp: NCResult, states: State*): Unit = set(states*) - require(client.ask("x", "userId").body == exp.body) + require(client.ask("x", "userId").getBody == exp.getBody) /** * diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala index a9f7c5b2..4452a1c4 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala @@ -49,8 +49,8 @@ class NCModelClientSpec: Using.resource(new NCModelClient(mdl)) { client => val res = client.ask("Lights on at second floor kitchen", "userId") - println(s"Intent: ${res.intentId}") - println(s"Body: ${res.body}") + println(s"Intent: ${res.getIntentId}") + println(s"Body: ${res.getBody}") client.validateSamples() diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec3.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec3.scala index f98408be..332822ed 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec3.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec3.scala @@ -49,7 +49,7 @@ class NCModelClientSpec3: Using.resource(new NCModelClient(mdl)) { client => def ask(): NCCallbackData = client.debugAsk("e1", "userId", true) def execCallback(cb: NCCallbackData): NCResult = cb.getCallback.apply(cb.getCallbackArguments) - def execCallbackOk(cb: NCCallbackData): Unit = println(s"Result: ${execCallback(cb).body}") + def execCallbackOk(cb: NCCallbackData): Unit = println(s"Result: ${execCallback(cb).getBody}") def execCallbackFail(cb: NCCallbackData): Unit = try execCallback(cb) catch case e: NCException => println(s"Expected error: ${e.getMessage}") diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala index 49168b2b..8f47d0cd 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala @@ -34,14 +34,18 @@ import scala.util.Using class NCModelPingPongSpec: private var client: NCModelClient = _ - private class R(resType: NCResultType, txt: String) extends NCResult(txt, resType): - override def toString: String = s"$resType ($txt)" + def mkResult(resType: NCResultType, txt: String): NCResult = + new NCResult (): + override def getBody: Any = txt + override def getType: NCResultType = resType + override def getIntentId: Option[String] = None + override def toString: String = s"$resType ($txt)" private val MDL: NCTestModelAdapter = new NCTestModelAdapter(): @NCIntent("intent=command term(command)={# == 'command'}") def onCommand(ctx: NCContext, im: NCIntentMatch, @NCIntentTerm("command") command: NCEntity): NCResult = - R(ASK_DIALOG, s"Confirm your request 'command'") + mkResult(ASK_DIALOG, s"Confirm your request 'command'") @NCIntent("intent=confirmCommand term(confirm)={# == 'confirm'}") def onConfirmCommand(ctx: NCContext, im: NCIntentMatch, @NCIntentTerm("confirm") confirm: NCEntity): NCResult = @@ -56,11 +60,11 @@ class NCModelPingPongSpec: println("'Command' confirmed and can be be executed here.") - R(ASK_RESULT, s"'dialog' confirmed.") + mkResult(ASK_RESULT, s"'dialog' confirmed.") @NCIntent("intent=other term(other)={# == 'other'}") def onOther(ctx: NCContext, im: NCIntentMatch, @NCIntentTerm("other") other: NCEntity): NCResult = - R(ASK_RESULT, s"Some request by: ${other.mkText}") + mkResult(ASK_RESULT, s"Some request by: ${other.mkText}") MDL.pipeline.entParsers += NCTestUtils.mkEnSemanticParser(List(STE("command"), STE("confirm"), STE("other"))) @@ -73,7 +77,7 @@ class NCModelPingPongSpec: private def ask(txt: String, typ: NCResultType): Unit = val res = client.ask(txt, "userId") println(s"Request [text=$txt, result=$res]") - require(res.resultType == typ) + require(res.getType == typ) private def askForDialog(txt: String): Unit = ask(txt, ASK_DIALOG) private def askForResult(txt: String): Unit = ask(txt, ASK_RESULT) diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala index 0d07e058..277a621a 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala @@ -72,5 +72,5 @@ class NCEntityMapperSpec: @Test def test(): Unit = Using.resource(new NCModelClient(mdl)) { client => - require(client.ask("a b c d", "userId").intentId.orNull == "abcd") + require(client.ask("a b c d", "userId").getIntentId.orNull == "abcd") } diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestResult.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestResult.scala index 77b74906..14b86626 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestResult.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestResult.scala @@ -23,10 +23,10 @@ import org.apache.nlpcraft.{NCResult, NCResultType} * */ object NCTestResult { - def apply(): NCTestResult = new NCTestResult() + def apply(): NCResult = TEST_RESULT } /** * */ -class NCTestResult extends NCResult("test", NCResultType.ASK_RESULT) +val TEST_RESULT = NCResult("test", NCResultType.ASK_RESULT) diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestUtils.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestUtils.scala index 7e4da7e9..d64924ca 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestUtils.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestUtils.scala @@ -113,7 +113,7 @@ object NCTestUtils: def ask(): NCResult = client.ask("test", "userId") if expectedOk then - println(ask().body) + println(ask().getBody) else try ask()
