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 dcce9148 Pizzeria examples refactoring.
dcce9148 is described below

commit dcce91486762d6d137483246698e80c2a7ba0b5f
Author: Sergey Kamov <[email protected]>
AuthorDate: Wed Jul 13 15:48:27 2022 +0300

    Pizzeria examples refactoring.
---
 .../nlpcraft/examples/pizzeria/PizzeriaModel.scala | 31 ++++++++++++----------
 .../nlpcraft/examples/pizzeria/PizzeriaOrder.scala |  6 ++---
 .../components/PizzeriaModelPipeline.scala         |  2 +-
 .../pizzeria/components/PizzeriaOrderMapper.scala  |  6 ++---
 4 files changed, 24 insertions(+), 21 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 bb4a5f73..d2248e4a 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
@@ -26,6 +26,16 @@ import 
org.apache.nlpcraft.examples.pizzeria.PizzeriaOrderState.*
 import org.apache.nlpcraft.examples.pizzeria.components.PizzeriaModelPipeline
 import org.apache.nlpcraft.nlp.*
 
+object PizzeriaExtractors:
+    def extractPizzaSize(e: NCEntity): String = 
e.get[String]("ord:pizza:size:value")
+    def extractQty(e: NCEntity, qty: String): Option[Int] = 
Option.when(e.contains(qty))(e.get[String](qty).toDouble.toInt)
+    def extractPizza(e: NCEntity): Pizza =
+        Pizza(e.get[String]("ord:pizza:value"), 
e.getOpt[String]("ord:pizza:size"), extractQty(e, "ord:pizza:qty"))
+    def extractDrink(e: NCEntity): Drink =
+        Drink(e.get[String]("ord:drink:value"), extractQty(e, "ord:drink:qty"))
+
+import PizzeriaExtractors.*
+
 /**
   * * Pizza model helper.
   */
@@ -33,28 +43,21 @@ object PizzeriaModel extends LazyLogging:
     type Result = (NCResult, State)
     private val UNEXPECTED_REQUEST = new NCRejection("Unexpected request for 
current dialog context.")
 
-    private def extractPizzaSize(e: NCEntity): String = 
e.get[String]("ord:pizza:size:value")
-    private def extractQty(e: NCEntity, qty: String): Option[Int] = 
Option.when(e.contains(qty))(e.get[String](qty).toDouble.toInt)
-    private def extractPizza(e: NCEntity): Pizza =
-        Pizza(e.get[String]("ord:pizza:value"), 
e.getOpt[String]("ord:pizza:size"), extractQty(e, "ord:pizza:qty"))
-    private def extractDrink(e: NCEntity): Drink =
-        Drink(e.get[String]("ord:drink:value"), extractQty(e, "ord:drink:qty"))
-
-    private def getOrder(ctx: NCContext): Order =
-        val data = ctx.getConversation.getData
+    private def getCurrentOrder()(using ctx: NCContext): Order =
+        val sess = ctx.getConversation.getData
         val usrId = ctx.getRequest.getUserId
-        data.getOpt[Order](usrId) match
+        sess.getOpt[Order](usrId) match
             case Some(ord) => ord
             case None =>
                 val ord = new Order()
-                data.put(usrId, ord)
+                sess.put(usrId, ord)
                 ord
 
     private def mkResult(msg: String): NCResult = NCResult(msg, ASK_RESULT)
     private def mkDialog(msg: String): NCResult = NCResult(msg, ASK_DIALOG)
 
     private def doRequest(body: Order => Result)(using ctx: NCContext, im: 
NCIntentMatch): NCResult =
-        val o = getOrder(ctx)
+        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}.")
@@ -224,6 +227,6 @@ class PizzeriaModel extends NCModelAdapter(new 
NCModelConfig("nlpcraft.pizzeria.
         o => if !o.isEmpty && o.fixPizzaWithoutSize(extractPizzaSize(size)) 
then askIsReadyOrAskSpecify(o) else throw UNEXPECTED_REQUEST
     )
 
-    override def onRejection(ctx: NCContext, im: Option[NCIntentMatch], e: 
NCRejection): Option[NCResult] =
-        if im.isEmpty || getOrder(ctx).isEmpty then throw e
+    override def onRejection(using ctx: NCContext, im: Option[NCIntentMatch], 
e: NCRejection): Option[NCResult] =
+        if im.isEmpty || getCurrentOrder().isEmpty then throw e
         Option(doShowMenuResult())
\ No newline at end of file
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 f510accd..5ade2a73 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
@@ -90,9 +90,9 @@ class PizzeriaOrder:
         for (p <- ps)
             def setPizza(pred: Pizza => Boolean, notFound: => () => Unit): 
Unit =
                 pizzas.find(pred) match
-                    case Some(found) =>
-                        if p.size.nonEmpty then found.size = p.size
-                        if p.qty.nonEmpty then found.qty = p.qty
+                    case Some(foundPizza) =>
+                        if p.size.nonEmpty then foundPizza.size = p.size
+                        if p.qty.nonEmpty then foundPizza.qty = p.qty
                     case None => notFound()
 
             if p.size.nonEmpty then setPizza(
diff --git 
a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/PizzeriaModelPipeline.scala
 
b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/PizzeriaModelPipeline.scala
index 73fafbc0..9baf9dad 100644
--- 
a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/PizzeriaModelPipeline.scala
+++ 
b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/PizzeriaModelPipeline.scala
@@ -23,7 +23,7 @@ object PizzeriaModelPipeline:
             private val ps = new PorterStemmer
             override def stem(txt: String): String = ps.synchronized { 
ps.stem(txt) }
 
-        import MapperDesc as D
+        import PizzeriaOrderMapperDesc as D
 
         new NCPipelineBuilder().
             withTokenParser(tokParser).
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 a16bf476..b3e3e500 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
@@ -28,7 +28,7 @@ import scala.collection.*
   * @param elementId Element.
   * @param propertyName Element's property name.
   */
-case class MapperDesc(elementId: String, propertyName: String)
+case class PizzeriaOrderMapperDesc(elementId: String, propertyName: String)
 
 /**
   * Element extender.
@@ -51,11 +51,11 @@ object PizzeriaOrderMapper:
     private def str(es: Iterable[NCEntity]): String =
         es.map(e => s"id=${e.getId}(${e.tokens.map(_.getIndex).mkString("[", 
",", "]")})").mkString("{", ", ", "}")
 
-    def apply(extra: MapperDesc, dests: MapperDesc*): PizzeriaOrderMapper = 
new PizzeriaOrderMapper(extra, dests)
+    def apply(extra: PizzeriaOrderMapperDesc, dests: 
PizzeriaOrderMapperDesc*): PizzeriaOrderMapper = new PizzeriaOrderMapper(extra, 
dests)
 
 import PizzeriaOrderMapper.*
 
-case class PizzeriaOrderMapper(extra: MapperDesc, dests: Seq[MapperDesc]) 
extends NCEntityMapper with LazyLogging:
+case class PizzeriaOrderMapper(extra: PizzeriaOrderMapperDesc, dests: 
Seq[PizzeriaOrderMapperDesc]) extends NCEntityMapper with LazyLogging:
     override def map(req: NCRequest, cfg: NCModelConfig, ents: 
List[NCEntity]): List[NCEntity] =
         def map(destEnt: NCEntity, destProp: String, extraEnt: NCEntity): 
NCEntity =
             new NCPropertyMapAdapter with NCEntity:

Reply via email to