Package org.apache.stanbol.entityhub.servicesapi.query

Examples of org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint


    /**
     * @param indexConstraint
     * @param refConstraint
     */
    private void initValueConstraint(IndexConstraint indexConstraint) {
        ValueConstraint valueConstraint  = (ValueConstraint)indexConstraint.getConstraint();
        if (valueConstraint.getValues() == null) {
            indexConstraint.setInvalid(String.format(
                "ValueConstraint without a value - that check only any value for " +
                "the parsed datatypes %s is present - can not be supported by a Solr query!",
                valueConstraint.getDataTypes()));
        } else {
            // first process the parsed dataTypes to get the supported types
            List<IndexDataType> indexDataTypes = new ArrayList<IndexDataType>();
            List<String> acceptedDataTypes = new ArrayList<String>();
            if (valueConstraint.getDataTypes() != null) {
                for (String dataType : valueConstraint.getDataTypes()) {
                    IndexDataTypeEnum indexDataTypeEnumEntry = IndexDataTypeEnum.forUri(dataType);
                    if (indexDataTypeEnumEntry != null) {
                        indexDataTypes.add(indexDataTypeEnumEntry.getIndexType());
                        acceptedDataTypes.add(dataType);
                    } else {
                        // TODO: Add possibility to add warnings to indexConstraints
                        log.warn("A Datatype parsed for a ValueConstraint is not " +
                            "supported and will be ignored (dataTypeUri={})",
                                dataType);
                    }
                }
            }
            //we support only a single dataType ...
            //  ... therefore remove additional data types from the ValueConstraint
            IndexDataType indexDataType = null;
            if(!indexDataTypes.isEmpty()) {
                indexDataType = indexDataTypes.get(0);
                if(indexDataTypes.size() > 1){
                    log.warn("Only a single DataType is supported for ValueConstraints!");
                    while(acceptedDataTypes.size()>1){
                        String ignored = acceptedDataTypes.remove(acceptedDataTypes.size()-1);
                        log.warn("  > ignore parsed dataType {}",ignored);
                    }
                }
            } //else empty we will initialise based on the first parsed value!
            ConstraintValue constraintValue = new ConstraintValue(valueConstraint.getMode());
            for(Object value : valueConstraint.getValues()){
                IndexValue indexValue;
                if(indexDataType == null){ // if no supported types are present
                    // get the dataType based on the type of the value
                    try {
                        indexValue = indexValueFactory.createIndexValue(value);
                    } catch (NoConverterException e) {
                        // if not found use the toString() and string as type
                        log.warn("Unable to create IndexValue for value {} (type: {}). Create IndexValue manually by using the first parsed IndexDataType {}",
                                    new Object[]{
                                        value, value.getClass(),
                                        IndexDataTypeEnum.STR.getIndexType()
                                    });
                        indexValue = new IndexValue(value.toString(),
                            IndexDataTypeEnum.STR.getIndexType());
                    }
                    //initialise the IndexDataType for this query based on the first parsed value
                    indexDataType = indexValue.getType();
                } else {
                    indexValue = new IndexValue(value.toString(), indexDataType);
                }
                constraintValue.getValues().add(indexValue); //add the constraint
            }
            //indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.DATATYPE, indexDataType);
            IndexField indexField;
            if(IndexDataTypeEnum.TXT.getIndexType().equals(indexDataType)){
                //NOTE: in case of TEXT we need also to add the language to create a valid
                //query!
                // * We take the language of the first parsed element
                indexField = new IndexField(indexConstraint.getPath(), indexDataType,
                    constraintValue.getValues().iterator().next().getLanguage());
            } else {
                indexField = new IndexField(indexConstraint.getPath(), indexDataType);
            }
            //set FIELD, DATATYPE and LANGUAGE constraint by using the indexField
            indexConstraint.setIndexFieldConstraints(indexField);
            //set the VALUE
            //TODO: We need to somehow pass the MODE so that the encoder knows how
            //      to encode the values
            indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.EQ, constraintValue);
            //update this constraint!
            if(valueConstraint instanceof ReferenceConstraint){
                indexConstraint.setFieldQueryConstraint(valueConstraint);
            } else {
                indexConstraint.setFieldQueryConstraint(
                    new ValueConstraint(valueConstraint.getValues(), Arrays.asList(indexDataType.getId())));
            }
        }
    }
