use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint in project stanbol by apache.
the class YardTest method testFindTextOfLanguage.
/**
* Test a simple {@link TextConstraint} for any language
*/
@Test
public void testFindTextOfLanguage() {
//init the test data
FieldQueryTestData data = getFieldQueryTestData();
//value1@en
FieldQuery query = getYard().getQueryFactory().createFieldQuery();
query.setConstraint(data.textField, new TextConstraint(data.textValue1.getText(), "en"));
query.addSelectedField(data.textField);
query.addSelectedField(data.refField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1en.getId()), Arrays.asList(data.textField, data.refField));
//value2@de
query = getYard().getQueryFactory().createFieldQuery();
query.setConstraint(data.textField, new TextConstraint(data.textValue2.getText(), "de"));
query.addSelectedField(data.textField);
query.addSelectedField(data.refField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r2de.getId()), Arrays.asList(data.textField, data.refField));
//value1@null
query = getYard().getQueryFactory().createFieldQuery();
query.setConstraint(data.textField, new TextConstraint(data.textValue1.getText(), (String) null));
query.addSelectedField(data.textField);
query.addSelectedField(data.refField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1.getId()), Arrays.asList(data.textField, data.refField));
//value1@null,en
query = getYard().getQueryFactory().createFieldQuery();
query.setConstraint(data.textField, new TextConstraint(data.textValue1.getText(), null, "en"));
query.addSelectedField(data.textField);
query.addSelectedField(data.refField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1.getId(), data.r1en.getId()), Arrays.asList(data.textField, data.refField));
//value1@en,de
query = getYard().getQueryFactory().createFieldQuery();
query.setConstraint(data.textField, new TextConstraint(data.textValue1.getText(), "en", "de"));
query.addSelectedField(data.textField);
query.addSelectedField(data.refField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1en.getId(), data.r1de.getId()), Arrays.asList(data.textField, data.refField));
}
use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint in project stanbol by apache.
the class YardTest method testFindText.
/**
* Test a simple {@link TextConstraint} for any language
*/
@Test
public void testFindText() {
//init the test data
FieldQueryTestData data = getFieldQueryTestData();
//query for all languages and value1
FieldQuery query = getYard().getQueryFactory().createFieldQuery();
query.setConstraint(data.textField, new TextConstraint(data.textValue1.getText()));
query.addSelectedField(data.textField);
query.addSelectedField(data.refField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1.getId(), data.r1en.getId(), data.r1de.getId()), Arrays.asList(data.textField, data.refField));
//same for value2
query = getYard().getQueryFactory().createFieldQuery();
query.setConstraint(data.textField, new TextConstraint(data.textValue2.getText()));
query.addSelectedField(data.textField);
query.addSelectedField(data.refField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r2.getId(), data.r2en.getId(), data.r2de.getId()), Arrays.asList(data.textField, data.refField));
}
use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint in project stanbol by apache.
the class YardTest method testfindOptionalTexts.
/**
* Tests a TextConstraint with multiple optional values
*/
@Test
public void testfindOptionalTexts() {
//init the test data
FieldQueryTestData data = getFieldQueryTestData();
//value1@en || value2@en
FieldQuery query = getYard().getQueryFactory().createFieldQuery();
query.setConstraint(data.textField, new TextConstraint(Arrays.asList(data.textValue1.getText(), data.textValue2.getText()), "en"));
query.addSelectedField(data.textField);
query.addSelectedField(data.refField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1en.getId(), data.r2en.getId()), Arrays.asList(data.textField, data.refField));
//value1@en,de || value2@en,de
query = getYard().getQueryFactory().createFieldQuery();
query.setConstraint(data.textField, new TextConstraint(Arrays.asList(data.textValue1.getText(), data.textValue2.getText()), "en", "de"));
query.addSelectedField(data.textField);
query.addSelectedField(data.refField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1en.getId(), data.r1de.getId(), data.r2en.getId(), data.r2de.getId()), Arrays.asList(data.textField, data.refField));
}
use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint 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));
}
}
}
use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint in project stanbol by apache.
the class DefaultFieldMapperImpl method applyMappings.
/* (non-Javadoc)
* @see org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper#applyMappings(org.apache.stanbol.entityhub.servicesapi.model.Representation, org.apache.stanbol.entityhub.servicesapi.model.Representation)
*/
public Representation applyMappings(Representation source, Representation target, ValueFactory valueFactory) {
Collection<String> fields = new HashSet<String>();
for (Iterator<String> fieldIt = source.getFieldNames(); fieldIt.hasNext(); ) {
fields.add(fieldIt.next());
}
for (String field : fields) {
// log.info(" > process field: "+field);
//get the active Mappings
List<FieldMapping> activeMappings = getMappings(field);
if (!activeMappings.isEmpty()) {
//get all the values (store them in an Collection, because we need them more than once)
Collection<Object> values = new ArrayList<Object>();
for (Iterator<Object> valueIt = source.get(field); valueIt.hasNext(); ) {
values.add(valueIt.next());
}
//only to be sure, that this is not changed by Filters!
values = Collections.unmodifiableCollection(values);
/*
* (1) Before working with the values first analyse the active
* mappings and filters. Two things
* a) Init Wildcard Filters:
* Language filters set on namespaces are executed on all field
* mappings that define no language filter
* b) calculate the mapped fields. Possible there are no mappings
* left. Than we need not to process all the values
*/
Set<String> targetFields = new HashSet<String>();
TextConstraint globalFilter = null;
Collection<Object> globalFiltered = null;
/*
* NOTE: the mappings are sorted in the way, that the most
* prominent one will be at index 0. The wildcard "*" will
* be always the last.
* So we need to parse backwards because than more prominent
* things will overwrite and win!
*/
for (int i = activeMappings.size() - 1; i >= 0; i--) {
FieldMapping mapping = activeMappings.get(i);
if (//if wildcard
mapping.usesWildcard() && //and not ignore
!mapping.ignoreField() && //and a filter is present
mapping.getFilter() != null && mapping.getFilter().getType() == ConstraintType.text) {
//and of type text
//set the global text filter.
//NOTE: the active mappings are sorted in that way, that
// the most specific one is set last
globalFilter = (TextConstraint) mapping.getFilter();
}
for (String targetField : mapping.getMappings()) {
if (mapping.ignoreField()) {
targetFields.remove(targetField);
} else {
targetFields.add(targetField);
}
}
}
// log.info(" o global text filter: "+globalFilter);
if (globalFilter != null) {
globalFiltered = new HashSet<Object>(values);
//parse false ass third argument, because we need not to filter
//non-Text values for wildcard filter!
processFilter(globalFilter, globalFiltered, false);
}
//now process the mappings
for (FieldMapping mapping : activeMappings) {
if (!mapping.ignoreField() && !Collections.disjoint(targetFields, mapping.getMappings())) {
processMapping(mapping, valueFactory, field, values, globalFiltered, targetFields, target);
// } else if(!mapping.ignoreField()) {
// log.info(String.format(" << ignore mapping %s ",mapping));
// } else {
// log.info(String.format(" << %s ",mapping));
}
}
}
}
/*
* TODO: return a "MappingReport"
* All mapping activities should be documented and stored with the
* MappedEntity as MappingActivity!
*/
return target;
}
Aggregations