You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by ar...@apache.org on 2020/12/16 20:57:38 UTC

[incubator-nlpcraft] branch NLPCRAFT-193 updated: Fixed NLPCRAFT-193.

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

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


The following commit(s) were added to refs/heads/NLPCRAFT-193 by this push:
     new 2ed70a1  Fixed NLPCRAFT-193.
2ed70a1 is described below

commit 2ed70a1b386635e5e0c94ee70b29218fceb3bd24
Author: Aaron Radzinski <ar...@datalingvo.com>
AuthorDate: Wed Dec 16 12:57:22 2020 -0800

    Fixed NLPCRAFT-193.
---
 .../model/tools/embedded/NCEmbeddedProbe.java      |  3 +-
 .../model/tools/test/NCTestAutoModelValidator.java | 90 ++++++++++------------
 .../test/impl/NCTestAutoModelValidatorImpl.scala   | 71 ++---------------
 .../org/apache/nlpcraft/probe/NCProbeBoot.scala    | 15 ++++
 .../scala/org/apache/nlpcraft/NCTestContext.scala  |  2 +-
 .../nlpcraft/model/intent/dsl/NCDslSpec.scala      |  2 +-
 6 files changed, 66 insertions(+), 117 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/embedded/NCEmbeddedProbe.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/embedded/NCEmbeddedProbe.java
