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

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


The following commit(s) were added to refs/heads/NLPCRAFT-206 by this push:
     new c164ab3  WIP.
c164ab3 is described below

commit c164ab3e52f1c2a6933a54175882b2e5db74c6ff
Author: Aaron Radzinzski <[email protected]>
AuthorDate: Sun Feb 28 21:35:42 2021 -0800

    WIP.
---
 .../model/intent/impl/antlr4/NCIntentDsl.g4        | 16 ++++++---
 .../impl/ver2/NCIntentDslBaselCompiler.scala       | 39 +++++++++++-----------
 .../intent/impl/ver2/NCIntentDslCompiler.scala     |  4 +--
 .../impl/ver2/NCIntentDslFragmentCache.scala       | 24 ++++++++++++-
 4 files changed, 57 insertions(+), 26 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/antlr4/NCIntentDsl.g4
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/antlr4/NCIntentDsl.g4
index 960c336..a2bcc5f 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/antlr4/NCIntentDsl.g4
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/antlr4/NCIntentDsl.g4
@@ -23,10 +23,14 @@ dslItems
     : dslItem
     | dslItems dslItem
     ;
-dslItem: intent | frag;
+dslItem
+    : intent
+    | frag
+    ;
 frag: fragId terms;
 fragId: FRAG ASSIGN id;
-fragRef: FRAG LPAR id RPAR;
+fragRef: FRAG LPAR id fragArg? RPAR;
+fragArg: COMMA jsonObj;
 intent: intentId orderedDecl? flowDecl? metaDecl? terms;
 intentId: 'intent' ASSIGN id;
 orderedDecl: 'ordered' ASSIGN BOOL;
@@ -53,7 +57,10 @@ jsonArr
 terms
     : termItem
     | terms termItem;
-termItem: term | fragRef;
+termItem
+    : term
+    | fragRef
+    ;
 termEq
     : ASSIGN // Do not use conversation.
     | TILDA // Use conversation.
@@ -116,6 +123,7 @@ FUN_NAME
     | 'meta_company'
     | 'meta_sys'
     | 'meta_conv'
+    | 'meta_frag'
     | 'json'
     | 'if'
     | 'id'
@@ -295,4 +303,4 @@ fragment LETTER: [a-zA-Z];
 ID: 
(UNI_CHAR|UNDERSCORE|LETTER|DOLLAR)+(UNI_CHAR|DOLLAR|LETTER|[0-9]|COLON|MINUS|UNDERSCORE)*;
 COMMENT : ('//' ~[\r\n]* '\r'? ('\n'| EOF) | '/*' .*? '*/' ) -> skip;
 WS: [ \r\t\u000C\n]+ -> skip;
-ErrorCharacter: .;
+ErrorChar: .;
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslBaselCompiler.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslBaselCompiler.scala
index 2929b07..990766d 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslBaselCompiler.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslBaselCompiler.scala
@@ -17,7 +17,7 @@
 
 package org.apache.nlpcraft.model.intent.impl.ver2
 
-import org.antlr.v4.runtime.ParserRuleContext
+import org.antlr.v4.runtime.{ParserRuleContext ⇒ PRC}
 import org.antlr.v4.runtime.tree.{TerminalNode ⇒ TN}
 import org.apache.commons.lang3.StringUtils
 import org.apache.nlpcraft.model.NCToken
