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

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


The following commit(s) were added to refs/heads/NLPCRAFT-491 by this push:
     new 62ca618d Pizzeria examples refactoring.
62ca618d is described below

commit 62ca618d29fe9b1bb3fb2a6748afd22f23558b90
Author: Sergey Kamov <[email protected]>
AuthorDate: Wed Jul 13 16:51:20 2022 +0300

    Pizzeria examples refactoring.
---
 .../nlpcraft/examples/pizzeria/PizzeriaModel.scala | 10 +++++-----
 .../nlpcraft/examples/pizzeria/PizzeriaOrder.scala | 20 ++------------------
 .../pizzeria/components/PizzeriaOrderMapper.scala  | 22 +++++++++++-----------
 .../examples/pizzeria/PizzeriaModelSpec.scala      |  2 +-
 4 files changed, 19 insertions(+), 35 deletions(-)

diff --git 
a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModel.scala
 
b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModel.scala
index d2248e4a..4f528796 100644
--- 
a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModel.scala
+++ 
b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModel.scala
@@ -60,12 +60,12 @@ object PizzeriaModel extends LazyLogging:
         val o = getCurrentOrder()
 
         logger.info(s"Intent '${im.getIntentId}' activated for text: 
'${ctx.getRequest.getText}'.")
-        logger.info(s"Before call [desc=${o.getState.toString}, resState: 
${o.getDescription}.")
+        logger.info(s"Before call [desc=${o.getState.toString}, resState: $o.")
 
         val (res, resState) = body.apply(o)
         o.setState(resState)
 
-        logger.info(s"After call [desc=${o.getDescription}, resState: 
$resState.")
+        logger.info(s"After call [desc=$o, resState: $resState.")
 
         res
 
@@ -91,11 +91,11 @@ object PizzeriaModel extends LazyLogging:
 
     private def doShowMenu(state: State): Result = doShowMenuResult() -> state
 
-    private def doShowStatus(o: Order, state: State): Result = 
mkResult(s"Current order state: ${o.getDescription}.") -> state
+    private def doShowStatus(o: Order, state: State): Result = 
mkResult(s"Current order state: $o.") -> state
 
     private def askConfirm(o: Order): Result =
         require(o.isValid)
-        mkDialog(s"Let's specify your order: ${o.getDescription}. Is it 
correct?") -> DIALOG_CONFIRM
+        mkDialog(s"Let's specify your order: $o. Is it correct?") -> 
DIALOG_CONFIRM
 
     private def doResultWithClear(msg: String)(using ctx: NCContext, im: 
NCIntentMatch): Result =
         val conv = ctx.getConversation
@@ -127,7 +127,7 @@ class PizzeriaModel extends NCModelAdapter(new 
NCModelConfig("nlpcraft.pizzeria.
     // This method is defined in class scope and has package access level for 
tests reasons.
     private[pizzeria] def doExecute(o: Order)(using ctx: NCContext, im: 
NCIntentMatch): Result =
         require(o.isValid)
-        doResultWithClear(s"Executed: ${o.getDescription}.")
+        doResultWithClear(s"Executed: $o.")
 
     private def doExecuteOrAskSpecify(o: Order)(using ctx: NCContext, im: 
NCIntentMatch): Result = if o.isValid then doExecute(o) else askSpecify(o)
 
diff --git 
a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaOrder.scala
 
b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaOrder.scala
index 5ade2a73..5b5876d9 100644
--- 
a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaOrder.scala
+++ 
b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaOrder.scala
@@ -84,7 +84,7 @@ class PizzeriaOrder:
     def add(ps: Seq[Pizza], ds: Seq[Drink]): Unit =
         def setByName[T <: OrderPosition](buf: mutable.ArrayBuffer[T], t: T): 
Unit =
             buf.find(_.name == t.name) match
-                case Some(found) => if t.qty.nonEmpty then found.qty = t.qty
+                case Some(foundT) => if t.qty.nonEmpty then foundT.qty = t.qty
                 case None => buf += t
 
         for (p <- ps)
@@ -103,18 +103,6 @@ class PizzeriaOrder:
 
         for (d <- ds) setByName(drinks, d)
 
-    /**
-      *
-      * @return
-      */
-    def getPizzas: Seq[Pizza] = pizzas.toSeq
-
-    /**
-      *
-      * @return
-      */
-    def getDrinks: Seq[Drink] = drinks.toSeq
-
     /**
       *
       * @return
@@ -143,11 +131,7 @@ class PizzeriaOrder:
       */
     def setState(state: PizzeriaOrderState): Unit = this.state = state
 
-    /**
-      *
-      * @return
-      */
-    def getDescription: String =
+    override def toString: String =
         if !isEmpty then
             val ps = if pizzas.nonEmpty then s"pizza: ${pizzas.mkString(", 
")}" else ""
             val ds = if drinks.nonEmpty then s"drinks: ${drinks.mkString(", 
")}" else ""
diff --git 
a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/PizzeriaOrderMapper.scala
 
b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/PizzeriaOrderMapper.scala
index b3e3e500..e267b332 100644
--- 
a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/PizzeriaOrderMapper.scala
+++ 
b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/PizzeriaOrderMapper.scala
@@ -65,22 +65,22 @@ case class PizzeriaOrderMapper(extra: 
PizzeriaOrderMapperDesc, dests: Seq[Pizzer
                 override val getRequestId: String = req.getRequestId
                 override val getId: String = destEnt.getId
 
-        val mainById = dests.map(p => p.elementId -> p).toMap
-        val descEnts = mutable.HashSet.empty ++ ents.filter(e => 
mainById.contains(e.getId))
+        val destsMap = dests.map(p => p.elementId -> p).toMap
+        val destEnts = mutable.HashSet.empty ++ ents.filter(e => 
destsMap.contains(e.getId))
         val extraEnts = ents.filter(_.getId == extra.elementId)
 
-        if descEnts.nonEmpty && extraEnts.nonEmpty && descEnts.size >= 
extraEnts.size then
-            val used = (descEnts ++ extraEnts).toSet
-            val main2Extra = mutable.HashMap.empty[NCEntity, NCEntity]
+        if destEnts.nonEmpty && extraEnts.nonEmpty && destEnts.size >= 
extraEnts.size then
+            val used = (destEnts ++ extraEnts).toSet
+            val dest2Extra = mutable.HashMap.empty[NCEntity, NCEntity]
 
-            for (e <- extraEnts)
-                val m = descEnts.minBy(m => Math.abs(m.position - e.position))
-                descEnts -= m
-                main2Extra += m -> e
+            for (extraEnt <- extraEnts)
+                val destEnt = destEnts.minBy(m => Math.abs(m.position - 
extraEnt.position))
+                destEnts -= destEnt
+                dest2Extra += destEnt -> extraEnt
 
             val unrelated = ents.filter(e => !used.contains(e))
-            val artificial = for ((m, e) <- main2Extra) yield map(m, 
mainById(m.getId).propertyName, e)
-            val unused = descEnts
+            val artificial = for ((m, e) <- dest2Extra) yield map(m, 
destsMap(m.getId).propertyName, e)
+            val unused = destEnts
 
             val res = (unrelated ++ artificial ++ 
unused).sortBy(_.tokens.head.getIndex)
 
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 d0efd41d..f0f80a75 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
@@ -95,7 +95,7 @@ class PizzeriaModelSpec:
                 // Check execution result on last request.
                 if idx == reqs.size - 1 then
                     val lastOrder = mdl.getLastExecutedOrder
-                    def s(o: PizzeriaOrder) = if o == null then null else 
s"Order [state=${o.getState}, desc=${o.getDescription}]"
+                    def s(o: PizzeriaOrder) = if o == null then null else 
s"Order [state=${o.getState}, desc=$o]"
                     val s1 = s(exp)
                     val s2 = s(lastOrder)
                     if s1 != s2 then

Reply via email to