Search in sources :

Example 1 with ReconcileValue

use of org.apache.stanbol.entityhub.jersey.grefine.ReconcileValue in project stanbol by apache.

the class BaseGoogleRefineReconcileResource method addTypeConstraint.

/**
 * @param rQuery
 * @param query
 */
private void addTypeConstraint(ReconcileQuery rQuery, FieldQuery query) {
    // maybe an other column was also mapped to the TYPE_FIELD property
    Collection<ReconcileValue> additionalTypes = rQuery.removeProperty(TYPE_FIELD);
    Set<String> queryTypes = rQuery.getTypes();
    Set<String> types = null;
    if (additionalTypes == null) {
        if (queryTypes != null) {
            types = queryTypes;
        }
    } else {
        types = new HashSet<String>();
        if (queryTypes != null) {
            types.add(rQuery.getQuery());
        }
        for (ReconcileValue value : additionalTypes) {
            if (value != null) {
                if (value.getId() != null) {
                    types.add(value.getId());
                } else if (value.getValue() instanceof String) {
                    // TODO: check if the assumption that String values are
                    // good for types is valid
                    types.add((String) value.getValue());
                }
            }
        // else null -> ignore
        }
    }
    if (!types.isEmpty()) {
        query.setConstraint(TYPE_FIELD, new ReferenceConstraint(types));
    }
}
Also used : ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) ReconcileValue(org.apache.stanbol.entityhub.jersey.grefine.ReconcileValue)

Example 2 with ReconcileValue

use of org.apache.stanbol.entityhub.jersey.grefine.ReconcileValue in project stanbol by apache.

the class BaseGoogleRefineReconcileResource method addPropertyConstraints.

/**
 * @param rQuery
 * @param query
 */
