use of org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint in project stanbol by apache.
the class EntityhubImpl method deleteMappingsbyTarget.
private void deleteMappingsbyTarget(String id) throws YardException {
if (id != null && !id.isEmpty()) {
FieldQuery fieldQuery = getQueryFactory().createFieldQuery();
fieldQuery.setConstraint(RdfResourceEnum.mappingTarget.getUri(), new ReferenceConstraint(id));
deleteEntities(ModelUtils.asCollection(entityhubYard.findReferences(fieldQuery).iterator()));
}
}
use of org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint in project stanbol by apache.
the class EntityhubImpl method deleteEntities.
private void deleteEntities(Collection<String> ids) throws YardException {
FieldQuery fieldQuery = getQueryFactory().createFieldQuery();
Collection<String> toDelete = new HashSet<String>(ids);
for (String id : ids) {
if (id != null && !id.isEmpty()) {
fieldQuery.setConstraint(RdfResourceEnum.aboutRepresentation.getUri(), new ReferenceConstraint(id));
for (Iterator<String> it = entityhubYard.findReferences(fieldQuery).iterator(); it.hasNext(); ) {
toDelete.add(it.next());
}
}
}
if (!toDelete.isEmpty()) {
entityhubYard.remove(toDelete);
}
}
use of org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint 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.query.ReferenceConstraint in project stanbol by apache.
the class YardTest method testFindReference.
/**
* Test a {@link ReferenceConstraint}
*/
@Test
public void testFindReference() {
//init the test data
FieldQueryTestData data = getFieldQueryTestData();
//query for all languages and value1
FieldQuery query = getYard().getQueryFactory().createFieldQuery();
query.setConstraint(data.refField, new ReferenceConstraint(data.refValue1.getReference()));
query.addSelectedField(data.intField);
query.addSelectedField(data.refField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1.getId(), data.r1en.getId(), data.r1de.getId(), data.r5.getId()), Arrays.asList(data.intField, data.refField));
//same for value2
query = getYard().getQueryFactory().createFieldQuery();
query.setConstraint(data.refField, new ReferenceConstraint(data.refValue2.getReference()));
query.addSelectedField(data.intField);
query.addSelectedField(data.refField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r2.getId(), data.r2en.getId(), data.r2de.getId(), data.r10.getId()), Arrays.asList(data.intField, data.refField));
}
use of org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint in project stanbol by apache.
the class YardTest method testFindMultipleConstraints.
/**
* Tests simple {@link RangeConstraint}
*/
@Test
public void testFindMultipleConstraints() {
//init the test data
FieldQueryTestData data = getFieldQueryTestData();
//Integer Range and reference query
FieldQuery query = getYard().getQueryFactory().createFieldQuery();
//selects r2, r2en, r2de, r5
query.setConstraint(data.intField, new RangeConstraint(data.intValue2, data.intValue5, true));
//selects r1, r1en, r1de, r5
query.setConstraint(data.refField, new ReferenceConstraint(data.refValue1.getReference()));
query.addSelectedField(data.intField);
query.addSelectedField(data.refField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r5.getId()), Arrays.asList(data.intField, data.refField));
//text and reference
query = getYard().getQueryFactory().createFieldQuery();
//selects r1en and r2en
String wildcard = data.textValue1.getText();
wildcard = wildcard.substring(0, wildcard.length() - 1) + "*";
query.setConstraint(data.textField, new TextConstraint(wildcard, PatternType.wildcard, false, "en"));
//selects r1, r1en, r1de, r5
query.setConstraint(data.refField, new ReferenceConstraint(data.refValue1.getReference()));
query.addSelectedField(data.refField);
query.addSelectedField(data.textField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1en.getId()), Arrays.asList(data.refField, data.textField));
//text and value
query = getYard().getQueryFactory().createFieldQuery();
//selects r1de and r2de
query.setConstraint(data.textField, new TextConstraint(wildcard, PatternType.wildcard, false, "de"));
//selects r2, r2en, r2de
query.setConstraint(data.intField, new ValueConstraint(data.intValue2));
query.addSelectedField(data.refField);
query.addSelectedField(data.textField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r2de.getId()), Arrays.asList(data.refField, data.textField));
}
Aggregations