This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-398
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-398 by this push:
new 6f5ee58 WIP.
6f5ee58 is described below
commit 6f5ee58ab9eb2c82ce5db5b7ad7c96237e4f9e99
Author: Sergey Kamov <[email protected]>
AuthorDate: Mon Aug 9 16:45:04 2021 +0300
WIP.
---
.../apache/nlpcraft/model/NCModelAddClasses.java | 2 +-
.../probe/mgrs/deploy/NCDeployManager.scala | 81 ++++++++++------------
.../probe/mgrs/deploy/NCDeployManagerSpec.scala | 17 ++++-
.../mgrs/deploy/NCModelPackagesWrapperMix.scala | 19 -----
.../mgrs/deploy/jv/NCModelClassesWrapper.java | 5 +-
.../mgrs/deploy/jv/NCModelDeploySpecAdapter.java | 22 ++++--
.../mgrs/deploy/sc/NCModelClassesWrapper.scala | 22 ++++--
.../mgrs/deploy/sc/NCModelDeploySpecAdapter.scala | 17 +++++
.../mgrs/deploy/sc/NCModelPackagesWrapper.scala | 17 +++++
.../probe/mgrs/deploy/sc/NCNestedClass.scala | 20 +++++-
.../probe/mgrs/deploy/sc/NCNestedStatic.scala | 19 ++++-
11 files changed, 161 insertions(+), 80 deletions(-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCModelAddClasses.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCModelAddClasses.java
index 7f21e7b..7c7771a 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCModelAddClasses.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCModelAddClasses.java
@@ -26,5 +26,5 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Retention(value=RUNTIME)
@Target(value=TYPE)
public @interface NCModelAddClasses {
- String[] value();
+ Class<?>[] value();
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
index e2a762d..507ab45 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
@@ -109,22 +109,24 @@ object NCDeployManager extends NCService {
*/
case class SynonymHolder(elmId: String, syn: NCProbeSynonym)
- case class MethodHolder(objClass: Class[_], obj: Any, method: Method) {
- require(objClass != null ^ obj != null)
+ case class MethodHolder(objClassName: String, obj: Any, method: Method) {
+ require(objClassName != null ^ obj != null)
- private var o: Any = obj
+ private var lazyObj: Any = obj
- def getMethodObject: Any = {
- if (o == null)
+ def getObject: Any =
+ if (lazyObj == null) {
try
- o = objClass.getDeclaredConstructor().newInstance()
+ lazyObj = U.mkObject(objClassName)
catch {
// TODO:
- case e: Throwable => throw new NCE(s"Error initializing
object of type: $objClass", e)
+ case e: Throwable => throw new NCE(s"Error initializing
object of type: $objClassName", e)
}
- o
- }
+ lazyObj
+ }
+ else
+ lazyObj
}
/**
@@ -1242,7 +1244,7 @@ object NCDeployManager extends NCService {
*/
@throws[NCE]
private def invoke(mdlId: String, mh: MethodHolder, args: Array[AnyRef]):
NCResult = {
- val obj = if (Modifier.isStatic(mh.method.getModifiers)) null else
mh.getMethodObject
+ val obj = if (Modifier.isStatic(mh.method.getModifiers)) null else
mh.getObject
var flag = mh.method.canAccess(obj)
@@ -1580,7 +1582,7 @@ object NCDeployManager extends NCService {
throw new NCE(s"The intent cannot be bound to more than
one callback [" +
s"mdlId=$mdlId, " +
s"origin=${mdl.getOrigin}, " +
- s"class=${mh.objClass}, " +
+ s"class=${mh.objClassName}, " +
s"intentId=${intent.id}" +
s"]")
else {
@@ -1620,59 +1622,52 @@ object NCDeployManager extends NCService {
// Third, scan all methods for intent-callback bindings.
for (m <- getAllMethods(mdl))
- processMethod(MethodHolder(objClass = null, obj = mdl, method = m))
+ processMethod(MethodHolder(objClassName = null, obj = mdl, method
= m))
// External references.
- def getReferences[T <: Annotation](clazz: Class[T], getValues: T =>
Seq[String]): Option[Seq[String]] = {
+ def processReferences[T <: Annotation](clazz: Class[T], getReferences:
T => Seq[Class[_]]): Unit = {
val anns = mdl.getClass.getAnnotationsByType(clazz)
if (anns != null && anns.nonEmpty) {
- val refs = getValues(anns.head)
+ val refs = getReferences(anns.head)
if (refs == null || refs.isEmpty)
// TODO:
throw new NCE(
- s"Invalid empty reference [" +
+ s"Invalid empty references [" +
s"mdlId=$mdlId, " +
s"origin=${mdl.getOrigin}, " +
s"reference=${clazz.getName}" +
s"]"
)
- Some(refs)
- }
- else
- None
- }
- def processClassAndMethods(cls :Class[_]): Unit = {
- processClass(cls)
- getAllMethods(cls).foreach(m =>
processMethod(MethodHolder(objClass = cls, obj = null, method = m)))
+ refs.foreach(ref => {
+ processClass(ref)
+ getAllMethods(ref).foreach(m =>
processMethod(MethodHolder(objClassName = ref.getName, obj = null, method = m)))
+ })
+ }
}
- getReferences(CLS_MDL_CLASSES_REF, (a: NCModelAddClasses) =>
a.value()) match {
- case Some(refs) => refs.foreach(ref =>
processClassAndMethods(cl.loadClass(ref)))
- case None => // No-op.
- }
+ processReferences(CLS_MDL_CLASSES_REF, (a: NCModelAddClasses) =>
a.value())
- getReferences(CLS_MDL_PACKAGES_REF, (a: NCModelAddPackage) =>
a.value()) match {
- case Some(refs) =>
- refs.foreach(ref => {
- if (cl.getDefinedPackage(ref) == null)
- // TODO:
+ processReferences(
+ CLS_MDL_PACKAGES_REF,
+ (a: NCModelAddPackage) =>
+ a.value().flatMap(p => {
+ if (cl.getDefinedPackage(p) == null)
throw new NCE(
- s"Invalid package reference [" +
- s"mdlId=$mdlId, " +
- s"origin=${mdl.getOrigin}, " +
- s"reference=$ref" +
- s"]"
+ // TODO:
+ s"Invalid references [" +
+ s"mdlId=$mdlId, " +
+ s"origin=${mdl.getOrigin}, " +
+ s"reference=${CLS_MDL_PACKAGES_REF.getName}, "
+
+ s"package=$p" +
+ s"]"
)
- for (info <-
ClassPath.from(cl).getTopLevelClassesRecursive(ref).asScala)
- processClassAndMethods(info.load())
- }
- )
- case None => // No-op.
- }
+
ClassPath.from(cl).getTopLevelClassesRecursive(p).asScala.map(_.load())
+ })
+ )
val unusedIntents = intentDecls.filter(i => !intents.exists(_._1.id ==
i.id))
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManagerSpec.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManagerSpec.scala
index 2eb6f0c..1268f6e 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManagerSpec.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManagerSpec.scala
@@ -17,10 +17,12 @@
package org.apache.nlpcraft.probe.mgrs.deploy
-import org.apache.nlpcraft.model.{NCElement, NCModelAdapter}
-import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
+import org.apache.nlpcraft.model.{NCElement, NCModelAdapter, NCModelAddPackage}
+import org.apache.nlpcraft.{NCTestContext, NCTestElement, NCTestEnvironment}
import org.junit.jupiter.api.Test
+import java.util
+
@NCTestEnvironment(model =
classOf[org.apache.nlpcraft.probe.mgrs.deploy.sc.NCModelClassesWrapper],
startClient = true)
class NCModelClassesWrapperScalaSpec extends NCTestContext {
@Test
@@ -69,6 +71,17 @@ class NCModelMix extends
NCModelAdapter("nlpcraft.deploy.test.mdl", "Test Model"
override def getElements: java.util.Set[NCElement] = super.getElements
}
+@NCModelAddPackage(Array("org.apache.nlpcraft.probe.mgrs.deploy"))
+class NCModelPackagesWrapperMix extends
NCModelAdapter("nlpcraft.deploy.test.mdl", "Test Model", "1.0") {
+ override def getElements: util.Set[NCElement] =
+ Set(
+ NCTestElement("scalaClass"),
+ NCTestElement("scalaStatic"),
+ NCTestElement("javaClass"),
+ NCTestElement("javaStatic")
+ )
+}
+
@NCTestEnvironment(model =
classOf[org.apache.nlpcraft.probe.mgrs.deploy.NCModelPackagesWrapperMix],
startClient = true)
class NCModelPackagesWrapperMixSpec extends NCTestContext {
@Test
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCModelPackagesWrapperMix.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCModelPackagesWrapperMix.scala
deleted file mode 100644
index 4f43600..0000000
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCModelPackagesWrapperMix.scala
+++ /dev/null
@@ -1,19 +0,0 @@
-package org.apache.nlpcraft.probe.mgrs.deploy
-
-import org.apache.nlpcraft.NCTestElement
-import org.apache.nlpcraft.NCTestElement._
-import org.apache.nlpcraft.model.{NCElement, NCModelAdapter, NCModelAddPackage}
-
-import java.util
-
-@NCModelAddPackage(Array("org.apache.nlpcraft.probe.mgrs.deploy"))
-class NCModelPackagesWrapperMix extends
NCModelAdapter("nlpcraft.deploy.test.mdl", "Test Model", "1.0") {
- override def getElements: util.Set[NCElement] = {
- Set(
- NCTestElement("scalaClass"),
- NCTestElement("scalaStatic"),
- NCTestElement("javaClass"),
- NCTestElement("javaStatic")
- )
- }
-}
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/jv/NCModelClassesWrapper.java
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/jv/NCModelClassesWrapper.java
index b894a66..18dac28 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/jv/NCModelClassesWrapper.java
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/jv/NCModelClassesWrapper.java
@@ -19,10 +19,7 @@ package org.apache.nlpcraft.probe.mgrs.deploy.jv;
import org.apache.nlpcraft.model.NCModelAddClasses;
-@NCModelAddClasses({
- "org.apache.nlpcraft.probe.mgrs.deploy.jv.NCNestedClass",
- "org.apache.nlpcraft.probe.mgrs.deploy.jv.NCNestedStatic"
-})
+@NCModelAddClasses({NCNestedClass.class, NCNestedStatic.class})
public class NCModelClassesWrapper extends NCModelDeploySpecAdapter {
// No-op.
}
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/jv/NCModelDeploySpecAdapter.java
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/jv/NCModelDeploySpecAdapter.java
index 65c7ac1..3c557be 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/jv/NCModelDeploySpecAdapter.java
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/jv/NCModelDeploySpecAdapter.java
@@ -1,3 +1,20 @@
+/*
+ * 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
+ *
+ * https://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.deploy.jv;
import org.apache.nlpcraft.model.NCElement;
@@ -12,9 +29,6 @@ class NCModelDeploySpecAdapter extends NCModelAdapter {
@Override
public Set<NCElement> getElements() {
- return Set.of(
- (NCElement) () -> "javaClass",
- (NCElement) () -> "javaStatic"
- );
+ return Set.of((NCElement) () -> "javaClass", (NCElement) () ->
"javaStatic");
}
}
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCModelClassesWrapper.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCModelClassesWrapper.scala
index 994a36d..5456a08 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCModelClassesWrapper.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCModelClassesWrapper.scala
@@ -1,9 +1,23 @@
+/*
+ * 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
+ *
+ * https://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.deploy.sc
import org.apache.nlpcraft.model.NCModelAddClasses
-@NCModelAddClasses(Array(
- "org.apache.nlpcraft.probe.mgrs.deploy.sc.NCNestedClass",
- "org.apache.nlpcraft.probe.mgrs.deploy.sc.NCNestedStatic"
-))
+@NCModelAddClasses(Array(classOf[NCNestedClass], classOf[NCNestedStatic.type]))
class NCModelClassesWrapper extends NCModelDeploySpecAdapter
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCModelDeploySpecAdapter.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCModelDeploySpecAdapter.scala
index 1e42320..18a83f0 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCModelDeploySpecAdapter.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCModelDeploySpecAdapter.scala
@@ -1,3 +1,20 @@
+/*
+ * 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
+ *
+ * https://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.deploy.sc
import org.apache.nlpcraft.NCTestElement
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCModelPackagesWrapper.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCModelPackagesWrapper.scala
index ca8f1e4..2c3e8ad 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCModelPackagesWrapper.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCModelPackagesWrapper.scala
@@ -1,3 +1,20 @@
+/*
+ * 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
+ *
+ * https://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.deploy.sc
import org.apache.nlpcraft.model.NCModelAddPackage
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCNestedClass.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCNestedClass.scala
index 3d410eb..944ca2d 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCNestedClass.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCNestedClass.scala
@@ -1,9 +1,25 @@
+/*
+ * 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
+ *
+ * https://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.deploy.sc
import org.apache.nlpcraft.model.{NCIntent, NCResult}
class NCNestedClass {
@NCIntent("intent=scalaClass term={tok_id() == 'scalaClass'}")
- def a(): NCResult = NCResult.text("OK")
-
+ def m(): NCResult = NCResult.text("OK")
}
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCNestedStatic.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCNestedStatic.scala
index 645aa12..6b03841 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCNestedStatic.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/deploy/sc/NCNestedStatic.scala
@@ -1,8 +1,25 @@
+/*
+ * 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
+ *
+ * https://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.deploy.sc
import org.apache.nlpcraft.model.{NCIntent, NCResult}
object NCNestedStatic {
@NCIntent("intent=scalaStatic term={tok_id() == 'scalaStatic'}")
- def b(): NCResult = NCResult.text("OK")
+ def m(): NCResult = NCResult.text("OK")
}
\ No newline at end of file