Search in sources :

Example 11 with ReferenceConstraint

use of org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint in project stanbol by apache.

the class EntityhubImpl method getMappingsByTarget.

@Override
public Collection<Entity> getMappingsByTarget(String targetId) throws YardException {
    if (targetId == null) {
        log.warn("NULL parsed as Reference -> call to getMappingsBySymbol ignored (return null)");
        return null;
    }
    FieldQuery fieldQuery = getQueryFactory().createFieldQuery();
    fieldQuery.setConstraint(RdfResourceEnum.mappingTarget.getUri(), new ReferenceConstraint(targetId));
    QueryResultList<Representation> resultList = entityhubYard.findRepresentation(fieldQuery);
    Collection<Entity> mappings = new HashSet<Entity>();
    for (Representation rep : resultList) {
        mappings.add(loadEntity(rep));
    }
    return mappings;
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) HashSet(java.util.HashSet)

Example 12 with ReferenceConstraint

use of org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint in project stanbol by apache.

the class EntityhubImpl method getMappingBySource.

@Override
public Entity getMappingBySource(String reference) throws YardException {
    if (reference == null) {
        log.warn("NULL parsed as Reference -> call to getMappingByEntity ignored (return null)");
        return null;
    }
    FieldQuery fieldQuery = getQueryFactory().createFieldQuery();
    fieldQuery.setConstraint(RdfResourceEnum.mappingSource.getUri(), new ReferenceConstraint(reference));
    QueryResultList<Representation> resultList = entityhubYard.findRepresentation(fieldQuery);
    if (!resultList.isEmpty()) {
        Iterator<Representation> resultIterator = resultList.iterator();
        Entity mapping = loadEntity(resultIterator.next());
        //print warnings in case of multiple mappings
        if (resultIterator.hasNext()) {
            log.warn("Multiple Mappings found for Entity {}!", reference);
            log.warn("  > {} -> returned instance", mapping.getId());
            while (resultIterator.hasNext()) {
                log.warn("  > {} -> ignored", resultIterator.next());
            }
        }
        if (!EntityMapping.isValid(mapping)) {
            log.warn("Entity {} is not a valid EntityMapping. -> return null", mapping);
            mapping = null;
        }
        return mapping;
    } else {
        log.debug("No Mapping found for Entity {}", reference);
        return null;
    }
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)

Example 13 with ReferenceConstraint

use of org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint 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;
}
Also used : SimilarityConstraint(org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) JSONArray(org.codehaus.jettison.json.JSONArray) ArrayList(java.util.ArrayList) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) RangeConstraint(org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint) JSONObject(org.codehaus.jettison.json.JSONObject) DataTypeEnum(org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Example 14 with ReferenceConstraint

use of org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint in project stanbol by apache.

the class FieldQueryReader method parseReferenceConstraint.

/**
     * @param jConstraint
     * @return
     * @throws JSONException
     */
private static Constraint parseReferenceConstraint(JSONObject jConstraint, NamespacePrefixService nsPrefixService) throws JSONException {
    final List<String> refList;
    if (jConstraint.has("value") && !jConstraint.isNull("value")) {
        Object value = jConstraint.get("value");
        if (value instanceof JSONArray) {
            refList = new ArrayList<String>(((JSONArray) value).length());
            for (int i = 0; i < ((JSONArray) value).length(); i++) {
                String field = ((JSONArray) value).getString(i);
                field = field != null ? nsPrefixService.getFullName(field) : null;
                if (field != null && !field.isEmpty()) {
                    refList.add(field);
                }
            }
        } else if (value instanceof JSONObject) {
            log.warn("Parsed ValueConstraint does define illegal values (values={})!", value);
            StringBuilder message = new StringBuilder();
            message.append("Parsed ValueConstraint does define illegal value for field 'value'" + "(value MUST NOT be an JSON object. Only values and JSONArray to parse" + "multiple values are allowed)!\n");
            message.append("Parsed Constraint: \n");
            message.append(jConstraint.toString(4));
            throw new IllegalArgumentException(message.toString());
        } else {
            String field = jConstraint.getString("value");
            field = field != null ? nsPrefixService.getFullName(field) : null;
            if (field != null) {
                refList = Collections.singletonList(field);
            } else {
                refList = Collections.emptyList();
            }
        }
        if (refList.isEmpty()) {
            log.warn("Parsed ReferenceConstraint does not define a single valid \"value\"!");
            StringBuilder message = new StringBuilder();
            message.append("Parsed ReferenceConstraint does not define a single valid 'value'!\n");
            message.append("This means values where only null, empty string or '{prefix}:{localname}' values with unknown {prefix}\n");
            message.append("Parsed Constraint: \n");
            message.append(jConstraint.toString(4));
            throw new IllegalArgumentException(message.toString());
        }
        MODE mode = parseConstraintValueMode(jConstraint);
        return new ReferenceConstraint(refList, mode);
    } else {
        log.warn("Parsed ReferenceConstraint does not define the required field \"value\"!");
        StringBuilder message = new StringBuilder();
        message.append("Parsed ReferenceConstraint does not define the required field 'value'!\n");
        message.append("Parsed Constraint: \n");
        message.append(jConstraint.toString(4));
        throw new IllegalArgumentException(message.toString());
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) MODE(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint.MODE) JSONArray(org.codehaus.jettison.json.JSONArray) JSONObject(org.codehaus.jettison.json.JSONObject) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) SimilarityConstraint(org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint) RangeConstraint(org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint)

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