View Full Code Here


    private static JSONObject convertConstraintToJSON(Constraint constraint, NamespacePrefixService nsPrefixService) throws JSONException {
        JSONObject jConstraint = new JSONObject();
        jConstraint.put("type", constraint.getType().name());
        switch (constraint.getType()) {
            case value: //both ValueConstraint and ReferenceConstraint
                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();
View Full Code Here

                    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();
            texts.clear();
View Full Code Here

    private static JSONObject convertConstraintToJSON(Constraint constraint, NamespacePrefixService nsPrefixService) throws JSONException {
        JSONObject jConstraint = new JSONObject();
        jConstraint.put("type", constraint.getType().name());
        switch (constraint.getType()) {
            case value: //both ValueConstraint and ReferenceConstraint
                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();
View Full Code Here

    /**
     * @param indexConstraint
     * @param refConstraint
     */
    private void initValueConstraint(IndexConstraint indexConstraint) {
        ValueConstraint valueConstraint  = (ValueConstraint)indexConstraint.getConstraint();
        if (valueConstraint.getValues() == null) {
            indexConstraint.setInvalid(String.format(
                "ValueConstraint without a value - that check only any value for " +
                "the parsed datatypes %s is present - can not be supported by a Solr query!",
                valueConstraint.getDataTypes()));
        } else {
            // first process the parsed dataTypes to get the supported types
            List<IndexDataType> indexDataTypes = new ArrayList<IndexDataType>();
            List<String> acceptedDataTypes = new ArrayList<String>();
            if (valueConstraint.getDataTypes() != null) {
                for (String dataType : valueConstraint.getDataTypes()) {
                    IndexDataTypeEnum indexDataTypeEnumEntry = IndexDataTypeEnum.forUri(dataType);
                    if (indexDataTypeEnumEntry != null) {
                        indexDataTypes.add(indexDataTypeEnumEntry.getIndexType());
                        acceptedDataTypes.add(dataType);
                    } else {
                        // TODO: Add possibility to add warnings to indexConstraints
                        log.warn("A Datatype parsed for a ValueConstraint is not " +
                            "supported and will be ignored (dataTypeUri={})",
                                dataType);
                    }
                }
            }
            //we support only a single dataType ...
            //  ... therefore remove additional data types from the ValueConstraint
            IndexDataType indexDataType = null;
            if(!indexDataTypes.isEmpty()) {
                indexDataType = indexDataTypes.get(0);
                if(indexDataTypes.size() > 1){
                    log.warn("Only a single DataType is supported for ValueConstraints!");
                    while(acceptedDataTypes.size()>1){
                        String ignored = acceptedDataTypes.remove(acceptedDataTypes.size()-1);
                        log.warn("  > ignore parsed dataType {}",ignored);
                    }
                }
            } //else empty we will initialise based on the first parsed value!
            ConstraintValue constraintValue = new ConstraintValue(valueConstraint.getMode());
            addBoost(constraintValue, valueConstraint); //init the boost
            for(Object value : valueConstraint.getValues()){
                IndexValue indexValue;
                if(indexDataType == null){ // if no supported types are present
                    // get the dataType based on the type of the value
                    try {
                        indexValue = indexValueFactory.createIndexValue(value);
                    } catch (NoConverterException e) {
                        // if not found use the toString() and string as type
                        log.warn("Unable to create IndexValue for value {} (type: {}). Create IndexValue manually by using the first parsed IndexDataType {}",
                                    new Object[]{
                                        value, value.getClass(),
                                        IndexDataTypeEnum.STR.getIndexType()
                                    });
                        indexValue = new IndexValue(value.toString(),
                            IndexDataTypeEnum.STR.getIndexType());
                    }
                    //initialise the IndexDataType for this query based on the first parsed value
                    indexDataType = indexValue.getType();
                } else {
                    indexValue = new IndexValue(value.toString(), indexDataType);
                }
                constraintValue.getValues().add(indexValue); //add the constraint
            }
            //indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.DATATYPE, indexDataType);
            IndexField indexField;
            if(IndexDataTypeEnum.TXT.getIndexType().equals(indexDataType)){
                //NOTE: in case of TEXT we need also to add the language to create a valid
                //query!
                // * We take the language of the first parsed element
                indexField = new IndexField(indexConstraint.getPath(), indexDataType,
                    constraintValue.getValues().iterator().next().getLanguage());
            } else {
                indexField = new IndexField(indexConstraint.getPath(), indexDataType);
            }
            //set FIELD, DATATYPE and LANGUAGE constraint by using the indexField
            indexConstraint.setIndexFieldConstraints(indexField);
            //set the VALUE
            //TODO: We need to somehow pass the MODE so that the encoder knows how
            //      to encode the values
            indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.EQ, constraintValue);
            //update this constraint!
            if(valueConstraint instanceof ReferenceConstraint){
                indexConstraint.setFieldQueryConstraint(valueConstraint);
            } else {
                indexConstraint.setFieldQueryConstraint(
                    new ValueConstraint(valueConstraint.getValues(), Arrays.asList(indexDataType.getId())));
            }
        }
    }
View Full Code Here

            message.append("Parsed Constraint: \n");
            message.append(jConstraint.toString(4));
            throw new IllegalArgumentException(message.toString());
        }
        MODE mode = parseConstraintValueMode(jConstraint);
        return new ValueConstraint(valueList,dataTypes,mode);
    }
View Full Code Here

            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 {
View Full Code Here

                    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();
            texts.clear();
View Full Code Here

            if(dataTypes.isEmpty()){
                log.warn(String.format("Unable to parse a valied data type form \"%s\"! A data type filter MUST define at least a single dataType. No filter will be used.",
                        filterString));
                return null;
            } else {
                return new ValueConstraint(null, dataTypes);
            }
        } else if (filterString.startsWith("@=")){
            String[] langs = filterString.substring(2).split(";");
            for(int i =0;i<langs.length;i++){
                if(langs[i].length()<1 || "null".equals(langs[i])){
View Full Code Here

            message.append("Parsed Constraint: \n");
            message.append(jConstraint.toString(4));
            throw new IllegalArgumentException(message.toString());
        }
        MODE mode = parseConstraintValueMode(jConstraint);
        return new ValueConstraint(valueList,dataTypes,mode);
    }
View Full Code Here

TOP

Related Classes of org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.