This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch NLPCRAFT-383_1 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit 29d3ee2604c26baca767e1589db5782b88838df7 Author: Sergey Kamov <[email protected]> AuthorDate: Sun Aug 8 10:54:08 2021 +0300 WIP. --- nlpcraft-examples/solarsystem/pom.xml | 26 ++++++++++ .../examples/solarsystem/SolarSystemApp.java | 17 +++++++ .../examples/solarsystem/SolarSystemConfig.java | 33 +++++++++++++ .../examples/solarsystem/SolarSystemModel.scala | 18 +------ .../examples/solarsystem/SolarSystemProbe.java | 30 ++++++++++++ .../api/SolarSystemOpenApiService.scala | 26 ++++------ .../loaders/SolarSystemDiscoversValueLoader.java | 54 +++++++++++++++++++++ .../loaders/SolarSystemDiscoversValueLoader.scala | 53 --------------------- .../loaders/SolarSystemPlanetsValueLoader.java | 55 ++++++++++++++++++++++ .../loaders/SolarSystemPlanetsValueLoader.scala | 35 -------------- .../solarsystem/src/main/resources/probe.conf | 7 +++ .../org/apache/nlpcraft/probe/NCProbeBoot.scala | 2 +- 12 files changed, 234 insertions(+), 122 deletions(-) diff --git a/nlpcraft-examples/solarsystem/pom.xml b/nlpcraft-examples/solarsystem/pom.xml index 23b4079..b104a3b 100644 --- a/nlpcraft-examples/solarsystem/pom.xml +++ b/nlpcraft-examples/solarsystem/pom.xml @@ -43,6 +43,28 @@ <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + <version>1.5.11.RELEASE</version> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot</artifactId> + <version>1.5.11.RELEASE</version> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-logging</artifactId> + <version>1.5.11.RELEASE</version> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-slf4j-impl</artifactId> + <version>2.14.1</version> + <scope>test</scope> + </dependency> + <!-- Test dependencies. --> <dependency> <groupId>org.junit.jupiter</groupId> @@ -62,6 +84,10 @@ <build> <plugins> <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + </plugin> + <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.plugin.ver}</version> diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemApp.java b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemApp.java new file mode 100644 index 0000000..24cb8b1 --- /dev/null +++ b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemApp.java @@ -0,0 +1,17 @@ +package org.apache.nlpcraft.examples.solarsystem; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class SolarSystemApp { + public static void main(String[] args) { + SpringApplication.run(SolarSystemApp.class, args); + } + + @Bean + public SolarSystemProbe getSolarSystemProbe() { + return new SolarSystemProbe(); + } +} diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemConfig.java b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemConfig.java new file mode 100644 index 0000000..c49fbd4 --- /dev/null +++ b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemConfig.java @@ -0,0 +1,33 @@ +package org.apache.nlpcraft.examples.solarsystem; + +import org.apache.nlpcraft.examples.solarsystem.SolarSystemModel; +import org.apache.nlpcraft.examples.solarsystem.SolarSystemProbe; +import org.apache.nlpcraft.examples.solarsystem.api.SolarSystemOpenApiService; +import org.apache.nlpcraft.examples.solarsystem.loaders.SolarSystemDiscoversValueLoader; +import org.apache.nlpcraft.examples.solarsystem.loaders.SolarSystemPlanetsValueLoader; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +public class SolarSystemConfig { + + + @Bean + public SolarSystemModel getSolarSystemModel(SolarSystemOpenApiService s) { + return new SolarSystemModel(s); + } + + @Bean + public SolarSystemOpenApiService getSolarSystemOpenApiService() { + return new SolarSystemOpenApiService(); + } + + @Bean + public SolarSystemPlanetsValueLoader getSolarSystemPlanetsValueLoader(SolarSystemOpenApiService s) { + return new SolarSystemPlanetsValueLoader(s); + } + + @Bean + public SolarSystemDiscoversValueLoader getSolarSystemDiscoversValueLoader(SolarSystemOpenApiService s) { + return new SolarSystemDiscoversValueLoader(s); + } +} diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemModel.scala b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemModel.scala index 6bf36ac..ee88135 100644 --- a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemModel.scala +++ b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemModel.scala @@ -20,27 +20,13 @@ package org.apache.nlpcraft.examples.solarsystem import com.typesafe.scalalogging.LazyLogging import org.apache.nlpcraft.examples.solarsystem.api.SolarSystemOpenApiService import org.apache.nlpcraft.model.{NCIntent, NCIntentSample, NCIntentTerm, NCModelFileAdapter, NCResult, NCToken} +import org.springframework.beans.factory.annotation.Autowired import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder, DateTimeParseException} import java.time.temporal.ChronoField._ import java.time.{LocalDate, ZoneOffset} -class SolarSystemModel extends NCModelFileAdapter("solarsystem_model.yaml") with LazyLogging { - private var api: SolarSystemOpenApiService = _ - - override def onInit(): Unit = { - api = SolarSystemOpenApiService.getInstance() - - logger.info("Solar System Model started.") - } - - override def onDiscard(): Unit = { - if (api != null) - api.stop() - - logger.info("Solar System Model stopped.") - } - +class SolarSystemModel(api: SolarSystemOpenApiService) extends NCModelFileAdapter("solarsystem_model.yaml") with LazyLogging { @NCIntentSample( Array( "Moon!", diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemProbe.java b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemProbe.java new file mode 100644 index 0000000..07e50c0 --- /dev/null +++ b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemProbe.java @@ -0,0 +1,30 @@ +package org.apache.nlpcraft.examples.solarsystem; + +import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe; +import org.apache.nlpcraft.probe.NCProbe; +import org.apache.nlpcraft.probe.NCProbeBoot; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +import java.util.concurrent.CompletableFuture; + +public class SolarSystemProbe { + @PostConstruct + public void start() { + CompletableFuture<Integer> fut = new CompletableFuture<>(); + + NCProbeBoot.start(new String[]{}, fut); +// +// while (!fut.isDone()) +// fut.get(); +// } +// +// if (fut.get() != 0) +// System.exit(fut.get) + } + + @PreDestroy + public void stop() { + NCEmbeddedProbe.stop(); + } +} diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/api/SolarSystemOpenApiService.scala b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/api/SolarSystemOpenApiService.scala index f859583..425fe5a 100644 --- a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/api/SolarSystemOpenApiService.scala +++ b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/api/SolarSystemOpenApiService.scala @@ -20,29 +20,19 @@ package org.apache.nlpcraft.examples.solarsystem.api import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.scala.DefaultScalaModule import com.typesafe.scalalogging.LazyLogging -import org.apache.nlpcraft.examples.solarsystem.api.SolarSystemOpenApiService.BodiesBean -import java.net.{URI, URLEncoder} import java.net.http.HttpClient.Version import java.net.http.{HttpClient, HttpRequest, HttpResponse} +import java.net.{URI, URLEncoder} +import javax.annotation.{PostConstruct, PreDestroy} +import scala.jdk.CollectionConverters.{MapHasAsJava, SeqHasAsJava} object SolarSystemOpenApiService { case class BodiesBean(bodies: Seq[Map[String, Object]]) - - private var s: SolarSystemOpenApiService = _ - - def getInstance(): SolarSystemOpenApiService = - this.synchronized { - if (s == null) { - s = new SolarSystemOpenApiService - - s.start() - } - - s - } } +import SolarSystemOpenApiService._ + class SolarSystemOpenApiService extends LazyLogging { private final val URL_BODIES = "https://api.le-systeme-solaire.net/rest/bodies" private final val MAPPER = new ObjectMapper().registerModule(DefaultScalaModule) @@ -100,6 +90,7 @@ class SolarSystemOpenApiService extends LazyLogging { } } + @PostConstruct def start(): Unit = { client = HttpClient.newBuilder.version(Version.HTTP_2).build @@ -119,6 +110,7 @@ class SolarSystemOpenApiService extends LazyLogging { ) } + @PreDestroy def stop(): Unit = { planets = null discovers = null @@ -128,6 +120,6 @@ class SolarSystemOpenApiService extends LazyLogging { logger.info(s"Solar System Open Api Service stopped.") } - def getAllPlanets: Map[String, String] = planets - def getAllDiscovers: Seq[String] = discovers + def getAllPlanets: java.util.Map[String, String] = planets.asJava + def getAllDiscovers: java.util.List[String] = discovers.asJava } diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemDiscoversValueLoader.java b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemDiscoversValueLoader.java new file mode 100644 index 0000000..b69f176 --- /dev/null +++ b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemDiscoversValueLoader.java @@ -0,0 +1,54 @@ +package org.apache.nlpcraft.examples.solarsystem.loaders; + +import org.apache.nlpcraft.examples.solarsystem.api.SolarSystemOpenApiService; +import org.apache.nlpcraft.model.NCElement; +import org.apache.nlpcraft.model.NCValue; +import org.apache.nlpcraft.model.NCValueLoader; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class SolarSystemDiscoversValueLoader implements NCValueLoader { + private SolarSystemOpenApiService api; + + public SolarSystemDiscoversValueLoader(SolarSystemOpenApiService api) { + this.api = api; + } + + private NCValue mkValue(String discInfo) { + List<String> syns = new ArrayList<>(); + + Arrays.stream(discInfo.split(" ")).map(p -> p.strip()).filter(p -> !p.isEmpty()).forEach(p -> { + String lc = p.toLowerCase(); + + syns.add(lc); + + int lastNameIdx = lc.lastIndexOf(" "); + + // Tries to detect last name. + if (lastNameIdx > 0) + syns.add(lc.substring(lastNameIdx + 1)); + + }); + + return new NCValue() { + @Override + public String getName() { + return discInfo; + } + + @Override + public List<String> getSynonyms() { + return syns; + } + }; + } + + @Override + public Set<NCValue> load(NCElement owner) { + return api.getAllDiscovers().stream().map(this::mkValue).collect(Collectors.toSet()); + } +} diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemDiscoversValueLoader.scala b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemDiscoversValueLoader.scala deleted file mode 100644 index 7598419..0000000 --- a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemDiscoversValueLoader.scala +++ /dev/null @@ -1,53 +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.examples.solarsystem.loaders - -import org.apache.nlpcraft.examples.solarsystem.api.SolarSystemOpenApiService -import org.apache.nlpcraft.model.{NCElement, NCValue, NCValueLoader} - -import java.util -import scala.collection.mutable.ArrayBuffer -import scala.jdk.CollectionConverters.{SeqHasAsJava, SetHasAsJava} - -class SolarSystemDiscoversValueLoader extends NCValueLoader { - // Example: "Scott S. Sheppard, David C. Jewitt" - private def mkValue(discInfo: String): NCValue = { - val syns = ArrayBuffer.empty[String] - - for (d <- discInfo.split(",").map(_.trim).filter(_.nonEmpty)) { - val lc = d.toLowerCase - - syns += lc - - val lastNameIdx = lc.lastIndexOf(" ") - - // Tries to detect last name. - if (lastNameIdx > 0) - syns += lc.substring(lastNameIdx + 1) - } - - new NCValue { - override def getName: String = discInfo - override def getSynonyms: util.List[String] = syns.asJava - } - } - - override def load(owner: NCElement): util.Set[NCValue] = - SolarSystemOpenApiService.getInstance().getAllDiscovers.map(mkValue).toSet.asJava - -} diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemPlanetsValueLoader.java b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemPlanetsValueLoader.java new file mode 100644 index 0000000..ce5620f --- /dev/null +++ b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemPlanetsValueLoader.java @@ -0,0 +1,55 @@ +/* + * 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.examples.solarsystem.loaders; + +import org.apache.nlpcraft.examples.solarsystem.api.SolarSystemOpenApiService; +import org.apache.nlpcraft.model.NCElement; +import org.apache.nlpcraft.model.NCValue; +import org.apache.nlpcraft.model.NCValueLoader; + +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class SolarSystemPlanetsValueLoader implements NCValueLoader { + private SolarSystemOpenApiService api; + + public SolarSystemPlanetsValueLoader(SolarSystemOpenApiService api) { + this.api = api; + } + + private NCValue mkValue(String id, String v) { + return new NCValue() { + + @Override + public String getName() { + return id; + } + + @Override + public List<String> getSynonyms() { + return List.of(id.toLowerCase(), v.toLowerCase()); + } + }; + } + + @Override + public Set<NCValue> load(NCElement owner) { + return api.getAllPlanets().entrySet().stream().map(p -> mkValue(p.getKey(), p.getValue())).collect(Collectors.toSet()); + } +} diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemPlanetsValueLoader.scala b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemPlanetsValueLoader.scala deleted file mode 100644 index fb90fe3..0000000 --- a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemPlanetsValueLoader.scala +++ /dev/null @@ -1,35 +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.examples.solarsystem.loaders - -import org.apache.nlpcraft.examples.solarsystem.api.SolarSystemOpenApiService -import org.apache.nlpcraft.model.{NCElement, NCValue, NCValueLoader} - -import java.util -import scala.jdk.CollectionConverters.{SeqHasAsJava, SetHasAsJava} - -class SolarSystemPlanetsValueLoader extends NCValueLoader { - private def mkValue(id: String, v: String): NCValue = - new NCValue { - override def getName: String = id - override def getSynonyms: util.List[String] = Seq(id.toLowerCase, v.toLowerCase).asJava - } - - override def load(owner: NCElement): util.Set[NCValue] = - SolarSystemOpenApiService.getInstance().getAllPlanets.map{ case (id, v) => mkValue(id, v) }.toSet.asJava -} diff --git a/nlpcraft-examples/solarsystem/src/main/resources/probe.conf b/nlpcraft-examples/solarsystem/src/main/resources/probe.conf index faea33f..997387a 100644 --- a/nlpcraft-examples/solarsystem/src/main/resources/probe.conf +++ b/nlpcraft-examples/solarsystem/src/main/resources/probe.conf @@ -108,6 +108,13 @@ nlpcraft { # on how to configure 3rd party token providers: models = org.apache.nlpcraft.examples.solarsystem.SolarSystemModel + modelFactory = { + type = "org.apache.nlpcraft.model.factories.spring.NCSpringModelFactory" + properties = { + javaConfig = "org.apache.nlpcraft.examples.solarsystem.SolarSystemConfig" + } + } + # Specify class names for probe life cycle components. # Each class should extend 'NCProbeLifecycle' interface and provide a no-arg constructor. # diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala index 756481d..fa31450 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala @@ -191,7 +191,7 @@ private [probe] object NCProbeBoot extends LazyLogging with NCOpenCensusTrace { override def run(): Unit = start0(cfg, fut) }.start() } - + /** * * @param cfg Probe configuration.
