use of org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint in project stanbol by apache.
the class FieldQueryToJsonUtils method convertConstraintToJSON.
/**
* Converts a {@link Constraint} to JSON
*
* @param constraint the {@link Constraint}
* @param nsPrefixService Optionally the service that is used to convert data type
* URIs to '{prefix}:{localname}'
* @return the JSON representation
* @throws JSONException
*/
private static JSONObject convertConstraintToJSON(Constraint constraint, NamespacePrefixService nsPrefixService) throws JSONException {
JSONObject jConstraint = new JSONObject();
jConstraint.put("type", constraint.getType().name());
switch(constraint.getType()) {
case //both ValueConstraint and ReferenceConstraint
value:
ValueConstraint valueConstraint = ((ValueConstraint) constraint);
if (valueConstraint.getValues() != null) {
if (valueConstraint.getValues().size() == 1) {
jConstraint.put("value", valueConstraint.getValues().iterator().next());
} else {
jConstraint.put("value", new JSONArray(valueConstraint.getValues()));
}
}
if (constraint instanceof ReferenceConstraint) {
//the type "reference" is not present in the ConstraintType
//enum, because internally ReferenceConstraints are just a
//ValueConstraint with a predefined data type, but "reference"
//is still a valid value of the type property in JSON
jConstraint.put("type", "reference");
} else {
// valueConstraint
jConstraint.put("type", constraint.getType().name());
//for valueConstraints we need to add also the dataType(s)
Collection<String> dataTypes = valueConstraint.getDataTypes();
if (dataTypes != null && !dataTypes.isEmpty()) {
if (dataTypes.size() == 1) {
String dataType = dataTypes.iterator().next();
jConstraint.put("datatype", nsPrefixService != null ? nsPrefixService.getShortName(dataType) : dataType);
} else {
ArrayList<String> dataTypeValues = new ArrayList<String>(dataTypes.size());
for (String dataType : dataTypes) {
dataTypeValues.add(nsPrefixService != null ? nsPrefixService.getShortName(dataType) : dataType);
}
jConstraint.put("datatype", dataTypeValues);
}
}
}
//finally write the MODE
if (valueConstraint.getMode() != null) {
jConstraint.put("mode", valueConstraint.getMode());
}
break;
case text:
TextConstraint textConstraint = (TextConstraint) constraint;
Collection<String> languages = textConstraint.getLanguages();
if (languages != null && !languages.isEmpty()) {
if (languages.size() == 1) {
jConstraint.put("language", languages.iterator().next());
} else {
jConstraint.put("language", new JSONArray(languages));
}
}
jConstraint.put("patternType", textConstraint.getPatternType().name());
if (textConstraint.getTexts() != null && !textConstraint.getTexts().isEmpty()) {
if (textConstraint.getTexts().size() == 1) {
//write a string
jConstraint.put("text", textConstraint.getTexts().get(0));
} else {
//write an array
jConstraint.put("text", textConstraint.getTexts());
}
}
if (textConstraint.isCaseSensitive()) {
jConstraint.put("caseSensitive", true);
}
//write the proximity ranking state (if defined)
if (textConstraint.isProximityRanking() != null) {
jConstraint.put("proximityRanking", textConstraint.isProximityRanking());
}
break;
case range:
RangeConstraint rangeConstraint = (RangeConstraint) constraint;
Set<DataTypeEnum> dataTypes = EnumSet.noneOf(DataTypeEnum.class);
if (rangeConstraint.getLowerBound() != null) {
jConstraint.put("lowerBound", rangeConstraint.getLowerBound());
dataTypes.addAll(DataTypeEnum.getPrimaryDataTypes(rangeConstraint.getLowerBound().getClass()));
}
if (rangeConstraint.getUpperBound() != null) {
jConstraint.put("upperBound", rangeConstraint.getUpperBound());
dataTypes.addAll(DataTypeEnum.getPrimaryDataTypes(rangeConstraint.getUpperBound().getClass()));
}
jConstraint.put("inclusive", rangeConstraint.isInclusive());
if (!dataTypes.isEmpty()) {
jConstraint.put("datatype", dataTypes.iterator().next().getShortName());
}
break;
case similarity:
SimilarityConstraint simConstraint = (SimilarityConstraint) constraint;
jConstraint.put("context", simConstraint.getContext());
if (!simConstraint.getAdditionalFields().isEmpty()) {
jConstraint.put("addFields", new JSONArray(simConstraint.getAdditionalFields()));
}
break;
default:
//unknown constraint type
log.warn("Unsupported Constriant Type " + constraint.getType() + " (implementing class=" + constraint.getClass() + "| toString=" + constraint + ") -> skiped");
break;
}
return jConstraint;
}
use of org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint in project stanbol by apache.
the class FieldQueryReader method parseReferenceConstraint.
/**
* @param jConstraint
* @return
* @throws JSONException
*/
private static Constraint parseReferenceConstraint(JSONObject jConstraint, NamespacePrefixService nsPrefixService) throws JSONException {
final List<String> refList;
if (jConstraint.has("value") && !jConstraint.isNull("value")) {
Object value = jConstraint.get("value");
if (value instanceof JSONArray) {
refList = new ArrayList<String>(((JSONArray) value).length());
for (int i = 0; i < ((JSONArray) value).length(); i++) {
String field = ((JSONArray) value).getString(i);
field = field != null ? nsPrefixService.getFullName(field) : null;
if (field != null && !field.isEmpty()) {
refList.add(field);
}
}
} else if (value instanceof JSONObject) {
log.warn("Parsed ValueConstraint does define illegal values (values={})!", value);
StringBuilder message = new StringBuilder();
message.append("Parsed ValueConstraint does define illegal value for field 'value'" + "(value MUST NOT be an JSON object. Only values and JSONArray to parse" + "multiple values are allowed)!\n");
message.append("Parsed Constraint: \n");
message.append(jConstraint.toString(4));
throw new IllegalArgumentException(message.toString());
} else {
String field = jConstraint.getString("value");
field = field != null ? nsPrefixService.getFullName(field) : null;
if (field != null) {
refList = Collections.singletonList(field);
} else {
refList = Collections.emptyList();
}
}
if (refList.isEmpty()) {
log.warn("Parsed ReferenceConstraint does not define a single valid \"value\"!");
StringBuilder message = new StringBuilder();
message.append("Parsed ReferenceConstraint does not define a single valid 'value'!\n");
message.append("This means values where only null, empty string or '{prefix}:{localname}' values with unknown {prefix}\n");
message.append("Parsed Constraint: \n");
message.append(jConstraint.toString(4));
throw new IllegalArgumentException(message.toString());
}
MODE mode = parseConstraintValueMode(jConstraint);
return new ReferenceConstraint(refList, mode);
} else {
log.warn("Parsed ReferenceConstraint does not define the required field \"value\"!");
StringBuilder message = new StringBuilder();
message.append("Parsed ReferenceConstraint does not define the required field 'value'!\n");
message.append("Parsed Constraint: \n");
message.append(jConstraint.toString(4));
throw new IllegalArgumentException(message.toString());
}
}
use of org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint in project stanbol by apache.
the class FieldQueryReader method parseValueConstraint.
/**
* @param jConstraint
* @return
* @throws JSONException
*/
private static Constraint parseValueConstraint(JSONObject jConstraint, NamespacePrefixService nsPrefixService) throws JSONException {
Collection<String> dataTypes = parseDatatypeProperty(jConstraint, nsPrefixService);
final List<Object> valueList;
if (jConstraint.has("value") && !jConstraint.isNull("value")) {
Object value = jConstraint.get("value");
if (value instanceof JSONArray) {
valueList = new ArrayList<Object>(((JSONArray) value).length());
for (int i = 0; i < ((JSONArray) value).length(); i++) {
Object v = ((JSONArray) value).get(i);
if (v == null || v instanceof JSONArray || v instanceof JSONObject) {
log.warn("Parsed ValueConstraint does define illegal values (values={})!", value);
StringBuilder message = new StringBuilder();
message.append("Parsed ValueConstraint does define illegal values for field 'value'" + "(value MUST NOT contain NULL, JSONObject nor JSONArray values)!\n");
message.append("Parsed Constraint: \n");
message.append(jConstraint.toString(4));
throw new IllegalArgumentException(message.toString());
}
valueList.add(v);
}
} else if (value instanceof JSONObject) {
log.warn("Parsed ValueConstraint does define illegal values (values={})!", value);
StringBuilder message = new StringBuilder();
message.append("Parsed ValueConstraint does define illegal value for field 'value'" + "(value MUST NOT be an JSON object. Only values and JSONArray to parse" + "multiple values are allowed)!\n");
message.append("Parsed Constraint: \n");
message.append(jConstraint.toString(4));
throw new IllegalArgumentException(message.toString());
} else {
valueList = Collections.singletonList(jConstraint.get("value"));
}
} else {
log.warn("Parsed ValueConstraint does not define the required field \"value\"!");
StringBuilder message = new StringBuilder();
message.append("Parsed ValueConstraint does not define the required field 'value'!\n");
message.append("Parsed Constraint: \n");
message.append(jConstraint.toString(4));
throw new IllegalArgumentException(message.toString());
}
MODE mode = parseConstraintValueMode(jConstraint);
return new ValueConstraint(valueList, dataTypes, mode);
}
use of org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint in project stanbol by apache.
the class YardTest method testFindValues.
/**
* Tests simple {@link ValueConstraint}s
*/
@Test
public void testFindValues() {
//init the test data
FieldQueryTestData data = getFieldQueryTestData();
//query for all languages and value1
FieldQuery query = getYard().getQueryFactory().createFieldQuery();
query.setConstraint(data.intField, new ValueConstraint(data.intValue1));
query.addSelectedField(data.intField);
query.addSelectedField(data.textField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1.getId(), data.r1en.getId(), data.r1de.getId()), Arrays.asList(data.intField, data.textField));
//same for value2
query = getYard().getQueryFactory().createFieldQuery();
query.setConstraint(data.intField, new ValueConstraint(data.intValue2));
query.addSelectedField(data.intField);
query.addSelectedField(data.textField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r2.getId(), data.r2en.getId(), data.r2de.getId()), Arrays.asList(data.intField, data.textField));
}
use of org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint in project stanbol by apache.
the class DefaultFieldMapperImpl method processMapping.
/**
*
* @param mapping
* @param valueFactory The value factory used to create converted values
* @param field
* @param values
* @param globalFiltered
* @param targets
*/
private void processMapping(FieldMapping mapping, ValueFactory valueFactory, String field, Collection<Object> values, Collection<Object> globalFiltered, Set<String> activeTargets, Representation targetRepresentation) {
//parsed mappings are all !ignore and some mappings are active
//this collection will be modified by the filters later on
Collection<Object> filtered;
if (//if no global filter is present and therefore globalFiltered == null or
globalFiltered == null || //there is a more special text filter defined in this mapping
mapping.getFilter() != null && mapping.getFilter().getType() == ConstraintType.text) {
//start with all values
filtered = new HashSet<Object>(values);
} else {
//start with the values filtered by the global filter
filtered = new HashSet<Object>(globalFiltered);
}
if (mapping.getFilter() != null) {
switch(mapping.getFilter().getType()) {
case value:
ValueConstraint valueConstraint = (ValueConstraint) mapping.getFilter();
processFilter(valueConstraint, filtered, valueFactory);
break;
case text:
TextConstraint textConstraint = (TextConstraint) mapping.getFilter();
//for wildcard mappings only filter TextValues. if the mapping is
//for a specific field filter also non text values.
processFilter(textConstraint, filtered, !mapping.usesWildcard());
break;
default:
log.warn(String.format("Filter of type %s are not supported -> select all values! (Constraint=%s)", mapping.getFilter().getType(), mapping.getFilter()));
break;
}
/*
* TODO: add general purpose functionality to apply Constraints.
* Currently this is done by the specific Query Implementations :(
* - use the constraint to filter the values collection!
*/
}
//nothing to do
for (String mappedField : mapping.getMappings()) {
// -> this is because wildcard filters can not know the actual field name
if (activeTargets.contains(mappedField)) {
//so use null to match
if (mappedField == null) {
//and than replace null with the field name
mappedField = field;
}
// log.info(String.format(" >> copy%s to %s &d values",
// mappedField.equals(field)?"":" from "+field,mappedField,filtered.size()));
targetRepresentation.add(mappedField, filtered);
// } else {
// log.info(String.format(" << ignore%s %s",
// mappedField.equals(field)?"":"mapping from "+field+"to",mappedField));
}
}
}
Aggregations