Search in sources :

Example 1 with ReferenceConstraint

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()));
    }
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)

Example 2 with ReferenceConstraint

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);
    }
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) HashSet(java.util.HashSet)

Example 3 with ReferenceConstraint

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;
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) DataTypeEnum(org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Example 4 with ReferenceConstraint

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));
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) Test(org.junit.Test)

Example 5 with ReferenceConstraint

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));
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) RangeConstraint(org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) Test(org.junit.Test)

Aggregations

ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)14 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)9 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)7 ValueConstraint (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)6 ArrayList (java.util.ArrayList)4 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)4 HashSet (java.util.HashSet)3 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)3 Constraint (org.apache.stanbol.entityhub.servicesapi.query.Constraint)3 RangeConstraint (org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint)3 SimilarityConstraint (org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 ReconcileValue (org.apache.stanbol.entityhub.jersey.grefine.ReconcileValue)2 DataTypeEnum (org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum)2 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)2 FieldQueryFactory (org.apache.stanbol.entityhub.servicesapi.query.FieldQueryFactory)2 JSONArray (org.codehaus.jettison.json.JSONArray)2 Test (org.junit.Test)2 Collection (java.util.Collection)1 LinkedHashSet (java.util.LinkedHashSet)1