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 db93929 WIP.
db93929 is described below
commit db93929d7c48b844cd28d93816bb7ac8938599e4
Author: Aaron Radzinzski <[email protected]>
AuthorDate: Mon Mar 15 18:47:59 2021 -0700
WIP.
---
.../org/apache/nlpcraft/common/util/NCUtils.scala | 34 +++++++++++++++++++++-
.../model/intent/compiler/NCDslCompiler.scala | 31 ++++----------------
.../model/intent/solver/NCIntentSolverEngine.scala | 34 ++++++++++++++++++++--
3 files changed, 70 insertions(+), 29 deletions(-)
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 56a2f3f..f96acca 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
@@ -62,6 +62,7 @@ import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.io.{BufferedSource, Source}
import scala.language.{implicitConversions, postfixOps}
+import scala.reflect.{ClassTag, classTag}
import scala.reflect.runtime.universe._
import scala.sys.SystemProperties
import scala.util.control.Exception.ignoring
@@ -1919,6 +1920,37 @@ object NCUtils extends LazyLogging {
}(ec)
}).foreach(Await.result(_, Duration.Inf))
}
+
+ /**
+ *
+ * @param objFac
+ * @param mtdName
+ * @param arg
+ * @tparam T
+ * @return
+ */
+ def callMethod[T: ClassTag, R](objFac: () ⇒ Object, mtdName: String, arg:
T): R = {
+ val obj = objFac()
+ val mtd = obj.getClass.getMethod(mtdName, classTag[T].runtimeClass)
+
+ var flag = mtd.canAccess(obj)
+
+ try {
+ if (!flag) {
+ mtd.setAccessible(true)
+
+ flag = true
+ }
+ else
+ flag = false
+
+ mtd.invoke(obj, arg.asInstanceOf[Object]).asInstanceOf[R]
+ }
+ finally {
+ if (flag)
+ mtd.setAccessible(false)
+ }
+ }
/**
*
@@ -1995,7 +2027,7 @@ object NCUtils extends LazyLogging {
*/
@throws[NCE]
def extractYamlResource[T](res: String, ignoreCase: Boolean, tr:
TypeReference[T]): T =
- extractYamlString(readStream(getStream(res), "UTF-8").mkString("\n"),
res, ignoreCase, tr)
+ extractYamlString(readStream(getStream(res)).mkString("\n"), res,
ignoreCase, tr)
/**
* Extracts type `T` from given YAML `data`.
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCDslCompiler.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCDslCompiler.scala
index 3c582c6..2de7559 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCDslCompiler.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCDslCompiler.scala
@@ -236,32 +236,11 @@ object NCDslCompiler extends LazyLogging {
val mdlCls = if (clsName == null)
mdl.meta[String](MDL_META_MODEL_CLASS_KEY) else clsName
try {
- val obj = if (clsName == null) mdl else
U.mkObject(clsName)
- val mtd =
Thread.currentThread().getContextClassLoader.loadClass(mdlCls)
- .getMethod(mtdName,
classOf[NCTokenPredicateContext])
-
- var flag = mtd.canAccess(mdl)
-
- val res = try {
- if (!flag) {
- mtd.setAccessible(true)
-
- flag = true
- }
- else
- flag = false
-
- mtd.invoke(obj,
javaCtx).asInstanceOf[NCTokenPredicateResult]
- }
- finally {
- if (flag)
- try
- mtd.setAccessible(false)
- catch {
- case e: SecurityException ⇒
- throw new NCE(s"Access or security
error in custom intent term: $mdlCls.$mtdName", e)
- }
- }
+ val res = U.callMethod[NCTokenPredicateContext,
NCTokenPredicateResult](
+ () ⇒ if (clsName == null) mdl else
U.mkObject(clsName),
+ mtdName,
+ javaCtx
+ )
(res.getResult, res.wasTokenUsed())
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolverEngine.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolverEngine.scala
index 18f986b..6088d7c 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolverEngine.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolverEngine.scala
@@ -23,7 +23,7 @@ import org.apache.nlpcraft.common.debug.{NCLogGroupToken,
NCLogHolder}
import org.apache.nlpcraft.common.opencensus.NCOpenCensusTrace
import org.apache.nlpcraft.common._
import org.apache.nlpcraft.model.impl.NCTokenLogger
-import org.apache.nlpcraft.model.{NCContext, NCIntentMatch, NCResult, NCToken}
+import org.apache.nlpcraft.model.{NCContext, NCDialogFlowItem, NCIntentMatch,
NCResult, NCToken}
import org.apache.nlpcraft.probe.mgrs.dialogflow.NCDialogFlowManager
import org.apache.nlpcraft.model.impl.NCTokenPimp._
import org.apache.nlpcraft.model.intent.{NCDslContext, NCDslIntent, NCDslTerm}
@@ -475,7 +475,37 @@ object NCIntentSolverEngine extends LazyLogging with
NCOpenCensusTrace {
x("matched")
}
else if (intent.flowMtdName.isDefined) {
- // TODO.
+ require(intent.flowClsName.isDefined)
+
+ val clsName = intent.flowClsName.get
+ val mtdName = intent.flowMtdName.get
+
+ val fqn = s"$clsName.$mtdName(java.util.List[NCDialogFlowItem])"
+
+ val res =
+ try
+ U.callMethod[java.util.List[NCDialogFlowItem],
java.lang.Boolean](
+ U.mkObject(clsName),
+ mtdName,
+ flow.toList.asJava
+ )
+ catch {
+ case e: Exception ⇒
+ throw new NCE(s"Failed to invoke custom flow callback:
$fqn", e)
+ }
+
+ def x(s: String): Unit = {
+ logger.info(s"Intent '$intentId' ${bo(s)} custom flow callback
$varStr:")
+ logger.info(s" +-- ${c("Custom callback :")} $fqn")
+ }
+
+ if (!res) {
+ x("did not match")
+
+ flowMatched = false
+ }
+ else
+ x("matched")
}
if (flowMatched) {