index a25da3f..7786ea4 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/embedded/NCEmbeddedProbe.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/embedded/NCEmbeddedProbe.java
@@ -89,6 +89,7 @@ public class NCEmbeddedProbe {
      * @param mdlClasses Optional data model classes to be deployed by the embedded probe. If provided -
      *      these will override {@code nlpcraft.probe.models} configuration property. If {@code null} - the models
      *      defined in the configuration (default or provided via {@code cfgFile} parameter) will be used.
+     *      Each class should be a fully qualified class name.
      * @throws NCException Thrown in case of any errors starting the data probe.
      * @return Whether or not probe started ok.
      */
@@ -108,7 +109,7 @@ public class NCEmbeddedProbe {
      * @param upLink Probe up-link to the server override.
      * @param dnLink Probe down-link from the server override.
      * @param mdlClasses One or more data model classes overrides to be deployed by the embedded probe. At least
-     *      model must be provided.
+     *      model must be provided. Each class should be a fully qualified class name.
      * @throws NCException Thrown in case of any errors starting the data probe.
      * @return  Whether or not probe started ok.
      */
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/NCTestAutoModelValidator.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/NCTestAutoModelValidator.java
index a2635b2..21321a2 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/NCTestAutoModelValidator.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/NCTestAutoModelValidator.java
@@ -20,12 +20,9 @@ package org.apache.nlpcraft.model.tools.test;
 import org.apache.nlpcraft.model.*;
 import org.apache.nlpcraft.model.tools.test.impl.*;
 
-import java.util.*;
-
 /**
- * Auto-validator for models based on {@link NCIntentSample} annotations. This class takes one or more model IDs
- * (or class names) and performs validation. Validation consists of starting an embedded probe with a given model,
- * scanning for {@link NCIntentSample} annotations and their corresponding callback methods, submitting each sample input
+ * Data model auto-validator is based on {@link NCIntentSample} annotations. Validation consists of starting an embedded
+ * probe, scanning for {@link NCIntentSample} annotations and their corresponding callback methods, submitting each sample input
  * sentences from {@link NCIntentSample} annotation and checking that resulting intent matches the intent the sample
  * was attached to.
  * <p>
@@ -37,13 +34,13 @@ import java.util.*;
  * This class can be used in two modes:
  * <ul>
  *     <li>
- *         As a standalone application supplying model classes using <code>NLPCRAFT_TEST_MODELS</code> system property.
+ *         As a standalone application. See {@link #main(String[])} method for details. 
  *         In this mode it can be used to automatically test models from IDE, maven builds, etc. without
  *         creating a separate, dedicated unit test for it.
  *     </li>
  *     <li>
  *         As a utility class that can be called programmatically from other classes, e.g. unit tests. See
- *         various <code>isValid(...)</code> methods for more details.
+ *         {@link #isValid()} and {@link #isValid(Class)} methods for more details.
  *     </li>
  * </ul>
  * <p>
@@ -57,14 +54,24 @@ import java.util.*;
  * @see NCIntentRef
  */
 public class NCTestAutoModelValidator {
+    private final static String PROP_MODELS = "NLPCRAFT_TEST_MODELS";
+
     /**
      * Performs validation based on {@link NCIntentSample} annotations.
      * <p>
-     * Entry point for a standalone application that expects model classes supplied
-     * using <code>NLPCRAFT_TEST_MODELS</code> system property. This system property should have a comma separated
-     * list of data model class names. In this mode it can be used to automatically test
-     * models from IDE, maven builds, etc. without creating a separate, dedicated unit test for it.
-     * <p>
+     * This is an entry point for a standalone application that expects two system properties (both optional):
+     * <ul>
+     *     <li>
+     *         <code>NLPCRAFT_TEST_MODELS</code> - optional comma separated list of fully qualified data model class names to
+     *         test. Validator will start the embedded probe with these models as an override for
+     *         <code>'nlpcraft.probe.models'</code> configuration value. If not provided - the models defined in configuration (default or
+     *         provided via <code>NLPCRAFT_PROBE_CONFIG</code> property) will be used.
+     *     </li>
+     *     <li>
+     *         <code>NLPCRAFT_PROBE_CONFIG</code> - optional path to probe configuration file. If not provided -
+     *         the default NLPCraft configuration will be used.
+     *     </li>
+     * </ul>
      * Note that standard validation output will be printed out to the configured logger (e.g. log4j), if any.
      * 
      * @param args These arguments are ignored.
@@ -72,14 +79,26 @@ public class NCTestAutoModelValidator {
      *      output will be printed out to the configured logger.
      */
     public static void main(String[] args) throws Exception {
-        System.exit(NCTestAutoModelValidatorImpl.isValid(args) ? 0 : 1);
+        System.exit(NCTestAutoModelValidatorImpl.isValid() ? 0 : 1);
     }
 
     /**
-     * Performs validation based on {@link NCIntentSample} annotations. Similar to the standard
-     * <code>main(String[])</code>, this method expects model classes supplied
-     * using <code>NLPCRAFT_TEST_MODELS</code> system property. This system property should have a comma separated
-     * list of data model class names.
+     * Performs validation based on {@link NCIntentSample} annotations.
+     * <p>
+     * This method accepts two system properties (both optional):
+     * <ul>
+     *     <li>
+     *         <code>NLPCRAFT_TEST_MODELS</code> - optional comma separated list of fully qualified data model class names to
+     *         test. Validator will start the embedded probe with these models as an override for
+     *         <code>'nlpcraft.probe.models'</code> configuration value. If not provided - the models defined in configuration (default or
+     *         provided via <code>NLPCRAFT_PROBE_CONFIG</code> property) will be used.
+     *     </li>
+     *     <li>
+     *         <code>NLPCRAFT_PROBE_CONFIG</code> - optional path to probe configuration file. If not provided -
+     *         the default NLPCraft configuration will be used.
+     *     </li>
+     * </ul>
+     * Note that standard validation output will be printed out to the configured logger (e.g. log4j), if any.
      * 
      * @return <code>True</code> if no validation errors found, <code>false</code> otherwise. Note that
      *      standard validation output will be printed out to the configured logger (e.g. log4j), if any.
@@ -87,11 +106,14 @@ public class NCTestAutoModelValidator {
      *      output will be printed out to the configured logger (e.g. log4j), if any.
      */
     public static boolean isValid() throws Exception {
-        return NCTestAutoModelValidatorImpl.isValid(new String[] {});
+        return NCTestAutoModelValidatorImpl.isValid();
     }
 
     /**
      * Performs validation based on {@link NCIntentSample} annotations for given model.
+     * <p>
+     * This is a convenient shortcut that is equivalent to setting <code>NLPCRAFT_TEST_MODELS</code> system
+     * property (overriding any existing value) with given mode class name and calling {@link #isValid()} method.
      *
      * @param claxx Data model class.
      * @return <code>True</code> if no validation errors found, <code>false</code> otherwise. Note that
@@ -100,36 +122,8 @@ public class NCTestAutoModelValidator {
      *      output will be printed out to the configured logger (e.g. log4j), if any.
      */
     public static boolean isValid(Class<NCModel> claxx) throws Exception {
-        return NCTestAutoModelValidatorImpl.isValidForClass(claxx);
-    }
-
-    /**
-     * Performs validation based on {@link NCIntentSample} annotations for given models.
-     *
-     * @param mdlIds One or more ID of the model to validate.
-     * @return <code>True</code> if no validation errors found, <code>false</code> otherwise. Note that
-     *      standard validation output will be printed out to the configured logger (e.g. log4j), if any.
-     * @throws Exception Thrown in case of any unexpected errors during validation. Note that standard validation
-     *      output will be printed out to the configured logger (e.g. log4j), if any.
-     *
-     * @see NCModelView#getId()
-     */
-    public static boolean isValid(String... mdlIds) throws Exception {
-        return NCTestAutoModelValidatorImpl.isValidForModelIds(mdlIds);
-    }
+        System.setProperty(PROP_MODELS, claxx.getName());
 
-    /**
-     * Performs validation based on {@link NCIntentSample} annotations for given models.
-     *
-     * @param mdlIds Collection of model IDs to validate.
-     * @return <code>True</code> if no validation errors found, <code>false</code> otherwise. Note that
-     *      standard validation output will be printed out to the configured logger (e.g. log4j), if any.
-     * @throws Exception Thrown in case of any unexpected errors during validation. Note that standard validation
-     *      output will be printed out to the configured logger (e.g. log4j), if any.
-     *
-     * @see NCModelView#getId()
-     */
-    public static boolean isValid(Collection<String> mdlIds) throws Exception {
-        return NCTestAutoModelValidatorImpl.isValidForModelIds(mdlIds);
+        return NCTestAutoModelValidatorImpl.isValid();
     }
 }
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/impl/NCTestAutoModelValidatorImpl.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/impl/NCTestAutoModelValidatorImpl.scala
index 88c686e..2a72c77 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/impl/NCTestAutoModelValidatorImpl.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/impl/NCTestAutoModelValidatorImpl.scala
@@ -22,7 +22,6 @@ import org.apache.nlpcraft.common.ascii.NCAsciiTable
 import org.apache.nlpcraft.common._
 import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe
 import org.apache.nlpcraft.model.tools.test.NCTestClientBuilder
-import org.apache.nlpcraft.model._
 import org.apache.nlpcraft.probe.mgrs.model.NCModelManager
 
 /**
@@ -30,66 +29,19 @@ import org.apache.nlpcraft.probe.mgrs.model.NCModelManager
   */
 private [test] object NCTestAutoModelValidatorImpl extends LazyLogging {
     private final val PROP_MODELS = "NLPCRAFT_TEST_MODELS"
+    private final val PROP_PROBE_CFG = "NLPCRAFT_PROBE_CONFIG"
 
     /**
      *
-     * @param args
      * @throws Exception Thrown in case of any errors.
      * @return
      */
     @throws[Exception]
-    def isValid(args: Array[String]): Boolean =
-        U.sysEnv(PROP_MODELS) match {
-            case Some(p) ⇒ isValid(getClasses(p.split(",")))
-            case None ⇒ throw new IllegalStateException(s"System property '$PROP_MODELS' is not defined.")
-        }
+    def isValid: Boolean = {
+        val classes = U.sysEnv(PROP_MODELS).orNull
+        val cfgFile = U.sysEnv(PROP_PROBE_CFG).orNull
 
-    /**
-     *
-     * @param claxx
-     * @throws Exception Thrown in case of any errors.
-     * @return
-     */
-    @throws[Exception]
-    def isValidForClass(claxx: Class[_ <: NCModel]): Boolean = isValid(Seq(claxx))
-
-    /**
-     *
-     * @param mdlIds
-     * @throws Exception Thrown in case of any errors.
-     * @return
-     */
-    @throws[Exception]
-    def isValidForModelIds(mdlIds: String): Boolean = isValid(getClasses(mdlIds.split(",")))
-
-    /**
-     *
-     * @param mdlIds
-     * @throws Exception Thrown in case of any errors.
-     * @return
-     */
-    @throws[Exception]
-    def isValidForModelIds(mdlIds: java.util.Collection[String]): Boolean =
-        isValid(getClasses(mdlIds.toArray().asInstanceOf[Array[String]]))
-
-    /**
-     *
-     * @param mdlIds
-     * @throws Exception Thrown in case of any errors.
-     * @return
-     */
-    @throws[Exception]
-    def isValidForModelIds(mdlIds: Array[String]): Boolean = isValid(getClasses(mdlIds))
-
-    /**
-     *
-     * @param classes
-     * @throws Exception Thrown in case of any errors.
-     * @return
-     */
-    @throws[Exception]
-    private def isValid(classes: Seq[Class[_ <: NCModel]]): Boolean = {
-        if (NCEmbeddedProbe.start(classes: _*))
+        if (NCEmbeddedProbe.start(cfgFile, classes))
             try
                 process(NCModelManager.getAllModels().map(p ⇒ p.model.getId → p.samples.toMap).toMap.filter(_._2.nonEmpty))
             finally
@@ -159,17 +111,4 @@ private [test] object NCTestAutoModelValidatorImpl extends LazyLogging {
         
         failCnt == 0
     }
-
-    /**
-      *
-      * @param s
-      * @return
-      */
-    private def getClasses(s: Array[String]): Seq[Class[_ <: NCModel]] = {
-        val clsLdr = Thread.currentThread().getContextClassLoader
-
-        U.trimFilter(s).
-            map(clsLdr.loadClass).
-            map(_.asSubclass(classOf[NCModel]))
-    }
 }
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 bf1ffa7..96120b9 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
@@ -167,6 +167,21 @@ private [probe] object NCProbeBoot extends LazyLogging with NCOpenCensusTrace {
             Cfg.lifecycle
         )
     }
+
+    /**
+     *
+     * @param args
+     * @param fut
+     */
+    private [probe] def start(args: Array[String], fut: CompletableFuture[Integer]): Unit = {
+        checkStarted()
+
+        val cfg = initializeConfig(args, None)
+
+        new Thread() {
+            override def run(): Unit = start0(cfg, fut)
+        }.start()
+    }
     
     /**
       *
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContext.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContext.scala
index 1c3eccc..a48c934 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContext.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContext.scala
@@ -74,7 +74,7 @@ abstract class NCTestContext {
 
                 probeStarted = false
 
-                if (NCEmbeddedProbe.start(ann.model())) {
+                if (NCEmbeddedProbe.start(null, ann.model().getName)) {
                     probeStarted = true
                 
                     if (ann.startClient()) {
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslSpec.scala
index 0da445d..8dc75c5 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslSpec.scala
@@ -36,7 +36,7 @@ class NCDslSpec {
     @throws[IOException]
     private[dsl] def setUp(): Unit = {
         // Start embedded probe with the test model.
-        if (NCEmbeddedProbe.start(classOf[NCDslTestModel])) {
+        if (NCEmbeddedProbe.start(null, classOf[NCDslTestModel].getName)) {
             cli = new NCTestClientBuilder().newBuilder.build
 
             cli.open("nlpcraft.dsl.test")