This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These changes allow NLU classification requests to follow either a synchronous or asynchronous pattern. They also introduce a context object for NLU operations so that trace events can be surfaced to interested parties. Doing this involved an abstraction of `SpeechContext`'s tracing capabilities to make them resuable across components.
- Loading branch information
1 parent
716d57b
commit b93f227
Showing
15 changed files
with
411 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/java/io/spokestack/spokestack/nlu/NLUContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package io.spokestack.spokestack.nlu; | ||
|
||
import io.spokestack.spokestack.SpeechConfig; | ||
import io.spokestack.spokestack.util.EventTracer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Context for NLU operations, including request metadata and a facility for | ||
* dispatch of trace events. | ||
*/ | ||
final class NLUContext { | ||
|
||
private final EventTracer tracer; | ||
private final List<TraceListener> listeners; | ||
private Map<String, Object> requestMetadata; | ||
|
||
/** | ||
* Create a new dispatcher. | ||
* | ||
* @param config the dispatcher's configuration | ||
*/ | ||
NLUContext(SpeechConfig config) { | ||
int traceLevel = config.getInteger( | ||
"trace-level", | ||
EventTracer.Level.NONE.value()); | ||
|
||
this.tracer = new EventTracer(traceLevel); | ||
this.listeners = new ArrayList<>(); | ||
} | ||
|
||
/** | ||
* Add a listener interested in receiving NLU trace events. | ||
* | ||
* @param listener the listener to add | ||
*/ | ||
public void addTraceListener(TraceListener listener) { | ||
this.listeners.add(listener); | ||
} | ||
|
||
/** | ||
* @return the metadata for the current request. | ||
*/ | ||
public Map<String, Object> getRequestMetadata() { | ||
return requestMetadata; | ||
} | ||
|
||
/** | ||
* Set the metadata for the current request. | ||
* @param metadata the metadata for the current request. | ||
*/ | ||
public void setRequestMetadata(Map<String, Object> metadata) { | ||
this.requestMetadata = metadata; | ||
} | ||
|
||
/** | ||
* Traces a debug level message. | ||
* | ||
* @param format trace message format string | ||
* @param params trace message format parameters | ||
*/ | ||
public void traceDebug(String format, Object... params) { | ||
trace(EventTracer.Level.DEBUG, format, params); | ||
} | ||
|
||
/** | ||
* Traces a performance level message. | ||
* | ||
* @param format trace message format string | ||
* @param params trace message format parameters | ||
*/ | ||
public void tracePerf(String format, Object... params) { | ||
trace(EventTracer.Level.PERF, format, params); | ||
} | ||
|
||
/** | ||
* Traces an informational level message. | ||
* | ||
* @param format trace message format string | ||
* @param params trace message format parameters | ||
*/ | ||
public void traceInfo(String format, Object... params) { | ||
trace(EventTracer.Level.INFO, format, params); | ||
} | ||
|
||
/** | ||
* Raises a trace event. | ||
* | ||
* @param level tracing level | ||
* @param format trace message format string | ||
* @param params trace message format parameters | ||
*/ | ||
public void trace( | ||
EventTracer.Level level, | ||
String format, | ||
Object... params) { | ||
if (this.tracer.canTrace(level)) { | ||
String message = String.format(format, params); | ||
dispatchTrace(message); | ||
} | ||
} | ||
|
||
/** | ||
* Dispatches an NLU trace message. | ||
* | ||
* @param message the trace message to publish | ||
*/ | ||
public void dispatchTrace(String message) { | ||
for (TraceListener listener : this.listeners) { | ||
try { | ||
listener.onTrace(message); | ||
} catch (Exception e) { | ||
// failed traces fail in silence | ||
} | ||
} | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
src/main/java/io/spokestack/spokestack/nlu/NLURequest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/io/spokestack/spokestack/nlu/TraceListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.spokestack.spokestack.nlu; | ||
|
||
/** | ||
* A simple interface implemented by classes interested in receiving trace | ||
* events. | ||
*/ | ||
public interface TraceListener { | ||
|
||
/** | ||
* A notification that a trace event has occurred. | ||
* @param message the trace event's message. | ||
*/ | ||
void onTrace(String message); | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/io/spokestack/spokestack/util/AsyncResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.spokestack.spokestack.util; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.CancellationException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.FutureTask; | ||
|
||
/** | ||
* A subclass of {@link FutureTask} that allows the user to register a callback | ||
* to be executed when the task's result is available. | ||
* | ||
* @param <T> The type of result produced by the task and expected by the | ||
* optional callback. | ||
*/ | ||
public class AsyncResult<T> extends FutureTask<T> { | ||
|
||
private Callback<T> completionCallback; | ||
|
||
/** | ||
* Create a new task. | ||
* | ||
* @param callable The {@code Callable} representing the task's work. | ||
*/ | ||
public AsyncResult(@NotNull Callable<T> callable) { | ||
super(callable); | ||
} | ||
|
||
/** | ||
* Register a callback to be executed when the task's result is available. | ||
* If the task has already been completed, the callback will be called | ||
* immediately. | ||
* | ||
* @param callback The function to be called with the task's result. | ||
*/ | ||
public void registerCallback(Callback<T> callback) { | ||
this.completionCallback = callback; | ||
if (isDone()) { | ||
try { | ||
callback.call(get()); | ||
} catch (CancellationException | InterruptedException | ||
| ExecutionException e) { | ||
callback.onError(e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected void done() { | ||
if (this.completionCallback != null) { | ||
try { | ||
completionCallback.call(get()); | ||
} catch (CancellationException | InterruptedException | ||
| ExecutionException e) { | ||
completionCallback.onError(e); | ||
} | ||
} | ||
super.done(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.spokestack.spokestack.util; | ||
|
||
/** | ||
* A simple interface representing a callback function called with a single | ||
* argument. Since callbacks are often used to propagate results of | ||
* asynchronous operations, the callback may also receive an error generated | ||
* during the task meant to produce its result. | ||
* | ||
* @param <T> The type of result with which the callback should be executed. | ||
*/ | ||
public interface Callback<T> { | ||
|
||
/** | ||
* Call the callback with the specified argument. | ||
* @param arg The callback's argument. | ||
*/ | ||
void call(T arg); | ||
|
||
/** | ||
* Call the callback with an error generated during an asynchronous task. | ||
* @param err An error generated instead of the callback's intended result. | ||
*/ | ||
void onError(Throwable err); | ||
} |
Oops, something went wrong.