@@ -40,10 +40,10 @@ trait NCIntentDslBaselCompiler {
      * @param ctx
      * @return
      */
-    def newSyntaxError(errMsg: String)(implicit ctx: ParserRuleContext): NCE = 
{
-        val tok = ctx.start
+    def newSyntaxError(errMsg: String)(implicit ctx: PRC): NCE = {
+        val src = ctx.start.getTokenSource
 
-        syntaxError(errMsg, tok.getInputStream.getSourceName, tok.getLine, 
tok.getCharPositionInLine)
+        syntaxError(errMsg, src.getSourceName, src.getLine, 
src.getCharPositionInLine)
     }
     /**
       *
@@ -51,7 +51,7 @@ trait NCIntentDslBaselCompiler {
       * @param ctx
       * @return
       */
-    def newRuntimeError(errMsg: String, cause: Exception = null)(implicit ctx: 
ParserRuleContext): NCE = {
+    def newRuntimeError(errMsg: String, cause: Exception = null)(implicit ctx: 
PRC): NCE = {
         val src = ctx.start.getTokenSource
         
         runtimeError(errMsg, src.getSourceName, src.getLine, 
src.getCharPositionInLine, cause)
@@ -79,17 +79,17 @@ trait NCIntentDslBaselCompiler {
         stack.push(NCDslTermRetVal(Boolean.box(any), usedTok))
 
     // Runtime errors.
-    def rtUnaryOpError(op: String, v: AnyRef)(implicit ctx: 
ParserRuleContext): NCE =
+    def rtUnaryOpError(op: String, v: AnyRef)(implicit ctx: PRC): NCE =
         newRuntimeError(s"Unexpected '$op' DSL operation for value: $v")
-    def rtBinaryOpError(op: String, v1: AnyRef, v2: AnyRef)(implicit ctx: 
ParserRuleContext): NCE =
+    def rtBinaryOpError(op: String, v1: AnyRef, v2: AnyRef)(implicit ctx: 
PRC): NCE =
         newRuntimeError(s"Unexpected '$op' DSL operation for values: $v1, $v2")
-    def rtUnknownFunError(fun: String)(implicit ctx: ParserRuleContext): NCE =
+    def rtUnknownFunError(fun: String)(implicit ctx: PRC): NCE =
         newRuntimeError(s"Unknown DSL function: $fun()")
-    def rtMinParamNumError(min: Int, fun: String)(implicit ctx: 
ParserRuleContext): NCE =
+    def rtMinParamNumError(min: Int, fun: String)(implicit ctx: PRC): NCE =
         newRuntimeError(s"Invalid number of parameters for DSL function ($min 
is required): $fun()")
-    def rtParamNumError(fun: String)(implicit ctx: ParserRuleContext): NCE =
+    def rtParamNumError(fun: String)(implicit ctx: PRC): NCE =
         newRuntimeError(s"Invalid number of parameters for DSL function: 
$fun()")
-    def rtParamTypeError(fun: String, param: AnyRef, expectType: 
String)(implicit ctx: ParserRuleContext): NCE =
+    def rtParamTypeError(fun: String, param: AnyRef, expectType: 
String)(implicit ctx: PRC): NCE =
         newRuntimeError(s"Expecting '$expectType' type of parameter for DSL 
function '$fun()', found: $param")
 
     /**
@@ -142,7 +142,7 @@ trait NCIntentDslBaselCompiler {
      * @param lteq
      * @param gteq
      */
-    def parseCompExpr(lt: TN, gt: TN, lteq: TN, gteq: TN)(implicit ctx: 
ParserRuleContext): Instr = (_, stack: StackType, _) ⇒ {
+    def parseCompExpr(lt: TN, gt: TN, lteq: TN, gteq: TN)(implicit ctx: PRC): 
Instr = (_, stack: StackType, _) ⇒ {
         implicit val s: StackType = stack
 
         val (v1, v2, f1, f2) = pop2()
@@ -190,7 +190,7 @@ trait NCIntentDslBaselCompiler {
      * @param mod
      * @param div
      */
-    def parseMultExpr(mult: TN, mod: TN, div: TN)(implicit ctx: 
ParserRuleContext): Instr = (_, stack: StackType, _) ⇒ {
+    def parseMultExpr(mult: TN, mod: TN, div: TN)(implicit ctx: PRC): Instr = 
(_, stack: StackType, _) ⇒ {
         implicit val s: StackType = stack
 
         val (v1, v2, f1, f2) = pop2()
@@ -227,7 +227,7 @@ trait NCIntentDslBaselCompiler {
      * @param or
      * @return
      */
-    def parseLogExpr(and: TN, or : TN)(implicit ctx: ParserRuleContext): Instr 
= (_, stack: StackType, _) ⇒ {
+    def parseLogExpr(and: TN, or : TN)(implicit ctx: PRC): Instr = (_, stack: 
StackType, _) ⇒ {
         implicit val s: StackType = stack
 
         val (v1, v2, f1, f2) = pop2()
@@ -250,7 +250,7 @@ trait NCIntentDslBaselCompiler {
      * @param neq
      * @return
      */
-    def parseEqExpr(eq: TN, neq: TN)(implicit ctx: ParserRuleContext): Instr = 
(_, stack: StackType, _) ⇒ {
+    def parseEqExpr(eq: TN, neq: TN)(implicit ctx: PRC): Instr = (_, stack: 
StackType, _) ⇒ {
         implicit val s: StackType = stack
 
         val (v1, v2, f1, f2) = pop2()
@@ -280,7 +280,7 @@ trait NCIntentDslBaselCompiler {
      * @param plus
      * @param minus
      */
-    def parsePlusExpr(plus: TN, minus: TN)(implicit ctx: ParserRuleContext): 
Instr = (_, stack: StackType, _) ⇒ {
+    def parsePlusExpr(plus: TN, minus: TN)(implicit ctx: PRC): Instr = (_, 
stack: StackType, _) ⇒ {
         implicit val s: StackType = stack
 
         val (v1, v2, f1, f2) = pop2()
@@ -313,7 +313,7 @@ trait NCIntentDslBaselCompiler {
      * @param not
      * @return
      */
-    def parseUnaryExpr(minus: TN, not: TN)(implicit ctx: ParserRuleContext): 
Instr = (_, stack: StackType, _) ⇒ {
+    def parseUnaryExpr(minus: TN, not: TN)(implicit ctx: PRC): Instr = (_, 
stack: StackType, _) ⇒ {
         implicit val s: StackType = stack
 
         val (v, usedTok) = pop1()
@@ -338,7 +338,7 @@ trait NCIntentDslBaselCompiler {
      * @param txt
      * @return
      */
-    def parseAtom(txt: String)(implicit ctx: ParserRuleContext): Instr = {
+    def parseAtom(txt: String)(implicit ctx: PRC): Instr = {
         val atom =
             if (txt == "null") null // Try 'null'.
             else if (txt == "true") Boolean.box(true) // Try 'boolean'.
@@ -368,7 +368,7 @@ trait NCIntentDslBaselCompiler {
      * @param id
      * @return
      */
-    def parseCallExpr(id: TN)(implicit ctx: ParserRuleContext): Instr = (tok, 
stack: StackType, termCtx) ⇒ {
+    def parseCallExpr(id: TN)(implicit ctx: PRC): Instr = (tok, stack: 
StackType, termCtx) ⇒ {
         val fun = id.getText
 
         implicit val s2: StackType = stack
@@ -599,6 +599,7 @@ trait NCIntentDslBaselCompiler {
             case "meta_company" ⇒ doCompMeta()
             case "meta_sys" ⇒ doSysMeta()
             case "meta_conv" ⇒ doConvMeta()
+            case "meta_frag" ⇒
 
             // Converts JSON to map.
             case "json" ⇒ doJson()
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala
index ed16c1d..95c8cbc 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala
@@ -243,8 +243,8 @@ object NCIntentDslCompiler extends LazyLogging {
     private def mkRuntimeError(
         msg: String,
         srcName: String,
-        line: Int,
-        charPos: Int,
+        line: Int, // 1, 2, ...
+        charPos: Int, // 0, 1, 2, ...
         dsl: String,
         mdlId: String): String = mkError("runtime", msg, srcName, line, 
charPos, dsl, mdlId)
 
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslFragmentCache.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslFragmentCache.scala
index 7c72683..158e972 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslFragmentCache.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslFragmentCache.scala
@@ -17,9 +17,31 @@
 
 package org.apache.nlpcraft.model.intent.impl.ver2
 
+import org.apache.nlpcraft.model.intent.utils.ver2.NCDslFragment
+
+import scala.collection.concurrent.TrieMap
+import scala.collection.mutable
+
 /**
   * Global intent DSL fragment cache.
   */
 object NCIntentDslFragmentCache {
-
+    private final val cache = TrieMap.empty[String /* Model ID. */, 
mutable.Map[String, NCDslFragment]]
+    
+    /**
+      *
+      * @param mdlId
+      * @param frag
+      */
+    def add(mdlId: String, frag: NCDslFragment): Unit =
+        cache.getOrElse(mdlId, mutable.HashMap.empty[String, NCDslFragment]) 
+= (frag.id → frag)
+    
+    /**
+      *
+      * @param mdlId
+      * @param fragId
+      * @return
+      */
+    def get(mdlId: String, fragId: String): Option[NCDslFragment] =
+        cache.get(mdlId).flatMap(_.get(fragId))
 }

Reply via email to