private def replaceContext()

in nlpcraft/src/main/scala/org/apache/nlpcraft/internal/conversation/NCConversationData.scala [61:110]


    private def replaceContext(newCtx: mutable.ArrayBuffer[NCEntity]): Unit =
        require(Thread.holdsLock(stm))
        ctx.clear()
        ctx ++= newCtx

    /**
      *
      */
    private def squeezeEntities(): Unit =
        require(Thread.holdsLock(stm))
        stm --= stm.filter(_.holders.isEmpty)

    /**
      * Gets called on each input request for given user and model.
      */
    def updateEntities(): Unit =
        val now = NCUtils.nowUtcMs()

        stm.synchronized {
            depth += 1

            lazy val z = s"usrId=$usrId, mdlId=$mdlId"

            // Conversation cleared by timeout or when there are too much unsuccessful requests.
            if now - lastUpdateTstamp > timeoutMs then
                stm.clear()
                logger.trace(s"STM is reset by timeout [$z]")
            else if depth > maxDepth then
                stm.clear()
                logger.trace(s"STM is reset after reaching max depth [$z]")
            else
                val minUsageTime = now - timeoutMs
                val ents = lastEnts.flatten

                for (item <- stm)
                    val delHs =
                        // Deleted by timeout for entity type or when an entity type used too many requests ago.
                        item.holders.filter(h => h.entityTypeUsageTime < minUsageTime || !ents.contains(h.entity))

                    if delHs.nonEmpty then
                        item.holders --= delHs
                        logger.trace(s"STM entity removed [$z, reqId=${item.reqId}]")
                        stepLogEntity(delHs.toSeq.map(_.entity))

                squeezeEntities()

            lastUpdateTstamp = now
            replaceContext(stm.flatMap(_.holders.map(_.entity)))
            ack()
        }