private void addPropertyConstraints(ReconcileQuery rQuery, FieldQuery query) {
    Collection<String> ids = new HashSet<String>();
    // keep order for texts
    List<String> texts = new ArrayList<String>();
    Collection<Object> values = new HashSet<Object>();
    // hold all references for @references special property
    HashSet<String> references = new HashSet<String>();
    // holds all texts for @fullText special property
    List<String> fullText = null;
    // holds the context for the @similarity special property
    Collection<String> similarityContext = null;
    // the field used for the @similarity special property
    HashSet<String> similarityFields = new LinkedHashSet<String>();
    for (Entry<ReconcileProperty, Collection<ReconcileValue>> propertyEntry : rQuery.getProperties()) {
        ReconcileProperty property = propertyEntry.getKey();
        // collect the properties
        for (ReconcileValue value : propertyEntry.getValue()) {
            if (value.getId() != null) {
                ids.add(value.getId());
            }
            if (value.getValue() instanceof String) {
                texts.add((String) value.getValue());
            } else {
                values.add(value.getValue());
            }
        }
        // handle supported special properties
        if (property.isSpecial()) {
            if (property.getName().equalsIgnoreCase("references")) {
                // if Users do parse parameters - so we need to collect all values
                if (property.getParameter() != null) {
                    log.warn("parameters are not supported for @references -> ignore '{}'", property.getParameter());
                }
                if (ids.isEmpty()) {
                    log.warn("No URI values present for parsed @references property! (values: " + propertyEntry.getValue());
                }
                for (String id : ids) {
                    references.add(id);
                }
            } else if (property.getName().equalsIgnoreCase("fulltext")) {
                // if Users do parse parameters - so we need to collect all values
                if (property.getParameter() != null) {
                    log.warn("parameters are not supported for @fullText -> ignore '{}'", property.getParameter());
                }
                fullText = texts;
            } else if (property.getName().equalsIgnoreCase("similarity")) {
                String propUri = property.getParameter() != null ? nsPrefixService.getFullName(property.getParameter()) : SpecialFieldEnum.fullText.getUri();
                if (propUri != null) {
                    similarityFields.add(propUri);
                } else {
                    // TODO: maybe throw an Exception instead
                    log.warn("Unknown prefix '{}' used by Google Refine query parameter of property '{}'! " + "Will use the full text field as fallback", NamespaceMappingUtils.getPrefix(property.getParameter()), property);
                    similarityFields.add(SpecialFieldEnum.fullText.getUri());
                }
                similarityContext = texts;
            } else {
                // TODO: implement LDPATH support
                log.warn("ignore unsupported special property {}", property);
            }
        } else {
            // * non Reference | Text | Datatype values are ignored
            if (!ids.isEmpty()) {
                // only references -> create reference constraint
                query.setConstraint(property.getName(), new ReferenceConstraint(ids));
                if (ids.size() != propertyEntry.getValue().size()) {
                    log.info("Only some of the parsed values of the field {} contain" + "references -> will ignore values with missing references");
                }
            } else if (!texts.isEmpty()) {
                // NOTE: This will use OR over all texts. To enforce AND one
                // would need to parse a single string with all values e.g. by
                // using StringUtils.join(texts," ")
                query.setConstraint(property.getName(), new TextConstraint(texts));
                if (ids.size() != propertyEntry.getValue().size()) {
                    log.info("Only some of the parsed values of the field {} are" + "of type String -> will ignore non-string values");
                }
            } else if (!values.isEmpty()) {
                query.setConstraint(property.getName(), new ValueConstraint(values));
            }
        // else no values ... ignore property
        }
        // clean up
        ids.clear();
        values.clear();
    }
    // now add constraints for the collected special properties
    if (!references.isEmpty()) {
        // add references constraint
        ReferenceConstraint refConstraint = new ReferenceConstraint(references, MODE.all);
        query.setConstraint(SpecialFieldEnum.references.getUri(), refConstraint);
    }
    if (fullText != null && !fullText.isEmpty()) {
        TextConstraint textConstraint = new TextConstraint(fullText);
        query.setConstraint(SpecialFieldEnum.fullText.getUri(), textConstraint);
    // add full text constraint
    }
    if (similarityContext != null && !similarityContext.isEmpty()) {
        // add similarity constraint
        Iterator<String> fieldIt = similarityFields.iterator();
        String field = fieldIt.next();
        SimilarityConstraint simConstraint;
        if (fieldIt.hasNext()) {
            List<String> addFields = new ArrayList<String>(similarityFields.size() - 1);
            while (fieldIt.hasNext()) {
                addFields.add(fieldIt.next());
            }
            simConstraint = new SimilarityConstraint(similarityContext, DataTypeEnum.Text, addFields);
        } else {
            simConstraint = new SimilarityConstraint(similarityContext, DataTypeEnum.Text);
        }
        query.setConstraint(field, simConstraint);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SimilarityConstraint(org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) ArrayList(java.util.ArrayList) ReconcileProperty(org.apache.stanbol.entityhub.jersey.grefine.ReconcileProperty) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) Collection(java.util.Collection) JSONObject(org.codehaus.jettison.json.JSONObject) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ReconcileValue(org.apache.stanbol.entityhub.jersey.grefine.ReconcileValue) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with ReconcileValue

use of org.apache.stanbol.entityhub.jersey.grefine.ReconcileValue in project stanbol by apache.

the class BaseGoogleRefineReconcileResource method addNameConstraint.

/**
 * @param rQuery
 * @param query
 */
private void addNameConstraint(ReconcileQuery rQuery, FieldQuery query) {
    // maybe an other column was also mapped to the NAME_FIELD property
    Collection<ReconcileValue> additionalValues = rQuery.removeProperty(NAME_FIELD);
    List<String> values;
    if (additionalValues == null) {
        values = Collections.singletonList(rQuery.getQuery());
    } else {
        values = new ArrayList<String>(additionalValues.size() + 1);
        values.add(rQuery.getQuery());
        for (ReconcileValue value : additionalValues) {
            if (value != null && value.getValue() instanceof String) {
                values.add((String) value.getValue());
            }
        }
    }
    query.setConstraint(NAME_FIELD, new TextConstraint(values));
}
Also used : TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ReconcileValue(org.apache.stanbol.entityhub.jersey.grefine.ReconcileValue)

Aggregations

ReconcileValue (org.apache.stanbol.entityhub.jersey.grefine.ReconcileValue)3 ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)2 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 ReconcileProperty (org.apache.stanbol.entityhub.jersey.grefine.ReconcileProperty)1 SimilarityConstraint (org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint)1 ValueConstraint (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)1 JSONObject (org.codehaus.jettison.json.JSONObject)1