use of org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum in project stanbol by apache.
the class AbstractBackend method listSubjects.
@Override
public Collection<Object> listSubjects(Object property, Object object) {
FieldQuery query = createQuery();
if (this.isURI(object)) {
query.setConstraint(property.toString(), new ReferenceConstraint(object.toString()));
} else if (object instanceof Text) {
Text text = (Text) object;
TextConstraint constraint;
if (text.getLanguage() == null) {
constraint = new TextConstraint(text.getText(), PatternType.none, true);
} else {
constraint = new TextConstraint(text.getText(), PatternType.none, true, text.getLanguage());
}
query.setConstraint(property.toString(), constraint);
} else {
Set<DataTypeEnum> dataTypes = DataTypeEnum.getPrimaryDataTypes(object.getClass());
if (dataTypes == null || dataTypes.isEmpty()) {
query.setConstraint(property.toString(), new ValueConstraint(object));
} else {
Collection<String> types = new ArrayList<String>(dataTypes.size());
for (DataTypeEnum type : dataTypes) {
types.add(type.getUri());
}
query.setConstraint(property.toString(), new ValueConstraint(object, types));
}
}
query.setLimit(Integer.valueOf(DEFAULT_MAX_SELECT));
QueryResultList<String> results;
try {
results = query(query);
} catch (EntityhubException e) {
throw new IllegalStateException("Unable to query for resources with value '" + object + "' on property '" + property + "'!", e);
}
Collection<Object> references;
if (results.isEmpty()) {
references = Collections.emptySet();
} else if (results.size() == 1) {
// assuming that a single result is a likely case
references = Collections.singleton((Object) getValueFactory().createReference(results.iterator().next()));
} else {
int offset = 0;
references = new HashSet<Object>(results.size());
for (String result : results) {
references.add(getValueFactory().createReference(result));
}
while (results.size() >= DEFAULT_MAX_SELECT && references.size() <= DEFAULT_MAX_RESULTS - DEFAULT_MAX_SELECT) {
offset = offset + results.size();
query.setOffset(offset);
try {
results = query(query);
} catch (EntityhubException e) {
throw new IllegalStateException("Unable to query for resources with value '" + object + "' on property '" + property + "'!", e);
}
for (String result : results) {
references.add(getValueFactory().createReference(result));
}
}
}
return references;
}
use of org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum in project stanbol by apache.
the class DefaultFieldMapperImpl method processFilter.
/**
* This method converts - or if not possible filters the parsed values based
* on the parsed constraint
* @param valueConstraint
* @param values
* @return
*/
private Collection<Object> processFilter(ValueConstraint valueConstraint, Collection<Object> values, ValueFactory valueFactory) {
if (valueConstraint.getValues() != null) {
log.warn("Filtering based on values is not yet implemented");
}
// 1) collect all active dataTypes
// first a EnumSet for really fast containsAll ... operations
Set<DataTypeEnum> activeDataTypes = EnumSet.noneOf(DataTypeEnum.class);
// second a List to keep track of the ordering of the dataTypes in the
// constraint for later conversions!
List<DataTypeEnum> sortedActiveDataTypes = new ArrayList<DataTypeEnum>(valueConstraint.getDataTypes().size());
// gives constant processing time even for bulk operations!
for (String dataTypeUri : valueConstraint.getDataTypes()) {
DataTypeEnum dataType = DataTypeEnum.getDataType(dataTypeUri);
if (dataType == null) {
log.warn(String.format("DataType %s not supported"));
} else {
if (activeDataTypes.add(dataType)) {
// only of set has changed to avoid duplicates in the list
sortedActiveDataTypes.add(dataType);
}
}
}
// 2) now process the values
// log.info(" --- Filter values ---");
// calculating acceptable and not acceptable types needs some processing time
// and usually values will be only of very less different types.
// Therefore it makes sense to cache accepted and rejected types!
Set<Class<?>> accepted = new HashSet<Class<?>>();
Set<Class<?>> rejected = new HashSet<Class<?>>();
// Set that stores rejected values. Such will be converted later on!
Set<Object> needConversion = new HashSet<Object>();
for (Iterator<Object> it = values.iterator(); it.hasNext(); ) {
Object value = it.next();
// } else
if (rejected.contains(value.getClass())) {
// remove also the current value of that type
it.remove();
// save as value that need to be converted
needConversion.add(value);
// log.info(String.format(" - value %s(type:%s) rejected by value filter",value,value.getClass()));
} else {
// new class ... calculate
Set<DataTypeEnum> valueTypes = DataTypeEnum.getAllDataTypes(value.getClass());
if (valueTypes.removeAll(activeDataTypes)) {
accepted.add(value.getClass());
// log.info(String.format(" + value %s(type:%s) accepted by value filter",value,value.getClass()));
} else {
rejected.add(getClass());
// remove the Item
it.remove();
// save as value that need to be converted
needConversion.add(value);
// log.info(String.format(" - value %s(type:%s) rejected by value filter",value,value.getClass()));
}
}
}
// log.info(" --- Try to Convert rejected values ---");
for (Object value : needConversion) {
Object converted = null;
DataTypeEnum convertedTo = null;
for (// iterate over all active dataTypes
Iterator<DataTypeEnum> dataTypes = sortedActiveDataTypes.iterator(); converted == null && dataTypes.hasNext(); ) {
// while converted still null and more dataTypes to try
convertedTo = dataTypes.next();
// try the conversion
converted = valueConverter.convert(value, convertedTo.getUri(), valueFactory);
}
if (converted != null) {
// log.info(String.format(" + value %s(javaType=%s) successfully converted to %s(datatype=%s)",
// value,value.getClass().getSimpleName(),converted,convertedTo.getShortName()));
values.add(converted);
// } else {
// log.info(String.format(" - value %s(javaType=%s) could not be converted"),
// value,value.getClass().getSimpleName());
}
}
return values;
}
use of org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum in project stanbol by apache.
the class FieldMappingUtils method parseConstraint.
private static Constraint parseConstraint(String filterString) {
if (filterString.startsWith("d=")) {
String[] dataTypeStrings = filterString.substring(2).split(";");
Set<String> dataTypes = new HashSet<String>();
for (int i = 0; i < dataTypeStrings.length; i++) {
DataTypeEnum dataType = DataTypeEnum.getDataTypeByShortName(dataTypeStrings[i]);
if (dataType == null) {
dataType = DataTypeEnum.getDataType(dataTypeStrings[i]);
}
if (dataType != null) {
dataTypes.add(dataType.getUri());
} else {
log.warn(String.format("DataType %s not supported! Datatype get not used by this Filter", dataTypeStrings[i]));
}
}
if (dataTypes.isEmpty()) {
log.warn(String.format("Unable to parse a valied data type form \"%s\"! A data type filter MUST define at least a single dataType. No filter will be used.", filterString));
return null;
} else {
return new ValueConstraint(null, dataTypes);
}
} else if (filterString.startsWith("@=")) {
String[] langs = filterString.substring(2).split(";");
for (int i = 0; i < langs.length; i++) {
if (langs[i].length() < 1 || "null".equals(langs[i])) {
langs[i] = null;
}
}
if (langs.length < 1) {
log.warn("Unable to parse a language form \"%s\"! A language filter MUST define at least a singel language. No filter will be used." + filterString);
return null;
} else {
return new TextConstraint((String) null, langs);
}
} else {
log.warn(String.format("Filters need to start with \"p=\" (dataType) or \"@=\" (language). Parsed filter: \"%s\".", filterString));
return null;
}
}
use of org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum in project stanbol by apache.
the class JsonModelWriter method convertFieldValueToJSON.
/**
* The value to write. Special support for {@link Reference} and {@link Text}.
* The {@link #toString()} Method is used to write the "value" key.
*
* @param value the value
* @return the {@link JSONObject} representing the value
* @throws JSONException
*/
private JSONObject convertFieldValueToJSON(Object value) throws JSONException {
JSONObject jValue = new JSONObject();
if (value instanceof Reference) {
jValue.put("type", "reference");
jValue.put("xsd:datatype", DataTypeEnum.AnyUri.getShortName());
jValue.put("value", ((Reference) value).getReference());
} else if (value instanceof Text) {
jValue.put("type", "text");
jValue.put("xml:lang", ((Text) value).getLanguage());
jValue.put("value", ((Text) value).getText());
} else if (value instanceof Date) {
jValue.put("type", "value");
jValue.put("value", TimeUtils.toString(DataTypeEnum.DateTime, (Date) value));
jValue.put("xsd:datatype", DataTypeEnum.DateTime.getShortName());
} else {
jValue.put("type", "value");
Set<DataTypeEnum> dataTypes = DataTypeEnum.getPrimaryDataTypes(value.getClass());
if (!dataTypes.isEmpty()) {
jValue.put("xsd:datatype", dataTypes.iterator().next().getShortName());
} else {
jValue.put("xsd:datatype", DataTypeEnum.String.getShortName());
}
jValue.put("value", value);
}
return jValue;
}
use of org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum 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;
}
Aggregations