This is an automated email from the ASF dual-hosted git repository.
aradzinski pushed a commit to branch NLPCRAFT-108
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-108 by this push:
new d10b808 WIP.
d10b808 is described below
commit d10b808ee108c318a27ca1202b89b8889ca37b29
Author: Aaron Radzinski <[email protected]>
AuthorDate: Tue Sep 15 21:22:56 2020 -0700
WIP.
---
.../apache/nlpcraft/common/ansi/NCAnsiColor.scala | 3 +-
.../nlpcraft/common/ascii/NCAsciiTable.scala | 21 +--
.../nlpcraft/examples/weather/weather_model.json | 2 -
.../nlpcraft/model/intent/utils/NCDslIntent.scala | 12 +-
.../org/apache/nlpcraft/probe/NCProbeBoot.scala | 4 +-
.../nlpcraft/probe/mgrs/model/NCModelManager.scala | 2 +-
.../org/apache/nlpcraft/server/NCServer.scala | 2 +-
.../nlpcraft/server/ctrl/NCControlManager.scala | 171 ---------------------
.../nlpcraft/common/ascii/NCAsciiTableSpec.scala | 18 ++-
9 files changed, 39 insertions(+), 196 deletions(-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiColor.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiColor.scala
index 7f9ff9e..2b2c81b 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiColor.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiColor.scala
@@ -47,9 +47,10 @@ trait NCAnsiColor {
private final val INVISIBLE = "\u001b[8m"
// Enabled by default.
+ // NOTE: it's not static as it can be changed at runtime.
private final val PROP = "NLPCRAFT_ANSI_COLOR_DISABLED"
- private def isEnabled: Boolean = !U.isSysEnvTrue(PROP)
+ def isEnabled: Boolean = !U.isSysEnvTrue(PROP)
def ansiBlackFg: String = if (isEnabled) BLACK else ""
def ansiBlackBg: String = if (isEnabled) BLACK_B else ""
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
index c5f0ba7..f8d645c 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
@@ -92,14 +92,10 @@ class NCAsciiTable {
*/
private sealed case class Cell(style: Style, lines: Seq[String]) {
// Cell's calculated width including padding.
- lazy val width: Int =
- if (height > 0)
- style.padding + lines.max(Ordering.by[String,
Int](stripAnsi(_).length)).length
- else
- style.padding
+ lazy val width: Int = style.padding + (if (height > 0)
lines.map(stripAnsi(_).length).max else 0)
// Gets height of the cell.
- def height: Int = lines.length
+ lazy val height: Int = lines.length
}
/**
@@ -312,6 +308,7 @@ class NCAsciiTable {
* @param lines
* @return
*/
+ // TODO: https://issues.apache.org/jira/browse/NLPCRAFT-125
private def breakUpByNearestSpace(maxWidth: Int, lines: Seq[String]):
Seq[String] =
lines.flatMap(line ⇒ {
if (line.isEmpty)
@@ -324,19 +321,17 @@ class NCAsciiTable {
var start = 0
var lastSpace = -1
var curr = 0
- val len = stripAnsi(line).length
+ val len = line.length
- def addLine(line: String) =
- if (buf.isEmpty)
- buf += line
- else
- buf += (space(leader) + line)
+ def addLine(s: String): Unit =
+ buf += (if (buf.isEmpty) s else space(leader) + s)
while (curr < len) {
if (curr - start > maxWidth) {
val end = if (lastSpace == -1) curr else lastSpace + 1
/* Keep space at the end of the line. */
addLine(line.substring(start, end))
+
start = end
}
@@ -349,7 +344,7 @@ class NCAsciiTable {
if (start < len) {
val lastLine = line.substring(start)
- if (lastLine.nonEmpty) {
+ if (lastLine.trim.nonEmpty) {
addLine(lastLine)
}
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/weather/weather_model.json
b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/weather/weather_model.json
index d6fcec1..b5d6577 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/weather/weather_model.json
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/weather/weather_model.json
@@ -38,7 +38,6 @@
"indicator"
],
"synonyms": [
- "A",
"{history|past|previous}"
]
},
@@ -49,7 +48,6 @@
"indicator"
],
"synonyms": [
- "A",
"{future|forecast|prognosis|prediction}"
]
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/NCDslIntent.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/NCDslIntent.scala
index 936dd63..945439f 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/NCDslIntent.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/NCDslIntent.scala
@@ -34,6 +34,14 @@ case class NCDslIntent(id: String, conv: Boolean, ordered:
Boolean, flow: Array[
*
* @return Full intent string representation in text DSL format.
*/
- def toDslString: String =
- s"intent=$id conv=$conv ordered=$ordered flow='${flow.mkString(" >>
")}' ${terms.mkString(" ")}"
+ def toDslString: String = {
+ val convStr = if (conv) "" else " conv=false"
+ val orderedStr = if (!ordered) "" else " ordered=true"
+ val flowStr = flow.mkString(" >> ") match {
+ case s: String if s.nonEmpty ⇒ s" flow=$s"
+ case _ ⇒ ""
+ }
+
+ s"intent=$id$convStr$orderedStr$flowStr ${terms.mkString(" ")}"
+ }
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
index ab8d227..bbedff5 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
@@ -368,7 +368,7 @@ private [probe] object NCProbeBoot extends LazyLogging with
NCOpenCensusTrace {
tbl += ("Down-Link", cfg.downLinkString)
tbl += ("Up-Link", cfg.upLinkString)
tbl += ("Lifecycle", cfg.lifecycle)
- tbl += ("Models" , cfg.modelsSeq)
+ tbl += (s"Models (${cfg.modelsSeq.size})" , cfg.modelsSeq)
tbl += ("JARs Folder", cfg.jarsFolder.getOrElse(""))
tbl.info(logger, Some("Probe Configuration:"))
@@ -384,7 +384,7 @@ private [probe] object NCProbeBoot extends LazyLogging with
NCOpenCensusTrace {
tbl.margin(top = 1)
- tbl += s"Probe started $dur"
+ tbl += s"Probe started $ansiBlueFg$dur$ansiReset"
tbl.info(logger)
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/model/NCModelManager.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/model/NCModelManager.scala
index 26b463f..b220a8c 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/model/NCModelManager.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/model/NCModelManager.scala
@@ -59,7 +59,7 @@ object NCModelManager extends NCService with DecorateAsScala {
mdl.getVersion,
w.elements.keySet.size,
synCnt,
- w.intents.size
+ w.intents.map(_.toDslString)
)
})
}
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
index 53927f1..be565bb 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
@@ -145,7 +145,7 @@ object NCServer extends App with NCIgniteInstance with
LazyLogging with NCOpenCe
tbl.margin(top = 1, bottom = 1)
- tbl += s"Server started $dur"
+ tbl += s"Server started $ansiBlueFg$dur$ansiReset"
tbl.info(logger)
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/ctrl/NCControlManager.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/ctrl/NCControlManager.scala
deleted file mode 100644
index 8984867..0000000
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/ctrl/NCControlManager.scala
+++ /dev/null
@@ -1,171 +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
- *
- * 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.
- */
-
-package org.apache.nlpcraft.server.ctrl
-
-import java.io.{EOFException, InterruptedIOException}
-import java.net.{InetSocketAddress, ServerSocket, Socket}
-import java.util.concurrent.atomic.AtomicBoolean
-
-import io.opencensus.trace.Span
-import org.apache.nlpcraft.common._
-import org.apache.nlpcraft.common.socket.NCSocket
-
-import scala.concurrent.ExecutionContext.Implicits.global
-import scala.concurrent.Future
-import scala.util.{Failure, Success}
-
-/**
- * Local host control protocol (used by `nlpcraft.{sh|cmd}` script.
- */
-object NCControlManager extends NCService {
- private final val PORT = 43011
- private final val LOCALHOST = "127.0.0.1"
- private final val SO_TIMEOUT = 5000
-
- @volatile private var srvThread: Thread = _
- @volatile private var isStopping: AtomicBoolean = _
-
- override def stop(parent: Span): Unit =
- startScopedSpan("start", parent) { _ ⇒
- isStopping = new AtomicBoolean(true)
-
- U.stopThread(srvThread)
-
- super.stop()
- }
-
- override def start(parent: Span = null): NCService =
- startScopedSpan("start", parent, "endpoint" → s"$LOCALHOST:$PORT") { _
⇒
- isStopping = new AtomicBoolean(false)
-
- srvThread = startServer()
-
- srvThread.start()
-
- this
- }
-
- /**
- *
- * @param sock
- */
- private def processSocket(sock: NCSocket): Unit = {
-
- }
-
- /**
- *
- */
- private def startServer(): Thread = {
- new Thread(s"ctrl-mgr") {
- private final val thName = getName
- private var srv: ServerSocket = _
- @volatile private var stopped = false
-
- override def isInterrupted: Boolean =
- super.isInterrupted || stopped
-
- override def interrupt(): Unit = {
- super.interrupt()
-
- U.close(srv)
-
- stopped = true
- }
-
- override def run(): Unit = {
- try {
- body()
- }
- catch {
- case _: InterruptedException ⇒ logger.trace(s"Thread
interrupted: $thName")
- case e: Throwable ⇒
- U.prettyError(
- logger,
- s"Unexpected error during '$thName' thread
execution:",
- e
- )
- }
- finally
- stopped = true
- }
-
- private def body(): Unit =
- while (!isInterrupted)
- try {
- srv = new ServerSocket()
-
- srv.bind(new InetSocketAddress(LOCALHOST, PORT))
- srv.setSoTimeout(SO_TIMEOUT)
-
- logger.info(s"Control server is listening on
'$LOCALHOST:$PORT'")
-
- while (!isInterrupted) {
- var sock: Socket = null
-
- try {
- sock = srv.accept()
-
- logger.trace(s"Control server accepted new
connection from: ${sock.getRemoteSocketAddress}")
- }
- catch {
- case _: InterruptedIOException ⇒ // No-op.
-
- // Note that server socket must be closed and
created again.
- // So, error should be thrown.
- case e: Exception ⇒
- U.close(sock)
-
- throw e
- }
-
- if (sock != null) {
- val fut = Future {
- processSocket(NCSocket(sock))
- }
-
- fut.onComplete {
- case Success(_) ⇒ // No-op.
-
- case Failure(e: NCE) ⇒
logger.warn(e.getMessage, e)
- case Failure(_: EOFException) ⇒ () // Just
ignoring.
- case Failure(e: Throwable) ⇒
logger.warn(s"Ignoring socket error: ${e.getLocalizedMessage}")
- }
- }
- }
- }
- catch {
- case e: Exception ⇒
- if (!isStopping.get) {
- // Release socket asap.
- U.close(srv)
-
- val ms = Config.reconnectTimeoutMs
-
- // Server socket error must be logged.
- logger.warn(s"$name server error, re-starting
in ${ms / 1000} sec.", e)
-
- U.sleep(ms)
- }
- }
- finally {
- U.close(srv)
- }
- }
- }
-}
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/common/ascii/NCAsciiTableSpec.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/common/ascii/NCAsciiTableSpec.scala
index 6918c91..2c26ecb 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/common/ascii/NCAsciiTableSpec.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/common/ascii/NCAsciiTableSpec.scala
@@ -18,6 +18,7 @@
package org.apache.nlpcraft.common.ascii
import org.junit.jupiter.api.Test
+import org.apache.nlpcraft.common.ansi.NCAnsiColor._
/**
* Test for ASCII text table.
@@ -37,7 +38,18 @@ class NCAsciiTableSpec {
t #= ("Header 1", Seq("Header 2.1", "Header 2.2"), "Header 3")
t += ("Row 1", Seq("Row 2"), Seq("Row 3.1", "Row 3.2"))
t += ("1234567890zxcvbnmasdASDFGHJKLQ", Seq("Row 2"), Seq("Row 3.1",
"Row 3.2"))
- t += (Seq("Row 31.1", "Row 31.2"), "Row 11", "Row 21")
+ t += (Seq(s"${ansiRedFg}Row 31.1$ansiReset", s"${ansiYellowFg}Row
31.2$ansiReset"), "Row 11", "Row 21")
+
+ t.render()
+ }
+
+ @Test
+ def testSmallTable(): Unit = {
+ val t = NCAsciiTable()
+
+ t.margin(top = 1, bottom = 1)
+
+ t += s"Small table with ANSI ${ansiBlueFg}code$ansiReset"
t.render()
}
@@ -61,7 +73,7 @@ class NCAsciiTableSpec {
@Test
def testWithVeryBigTable() {
- val NUM = 1000
+ val NUM = 10000
val start = System.currentTimeMillis()
@@ -72,7 +84,7 @@ class NCAsciiTableSpec {
t #= (Seq("Header 1", "Header 2", "Header 3"): _*)
for (i ← 0 to NUM)
- t += (s"Value 1:$i", s"Value 2:$i", s"Value 3:$i")
+ t += (s"${ansiGreenFg}Value 1:$i$ansiReset",
s"${ansiReversed}Value 2:$i$ansiReset", s"${ansiRedFg}Value 3:$i$ansiReset")
t.render()