|
| 1 | +/* |
| 2 | + * Copyright 2015 IBM Corp. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package com.ibm.watson.developer_cloud.conversation.v1.model; |
| 15 | + |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import com.google.gson.annotations.SerializedName; |
| 20 | +import com.ibm.watson.developer_cloud.conversation.v1.ConversationService; |
| 21 | +import com.ibm.watson.developer_cloud.service.model.GenericModel; |
| 22 | + |
| 23 | +/** |
| 24 | + * Object that represents the input and context of a conversation. This is used by the |
| 25 | + * {@link ConversationService#message(String, MessageRequest)} method |
| 26 | + * |
| 27 | + * @see <a href="http://www.ibm.com/watson/developercloud/conversation.html"> http://www.ibm.com/ |
| 28 | + * watson/developercloud/conversation.html</a> |
| 29 | + * |
| 30 | + */ |
| 31 | +public class MessageRequest extends GenericModel { |
| 32 | + |
| 33 | + /** |
| 34 | + * The Class Builder. |
| 35 | + */ |
| 36 | + public static class Builder { |
| 37 | + private static final String TEXT = "text"; |
| 38 | + private Map<String, Object> context; |
| 39 | + private Map<String, Object> input; |
| 40 | + private Boolean alternateIntents; |
| 41 | + |
| 42 | + /** |
| 43 | + * Instantiates a new Builder. |
| 44 | + */ |
| 45 | + public Builder() {} |
| 46 | + |
| 47 | + /** |
| 48 | + * Instantiates a new builder. |
| 49 | + * |
| 50 | + * @param messageRequest the message request |
| 51 | + */ |
| 52 | + private Builder(MessageRequest messageRequest) { |
| 53 | + this.input = new HashMap<String, Object>(messageRequest.input); |
| 54 | + this.context = new HashMap<String, Object>(messageRequest.context); |
| 55 | + this.alternateIntents = messageRequest.alternateIntents; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Generates a new {@link MessageRequest} object. It will contain the parameters set in the |
| 60 | + * builder. |
| 61 | + * |
| 62 | + * @return a {@link MessageRequest} instance |
| 63 | + */ |
| 64 | + public MessageRequest build() { |
| 65 | + return new MessageRequest(this); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Sets the context/state which is to be sent to the message API as a part of the service |
| 70 | + * request. Each response from the message API returns a <code>context</code> object which |
| 71 | + * represents the state as defined by the service. The state is not maintained by the service, |
| 72 | + * so the client must keep the state from each API call, and pass that state in as a part of any |
| 73 | + * subsequent requests. |
| 74 | + * |
| 75 | + * @param context a map containing key value pairs representing the state/context of the |
| 76 | + * conversation |
| 77 | + * |
| 78 | + * @return a builder object |
| 79 | + */ |
| 80 | + public Builder context(final Map<String, Object> context) { |
| 81 | + this.context = context != null ? new HashMap<String, Object>(context) : context; |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Sets the alternate intents flag. Set to true to return all matching intents |
| 87 | + * |
| 88 | + * @param alternateIntents the alternate intents flag |
| 89 | + * @return a builder object |
| 90 | + */ |
| 91 | + public Builder alternateIntents(final Boolean alternateIntents) { |
| 92 | + this.alternateIntents = alternateIntents; |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Sets the input which is to be sent to the message API as a part of the service request. |
| 98 | + * Typically the input will contain a <code>text</code> property (key and value). The |
| 99 | + * <code>text</code> property is generally interpreted as being the user/system input which the |
| 100 | + * service must parse for intents, entities etc..<br> |
| 101 | + * In advanced cases the client may pass in more than just text as a part of the service input. |
| 102 | + * For the majority of cases calling the {@link #inputText(String)} method is sufficient to send |
| 103 | + * text to the service on behalf of the user/system. |
| 104 | + * |
| 105 | + * @param input a map of properties to be sent to the service under the input property |
| 106 | + * @return a builder object |
| 107 | + */ |
| 108 | + public Builder input(Map<String, Object> input) { |
| 109 | + this.input = input != null ? new HashMap<String, Object>(input) : null; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Sets the input text which is to be sent to the message API as a part of the service request. |
| 115 | + * |
| 116 | + * @param text the textual value to be assigned to the 'text' property on the input object. |
| 117 | + * @return a builder object |
| 118 | + */ |
| 119 | + public Builder inputText(String text) { |
| 120 | + if (input == null) { |
| 121 | + input = new HashMap<String, Object>(); |
| 122 | + } |
| 123 | + input.put(TEXT, text); |
| 124 | + return this; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private Map<String, Object> context; |
| 129 | + private Map<String, Object> input; |
| 130 | + @SerializedName("alternate_intents") |
| 131 | + private Boolean alternateIntents; |
| 132 | + |
| 133 | + /** |
| 134 | + * Creates a new instance of the MessageRequest for the {@link ConversationService} service. |
| 135 | + * Clients must use the {@link Builder} class to construct new instances of the class. |
| 136 | + * |
| 137 | + * @param options a builder configured with the various parameters for the request |
| 138 | + */ |
| 139 | + private MessageRequest(Builder options) { |
| 140 | + this.context = options.context; |
| 141 | + this.input = options.input; |
| 142 | + this.alternateIntents = options.alternateIntents; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Returns a map used to store context/state for the message API. Each response from the message |
| 147 | + * API will return a context object as a part of the payload. This context must be maintained and |
| 148 | + * passed in as a part of subsequent API calls. |
| 149 | + * |
| 150 | + * @return a map of properties |
| 151 | + */ |
| 152 | + public Map<String, Object> context() { |
| 153 | + return context; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Whether to return more than one intent. Set to true to return all matching intents boolean<br/> |
| 158 | + * Default: false |
| 159 | + * |
| 160 | + * @return the boolean indicating if alternate intents should be returned |
| 161 | + */ |
| 162 | + public Boolean alternateIntents() { |
| 163 | + return alternateIntents; |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + /** |
| 168 | + * Returns a map storing the input which is to be sent to the service as a part of the API |
| 169 | + * request. |
| 170 | + * |
| 171 | + * @return a map of properties |
| 172 | + */ |
| 173 | + public Map<String, Object> input() { |
| 174 | + return input; |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + /** |
| 179 | + * Convenience method which allows the developer to quickly retrieve the 'text' property from the |
| 180 | + * input object. This is equivalent to calling: |
| 181 | + * |
| 182 | + * <pre> |
| 183 | + * Map<String, Object> input = request.getInput(); |
| 184 | + * String text = null; |
| 185 | + * if (input != null) { |
| 186 | + * text = input.get("text"); |
| 187 | + * } |
| 188 | + * </pre> |
| 189 | + * |
| 190 | + * @return the value of the <code>input.text</code> property |
| 191 | + */ |
| 192 | + public String inputText() { |
| 193 | + return (input != null && input.get("text") != null) ? input.get("text").toString() : null; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * New builder. |
| 198 | + * |
| 199 | + * @return the builder |
| 200 | + */ |
| 201 | + public Builder newBuilder() { |
| 202 | + return new Builder(this); |
| 203 | + } |
| 204 | +} |
0 commit comments