This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-471
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-471 by this push:
new b38467a WIP.
b38467a is described below
commit b38467a2f93bbed9aaa2b792b764fe12e91c4896
Author: Sergey Kamov <[email protected]>
AuthorDate: Tue Dec 28 11:16:08 2021 +0300
WIP.
---
.../scala/org/apache/nlpcraft/NCModelClient.java | 59 ++++++++++++++++------
.../apache/nlpcraft/internal/util/NCUtils.scala | 2 +-
.../enricher/NCOpenNlpTokenEnricherSpec.scala | 4 +-
3 files changed, 47 insertions(+), 18 deletions(-)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.java
index 8534528..8dfc3f7 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.java
@@ -43,14 +43,31 @@ public class NCModelClient implements NCLifecycle {
// TODO:
}
- private static void start(List<? extends NCLifecycle> list) {
+ private static void start(ExecutorService s, List<? extends NCLifecycle>
list) {
+ assert s != null;
+
if (list != null)
- list.forEach(p -> p.start());
+ list.forEach(p -> s.execute(() -> p.start()));
}
- private static void stop(List<? extends NCLifecycle> list) {
+ private static void stop(ExecutorService s, List<? extends NCLifecycle>
list) {
+ assert s != null;
+
if (list != null)
- list.forEach(p -> p.stop());
+ list.forEach(p -> s.execute(() -> p.stop()));
+ }
+
+ private static void stopExecutorService(ExecutorService s) {
+ try {
+ s.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
+ }
+ catch (InterruptedException e) {
+ throw new NCException("Thread interrupted.", e);
+ }
+ }
+
+ private static ExecutorService getExecutorService() {
+ return
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
}
@Override
@@ -58,22 +75,34 @@ public class NCModelClient implements NCLifecycle {
verify();
NCModelConfig cfg = mdl.getConfig();
-
- cfg.getTokenParser().start();
- start(cfg.getEntityParsers());
- start(cfg.getEntityEnrichers());
- start(cfg.getTokenEnrichers());
+ ExecutorService s = getExecutorService();
+
+ try {
+ s.execute(() -> cfg.getTokenParser().start());
+ start(s, cfg.getEntityParsers());
+ start(s, cfg.getEntityEnrichers());
+ start(s, cfg.getTokenEnrichers());
+ }
+ finally {
+ stopExecutorService(s);
+ }
}
@Override
public void stop() {
NCModelConfig cfg = mdl.getConfig();
-
- stop(cfg.getTokenEnrichers());
- stop(cfg.getEntityEnrichers());
- stop(cfg.getEntityParsers());
- cfg.getTokenParser().stop();
-
+ ExecutorService s = getExecutorService();
+
+ try {
+ stop(s, cfg.getTokenEnrichers());
+ stop(s, cfg.getEntityEnrichers());
+ stop(s, cfg.getEntityParsers());
+ s.execute(() -> cfg.getTokenParser().stop());
+
+ }
+ finally {
+ stopExecutorService(s);
+ }
}
/**
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCUtils.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCUtils.scala
index 5b9bd32..239e29f 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCUtils.scala
@@ -870,7 +870,7 @@ object NCUtils extends LazyLogging:
def readResource(res: String, enc: String = "UTF-8", log: Logger =
logger): List[String] =
val list =
try
- Using.resource(Source.fromInputStream(getStream(res),
enc))(_.getLines()).toList
+ Using.resource(Source.fromInputStream(getStream(res),
enc))(_.getLines().toSeq).toList
catch
case e: IOException => throw new NCException(s"Failed to read
stream.", e)
diff --git
a/nlpcraft/src/test/java/org/apache/nlpcraft/internal/nlp/token/enricher/NCOpenNlpTokenEnricherSpec.scala
b/nlpcraft/src/test/java/org/apache/nlpcraft/internal/nlp/token/enricher/NCOpenNlpTokenEnricherSpec.scala
index 2b826b4..d3ed968 100644
---
a/nlpcraft/src/test/java/org/apache/nlpcraft/internal/nlp/token/enricher/NCOpenNlpTokenEnricherSpec.scala
+++
b/nlpcraft/src/test/java/org/apache/nlpcraft/internal/nlp/token/enricher/NCOpenNlpTokenEnricherSpec.scala
@@ -42,7 +42,7 @@ class NCOpenNlpTokenEnricherSpec:
def add(res: String): Unit =
enrichers += NCTestUtils.makeAndStart(new
NCOpenNlpTokenEnricher(s"opennlp/$res"))
- NCUtils.executeParallel(
+ NCUtils.execPar(
// en-ner-time.bin is skipped. I can't find any working example.
() => add("en-ner-location.bin"),
() => add("en-ner-money.bin"),
@@ -54,7 +54,7 @@ class NCOpenNlpTokenEnricherSpec:
private def check(txt: String, expected: String): Unit =
val req = NCTestRequest(txt)
- val toks = parser.parse(req)
+ val toks = parser.parse(req, null)
enrichers.foreach(_.enrich(req, null, toks))
val toksSeq = toks.asScala.toSeq