use of com.ibm.watson.assistant.v1.model.Value in project dna by leifeld.
the class Sql method getStatements.
/**
* Get statements, potentially filtered by statement IDs, document
* meta-data, date/time range, and duplicates setting.
*
* @param statementIds Array of statement IDs to retrieve. Can be empty or
* null, in which case all statements are selected.
* @param startDateTime Date/time before which statements are discarded.
* @param stopDateTime Date/time after which statements are discarded.
* @param authors Array list of document authors to exclude. Can be
* empty or null, in which case all statements are selected.
* @param authorInclude Include authors instead of excluding them?
* @param sources Array list of document sources to exclude. Can be
* empty or null, in which case all statements are selected.
* @param sourceInclude Include sources instead of excluding them?
* @param sections Array list of document sections to exclude. Can be
* empty or null, in which case all statements are selected.
* @param sectionInclude Include sections instead of excluding them?
* @param types Array list of document types to exclude. Can be
* empty or null, in which case all statements are selected.
* @param typeInclude Include types instead of excluding them?
* @return Array list of statements with all details.
*/
public ArrayList<Statement> getStatements(int[] statementIds, LocalDateTime startDateTime, LocalDateTime stopDateTime, ArrayList<String> authors, boolean authorInclude, ArrayList<String> sources, boolean sourceInclude, ArrayList<String> sections, boolean sectionInclude, ArrayList<String> types, boolean typeInclude) {
String whereStatements = "";
String whereShortText = "";
String whereLongText = "";
String whereBoolean = "";
String whereInteger = "";
if (statementIds != null && statementIds.length > 0) {
String ids = "";
for (int i = 0; i < statementIds.length; i++) {
ids = ids + statementIds[i];
if (i < statementIds.length - 1) {
ids = ids + ", ";
}
}
whereStatements = "WHERE STATEMENTS.ID IN (" + ids + ") ";
whereShortText = "AND DATASHORTTEXT.StatementId IN (" + ids + ") ";
whereLongText = "AND DATALONGTEXT.StatementId IN (" + ids + ") ";
whereBoolean = "AND DATABOOLEAN.StatementId IN (" + ids + ") ";
whereInteger = "AND DATAINTEGER.StatementId IN (" + ids + ") ";
}
if (startDateTime != null) {
whereStatements = whereStatements + "AND Date >= " + startDateTime.toEpochSecond(ZoneOffset.UTC) + " ";
whereShortText = whereShortText + "AND Date >= " + startDateTime.toEpochSecond(ZoneOffset.UTC) + " ";
whereLongText = whereLongText + "AND Date >= " + startDateTime.toEpochSecond(ZoneOffset.UTC) + " ";
whereBoolean = whereBoolean + "AND Date >= " + startDateTime.toEpochSecond(ZoneOffset.UTC) + " ";
whereInteger = whereInteger + "AND Date >= " + startDateTime.toEpochSecond(ZoneOffset.UTC) + " ";
}
if (stopDateTime != null) {
whereStatements = whereStatements + "AND Date <= " + stopDateTime.toEpochSecond(ZoneOffset.UTC) + " ";
whereShortText = whereShortText + "AND Date <= " + stopDateTime.toEpochSecond(ZoneOffset.UTC) + " ";
whereLongText = whereLongText + "AND Date <= " + stopDateTime.toEpochSecond(ZoneOffset.UTC) + " ";
whereBoolean = whereBoolean + "AND Date <= " + stopDateTime.toEpochSecond(ZoneOffset.UTC) + " ";
whereInteger = whereInteger + "AND Date <= " + stopDateTime.toEpochSecond(ZoneOffset.UTC) + " ";
}
if (authors != null && authors.size() > 0) {
String authorNot = "";
if (!authorInclude) {
authorNot = "NOT ";
}
String authorWhere = "AND DOCUMENTS.Author " + authorNot + "IN (" + authors.stream().collect(Collectors.joining("', '")) + ") ";
whereStatements = whereStatements + authorWhere;
whereShortText = whereShortText + authorWhere;
whereLongText = whereLongText + authorWhere;
whereBoolean = whereBoolean + authorWhere;
whereInteger = whereInteger + authorWhere;
}
if (sources != null && sources.size() > 0) {
String sourceNot = "";
if (!sourceInclude) {
sourceNot = "NOT ";
}
String sourceWhere = "AND DOCUMENTS.Source " + sourceNot + "IN (" + sources.stream().collect(Collectors.joining("', '")) + ") ";
whereStatements = whereStatements + sourceWhere;
whereShortText = whereShortText + sourceWhere;
whereLongText = whereLongText + sourceWhere;
whereBoolean = whereBoolean + sourceWhere;
whereInteger = whereInteger + sourceWhere;
}
if (sections != null && sections.size() > 0) {
String sectionNot = "";
if (!sectionInclude) {
sectionNot = "NOT ";
}
String sectionWhere = "AND DOCUMENTS.Section " + sectionNot + "IN (" + sections.stream().collect(Collectors.joining("', '")) + ") ";
whereStatements = whereStatements + sectionWhere;
whereShortText = whereShortText + sectionWhere;
whereLongText = whereLongText + sectionWhere;
whereBoolean = whereBoolean + sectionWhere;
whereInteger = whereInteger + sectionWhere;
}
if (types != null && types.size() > 0) {
String typeNot = "";
if (!typeInclude) {
typeNot = "NOT ";
}
String typeWhere = "AND DOCUMENTS.Type " + typeNot + "IN (" + types.stream().collect(Collectors.joining("', '")) + ") ";
whereStatements = whereStatements + typeWhere;
whereShortText = whereShortText + typeWhere;
whereLongText = whereLongText + typeWhere;
whereBoolean = whereBoolean + typeWhere;
whereInteger = whereInteger + typeWhere;
}
if (whereStatements.startsWith("AND")) {
// ensure correct form if no statement ID filtering
whereStatements = whereStatements.replaceFirst("AND", "WHERE");
}
String subString = "SUBSTRING(DOCUMENTS.Text, Start + 1, Stop - Start) AS Text ";
if (Dna.sql.getConnectionProfile().getType().equals("postgresql")) {
subString = "SUBSTRING(DOCUMENTS.Text, CAST(Start + 1 AS INT4), CAST(Stop - Start AS INT4)) AS Text ";
}
String q1 = "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 " + whereStatements + "ORDER BY DOCUMENTS.DATE ASC;";
String q2 = "SELECT ID FROM STATEMENTTYPES;";
String q3 = "SELECT ID, Variable, DataType FROM VARIABLES;";
String q4a = "SELECT DATASHORTTEXT.StatementId, VARIABLES.ID AS VariableId, ENTITIES.ID AS EntityId, ENTITIES.Value AS Value, ENTITIES.Red AS Red, ENTITIES.Green AS Green, ENTITIES.Blue AS Blue, ENTITIES.ChildOf AS ChildOf FROM DATASHORTTEXT " + "INNER JOIN VARIABLES ON VARIABLES.ID = DATASHORTTEXT.VariableId " + "INNER JOIN ENTITIES ON ENTITIES.VariableId = VARIABLES.ID AND ENTITIES.ID = DATASHORTTEXT.Entity " + "INNER JOIN STATEMENTS ON STATEMENTS.ID = DATASHORTTEXT.StatementId " + "INNER JOIN DOCUMENTS ON DOCUMENTS.ID = STATEMENTS.DocumentId " + "WHERE VARIABLES.StatementTypeId = ? " + whereShortText + "ORDER BY 1, 2 ASC;";
String q4b = "SELECT DATALONGTEXT.StatementId, VARIABLES.ID AS VariableId, DATALONGTEXT.Value FROM DATALONGTEXT " + "INNER JOIN VARIABLES ON VARIABLES.ID = DATALONGTEXT.VariableId " + "INNER JOIN STATEMENTS ON STATEMENTS.ID = DATALONGTEXT.StatementId " + "INNER JOIN DOCUMENTS ON DOCUMENTS.ID = STATEMENTS.DocumentId " + "WHERE VARIABLES.StatementTypeId = ? " + whereLongText + "ORDER BY 1, 2 ASC;";
String q4c = "SELECT DATABOOLEAN.StatementId, VARIABLES.ID AS VariableId, DATABOOLEAN.Value FROM DATABOOLEAN " + "INNER JOIN VARIABLES ON VARIABLES.ID = DATABOOLEAN.VariableId " + "INNER JOIN STATEMENTS ON STATEMENTS.ID = DATABOOLEAN.StatementId " + "INNER JOIN DOCUMENTS ON DOCUMENTS.ID = STATEMENTS.DocumentId " + "WHERE VARIABLES.StatementTypeId = ? " + whereBoolean + "ORDER BY 1, 2 ASC;";
String q4d = "SELECT DATAINTEGER.StatementId, VARIABLES.ID AS VariableId, DATAINTEGER.Value FROM DATAINTEGER " + "INNER JOIN VARIABLES ON VARIABLES.ID = DATAINTEGER.VariableId " + "INNER JOIN STATEMENTS ON STATEMENTS.ID = DATAINTEGER.StatementId " + "INNER JOIN DOCUMENTS ON DOCUMENTS.ID = STATEMENTS.DocumentId " + "WHERE VARIABLES.StatementTypeId = ? " + whereInteger + "ORDER BY 1, 2 ASC;";
String q5 = "SELECT AttributeVariable, AttributeValue FROM ATTRIBUTEVALUES " + "INNER JOIN ATTRIBUTEVARIABLES ON ATTRIBUTEVARIABLES.ID = AttributeVariableId " + "WHERE EntityId = ?;";
ArrayList<Statement> listOfStatements = null;
int statementTypeId, statementId, variableId, entityId;
Color sColor, cColor;
// variable ID to variable name
HashMap<Integer, String> variableNameMap = new HashMap<Integer, String>();
// variable ID to data type
HashMap<Integer, String> variableDataTypeMap = new HashMap<Integer, String>();
// statement ID to Statement
HashMap<Integer, Statement> statementMap = new HashMap<Integer, Statement>();
ResultSet r3, r4, r5;
try (Connection conn = Dna.sql.getDataSource().getConnection();
PreparedStatement s1 = conn.prepareStatement(q1);
PreparedStatement s2 = conn.prepareStatement(q2);
PreparedStatement s3 = conn.prepareStatement(q3);
PreparedStatement s4a = conn.prepareStatement(q4a);
PreparedStatement s4b = conn.prepareStatement(q4b);
PreparedStatement s4c = conn.prepareStatement(q4c);
PreparedStatement s4d = conn.prepareStatement(q4d);
PreparedStatement s5 = conn.prepareStatement(q5)) {
// assemble statements without values for now and save them in a hash map
ResultSet r1 = s1.executeQuery();
while (r1.next()) {
statementId = r1.getInt("StatementId");
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"));
Statement statement = new Statement(statementId, r1.getInt("Start"), r1.getInt("Stop"), statementTypeId, r1.getString("StatementTypeLabel"), sColor, r1.getInt("CoderId"), r1.getString("CoderName"), cColor, new ArrayList<Value>(), r1.getInt("DocumentId"), r1.getString("Text"), LocalDateTime.ofEpochSecond(r1.getLong("Date"), 0, ZoneOffset.UTC));
statementMap.put(statementId, statement);
}
// get variables
r3 = s3.executeQuery();
while (r3.next()) {
variableNameMap.put(r3.getInt("ID"), r3.getString("Variable"));
variableDataTypeMap.put(r3.getInt("ID"), r3.getString("DataType"));
}
// get statement types
ResultSet r2 = s2.executeQuery();
while (r2.next()) {
statementTypeId = r2.getInt("ID");
// get values and put them into the statements
s4a.setInt(1, statementTypeId);
r4 = s4a.executeQuery();
while (r4.next()) {
variableId = r4.getInt("VariableId");
entityId = r4.getInt("EntityId");
HashMap<String, String> map = new HashMap<String, String>();
s5.setInt(1, entityId);
r5 = s5.executeQuery();
while (r5.next()) {
map.put(r5.getString("AttributeVariable"), r5.getString("AttributeValue"));
}
Entity e = new Entity(entityId, variableId, r4.getString("Value"), new Color(r4.getInt("Red"), r4.getInt("Green"), r4.getInt("Blue")), r4.getInt("ChildOf"), true, map);
statementMap.get(r4.getInt("StatementId")).getValues().add(new Value(variableId, variableNameMap.get(variableId), variableDataTypeMap.get(variableId), e));
}
s4b.setInt(1, statementTypeId);
r4 = s4b.executeQuery();
while (r4.next()) {
variableId = r4.getInt("VariableId");
String value = r4.getString("Value");
statementMap.get(r4.getInt("StatementId")).getValues().add(new Value(variableId, variableNameMap.get(variableId), variableDataTypeMap.get(variableId), value));
}
s4c.setInt(1, statementTypeId);
r4 = s4c.executeQuery();
while (r4.next()) {
variableId = r4.getInt("VariableId");
int value = r4.getInt("Value");
statementMap.get(r4.getInt("StatementId")).getValues().add(new Value(variableId, variableNameMap.get(variableId), variableDataTypeMap.get(variableId), value));
}
s4d.setInt(1, statementTypeId);
r4 = s4d.executeQuery();
while (r4.next()) {
variableId = r4.getInt("VariableId");
int value = r4.getInt("Value");
statementMap.get(r4.getInt("StatementId")).getValues().add(new Value(variableId, variableNameMap.get(variableId), variableDataTypeMap.get(variableId), value));
}
}
// assemble and sort all statements
Collection<Statement> s = statementMap.values();
listOfStatements = new ArrayList<Statement>(s);
Collections.sort(listOfStatements);
} catch (SQLException e) {
LogEvent l = new LogEvent(Logger.WARNING, "[SQL] Failed to retrieve statements.", "Attempted to retrieve a set of " + statementIds.length + " statements from the database, but something went wrong.", e);
Dna.logger.log(l);
}
return listOfStatements;
}
use of com.ibm.watson.assistant.v1.model.Value in project dna by leifeld.
the class AttributeManager method updateVariableBox.
/**
* Populate the variable combo box with variables after a statement type has
* been selected in the statement type combo box.
*/
private void updateVariableBox() {
StatementType st = (StatementType) statementTypeBox.getSelectedItem();
ArrayList<Value> values = st.getVariables();
variableBox.removeAllItems();
for (int i = 0; i < values.size(); i++) {
if (values.get(i).getDataType().equals("short text")) {
variableBox.addItem(values.get(i));
}
}
}
use of com.ibm.watson.assistant.v1.model.Value in project java-sdk by watson-developer-cloud.
the class ValuesIT method testUpdateValue.
/**
* Test updateValue.
*/
@Test
public void testUpdateValue() {
String entity = "beverage";
String entityValue1 = "coffee" + UUID.randomUUID().toString();
String entityValue2 = "coffee" + UUID.randomUUID().toString();
String synonym1 = "java";
String synonym2 = "joe";
// metadata
Map<String, Object> valueMetadata = new HashMap<String, Object>();
String metadataValue = "value for " + entityValue2;
valueMetadata.put("key", metadataValue);
try {
CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
service.createEntity(createOptions).execute();
} 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, entityValue1).build();
service.createValue(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
UpdateValueOptions updateOptions = new UpdateValueOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue1).newValue(entityValue2).newSynonyms(new ArrayList<String>(Arrays.asList(synonym1, synonym2))).newMetadata(valueMetadata).build();
Value response = service.updateValue(updateOptions).execute();
try {
assertNotNull(response);
assertNotNull(response.getValueText());
assertEquals(response.getValueText(), entityValue2);
GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue2).export(true).includeAudit(true).build();
ValueExport vResponse = service.getValue(getOptions).execute();
assertNotNull(vResponse);
assertNotNull(vResponse.getValueText());
assertEquals(vResponse.getValueText(), entityValue2);
assertNotNull(vResponse.getCreated());
assertNotNull(vResponse.getUpdated());
assertNotNull(vResponse.getSynonyms());
assertTrue(vResponse.getSynonyms().size() == 2);
assertTrue(vResponse.getSynonyms().contains(synonym1));
assertTrue(vResponse.getSynonyms().contains(synonym2));
// metadata
assertNotNull(response.getMetadata());
assertNotNull(response.getMetadata().get("key"));
assertEquals(response.getMetadata().get("key"), metadataValue);
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
// Clean up
DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue2).build();
service.deleteValue(deleteOptions).execute();
}
}
use of com.ibm.watson.assistant.v1.model.Value in project java-sdk by watson-developer-cloud.
the class Conversation method createValue.
/**
* Add entity value.
*
* Create a new value for an entity.
*
* @param createValueOptions the {@link CreateValueOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link Value}
*/
public ServiceCall<Value> createValue(CreateValueOptions createValueOptions) {
Validator.notNull(createValueOptions, "createValueOptions cannot be null");
String[] pathSegments = { "v1/workspaces", "entities", "values" };
String[] pathParameters = { createValueOptions.workspaceId(), createValueOptions.entity() };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
builder.query(VERSION, versionDate);
final JsonObject contentJson = new JsonObject();
contentJson.addProperty("value", createValueOptions.value());
if (createValueOptions.metadata() != null) {
contentJson.add("metadata", GsonSingleton.getGson().toJsonTree(createValueOptions.metadata()));
}
if (createValueOptions.synonyms() != null) {
contentJson.add("synonyms", GsonSingleton.getGson().toJsonTree(createValueOptions.synonyms()));
}
if (createValueOptions.patterns() != null) {
contentJson.add("patterns", GsonSingleton.getGson().toJsonTree(createValueOptions.patterns()));
}
if (createValueOptions.valueType() != null) {
contentJson.addProperty("type", createValueOptions.valueType());
}
builder.bodyJson(contentJson);
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Value.class));
}
use of com.ibm.watson.assistant.v1.model.Value in project java-sdk by watson-developer-cloud.
the class Conversation method updateValue.
/**
* Update entity value.
*
* Update the content of a value for an entity.
*
* @param updateValueOptions the {@link UpdateValueOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link Value}
*/
public ServiceCall<Value> updateValue(UpdateValueOptions updateValueOptions) {
Validator.notNull(updateValueOptions, "updateValueOptions cannot be null");
String[] pathSegments = { "v1/workspaces", "entities", "values" };
String[] pathParameters = { updateValueOptions.workspaceId(), updateValueOptions.entity(), updateValueOptions.value() };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
builder.query(VERSION, versionDate);
final JsonObject contentJson = new JsonObject();
if (updateValueOptions.newSynonyms() != null) {
contentJson.add("synonyms", GsonSingleton.getGson().toJsonTree(updateValueOptions.newSynonyms()));
}
if (updateValueOptions.valueType() != null) {
contentJson.addProperty("type", updateValueOptions.valueType());
}
if (updateValueOptions.newMetadata() != null) {
contentJson.add("metadata", GsonSingleton.getGson().toJsonTree(updateValueOptions.newMetadata()));
}
if (updateValueOptions.newPatterns() != null) {
contentJson.add("patterns", GsonSingleton.getGson().toJsonTree(updateValueOptions.newPatterns()));
}
if (updateValueOptions.newValue() != null) {
contentJson.addProperty("value", updateValueOptions.newValue());
}
builder.bodyJson(contentJson);
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Value.class));
}
Aggregations