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 f44416d WIP.
f44416d is described below
commit f44416da9adccbace4d761d1f39baaddcbefe12c
Author: Aaron Radzinski <[email protected]>
AuthorDate: Fri Sep 25 13:08:10 2020 -0700
WIP.
---
.../main/scala/org/apache/nlpcraft/NCStart.scala | 2 +-
.../org/apache/nlpcraft/common/ansi/NCAnsi.scala | 1 +
.../nlpcraft/common/ascii/NCAsciiTable.scala | 30 ++++++----
.../org/apache/nlpcraft/common/util/NCUtils.scala | 2 +-
.../model/intent/impl/NCIntentDslCompiler.scala | 7 ++-
.../model/tools/cmdline/NCCommandLine.scala | 69 +++++++++++++++++-----
.../org/apache/nlpcraft/probe/NCProbeBoot.scala | 2 +-
.../nlpcraft/probe/mgrs/model/NCModelManager.scala | 2 +-
.../probe/mgrs/nlp/NCProbeEnrichmentManager.scala | 2 +-
.../org/apache/nlpcraft/server/NCServer.scala | 2 +-
10 files changed, 83 insertions(+), 36 deletions(-)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCStart.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCStart.scala
index b5bcce1..bc289a5 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCStart.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCStart.scala
@@ -56,7 +56,7 @@ object NCStart extends App with LazyLogging {
U.NL +
U.asciiLogo() +
s"${U.NL}" +
- s"Version: $ansiBold${ver.version}${U.NL}$ansiReset" +
+ s"Version: ${ansiBold(ver.version)}${U.NL}" +
s"${NCVersion.copyright}${U.NL}"
)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
index a00b49e..053688f 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
@@ -81,6 +81,7 @@ sealed trait NCAnsi extends LazyLogging {
def ansiBlack(s: String): String = s"$ansiBlackFg$s$ansiReset"
def ansiWhite(s: String): String = s"$ansiWhiteFg$s$ansiReset"
def ansiBlue(s: String): String = s"$ansiBlueFg$s$ansiReset"
+ def ansiBold(s: String): String = s"$ansiBold$s$ansiReset"
}
object NCAnsi extends NCAnsi {
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 d314966..cc7e5af 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
@@ -18,6 +18,7 @@
package org.apache.nlpcraft.common.ascii
import java.io.{IOException, PrintStream}
+import java.util.regex.Pattern
import com.typesafe.scalalogging.Logger
import org.apache.nlpcraft.common._
@@ -32,6 +33,8 @@ import scala.collection.mutable
* `ASCII`-based table with minimal styling support.
*/
class NCAsciiTable {
+ private final val ANSI_SEQ = Pattern.compile("\u001B\\[[;\\d]*m")
+
/**
* Cell style.
*/
@@ -40,7 +43,7 @@ class NCAsciiTable {
var rightPad: Int = 1, // >= 0
var maxWidth: Int = Int.MaxValue, // > 0
var align: String = "center" // center, left, right
- ) {
+ ) {
/** Gets overall padding (left + right). */
def padding: Int = leftPad + rightPad
}
@@ -152,15 +155,16 @@ class NCAsciiTable {
// Dash drawing.
private def dash(ch: String, len: Int): String = ch * len
+
private def space(len: Int): String = " " * len
/**
* Sets table's margin.
*
- * @param top Top margin.
- * @param right Right margin.
+ * @param top Top margin.
+ * @param right Right margin.
* @param bottom Bottom margin.
- * @param left Left margin.
+ * @param left Left margin.
*/
def margin(top: Int = 0, right: Int = 0, bottom: Int = 0, left: Int = 0):
NCAsciiTable = {
margin = Margin(top, right, bottom, left)
@@ -208,7 +212,7 @@ class NCAsciiTable {
* @return
*/
private def stripAnsi(s: String): String =
- s.replaceAll("\u001B\\[[;\\d]*m", "")
+ ANSI_SEQ.matcher(s).replaceAll("")
/**
* Adds row (one or more row cells) with a given style.
@@ -231,10 +235,10 @@ class NCAsciiTable {
}
/**
- * Adds row.
- *
- * @param cells Row cells.
- */
+ * Adds row.
+ *
+ * @param cells Row cells.
+ */
def addRow(cells: java.util.List[Any]): NCAsciiTable = {
startRow()
@@ -274,10 +278,10 @@ class NCAsciiTable {
}
/**
- * Adds headers.
- *
- * @param cells Header cells.
- */
+ * Adds headers.
+ *
+ * @param cells Header cells.
+ */
def addHeaders(cells: java.util.List[Any]): NCAsciiTable = {
cells.asScala.foreach(addHeaderCell(_))
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
index d2f2dbf..b91f728 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
@@ -1406,7 +1406,7 @@ object NCUtils extends LazyLogging {
s"$errMsg $ansiCyanFg->$ansiReset ($fileName:$lineNum)"
msg.split("\n").foreach(line ⇒ {
- val s = s"${" " * indent}${if (first) ansiRed("+- ") else "
"}${line.trim}"
+ val s = s"${" " * indent}${if (first) ansiBlue("+- ") else "
"}${line.trim}"
if (err) logger.error(s) else logger.warn(s)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/NCIntentDslCompiler.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/NCIntentDslCompiler.scala
index 2b91361..abbc3aa 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/NCIntentDslCompiler.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/NCIntentDslCompiler.scala
@@ -24,6 +24,7 @@ import org.apache.nlpcraft.common.NCE
import org.apache.nlpcraft.model.NCToken
import org.apache.nlpcraft.model.intent.impl.antlr4.{NCIntentDslBaseListener,
NCIntentDslLexer, NCIntentDslParser}
import org.apache.nlpcraft.model.intent.utils.{NCDslFlowItem, NCDslIntent,
NCDslTerm, NCDslTokenPredicate}
+import org.apache.nlpcraft.common.ansi.NCAnsi._
import scala.collection.JavaConverters._
import scala.collection.mutable
@@ -329,9 +330,9 @@ object NCIntentDslCompiler extends LazyLogging {
e: RecognitionException): Unit = {
val errMsg = s"Intent DSL syntax error at line $line:$charPos -
$msg\n" +
- s" |- Model: $mdlId\n" +
- s" |- Intent: $dsl\n" +
- s" +- Error: ${makeCharPosPointer(dsl.length, charPos)}"
+ s" |- ${ansiCyan("Model:")} $mdlId\n" +
+ s" |- ${ansiCyan("Intent:")} $dsl\n" +
+ s" +- ${ansiCyan("Error:")} ${makeCharPosPointer(dsl.length,
charPos)}"
throw new NCE(errMsg)
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
index c1a31ce..28ad01a 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
@@ -46,6 +46,7 @@ object NCCommandLine extends App {
private final lazy val SCRIPT_NAME =
U.sysEnv("NLPCRAFT_CLI_SCRIPT").getOrElse(
s"nlpcraft.${if (SystemUtils.IS_OS_UNIX) "sh" else "cmd"}"
)
+ private final lazy val PROMPT = if (SCRIPT_NAME.endsWith("cmd")) ">" else
"$"
private final val T___ = " "
@@ -142,11 +143,11 @@ object NCCommandLine extends App {
),
examples = Seq(
Example(
- usage = s"$$ $SCRIPT_NAME start-server",
+ usage = s"$PROMPT $SCRIPT_NAME start-server",
desc = "Starts REST server with default configuration."
),
Example(
- usage = s"$$ $SCRIPT_NAME start-server
-c=/opt/nlpcraft/nlpcraft.conf",
+ usage = s"$PROMPT $SCRIPT_NAME start-server
-c=/opt/nlpcraft/nlpcraft.conf",
desc = "Starts REST server with alternative configuration
file."
)
)
@@ -161,7 +162,37 @@ object NCCommandLine extends App {
body = cmdNoAnsi,
examples = Seq(
Example(
- usage = s"$$ $SCRIPT_NAME help -c=repl no-ansi",
+ usage = s"$PROMPT $SCRIPT_NAME help -c=repl no-ansi",
+ desc = "Displays help for 'repl' commands without using
ANSI escape sequences."
+ )
+ )
+ ),
+ Command(
+ id = "ping-server",
+ names = Seq("ping-server"),
+ synopsis = s"Pings REST server.",
+ desc = Some(
+ s"REST server is pinged using '/health' REST call to check its
live status."
+ ),
+ body = cmdPingServer,
+ params = Seq(
+ Parameter(
+ id = "endpoint",
+ names = Seq("--endpoint", "-e"),
+ valueDesc = Some("{url}"),
+ optional = true,
+ desc = "Set of commands to show the manual for. Can be
used multiple times."
+ ),
+ Parameter(
+ id = "all",
+ names = Seq("--all", "-a"),
+ optional = true,
+ desc = "Flag to show full manual for all commands."
+ )
+ ),
+ examples = Seq(
+ Example(
+ usage = s"$PROMPT $SCRIPT_NAME help -c=repl no-ansi",
desc = "Displays help for 'repl' commands without using
ANSI escape sequences."
)
)
@@ -190,7 +221,7 @@ object NCCommandLine extends App {
names = Seq("--cmd", "-c"),
valueDesc = Some("{cmd}"),
optional = true,
- desc = "Set of commands to show the manual for."
+ desc = "Set of commands to show the manual for. Can be
used multiple times."
),
Parameter(
id = "all",
@@ -201,11 +232,11 @@ object NCCommandLine extends App {
),
examples = Seq(
Example(
- usage = s"$$ $SCRIPT_NAME help -c=repl --cmd=ver",
+ usage = s"$PROMPT $SCRIPT_NAME help -c=repl --cmd=ver",
desc = "Displays help for 'repl' and 'version' commands."
),
Example(
- usage = s"$$ $SCRIPT_NAME help -all",
+ usage = s"$PROMPT $SCRIPT_NAME help -all",
desc = "Displays help for all commands."
)
)
@@ -243,7 +274,7 @@ object NCCommandLine extends App {
)
).sortBy(_.id)
- private final val HELP_CMD = CMDS.find(_.id == "help").get
+ private final val HELP_CMD = CMDS.find(_.id == "help").get
private final val DFLT_CMD = CMDS.find(_.id == "repl").get
private final val NO_ANSI_CMD = CMDS.find(_.id == "no-ansi").get
@@ -285,6 +316,16 @@ object NCCommandLine extends App {
* @param cmd Command descriptor.
* @param params Parameters, if any, for this command.
*/
+ private def cmdPingServer(cmd: Command, params: Seq[String]): Unit = {
+ title()
+
+ // TODO
+ }
+
+ /**
+ * @param cmd Command descriptor.
+ * @param params Parameters, if any, for this command.
+ */
private def cmdStopServer(cmd: Command, params: Seq[String]): Unit = {
title()
@@ -334,13 +375,13 @@ object NCCommandLine extends App {
*/
def header(): Unit = log(
s"""|${U.asciiLogo()}
- |${ansiBold}NAME$ansiReset
+ |${ansiBold("NAME")}"
|$T___$SCRIPT_NAME - command line interface to control
NLPCraft.
|
- |${ansiBold}USAGE$ansiReset
+ |${ansiBold("USAGE")}
|$T___$SCRIPT_NAME [COMMAND] [PARAMETERS]
|
- |${ansiBold}COMMANDS$ansiReset""".stripMargin
+ |${ansiBold("COMMANDS")}""".stripMargin
)
/**
@@ -358,7 +399,7 @@ object NCCommandLine extends App {
if (cmd.params.nonEmpty) {
lines += ""
- lines += s"${ansiBold}PARAMETERS:$ansiReset"
+ lines += s"${ansiBold("PARAMETERS")}"
for (param ← cmd.params) {
val line =
@@ -381,10 +422,10 @@ object NCCommandLine extends App {
if (cmd.examples.nonEmpty) {
lines += ""
- lines += s"${ansiBold}EXAMPLES:$ansiReset"
+ lines += s"${ansiBold("EXAMPLES")}"
for (ex ← cmd.examples) {
- lines += s"$T___${ex.usage}"
+ lines += ansiYellow(s"$T___${ex.usage}")
lines += s"$T___$T___${ex.desc}"
}
}
@@ -513,7 +554,7 @@ object NCCommandLine extends App {
*
*/
private def errorHelp(): Unit =
- error(s"Run '${ansiCyan("$SCRIPT_NAME ${HELP_CMD.mainName")}' to read
the manual.")
+ error(s"Run '${ansiCyan(SCRIPT_NAME + " " + HELP_CMD.mainName)}' to
read the manual.")
/**
* Prints out the version and copyright title header.
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 d5d7177..0509216 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
@@ -350,7 +350,7 @@ private [probe] object NCProbeBoot extends LazyLogging with
NCOpenCensusTrace {
U.asciiLogo() +
s"${U.NL}" +
s"Embedded Data Probe${U.NL}" +
- s"Version: $ansiBold${ver.version}${U.NL}$ansiReset" +
+ s"Version: ${ansiBold(ver.version)}${U.NL}" +
s"${NCVersion.copyright}${U.NL}"
)
}
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 86bfa47..7c3c59c 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
@@ -65,7 +65,7 @@ object NCModelManager extends NCService with DecorateAsScala {
tbl += (
Seq(
s"${mdl.getName}",
- s"ID: $ansiBold${mdl.getId}$ansiReset, ver:
${mdl.getVersion}",
+ s"ID: ${ansiBold(mdl.getId)}, ver: ${mdl.getVersion}",
s"Elements: $elmCnt" + (if (elmCnt == 0) s"
${ansiRed("(!)")}" else ""),
s"Synonyms: $synCnt" + (if (synCnt == 0) s"
${ansiRed("(!)")}" else ""),
s"Intents: $intentCnt" + (if (intentCnt == 0) s"
${ansiRed("(!)")}" else "")
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala
index 304c026..d0c2dff 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala
@@ -227,7 +227,7 @@ object NCProbeEnrichmentManager extends NCService with
NCOpenCensusModelStats {
val tbl = NCAsciiTable()
- tbl += (s"${ansiBlueFg}Text$ansiReset", nlpSens.map(s ⇒
s"$ansiBold${ansiGreen(s.text)}"))
+ tbl += (s"${ansiBlueFg}Text$ansiReset", nlpSens.map(s ⇒
ansiGreen(s.text)))
tbl += (s"${ansiBlueFg}Model ID$ansiReset", mdlId)
tbl += (s"${ansiBlueFg}User ID$ansiReset", usrId)
tbl += (s"$ansiBlueFg First Name$ansiReset",
senMeta.getOrElse("FIRST_NAME", ""))
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 4b46039..f4f8ba5 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
@@ -77,7 +77,7 @@ object NCServer extends App with NCIgniteInstance with
LazyLogging with NCOpenCe
U.asciiLogo() +
s"${U.NL}" +
s"Server${U.NL}" +
- s"Version: $ansiBold${ver.version}${U.NL}$ansiReset" +
+ s"Version: ${ansiBold(ver.version)}${U.NL}" +
s"${NCVersion.copyright}${U.NL}"
)
}