use of com.ibm.watson.assistant.v1.model.Value in project java-sdk by watson-developer-cloud.
the class SynonymsIT method testUpdateSynonym.
/**
* Test updateSynonym.
*/
@Test
public void testUpdateSynonym() {
String entity = "beverage";
String entityValue = "coffee";
String synonym1 = "joe";
String synonym2 = "mud";
try {
CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
service.createEntity(createOptions).execute().getResult();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue).build();
service.createValue(createOptions).execute().getResult();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
service.createSynonym(createOptions).execute().getResult();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
UpdateSynonymOptions updateOptions = new UpdateSynonymOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).synonym(synonym1).newSynonym(synonym2).build();
Synonym response = service.updateSynonym(updateOptions).execute().getResult();
try {
assertNotNull(response);
assertNotNull(response.synonym());
assertEquals(response.synonym(), synonym2);
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
// Clean up
DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym2).build();
service.deleteSynonym(deleteOptions).execute().getResult();
}
}
use of com.ibm.watson.assistant.v1.model.Value in project java-sdk by watson-developer-cloud.
the class ValuesIT method testGetValue.
/**
* Test getValue.
*/
@Test
public void testGetValue() {
String entity = "beverage";
String entityValue = "coffee" + UUID.randomUUID().toString();
String synonym1 = "java";
String synonym2 = "joe";
try {
CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
service.createEntity(createOptions).execute().getResult();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue).synonyms(new ArrayList<String>(Arrays.asList(synonym1, synonym2))).build();
service.createValue(createOptions).execute().getResult();
Date start = new Date();
try {
GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue).export(true).includeAudit(true).build();
Value response = service.getValue(getOptions).execute().getResult();
assertNotNull(response);
assertNotNull(response.value());
assertEquals(response.value(), entityValue);
assertNotNull(response.created());
assertNotNull(response.updated());
Date now = new Date();
assertTrue(fuzzyBefore(response.created(), now));
assertTrue(fuzzyAfter(response.created(), start));
assertTrue(fuzzyBefore(response.updated(), now));
assertTrue(fuzzyAfter(response.updated(), start));
assertNotNull(response.synonyms());
assertTrue(response.synonyms().size() == 2);
assertTrue(response.synonyms().contains(synonym1));
assertTrue(response.synonyms().contains(synonym2));
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue).build();
service.deleteValue(deleteOptions).execute().getResult();
}
}
use of com.ibm.watson.assistant.v1.model.Value in project java-sdk by watson-developer-cloud.
the class ValuesIT method testCreateValue.
/**
* Test createValue.
*/
@Test
public void testCreateValue() {
String entity = "beverage";
String entityValue = "coffee" + UUID.randomUUID().toString();
// metadata
Map<String, Object> valueMetadata = new HashMap<String, Object>();
String metadataValue = "value for " + entityValue;
valueMetadata.put("key", metadataValue);
try {
CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
service.createEntity(createOptions).execute().getResult();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
CreateValueOptions createOptions = new CreateValueOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).metadata(valueMetadata).build();
Value response = service.createValue(createOptions).execute().getResult();
try {
assertNotNull(response);
assertNotNull(response.value());
assertEquals(response.value(), entityValue);
assertNotNull(response.metadata());
// metadata
assertNotNull(response.metadata());
assertNotNull(response.metadata().get("key"));
assertEquals(response.metadata().get("key"), metadataValue);
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
// Clean up
DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue).build();
service.deleteValue(deleteOptions).execute().getResult();
}
}
use of com.ibm.watson.assistant.v1.model.Value in project dna by leifeld.
the class Sql method getStatementTypes.
/* =========================================================================
* Statement types
* ====================================================================== */
/**
* Get an array list of all statement types in the database. The variable
* definitions are saved as an array list of {@link model.Value Value}
* objects containing the variable ID, variable name, and data type.
*
* @return An ArrayList of {@link model.StatementType StatementType} objects.
*
* @category statementtype
*/
public ArrayList<StatementType> getStatementTypes() {
ArrayList<StatementType> statementTypes = new ArrayList<StatementType>();
try (Connection conn = ds.getConnection();
PreparedStatement s1 = conn.prepareStatement("SELECT * FROM STATEMENTTYPES;");
PreparedStatement s2 = conn.prepareStatement("SELECT * FROM VARIABLES WHERE StatementTypeId = ?;")) {
ArrayList<Value> variables;
int statementTypeId;
ResultSet r1 = s1.executeQuery();
ResultSet r2;
Color color;
while (r1.next()) {
variables = new ArrayList<Value>();
statementTypeId = r1.getInt("ID");
color = new Color(r1.getInt("Red"), r1.getInt("Green"), r1.getInt("Blue"));
s2.setInt(1, statementTypeId);
r2 = s2.executeQuery();
while (r2.next()) {
variables.add(new Value(r2.getInt("ID"), r2.getString("Variable"), r2.getString("DataType")));
}
statementTypes.add(new StatementType(r1.getInt("ID"), r1.getString("Label"), color, variables));
}
LogEvent l = new LogEvent(Logger.MESSAGE, "[SQL] Retrieved " + statementTypes.size() + " statement types from the database.", "Retrieved " + statementTypes.size() + " statement types from the database.");
Dna.logger.log(l);
} catch (SQLException e1) {
LogEvent l = new LogEvent(Logger.ERROR, "[SQL] Failed to retrieve statement types from the database.", "Failed to retrieve statement types from the database. Check database connection and consistency of the STATEMENTTYPES and VARIABLES tables in the database.", e1);
Dna.logger.log(l);
}
return statementTypes;
}
use of com.ibm.watson.assistant.v1.model.Value in project dna by leifeld.
the class Sql method getStatement.
/**
* Get a statement from the database based on its ID.
*
* @param statementId The statement ID of the statement to be retrieved.
* @return A {@link model.Statement Statement} with all relevant
* values for the different variables.
*
* @category statement
*/
public Statement getStatement(int statementId) {
Statement statement = null;
ArrayList<Value> values;
int statementTypeId, variableId, entityId;
String variable, dataType;
Color aColor, sColor, cColor;
HashMap<String, String> map;
String subString = "SUBSTRING(DOCUMENTS.Text, Start + 1, Stop - Start) AS Text ";
if (getConnectionProfile().getType().equals("postgresql")) {
subString = "SUBSTRING(DOCUMENTS.Text, CAST(Start + 1 AS INT4), CAST(Stop - Start AS INT4)) AS Text ";
}
String s1Query = "SELECT STATEMENTS.ID AS StatementId, " + "StatementTypeId, " + "STATEMENTTYPES.Label AS StatementTypeLabel, " + "STATEMENTTYPES.Red AS StatementTypeRed, " + "STATEMENTTYPES.Green AS StatementTypeGreen, " + "STATEMENTTYPES.Blue AS StatementTypeBlue, " + "Start, " + "Stop, " + "STATEMENTS.Coder AS CoderId, " + "CODERS.Name AS CoderName, " + "CODERS.Red AS CoderRed, " + "CODERS.Green AS CoderGreen, " + "CODERS.Blue AS CoderBlue, " + "DocumentId, " + "DOCUMENTS.Date AS Date, " + subString + "FROM STATEMENTS " + "INNER JOIN CODERS ON STATEMENTS.Coder = CODERS.ID " + "INNER JOIN STATEMENTTYPES ON STATEMENTS.StatementTypeId = STATEMENTTYPES.ID " + "INNER JOIN DOCUMENTS ON DOCUMENTS.ID = STATEMENTS.DocumentId " + "WHERE STATEMENTS.ID = ?;";
try (Connection conn = ds.getConnection();
PreparedStatement s1 = conn.prepareStatement(s1Query);
PreparedStatement s2 = conn.prepareStatement("SELECT ID, Variable, DataType FROM VARIABLES WHERE StatementTypeId = ?;");
PreparedStatement s3 = conn.prepareStatement("SELECT E.ID AS EntityId, StatementId, E.VariableId, DST.ID AS DataId, E.Value, Red, Green, Blue, ChildOf FROM DATASHORTTEXT AS DST LEFT JOIN ENTITIES AS E ON E.ID = DST.Entity AND E.VariableId = DST.VariableId WHERE DST.StatementId = ? AND DST.VariableId = ?;");
PreparedStatement s4 = conn.prepareStatement("SELECT Value FROM DATALONGTEXT WHERE VariableId = ? AND StatementId = ?;");
PreparedStatement s5 = conn.prepareStatement("SELECT Value FROM DATAINTEGER WHERE VariableId = ? AND StatementId = ?;");
PreparedStatement s6 = conn.prepareStatement("SELECT Value FROM DATABOOLEAN WHERE VariableId = ? AND StatementId = ?;");
PreparedStatement s7 = conn.prepareStatement("SELECT AttributeVariable, AttributeValue FROM ATTRIBUTEVALUES AS AVAL INNER JOIN ATTRIBUTEVARIABLES AS AVAR ON AVAL.AttributeVariableId = AVAR.ID WHERE EntityId = ?;")) {
ResultSet r1, r2, r3, r4;
// first, get the statement information, including coder and statement type info
s1.setInt(1, statementId);
r1 = s1.executeQuery();
while (r1.next()) {
statementTypeId = r1.getInt("StatementTypeId");
sColor = new Color(r1.getInt("StatementTypeRed"), r1.getInt("StatementTypeGreen"), r1.getInt("StatementTypeBlue"));
cColor = new Color(r1.getInt("CoderRed"), r1.getInt("CoderGreen"), r1.getInt("CoderBlue"));
// second, get the variables associated with the statement type
s2.setInt(1, statementTypeId);
r2 = s2.executeQuery();
values = new ArrayList<Value>();
while (r2.next()) {
variableId = r2.getInt("ID");
variable = r2.getString("Variable");
dataType = r2.getString("DataType");
// third, get the values from DATABOOLEAN, DATAINTEGER, DATALONGTEXT, and DATASHORTTEXT
if (dataType.equals("short text")) {
s3.setInt(1, statementId);
s3.setInt(2, variableId);
r3 = s3.executeQuery();
while (r3.next()) {
entityId = r3.getInt("EntityId");
aColor = new Color(r3.getInt("Red"), r3.getInt("Green"), r3.getInt("Blue"));
// fourth, in the case of short text, also look up information in ENTITIES table
s7.setInt(1, entityId);
r4 = s7.executeQuery();
map = new HashMap<String, String>();
while (r4.next()) {
map.put(r4.getString("AttributeVariable"), r4.getString("AttributeValue"));
}
Entity entity = new Entity(entityId, variableId, r3.getString("Value"), aColor, r3.getInt("ChildOf"), true, map);
values.add(new Value(variableId, variable, dataType, entity));
}
} else if (dataType.equals("long text")) {
s4.setInt(1, variableId);
s4.setInt(2, statementId);
r3 = s4.executeQuery();
while (r3.next()) {
values.add(new Value(variableId, variable, dataType, r3.getString("Value")));
}
} else if (dataType.equals("integer")) {
s5.setInt(1, variableId);
s5.setInt(2, statementId);
r3 = s5.executeQuery();
while (r3.next()) {
values.add(new Value(variableId, variable, dataType, r3.getInt("Value")));
}
} else if (dataType.equals("boolean")) {
s6.setInt(1, variableId);
s6.setInt(2, statementId);
r3 = s6.executeQuery();
while (r3.next()) {
values.add(new Value(variableId, variable, dataType, r3.getInt("Value")));
}
}
}
// assemble the statement with all the information from the previous steps
statement = new Statement(statementId, r1.getInt("Start"), r1.getInt("Stop"), statementTypeId, r1.getString("StatementTypeLabel"), sColor, r1.getInt("CoderId"), r1.getString("CoderName"), cColor, values, r1.getInt("DocumentId"), r1.getString("Text"), LocalDateTime.ofEpochSecond(r1.getLong("Date"), 0, ZoneOffset.UTC));
LogEvent l = new LogEvent(Logger.MESSAGE, "[SQL] Statement " + statementId + " was retrieved from the database.", "Statement " + statementId + " was retrieved from the database.");
Dna.logger.log(l);
}
} catch (SQLException e) {
LogEvent l = new LogEvent(Logger.WARNING, "[SQL] Failed to retrieve Statement " + statementId + " from database.", "Failed to retrieve Statement " + statementId + " from database. Check if the connection is still there, the database file has not been moved, and make sure a statement with this ID actually exists in the database.", e);
Dna.logger.log(l);
}
return statement;
}
Aggregations