This is an automated email from the ASF dual-hosted git repository. aradzinski pushed a commit to branch NLPCRAFT-13 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-13 by this push: new 319e7d6 WIP. 319e7d6 is described below commit 319e7d648dde23bb0d626f7605635efdcd9233d1 Author: Aaron Radzinzski <aradzin...@datalingvo.com> AuthorDate: Tue Mar 24 15:53:41 2020 -0700 WIP. --- docs/data-model.html | 57 --------- docs/integrations.html | 19 --- .../apache/nlpcraft/examples/sql/SqlModel.scala | 10 -- .../nlpcraft/examples/sql/db/SqlBuilder.scala | 32 ++--- .../sql/db/SqlCondition.java} | 9 +- .../sql/db/SqlFunction.java} | 5 +- .../sql/db/SqlInCondition.java} | 7 +- .../sql/db/SqlSimpleCondition.java} | 4 +- .../apache/nlpcraft/examples/sql/sql_model.yaml | 1 - .../org/apache/nlpcraft/model/NCModelView.java | 3 - .../apache/nlpcraft/model/impl/NCTokenLogger.scala | 14 --- .../model/tools/sqlgen/NCSqlExtractor.java | 40 +----- .../model/tools/sqlgen/NCSqlModelGenerator.java | 7 +- .../nlpcraft/model/tools/sqlgen/NCSqlSort.java | 3 +- .../model/tools/sqlgen/impl/NCSqlBeans.scala | 42 ------- .../tools/sqlgen/impl/NCSqlExtractorImpl.scala | 111 +---------------- .../org/apache/nlpcraft/probe/NCProbeBoot.scala | 1 - .../probe/mgrs/nlp/NCProbeEnrichmentManager.scala | 2 - .../aggregation/NCAggregationEnricher.scala | 135 --------------------- .../nlp/enrichers/post/NCPostEnrichProcessor.scala | 11 +- .../mgrs/nlp/enrichers/NCEnrichersTestBeans.scala | 35 +----- 21 files changed, 37 insertions(+), 511 deletions(-) diff --git a/docs/data-model.html b/docs/data-model.html index 5a29711..bebff85 100644 --- a/docs/data-model.html +++ b/docs/data-model.html @@ -1863,25 +1863,6 @@ intents: </td> </tr> <tr> - <td><code>nlpcraft:aggregation</code></td> - <td> - This token denotes an aggregation function: - <code>min</code>, - <code>max</code>, - <code>avg</code>, - <code>sum</code>, or - <code>group</code>. Note this token always need another token that it references. - </td> - <td> - <ul> - <li> - Show the <code><b>average</b></code> <code>price</code> (assuming that 'price' is also - detected as a token). - </li> - </ul> - </td> - </tr> - <tr> <td><code>nlpcraft:relation</code></td> <td> This token denotes a relation function: @@ -2015,7 +1996,6 @@ intents: <li><a href="#nlpcraft:coordinate">Token ID <code>nlpcraft:coordinate</code></a></li> <li><a href="#nlpcraft:sort">Token ID <code>nlpcraft:sort</code></a></li> <li><a href="#nlpcraft:limit">Token ID <code>nlpcraft:limit</code></a></li> - <li><a href="#nlpcraft:aggregation">Token ID <code>nlpcraft:aggregation</code></a></li> <li><a href="#nlpcraft:relation">Token ID <code>nlpcraft:relation</code></a></li> <li><a href="#stanford:xxx">Token ID <code>stanford:xxx</code></a></li> <li><a href="#spacy:xxx">Token ID <code>spacy:xxx</code></a></li> @@ -3000,43 +2980,6 @@ intents: </tbody> </table> <br/> - <span id="nlpcraft:aggregation" class="section-sub-title">Token ID <code>nlpcraft:aggregation</code></span> - <p> - This token denotes an aggregation function. - Additionally to <code><b>nlpcraft:nlp:xxx</b></code> properties this type of token will have the following - metadata properties all of which are <b>mandatory</b> unless otherwise noted. - </p> - <table class="gradient-table"> - <thead> - <tr> - <th>Property</th> - <th>Java Type</th> - <th>Description</th> - </tr> - </thead> - <tbody> - <tr> - <td><code><b>nlpcraft:aggregation:indexes</b></code></td> - <td><code>java.util.List<Integer></code></td> - <td>Index (always only one) of the reference token (i.e. the token being aggregated on).</td> - </tr> - <tr> - <td><code><b>nlpcraft:aggregation:type</b></code></td> - <td><code>java.lang.String</code></td> - <td> - Type of the aggregation. One of the following values: - <ul> - <li><code>sum</code></li> - <li><code>min</code></li> - <li><code>max</code></li> - <li><code>avg</code></li> - <li><code>group</code></li> - </ul> - </td> - </tr> - </tbody> - </table> - <br/> <span id="nlpcraft:sort" class="section-sub-title">Token ID <code>nlpcraft:sort</code></span> <p> This token denotes a sorting or ordering function. diff --git a/docs/integrations.html b/docs/integrations.html index 857c5d8..61d7c8b 100644 --- a/docs/integrations.html +++ b/docs/integrations.html @@ -284,25 +284,6 @@ id: integrations </td> </tr> <tr> - <td><code>nlpcraft:aggregation</code></td> - <td> - This token denotes an aggregation function: - <code>min</code>, - <code>max</code>, - <code>avg</code>, - <code>sum</code>, or - <code>group</code>. Note this token always need another token that it references. - </td> - <td> - <ul> - <li> - Show the <code><b>average</b></code> <code>price</code> (assuming that 'price' is also - detected as a token). - </li> - </ul> - </td> - </tr> - <tr> <td><code>nlpcraft:relation</code></td> <td> This token denotes a relation function: diff --git a/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala b/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala index 5232364..d47b790 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala +++ b/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala @@ -89,8 +89,6 @@ class SqlModel extends NCModelFileAdapter("org/apache/nlpcraft/examples/sql/sql_ "term(condDates)={id == 'condition:date'}[0,7] " + "term(condFreeDate)={id == 'nlpcraft:date'}? " + // Simplified version of aggregation wih single function column and one optional group by column. - "term(aggrFunc)={id == 'nlpcraft:aggregation' && ~nlpcraft:aggregation:type != 'group'}? " + - "term(aggrGroup)={id == 'nlpcraft:aggregation' && ~nlpcraft:aggregation:type == 'group'}? " + "term(sort)={id == 'nlpcraft:sort'}? " + "term(limit)={id == 'nlpcraft:limit'}?" ) @@ -102,8 +100,6 @@ class SqlModel extends NCModelFileAdapter("org/apache/nlpcraft/examples/sql/sql_ @NCIntentTerm("condVals") condVals: Seq[NCToken], @NCIntentTerm("condDates") condDates: Seq[NCToken], @NCIntentTerm("condFreeDate") freeDateOpt: Option[NCToken], - @NCIntentTerm("aggrFunc") aggrFuncOpt: Option[NCToken], - @NCIntentTerm("aggrGroup") aggrGroupOpt: Option[NCToken], @NCIntentTerm("sort") sortTokOpt: Option[NCToken], @NCIntentTerm("limit") limitTokOpt: Option[NCToken] ): NCResult = { @@ -128,12 +124,6 @@ class SqlModel extends NCModelFileAdapter("org/apache/nlpcraft/examples/sql/sql_ ): _* ). withSorts(sortTokOpt.map(sortTok ⇒ ext.extractSort(sortTok)).toSeq: _*). - withAggregate( - if (aggrFuncOpt.isDefined || aggrGroupOpt.isDefined) - ext.extractAggregate(aggrFuncOpt.orNull, aggrGroupOpt.orNull) - else - null - ). withLimit(limitTokOpt.flatMap(limitTok ⇒ Some(ext.extractLimit(limitTok))).orNull). withFreeDateRange(freeDateOpt.flatMap(freeDate ⇒ Some(ext.extractDateRange(freeDate))).orNull). build() diff --git a/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlBuilder.scala b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlBuilder.scala index 02e4d37..b127183 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlBuilder.scala +++ b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlBuilder.scala @@ -66,7 +66,7 @@ case class SqlBuilder(schema: NCSqlSchema) extends LazyLogging { private var tabs: Seq[NCSqlTable] = Seq.empty private var cols: Seq[NCSqlColumn] = Seq.empty - private var conds: Seq[NCSqlCondition] = Seq.empty + private var conds: Seq[SqlCondition] = Seq.empty private var sorts: Seq[NCSqlSort] = Seq.empty private var freeDateRangeOpt: Option[NCSqlDateRange] = None private var aggregationOpt: Option[NCSqlAggregate] = None @@ -75,7 +75,7 @@ case class SqlBuilder(schema: NCSqlSchema) extends LazyLogging { private def sql(clause: NCSqlTable): String = clause.getTable private def sql(clause: NCSqlColumn): String = s"${clause.getTable}.${clause.getColumn}" private def sql(clause: NCSqlSort): String = s"${sql(clause.getColumn)} ${if (clause.isAscending) "ASC" else "DESC"}" - private def sql(clause: NCSqlFunction): String = s"${clause.getFunction.toLowerCase}(${sql(clause.getColumn)})" + private def sql(clause: SqlFunction): String = s"${clause.getFunction.toLowerCase}(${sql(clause.getColumn)})" // TODO: implement based on join type. private def sql(clause: NCSqlJoin): String = @@ -84,16 +84,16 @@ case class SqlBuilder(schema: NCSqlSchema) extends LazyLogging { map { case (fromCol, toCol) ⇒ s"${ clause.getFromTable}.$fromCol = ${clause.getToTable}.$toCol" }. mkString(" AND ") - private def sql(clause: NCSqlInCondition): String = + private def sql(clause: SqlInCondition): String = s"${sql(clause.getColumn)} IN (${(0 until clause.getValues.size()).map(_ ⇒ "?").mkString(",")})" - private def sql(clause: NCSqlSimpleCondition): String = s"${sql(clause.getColumn)} ${clause.getOperation} ?" + private def sql(clause: SqlSimpleCondition): String = s"${sql(clause.getColumn)} ${clause.getOperation} ?" @throws[RuntimeException] - private def sql(clause: NCSqlCondition): String = + private def sql(clause: SqlCondition): String = clause match { - case x: NCSqlSimpleCondition ⇒ sql(x) - case x: NCSqlInCondition ⇒ sql(x) + case x: SqlSimpleCondition ⇒ sql(x) + case x: SqlInCondition ⇒ sql(x) case _ ⇒ throw new RuntimeException("Unexpected condition") } @@ -197,12 +197,12 @@ case class SqlBuilder(schema: NCSqlSchema) extends LazyLogging { } private def extendConditions( - conds: Seq[NCSqlCondition], + conds: Seq[SqlCondition], extTabs: Seq[NCSqlTable], initTabs: Seq[NCSqlTable], freeDateColOpt: Option[NCSqlColumn] ): (Seq[String], Seq[Object]) = { - val (freeDateConds, freeDateParams): (Seq[NCSqlCondition], Seq[Object]) = + val (freeDateConds, freeDateParams): (Seq[SqlCondition], Seq[Object]) = freeDateColOpt match { case Some(col) ⇒ val range = freeDateRangeOpt.getOrElse(throw new AssertionError("Missed date range")) @@ -221,8 +221,8 @@ case class SqlBuilder(schema: NCSqlSchema) extends LazyLogging { etNames.flatMap(t ⇒ schemaJoins.filter(j ⇒ j.getFromTable == t && etNames.contains(j.getToTable))).map(sql), conds.flatMap(p ⇒ p match { - case x: NCSqlSimpleCondition ⇒ Seq(x.getValue) - case x: NCSqlInCondition ⇒ x.getValues.asScala + case x: SqlSimpleCondition ⇒ Seq(x.getValue) + case x: SqlInCondition ⇒ x.getValues.asScala case _ ⇒ throw new AssertionError(s"Unexpected condition: $p") } ) ++ freeDateParams @@ -241,18 +241,12 @@ case class SqlBuilder(schema: NCSqlSchema) extends LazyLogging { this } - def withAndConditions(conds: NCSqlCondition*): SqlBuilder = { + def withAndConditions(conds: SqlCondition*): SqlBuilder = { this.conds ++= conds this } - def withAggregate(aggregation: NCSqlAggregate): SqlBuilder = { - this.aggregationOpt = Option(aggregation) - - this - } - def withFreeDateRange(freeDateRange: NCSqlDateRange): SqlBuilder = { this.freeDateRangeOpt = Option(freeDateRange) @@ -281,7 +275,7 @@ case class SqlBuilder(schema: NCSqlSchema) extends LazyLogging { var tabsNorm = mutable.ArrayBuffer.empty[NCSqlTable] ++ this.tabs var colsNorm = mutable.ArrayBuffer.empty[NCSqlColumn] ++ this.cols - var condsNorm = mutable.ArrayBuffer.empty[NCSqlCondition] ++ this.conds + var condsNorm = mutable.ArrayBuffer.empty[SqlCondition] ++ this.conds var sortsNorm = mutable.ArrayBuffer.empty[NCSqlSort] ++ this.sorts colsNorm.foreach(col ⇒ tabsNorm += schemaTabsByNames(col.getTable)) diff --git a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlCondition.java b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlCondition.java similarity index 86% rename from src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlCondition.java rename to src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlCondition.java index f0f4490..385a44c 100644 --- a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlCondition.java +++ b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlCondition.java @@ -15,19 +15,20 @@ * limitations under the License. */ -package org.apache.nlpcraft.model.tools.sqlgen; +package org.apache.nlpcraft.examples.sql.db; import org.apache.nlpcraft.model.*; +import org.apache.nlpcraft.model.tools.sqlgen.*; /** * Object presentation of the base SQL condition. * * @see NCSqlSchemaBuilder#makeSchema(NCModel) * @see NCSqlExtractorBuilder#build(NCSqlSchema, NCVariant) - * @see NCSqlInCondition - * @see NCSqlSimpleCondition + * @see SqlInCondition + * @see SqlSimpleCondition */ -public interface NCSqlCondition { +public interface SqlCondition { /** * Gets the SQL column this condition is on. * diff --git a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlFunction.java b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlFunction.java similarity index 90% rename from src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlFunction.java rename to src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlFunction.java index 289cba2..0c15b0d 100644 --- a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlFunction.java +++ b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlFunction.java @@ -15,16 +15,17 @@ * limitations under the License. */ -package org.apache.nlpcraft.model.tools.sqlgen; +package org.apache.nlpcraft.examples.sql.db; import org.apache.nlpcraft.model.*; +import org.apache.nlpcraft.model.tools.sqlgen.*; /** * Object presentation of SQL function. * * @see NCSqlSchemaBuilder#makeSchema(NCModel) */ -public interface NCSqlFunction { +public interface SqlFunction { /** * Gets SQL column this function is referring to. * diff --git a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlInCondition.java b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlInCondition.java similarity index 86% rename from src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlInCondition.java rename to src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlInCondition.java index 954df42..f024c18 100644 --- a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlInCondition.java +++ b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlInCondition.java @@ -15,17 +15,18 @@ * limitations under the License. */ -package org.apache.nlpcraft.model.tools.sqlgen; +package org.apache.nlpcraft.examples.sql.db; import org.apache.nlpcraft.model.*; -import java.util.List; +import org.apache.nlpcraft.model.tools.sqlgen.*; +import java.util.*; /** * Object presentation of SQL IN condition. * * @see NCSqlSchemaBuilder#makeSchema(NCModel) */ -public interface NCSqlInCondition extends NCSqlCondition { +public interface SqlInCondition extends SqlCondition { /** * Gets the list of SQL IN values. * diff --git a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlSimpleCondition.java b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlSimpleCondition.java similarity index 88% rename from src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlSimpleCondition.java rename to src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlSimpleCondition.java index 2e04ff2..48d6a95 100644 --- a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlSimpleCondition.java +++ b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlSimpleCondition.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package org.apache.nlpcraft.model.tools.sqlgen; +package org.apache.nlpcraft.examples.sql.db; -public interface NCSqlSimpleCondition extends NCSqlCondition { +public interface SqlSimpleCondition extends SqlCondition { String getOperation(); Object getValue(); } \ No newline at end of file diff --git a/src/main/scala/org/apache/nlpcraft/examples/sql/sql_model.yaml b/src/main/scala/org/apache/nlpcraft/examples/sql/sql_model.yaml index d934131..8cfd6b6 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/sql/sql_model.yaml +++ b/src/main/scala/org/apache/nlpcraft/examples/sql/sql_model.yaml @@ -1466,7 +1466,6 @@ elements: enabledBuiltInTokens: - "nlpcraft:date" - "nlpcraft:num" - - "nlpcraft:aggregation" - "nlpcraft:relation" - "nlpcraft:limit" - "nlpcraft:sort" diff --git a/src/main/scala/org/apache/nlpcraft/model/NCModelView.java b/src/main/scala/org/apache/nlpcraft/model/NCModelView.java index 72320df..a6c03e3 100644 --- a/src/main/scala/org/apache/nlpcraft/model/NCModelView.java +++ b/src/main/scala/org/apache/nlpcraft/model/NCModelView.java @@ -134,7 +134,6 @@ public interface NCModelView extends NCMetadata { * <li><code>nlpcraft:city</code></li> * <li><code>nlpcraft:num</code></li> * <li><code>nlpcraft:coordinate</code></li> - * <li><code>nlpcraft:aggregation</code></li> * <li><code>nlpcraft:relation</code></li> * <li><code>nlpcraft:sort</code></li> * <li><code>nlpcraft:limit</code></li> @@ -152,7 +151,6 @@ public interface NCModelView extends NCMetadata { "nlpcraft:city", "nlpcraft:num", "nlpcraft:coordinate", - "nlpcraft:aggregation", "nlpcraft:relation", "nlpcraft:sort", "nlpcraft:limit" @@ -891,7 +889,6 @@ public interface NCModelView extends NCMetadata { * <li><code>nlpcraft:city</code></li> * <li><code>nlpcraft:num</code></li> * <li><code>nlpcraft:coordinate</code></li> - * <li><code>nlpcraft:aggregation</code></li> * <li><code>nlpcraft:relation</code></li> * <li><code>nlpcraft:sort</code></li> * <li><code>nlpcraft:limit</code></li> diff --git a/src/main/scala/org/apache/nlpcraft/model/impl/NCTokenLogger.scala b/src/main/scala/org/apache/nlpcraft/model/impl/NCTokenLogger.scala index a0c3e15..00e3140 100644 --- a/src/main/scala/org/apache/nlpcraft/model/impl/NCTokenLogger.scala +++ b/src/main/scala/org/apache/nlpcraft/model/impl/NCTokenLogger.scala @@ -47,7 +47,6 @@ object NCTokenLogger extends LazyLogging { "nlpcraft:city", "nlpcraft:date", "nlpcraft:num", - "nlpcraft:aggregation", "nlpcraft:relation", "nlpcraft:sort", "nlpcraft:limit", @@ -80,7 +79,6 @@ object NCTokenLogger extends LazyLogging { "nlpcraft:region" → Seq("region", "country", "subcontinent", "continent", "metro"), "nlpcraft:city" → Seq("city", "latitude", "longitude", "region", "country", "subcontinent", "continent"), "nlpcraft:date" → Seq("from", "to", "periods"), - "nlpcraft:aggregation" → Seq("type", "indexes", "note"), "nlpcraft:relation" → Seq("type", "indexes", "note"), "nlpcraft:sort" → Seq("asc", "indexes", "note"), "nlpcraft:limit" → Seq("limit", "indexes", "asc", "note") @@ -205,12 +203,6 @@ object NCTokenLogger extends LazyLogging { s"range=$r, periods=$ps" - case "nlpcraft:aggregation" ⇒ - val t = mkString("type") - val note = mkString("note") - - s"type=$t, indexes=[$getIndexes], note=$note" - case "nlpcraft:relation" ⇒ val t = mkString("type") val note = mkString("note") @@ -452,12 +444,6 @@ object NCTokenLogger extends LazyLogging { s"range=$r, periods=${ps.asScala.mkString(",")}" - case "nlpcraft:aggregation" ⇒ - val t = mkString("type") - val note = mkString("note") - - s"type=$t, indexes=[${getIndexes("indexes")}], note=$note" - case "nlpcraft:relation" ⇒ val t = mkString("type") val note = mkString("note") diff --git a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlExtractor.java b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlExtractor.java index c16949c..2772aba 100644 --- a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlExtractor.java +++ b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlExtractor.java @@ -17,9 +17,8 @@ package org.apache.nlpcraft.model.tools.sqlgen; +import org.apache.nlpcraft.common.*; import org.apache.nlpcraft.model.*; -import org.apache.nlpcraft.common.NCException; -import java.util.*; /** * Utility methods for extracting various SQL components from {@link NCToken} tokens. Instances @@ -45,43 +44,6 @@ public interface NCSqlExtractor { NCSqlLimit extractLimit(NCToken limitTok); /** - * Extracts date range conditions object from given tokens. Result list will have multiple conditions that - * all will have to be AND-combined to make <code>colTok</code> to be within a date range - * defined by <code>dateTok</code> parameter. - * - * @param colTok Token representing detected SQL column (i.e. detected model element that belongs - * to <code>column</code> group). - * @param dateTok Date range token with ID <code>nlpcraft:date</code>. - * @return List of conditions extracted from given parameters that have to be AND-combined. - * @throws NCException Thrown in case of any errors. - */ - List<NCSqlSimpleCondition> extractDateRangeConditions(NCToken colTok, NCToken dateTok); - - /** - * Extracts numeric conditions object from given tokens. Result list will have multiple conditions that - * all will have to be AND-combined to make <code>colTok</code> act as an operand to the numeric - * condition defined by <code>numTok</code> parameter. - * - * @param colTok Token representing detected SQL column (i.e. detected model element that belongs - * to <code>column</code> group). - * @param numTok Numeric token with ID <code>nlpcraft:num</code>. This token provides numeric value - * as well as type of logical condition on that numeric value. - * @return List of conditions extracted from given parameters that have to be AND-combined. - * @throws NCException Thrown in case of any errors. - */ - List<NCSqlSimpleCondition> extractNumConditions(NCToken colTok, NCToken numTok); - - /** - * - * @param valToks Zero or more tokens representing values for the SQL IN condition. Note that only - * those token that have non-{@code null} values (see {@link NCToken#getValue()} method) will - * be used in the result conditions. - * @return List of conditions extracted from given parameters that have to be AND-combined. - * @throws NCException Thrown in case of any errors. - */ - List<NCSqlInCondition> extractInConditions(NCToken... valToks); - - /** * * @param sortTok * @return diff --git a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlModelGenerator.java b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlModelGenerator.java index b950594..b1ea2c7 100644 --- a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlModelGenerator.java +++ b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlModelGenerator.java @@ -18,6 +18,7 @@ package org.apache.nlpcraft.model.tools.sqlgen; import org.apache.nlpcraft.model.*; +import org.apache.nlpcraft.common.NCException; import org.apache.nlpcraft.model.tools.sqlgen.impl.NCSqlModelGeneratorImpl; /** @@ -44,13 +45,13 @@ import org.apache.nlpcraft.model.tools.sqlgen.impl.NCSqlModelGeneratorImpl; * <li> * Use {@link NCSqlSchemaBuilder#makeSchema(NCModel)} method to get an object representation of the * SQL data schema for the model. You can use this object representation along with many utility - * methods in {@link NCSqlExtractors} class to efficiently auto-generate SQL queries against the source RDBMS. + * methods in {@link NCSqlExtractor} class to efficiently auto-generate SQL queries against the source RDBMS. * </li> * </ul> * * @see NCModelFileAdapter * @see NCSqlSchemaBuilder - * @see NCSqlExtractors + * @see NCSqlExtractor */ public class NCSqlModelGenerator { /** @@ -58,7 +59,7 @@ public class NCSqlModelGenerator { * * @param args Command line parameters. Execute with <code>--help</code> parameter to get a full * documentation. - * @throws NCSqlException Thrown in case of any errors. + * @throws NCException Thrown in case of any errors. */ public static void main(String[] args) { // Calling out Scala engine. diff --git a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlSort.java b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlSort.java index 39541f8..319df8e 100644 --- a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlSort.java +++ b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlSort.java @@ -18,13 +18,12 @@ package org.apache.nlpcraft.model.tools.sqlgen; import org.apache.nlpcraft.model.*; -import java.util.*; /** * Object presentation of SQL sorting. * * @see NCSqlTable#getDefaultSort() - * @see NCSqlExtractors#extractSorts(NCSqlSchema, List, NCToken) + * @see NCSqlExtractor#extractSort(NCToken) */ public interface NCSqlSort { /** diff --git a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlBeans.scala b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlBeans.scala index cc3e826..dd10cd3 100644 --- a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlBeans.scala +++ b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlBeans.scala @@ -29,16 +29,6 @@ import scala.compat.java8.OptionConverters._ /** * - * @param select - * @param groupBy - */ -case class NCSqlAggregateImpl(select: Seq[NCSqlFunction], groupBy: Seq[NCSqlColumn]) extends NCSqlAggregate { - override def getSelect: util.List[NCSqlFunction] = select.asJava - override def getGroupBy: util.List[NCSqlColumn] = groupBy.asJava -} - -/** - * * @param table * @param column * @param dataType @@ -53,28 +43,6 @@ case class NCSqlColumnImpl(table: String, column: String, dataType: Int, isPk: B /** * - * @param col - * @param op - * @param value - */ -case class NCSqlSimpleConditionImpl(col: NCSqlColumn, op: String, value: Object) extends NCSqlSimpleCondition { - override def getColumn: NCSqlColumn = col - override def getOperation: String = op - override def getValue: Object = value -} - -/** - * - * @param col - * @param values - */ -case class NCSqlInConditionImpl(col: NCSqlColumn, values: Seq[Object]) extends NCSqlInCondition { - override def getColumn: NCSqlColumn = col - override def getValues: util.List[Object] = values.asJava -} - -/** - * * @param from * @param to */ @@ -85,16 +53,6 @@ case class NCSqlDateRangeImpl(from: Timestamp, to: Timestamp) extends NCSqlDateR /** * - * @param column - * @param function - */ -case class NCSqlFunctionImpl(column: NCSqlColumn, function: String) extends NCSqlFunction { - override def getColumn: NCSqlColumn = column - override def getFunction: String = function -} - -/** - * * @param fromTable * @param toTable * @param fromColumns diff --git a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlExtractorImpl.scala b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlExtractorImpl.scala index 00de62a..96bbf84 100644 --- a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlExtractorImpl.scala +++ b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlExtractorImpl.scala @@ -72,19 +72,11 @@ class NCSqlExtractorImpl(schema: NCSqlSchema, variant: NCVariant) extends NCSqlE /** * * @param tok - * @return - */ - private def tree(tok: NCToken): Seq[NCToken] = - Seq(tok) ++ tok.findPartTokens().asScala - - /** - * - * @param tok * @param grp * @return */ private def getWithGroup(tok: NCToken, grp: String): Seq[NCToken] = - tree(tok).flatMap(p ⇒ if (p.getGroups.contains(grp)) Some(p) else None) + Seq(tok) ++ tok.findPartTokens().asScala.flatMap(p ⇒ if (p.getGroups.contains(grp)) Some(p) else None) /** * @@ -138,14 +130,6 @@ class NCSqlExtractorImpl(schema: NCSqlSchema, variant: NCVariant) extends NCSqlE /** * - * @param tok - * @return - */ - private def findAnyColumnToken(tok: NCToken): NCToken = - findAnyColumnTokenOpt(tok).getOrElse(throw new NCException(s"No columns found for token: $tok")) - - /** - * * @param limitTok * @return */ @@ -164,99 +148,6 @@ class NCSqlExtractorImpl(schema: NCSqlSchema, variant: NCVariant) extends NCSqlE /** * - * @param colTok - * @param dateTok - * @return - */ - override def extractDateRangeConditions(colTok: NCToken, dateTok: NCToken): util.List[NCSqlSimpleCondition] = { - checkTokenId(dateTok, "nlpcraft:date") - checkGroup(colTok, "column") - - val col = extractColumn(colTok) - val range = extractDateRange(dateTok) - - Seq[NCSqlSimpleCondition]( - NCSqlSimpleConditionImpl(col, ">=", range.getFrom), - NCSqlSimpleConditionImpl(col, "<=", range.getTo) - ) - .asJava - } - - /** - * - * @param colTok - * @param numTok - * @return - */ - override def extractNumConditions(colTok: NCToken, numTok: NCToken): util.List[NCSqlSimpleCondition] = { - checkTokenId(numTok, "nlpcraft:num") - checkGroup(colTok, "column") - - val col = extractColumn(colTok) - - val from: java.lang.Double = numTok.metax("nlpcraft:num:from") - val fromIncl: Boolean = numTok.metax("nlpcraft:num:fromincl") - val to: java.lang.Double = numTok.metax("nlpcraft:num:to") - val toIncl: Boolean = numTok.metax("nlpcraft:num:toincl") - - val isRangeCondition: Boolean = numTok.metax("nlpcraft:num:israngecondition") - val isEqualCondition: Boolean = numTok.metax("nlpcraft:num:isequalcondition") - val isNotEqualCondition: Boolean = numTok.metax("nlpcraft:num:isnotequalcondition") - val isFromNegativeInfinity: Boolean = numTok.metax("nlpcraft:num:isfromnegativeinfinity") - val isToPositiveInfinity: Boolean = numTok.metax("nlpcraft:num:istopositiveinfinity") - - val seq: Seq[NCSqlSimpleCondition] = - if (isEqualCondition) - Seq(NCSqlSimpleConditionImpl(col, "=", from)) - else if (isNotEqualCondition) - Seq(NCSqlSimpleConditionImpl(col, "<>", from)) - else { - require(isRangeCondition) - - if (isFromNegativeInfinity) - Seq(NCSqlSimpleConditionImpl(col, if (fromIncl) "<=" else "<", to)) - else if (isToPositiveInfinity) - Seq(NCSqlSimpleConditionImpl(col, if (fromIncl) ">=" else ">", from)) - else - Seq( - NCSqlSimpleConditionImpl(col, if (fromIncl) ">=" else ">", from), - NCSqlSimpleConditionImpl(col, if (toIncl) "<=" else "<", to) - ) - } - - seq.asJava - } - - /** - * - * @param allValsToks - * @return - */ - override def extractInConditions(allValsToks: NCToken*): util.List[NCSqlInCondition] = { - allValsToks.map(tok ⇒ { - val valToks = tree(tok).filter(_.getValue != null) - - val valTok = - valToks.size match { - case 1 ⇒ valToks.head - - case 0 ⇒ throw new NCException(s"Values column not found for: $tok") - case _ ⇒ throw new NCException(s"Too many values columns found for: $tok") - } - - extractColumn(valTok) → valTok.getValue - }) - .groupBy { case (col, _) ⇒ col }.map { case (col, seq) ⇒ - NCSqlInConditionImpl(col, seq.map { - case (_, value) ⇒ value - }).asInstanceOf[NCSqlInCondition] - } - .toSeq - .asJava - } - - /** - * * @param sortTok * @return */ diff --git a/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala b/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala index 15093a5..7941e69 100644 --- a/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala +++ b/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala @@ -38,7 +38,6 @@ import org.apache.nlpcraft.probe.mgrs.dialogflow.NCDialogFlowManager import org.apache.nlpcraft.probe.mgrs.lifecycle.NCLifecycleManager import org.apache.nlpcraft.probe.mgrs.model.NCModelManager import org.apache.nlpcraft.probe.mgrs.nlp.NCProbeEnrichmentManager -import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.aggregation.NCAggregationEnricher import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.dictionary.NCDictionaryEnricher import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.limit.NCLimitEnricher import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.model.NCModelEnricher diff --git a/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala b/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala index 69eb11a..9a031b8 100644 --- a/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala +++ b/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala @@ -39,7 +39,6 @@ import org.apache.nlpcraft.probe.mgrs.conn.NCConnectionManager import org.apache.nlpcraft.probe.mgrs.conversation.NCConversationManager import org.apache.nlpcraft.probe.mgrs.dialogflow.NCDialogFlowManager import org.apache.nlpcraft.probe.mgrs.model.NCModelManager -import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.aggregation.NCAggregationEnricher import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.dictionary.NCDictionaryEnricher import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.limit.NCLimitEnricher import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.model.NCModelEnricher @@ -362,7 +361,6 @@ object NCProbeEnrichmentManager extends NCService with NCOpenCensusModelStats { val loopEnrichers = Seq( Some(Holder(NCModelEnricher, () ⇒ nlpSen.flatten.filter(_.isUser))), - get("nlpcraft:aggregation", NCAggregationEnricher), get("nlpcraft:sort", NCSortEnricher), get("nlpcraft:limit", NCLimitEnricher), get("nlpcraft:relation", NCRelationEnricher) diff --git a/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/aggregation/NCAggregationEnricher.scala b/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/aggregation/NCAggregationEnricher.scala deleted file mode 100644 index 7884e27..0000000 --- a/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/aggregation/NCAggregationEnricher.scala +++ /dev/null @@ -1,135 +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.probe.mgrs.nlp.enrichers.aggregation - -import java.io.Serializable - -import io.opencensus.trace.Span -import org.apache.nlpcraft.common.makro.NCMacroParser -import org.apache.nlpcraft.common.nlp.core.NCNlpCoreManager -import org.apache.nlpcraft.common.nlp.{NCNlpSentence, NCNlpSentenceNote, NCNlpSentenceToken} -import org.apache.nlpcraft.common.{NCE, NCService} -import org.apache.nlpcraft.probe.mgrs.NCModelDecorator -import org.apache.nlpcraft.probe.mgrs.nlp.NCProbeEnricher - -import scala.collection.JavaConverters._ -import scala.collection.{Map, Seq, mutable} - -/** - * Aggregation enricher. - */ -object NCAggregationEnricher extends NCProbeEnricher { - case class Match( - funcType: String, - matched: Seq[NCNlpSentenceToken], - refNotes: Set[String], - refIndexes: java.util.List[Int] - ) - private final val TOK_ID = "nlpcraft:aggregation" - - @volatile private var FUNCS: Map[String, String] = _ - - /** - * Starts this component. - */ - override def start(parent: Span = null): NCService = startScopedSpan("start", parent) { _ ⇒ - val macros = NCMacroParser() - - FUNCS = { - val m = mutable.HashMap.empty[String, String] - - def add(f: String, syns: String*): Unit = - syns. - flatMap(macros.expand). - map(p ⇒ p.split(" ").map(_.trim).map(NCNlpCoreManager.stem).mkString(" ")). - foreach(s ⇒ m += s → f) - - add("sum", "{sum|summary} {of|*} {data|value|*}") - add("max", "{max|maximum} {of|*} {data|value|*}") - add("min", "{min|minimum|minimal} {of|*} {data|value|*}") - add("avg", "{avg|average} {of|*} {data|value|*}") - add("group", "{group|grouped} {of|by|with|for}") - - m.toMap - } - - super.start() - } - - override def stop(parent: Span = null): Unit = startScopedSpan("stop", parent) { _ ⇒ - super.stop() - } - - @throws[NCE] - override def enrich(mdl: NCModelDecorator, ns: NCNlpSentence, senMeta: Map[String, Serializable], parent: Span = null): Unit = - startScopedSpan("enrich", parent, - "srvReqId" → ns.srvReqId, - "modelId" → mdl.model.getId, - "txt" → ns.text) { _ ⇒ - val buf = mutable.Buffer.empty[Set[NCNlpSentenceToken]] - - for (toks ← ns.tokenMixWithStopWords() if areSuitableTokens(buf, toks)) - tryToMatch(toks) match { - case Some(m) ⇒ - //for (refNote ← m.refNotes if !hasReference(TOK_ID, "note", refNote, m.matched)) { - for (refNote ← m.refNotes) { - val note = NCNlpSentenceNote( - m.matched.map(_.index), - TOK_ID, - "type" → m.funcType, - "indexes" → m.refIndexes, - "note" → refNote - ) - - m.matched.foreach(_.add(note)) - - buf += toks.toSet - } - - case None ⇒ // No-op. - } - } - - /** - * - * @param toks - */ - private def tryToMatch(toks: Seq[NCNlpSentenceToken]): Option[Match] = { - val matchedCands = toks.takeWhile(!_.exists(_.isUser)) - - def try0(stem: String): Option[Match] = - FUNCS.get(stem) match { - case Some(funcType) ⇒ - val afterMatched = toks.drop(matchedCands.length) - val refCands = afterMatched.filter(_.exists(_.isUser)) - val commonNotes = getCommonNotes(refCands, Some((n: NCNlpSentenceNote) ⇒ n.isUser)) - - val ok = - commonNotes.nonEmpty && - afterMatched.diff(refCands).forall(t ⇒ !t.isQuoted && (t.isStopWord || t.pos == "IN")) - - if (ok) Some(Match(funcType, matchedCands, commonNotes, refCands.map(_.index).asJava)) else None - case None ⇒ None - } - - try0(matchedCands.map(_.stem).mkString(" ")) match { - case Some(m) ⇒ Some(m) - case None ⇒ try0(matchedCands.filter(!_.isStopWord).map(_.stem).mkString(" ")) - } - } -} \ No newline at end of file diff --git a/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/post/NCPostEnrichProcessor.scala b/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/post/NCPostEnrichProcessor.scala index 939a43a..86ccded 100644 --- a/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/post/NCPostEnrichProcessor.scala +++ b/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/post/NCPostEnrichProcessor.scala @@ -112,14 +112,6 @@ object NCPostEnrichProcessor extends NCService with LazyLogging { p.get("from"), p.get("to") ) - case "nlpcraft:aggregation" ⇒ - ( - p.wordIndexes, - p.noteType, - p.get("type"), - p.get("indexes"), - p.get("note") - ) case "nlpcraft:relation" ⇒ ( p.wordIndexes, @@ -225,8 +217,7 @@ object NCPostEnrichProcessor extends NCService with LazyLogging { unionStops(ns, notNlpTypes, history) val res = - Seq("nlpcraft:aggregation", "nlpcraft:relation", "nlpcraft:limit"). - forall(t ⇒ fixIndexesReferences(t, ns, history)) && + Seq("nlpcraft:relation", "nlpcraft:limit").forall(t ⇒ fixIndexesReferences(t, ns, history)) && fixIndexesReferencesList("nlpcraft:sort", "subjIndexes", "subjNotes", ns, history) && fixIndexesReferencesList("nlpcraft:sort", "byIndexes", "byNotes", ns, history) diff --git a/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/NCEnrichersTestBeans.scala b/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/NCEnrichersTestBeans.scala index ef3fe41..79cb207 100644 --- a/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/NCEnrichersTestBeans.scala +++ b/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/NCEnrichersTestBeans.scala @@ -219,26 +219,6 @@ case class NCTestRelationToken(text: String, `type`: String, indexes: Seq[Int], s", note=$note>" } -case class NCTestAggregationToken(text: String, `type`: String, indexes: Seq[Int], note: String) extends NCTestToken { - require(text != null) - require(`type` != null) - require(indexes != null) - require(indexes.nonEmpty) - require(note != null) - - override def id: String = "nlpcraft:aggregation" - override def toString: String = - s"$text(aggregation)" + - s"<type=${`type`}" + - s", indexes=[${indexes.mkString(",")}]" + - s", note=$note>" -} - -object NCTestAggregationToken { - def apply(text: String, `type`: String, index: Int, note: String): NCTestAggregationToken = - new NCTestAggregationToken(text, `type`, Seq(index), note) -} - case class NCTestLimitToken( text: String, limit: Double, @@ -312,10 +292,8 @@ object NCTestToken { case "nlpcraft:city" ⇒ NCTestCityToken(txt, city = t.meta("nlpcraft:city:city")) case "nlpcraft:region" ⇒ NCTestRegionToken(txt, region = t.meta("nlpcraft:region:region")) case "nlpcraft:country" ⇒ NCTestCountryToken(txt, country = t.meta("nlpcraft:country:country")) - case "nlpcraft:subcontinent" ⇒ - NCTestSubcontinentToken(txt, subcontinent = t.meta("nlpcraft:subcontinent:subcontinent")) - case "nlpcraft:continent" ⇒ - NCTestContinentToken(txt, continent = t.meta("nlpcraft:continent:continent")) + case "nlpcraft:subcontinent" ⇒ NCTestSubcontinentToken(txt, subcontinent = t.meta("nlpcraft:subcontinent:subcontinent")) + case "nlpcraft:continent" ⇒ NCTestContinentToken(txt, continent = t.meta("nlpcraft:continent:continent")) case "nlpcraft:metro" ⇒ NCTestMetroToken(txt, metro = t.meta("nlpcraft:metro:metro")) case "nlpcraft:sort" ⇒ val subjNotes: java.util.List[String] = t.meta("nlpcraft:sort:subjnotes") @@ -340,15 +318,6 @@ object NCTestToken { indexes = indexes.asScala, note = t.meta("nlpcraft:relation:note") ) - case "nlpcraft:aggregation" ⇒ - val indexes: java.util.List[Int] = t.meta("nlpcraft:aggregation:indexes") - - NCTestAggregationToken( - txt, - `type` = t.meta("nlpcraft:aggregation:type"), - indexes = indexes.asScala, - note = t.meta("nlpcraft:aggregation:note") - ) case "nlpcraft:limit" ⇒ val indexes: java.util.List[Int] = t.meta("nlpcraft:limit:indexes")