use of com.ibm.watson.discovery.v1.model.TrainingExample in project java-sdk by watson-developer-cloud.
the class Discovery method createTrainingExample.
/**
* Adds a new example to this training data query.
*
* @param createTrainingExampleOptions the {@link CreateTrainingExampleOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link TrainingExample}
*/
public ServiceCall<TrainingExample> createTrainingExample(CreateTrainingExampleOptions createTrainingExampleOptions) {
Validator.notNull(createTrainingExampleOptions, "createTrainingExampleOptions cannot be null");
String[] pathSegments = { "v1/environments", "collections", "training_data", "examples" };
String[] pathParameters = { createTrainingExampleOptions.environmentId(), createTrainingExampleOptions.collectionId(), createTrainingExampleOptions.queryId() };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
builder.query(VERSION, versionDate);
final JsonObject contentJson = new JsonObject();
if (createTrainingExampleOptions.documentId() != null) {
contentJson.addProperty("document_id", createTrainingExampleOptions.documentId());
}
if (createTrainingExampleOptions.crossReference() != null) {
contentJson.addProperty("cross_reference", createTrainingExampleOptions.crossReference());
}
if (createTrainingExampleOptions.relevance() != null) {
contentJson.addProperty("relevance", createTrainingExampleOptions.relevance());
}
builder.bodyJson(contentJson);
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(TrainingExample.class));
}
use of com.ibm.watson.discovery.v1.model.TrainingExample in project java-sdk by watson-developer-cloud.
the class DiscoveryTest method testUpdateTrainingQueryWOptions.
@Test
public void testUpdateTrainingQueryWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"query_id\": \"queryId\", \"natural_language_query\": \"naturalLanguageQuery\", \"filter\": \"filter\", \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\", \"examples\": [{\"document_id\": \"documentId\", \"collection_id\": \"collectionId\", \"relevance\": 9, \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\"}]}";
String updateTrainingQueryPath = "/v2/projects/testString/training_data/queries/testString";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the TrainingExample model
TrainingExample trainingExampleModel = new TrainingExample.Builder().documentId("testString").collectionId("testString").relevance(Long.valueOf("26")).build();
// Construct an instance of the UpdateTrainingQueryOptions model
UpdateTrainingQueryOptions updateTrainingQueryOptionsModel = new UpdateTrainingQueryOptions.Builder().projectId("testString").queryId("testString").naturalLanguageQuery("testString").examples(new java.util.ArrayList<TrainingExample>(java.util.Arrays.asList(trainingExampleModel))).filter("testString").build();
// Invoke operation with valid options model (positive test)
Response<TrainingQuery> response = discoveryService.updateTrainingQuery(updateTrainingQueryOptionsModel).execute();
assertNotNull(response);
TrainingQuery responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "POST");
// Check query
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
// Get query params
assertEquals(query.get("version"), "testString");
// Check request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, updateTrainingQueryPath);
}
use of com.ibm.watson.discovery.v1.model.TrainingExample in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceTest method createTrainingExampleIsSuccessful.
/**
* Creates the training example is successful.
*
* @throws InterruptedException the interrupted exception
*/
@Test
public void createTrainingExampleIsSuccessful() throws InterruptedException {
server.enqueue(jsonResponse(createTrainingExampleResp));
CreateTrainingExampleOptions.Builder builder = new CreateTrainingExampleOptions.Builder(environmentId, collectionId, queryId);
builder.documentId(documentId);
builder.relevance(0);
TrainingExample response = discoveryService.createTrainingExample(builder.build()).execute().getResult();
RecordedRequest request = server.takeRequest();
assertEquals(TRAINING2_PATH, request.getPath());
assertEquals(POST, request.getMethod());
assertEquals(createTrainingExampleResp, response);
}
use of com.ibm.watson.discovery.v1.model.TrainingExample in project java-sdk by watson-developer-cloud.
the class DiscoveryTest method testAddTrainingDataWOptions.
// Test the addTrainingData operation with a valid options model parameter
@Test
public void testAddTrainingDataWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "{\"query_id\": \"queryId\", \"natural_language_query\": \"naturalLanguageQuery\", \"filter\": \"filter\", \"examples\": [{\"document_id\": \"documentId\", \"cross_reference\": \"crossReference\", \"relevance\": 9}]}";
String addTrainingDataPath = "/v1/environments/testString/collections/testString/training_data";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
// Construct an instance of the TrainingExample model
TrainingExample trainingExampleModel = new TrainingExample.Builder().documentId("testString").crossReference("testString").relevance(Long.valueOf("26")).build();
// Construct an instance of the AddTrainingDataOptions model
AddTrainingDataOptions addTrainingDataOptionsModel = new AddTrainingDataOptions.Builder().environmentId("testString").collectionId("testString").naturalLanguageQuery("testString").filter("testString").examples(new java.util.ArrayList<TrainingExample>(java.util.Arrays.asList(trainingExampleModel))).build();
// Invoke addTrainingData() with a valid options model and verify the result
Response<TrainingQuery> response = discoveryService.addTrainingData(addTrainingDataOptionsModel).execute();
assertNotNull(response);
TrainingQuery responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request sent to the mock server
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "POST");
// Verify request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, addTrainingDataPath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("version"), "testString");
}
use of com.ibm.watson.discovery.v1.model.TrainingExample in project java-sdk by watson-developer-cloud.
the class Discovery method updateTrainingExample.
/**
* Change label or cross reference for example.
*
* <p>Changes the label or cross reference query for this training data example.
*
* @param updateTrainingExampleOptions the {@link UpdateTrainingExampleOptions} containing the
* options for the call
* @return a {@link ServiceCall} with a result of type {@link TrainingExample}
*/
public ServiceCall<TrainingExample> updateTrainingExample(UpdateTrainingExampleOptions updateTrainingExampleOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(updateTrainingExampleOptions, "updateTrainingExampleOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("environment_id", updateTrainingExampleOptions.environmentId());
pathParamsMap.put("collection_id", updateTrainingExampleOptions.collectionId());
pathParamsMap.put("query_id", updateTrainingExampleOptions.queryId());
pathParamsMap.put("example_id", updateTrainingExampleOptions.exampleId());
RequestBuilder builder = RequestBuilder.put(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/collections/{collection_id}/training_data/{query_id}/examples/{example_id}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "updateTrainingExample");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
builder.query("version", String.valueOf(this.version));
final JsonObject contentJson = new JsonObject();
if (updateTrainingExampleOptions.crossReference() != null) {
contentJson.addProperty("cross_reference", updateTrainingExampleOptions.crossReference());
}
if (updateTrainingExampleOptions.relevance() != null) {
contentJson.addProperty("relevance", updateTrainingExampleOptions.relevance());
}
builder.bodyJson(contentJson);
ResponseConverter<TrainingExample> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<TrainingExample>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
Aggregations