This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch master-model
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/master-model by this push:
new 89ab77e WIP.
89ab77e is described below
commit 89ab77e9ef3e17e3bda36228499cb68b540ed424
Author: Sergey Kamov <[email protected]>
AuthorDate: Fri Oct 8 09:45:34 2021 +0300
WIP.
---
.../apache/nlpcraft/{model => }/NCNlpcraft.java | 4 +-
.../{model/builders => }/NCNlpcraftBuilder.java | 3 +-
.../scala/org/apache/nlpcraft/model/NCResult.java | 234 +--------------------
.../org/apache/nlpcraft/{model => }/NCSpec.java | 17 +-
4 files changed, 22 insertions(+), 236 deletions(-)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCNlpcraft.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCNlpcraft.java
similarity index 92%
rename from nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCNlpcraft.java
rename to nlpcraft/src/main/scala/org/apache/nlpcraft/NCNlpcraft.java
index ae230d5..91ac031 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCNlpcraft.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCNlpcraft.java
@@ -1,4 +1,6 @@
-package org.apache.nlpcraft.model;
+package org.apache.nlpcraft;
+
+import org.apache.nlpcraft.model.NCResult;
import java.util.List;
import java.util.Map;
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/builders/NCNlpcraftBuilder.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCNlpcraftBuilder.java
similarity index 77%
rename from
nlpcraft/src/main/scala/org/apache/nlpcraft/model/builders/NCNlpcraftBuilder.java
rename to nlpcraft/src/main/scala/org/apache/nlpcraft/NCNlpcraftBuilder.java
index 6c8589b..b5d3255 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/builders/NCNlpcraftBuilder.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCNlpcraftBuilder.java
@@ -1,7 +1,6 @@
-package org.apache.nlpcraft.model.builders;
+package org.apache.nlpcraft;
import org.apache.nlpcraft.model.NCModel;
-import org.apache.nlpcraft.model.NCNlpcraft;
public class NCNlpcraftBuilder {
public NCNlpcraftBuilder withModel(NCModel mdl) {
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCResult.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCResult.java
index 1613589..ce77f12 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCResult.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCResult.java
@@ -23,236 +23,12 @@ import org.apache.nlpcraft.model.impl.NCMetadataAdapter;
import java.io.Serializable;
import java.util.Collection;
+public interface NCResult extends NCMetadata {
+ Collection<NCToken> getTokens();
-/**
- * Data model result returned from model intent callbacks. Result consists of
the
- * text body and the type. The type is similar in notion to MIME types.
- * <table class="dl-table">
- * <caption>Supported result types:</caption>
- * <tr>
- * <th>Result Type</th>
- * <th>Factory Method</th>
- * </tr>
- * <tr>
- * <td><code>text</code></td>
- * <td>{@link #text(String)}</td>
- * </tr>
- * <tr>
- * <td><code>html</code></td>
- * <td>{@link #html(String)}</td>
- * </tr>
- * <tr>
- * <td><code>json</code></td>
- * <td>{@link #json(String)}</td>
- * </tr>
- * <tr>
- * <td><code>yaml</code></td>
- * <td>{@link #yaml(String)}</td>
- * </tr>
- * <tr>
- * <td><code>confirm</code></td>
- * <td>{@link #confirm(String)}</td>
- * </tr>
- * </table>
- * Note that all of these types have specific meaning for client applications
that interpret them
- * accordingly. For example, the REST client interfacing between NLPCraft and
Amazon Alexa or Apple HomeKit can only
- * accept {@code text} result type and ignore everything else.
- */
-public class NCResult extends NCMetadataAdapter implements Serializable,
NCMetadata {
- // TODO: why is it not interface?
- /** Data Model result text. */
- private String body;
-
- /** Data Model result type. One of text, html, json or yaml. */
- private String type;
-
- /** Sequence of tokens represents a fully parsed (see {@link
NCContext#getVariants()} method) user input. */
- private Collection<NCToken> tokens;
-
- /** ID of the intent. */
- private String intentId;
-
- /**
- * Creates new result with given body and type.
- *
- * @param body Result body.
- * @param type Result type.
- * @throws IllegalArgumentException Thrown if type is invalid.
- */
- public NCResult(String body, String type) {
- assert body != null;
- assert type != null;
-
- this.body = body;
- this.type = checkType(type);
- }
-
- /**
- * No-arg constructor.
- */
- public NCResult() {
- // No-op.
- }
-
- /**
- * Creates {@code text} result.
- *
- * @param txt Textual result. Text interpretation will be defined by the
client receiving this result.
- * @return Newly created query result.
- */
- public static NCResult text(String txt) {
- return new NCResult(txt, "text");
- }
-
- /**
- * Creates {@code html} result.
- *
- * @param html HTML markup.
- * @return Newly created query result.
- */
- public static NCResult html(String html) {
- return new NCResult(html, "html");
- }
-
- /**
- * Creates {@code confirm} result.
- *
- * @param body Confirm result body.
- * @return Newly created query result.
- */
- public static NCResult confirm(String body) {
- return new NCResult(body, "confirm");
- }
-
- /**
- * Creates {@code json} result. Note that this method will test given JSON
string
- * for validness by using <code>com.google.gson.Gson</code> JSON utility.
If JSON string is invalid
- * the {@link IllegalArgumentException} exception will be thrown.
- *
- * @param json Any JSON string to be rendered on the client.
- * @return Newly created query result.
- * @throws IllegalArgumentException Thrown if given JSON string is invalid.
- */
- public static NCResult json(String json) {
- // Validation.
- try {
- NCUtils.jsonToObject(json);
- }
- catch (NCException e) {
- throw new IllegalArgumentException(String.format("Invalid JSON
value: %s.", json), e.getCause());
- }
-
- return new NCResult(json, "json");
- }
-
- /**
- * Creates {@code yaml} result.
- *
- * @param yaml Any YAML string to be rendered on the client.
- * @return Newly created query result.
- */
- public static NCResult yaml(String yaml) {
- return new NCResult(yaml, "yaml");
- }
-
- /**
- *
- * @param type Type to check.
- * @throws IllegalArgumentException Thrown if type is invalid.
- */
- private String checkType(String type) {
- String typeLc = type.toLowerCase();
-
- if (!typeLc.equals("html") &&
- !typeLc.equals("json") &&
- !typeLc.equals("yaml") &&
- !typeLc.equals("confirm") &&
- !typeLc.equals("text"))
- throw new IllegalArgumentException("Invalid result type: " + type);
- else
- return typeLc;
- }
-
- /**
- * Sets result body.
- *
- * @param body Result body.
- */
- public void setBody(String body) {
- this.body = body;
- }
-
- /**
- * Set result type.
- *
- * @param type Result type.
- * @throws IllegalArgumentException Thrown if type is invalid.
- */
- public void setType(String type) {
- this.type = checkType(type);
- }
-
- /**
- * Gets tokens that were used to produce this query result. Note that the
- * returned tokens can come from the current request as well as from the
conversation (i.e. from
- * previous requests). Order of tokens is not important.
- *
- * @return Gets tokens that were used to produce this query result.
- * @see #setTokens(Collection)
- */
- public Collection<NCToken> getTokens() {
- return tokens;
- }
-
- /**
- * Sets a collection of tokens that was used to produce this query result.
Note that the
- * returned tokens can come from the current request as well as from the
conversation (i.e. from
- * previous requests). Order of tokens is not important.
- * <p>
- * Providing these tokens is necessary for proper STM operation. If
conversational support isn't used
- * setting these tokens is not required. Note that built-in intent based
matched automatically sets
- * these tokens.
- *
- * @param tokens Collection of tokens that was used to produce this query
result.
- * @see #getTokens()
- */
- public void setTokens(Collection<NCToken> tokens) {
- this.tokens = tokens;
- }
-
- /**
- * Gets result type.
- *
- * @return Result type.
- */
- public String getType() {
- return type;
- }
-
- /**
- * Gets result body.
- *
- * @return Result body.
- */
- public String getBody() {
- return body;
- }
+ String getType();
- /**
- * Get optional intent ID.
- *
- * @return Intent ID or {@code null} if intent ID was not available.
- */
- public String getIntentId() {
- return intentId;
- }
+ Object getBody();
- /**
- * Sets optional intent ID.
- *
- * @param intentId Intent ID to set for this result.
- */
- public void setIntentId(String intentId) {
- this.intentId = intentId;
- }
+ String getIntentId();
}
diff --git a/nlpcraft/src/test/java/org/apache/nlpcraft/model/NCSpec.java
b/nlpcraft/src/test/java/org/apache/nlpcraft/NCSpec.java
similarity index 84%
rename from nlpcraft/src/test/java/org/apache/nlpcraft/model/NCSpec.java
rename to nlpcraft/src/test/java/org/apache/nlpcraft/NCSpec.java
index dc501be..b82b707 100644
--- a/nlpcraft/src/test/java/org/apache/nlpcraft/model/NCSpec.java
+++ b/nlpcraft/src/test/java/org/apache/nlpcraft/NCSpec.java
@@ -1,10 +1,19 @@
-package org.apache.nlpcraft.model;
-
+package org.apache.nlpcraft;
+
+import org.apache.nlpcraft.NCNlpcraft;
+import org.apache.nlpcraft.NCNlpcraftBuilder;
+import org.apache.nlpcraft.model.NCElement;
+import org.apache.nlpcraft.model.NCIntentMatch;
+import org.apache.nlpcraft.model.NCModel;
+import org.apache.nlpcraft.model.NCRejection;
+import org.apache.nlpcraft.model.NCResult;
+import org.apache.nlpcraft.model.NCValue;
+import org.apache.nlpcraft.model.NCValueLoader;
import org.apache.nlpcraft.model.annotations.NCIntentRef;
-import org.apache.nlpcraft.model.builders.NCNlpcraftBuilder;
import org.apache.nlpcraft.model.builders.NCSingleElementBuilder;
import org.apache.nlpcraft.model.builders.NCModelBuilder;
import org.apache.nlpcraft.model.builders.NCMultiElementsBuilder;
+import org.apache.nlpcraft.model.builders.NCResultBuilder;
import org.junit.jupiter.api.Test;
import java.util.Collections;
@@ -70,7 +79,7 @@ public class NCSpec {
withOnRejection(new BiFunction<NCIntentMatch, NCRejection,
NCResult>() {
@Override
public NCResult apply(NCIntentMatch math, NCRejection rej)
{
- return NCResult.text("OK");
+ return new
NCResultBuilder().withBody("test").getResult();
}
}).
getModel();