Search in sources :

Example 6 with Property

use of org.apache.rya.indexing.entity.model.Property in project incubator-rya by apache.

the class DuplicateDataDetector method compareEntities.

/**
 * Compares two entities to determine if they have nearly identical data.
 * @param entity1 the first {@link Entity}. (not {@code null})
 * @param entity2 the second {@link Entity}. (not {@code null})
 * @return {@code true} if the two entities have nearly identical data.
 * {@code false} otherwise.
 * @throws SmartUriException
 */
public boolean compareEntities(final Entity entity1, final Entity entity2) throws SmartUriException {
    requireNonNull(entity1);
    requireNonNull(entity2);
    boolean allValuesNearlyEqual = true;
    final List<RyaURI> types1 = entity1.getExplicitTypeIds();
    final List<RyaURI> types2 = entity2.getExplicitTypeIds();
    final boolean doBothHaveSameTypes = types1.containsAll(types2);
    if (!doBothHaveSameTypes) {
        return false;
    }
    for (final Entry<RyaURI, ImmutableMap<RyaURI, Property>> entry : entity1.getProperties().entrySet()) {
        final RyaURI typeIdUri = entry.getKey();
        for (final Entry<RyaURI, Property> typeProperty : entry.getValue().entrySet()) {
            final RyaURI propertyNameUri = typeProperty.getKey();
            final Property property1 = typeProperty.getValue();
            final Optional<Property> p2 = entity2.lookupTypeProperty(typeIdUri, propertyNameUri);
            if (p2.isPresent()) {
                final Property property2 = p2.get();
                final RyaType value1 = property1.getValue();
                final RyaType value2 = property2.getValue();
                final String data1 = value1.getData();
                final String data2 = value2.getData();
                final URI xmlSchemaUri1 = value1.getDataType();
                final ApproxEqualsDetector<?> approxEqualsDetector = uriMap.get(xmlSchemaUri1);
                if (approxEqualsDetector == null) {
                    throw new SmartUriException("No appropriate detector found for the type: " + xmlSchemaUri1);
                }
                final boolean approxEquals = approxEqualsDetector.areApproxEquals(data1, data2);
                if (!approxEquals) {
                    allValuesNearlyEqual = false;
                    break;
                }
            } else {
                allValuesNearlyEqual = false;
                break;
            }
        }
        if (!allValuesNearlyEqual) {
            break;
        }
    }
    return allValuesNearlyEqual;
}
Also used : SmartUriException(org.apache.rya.indexing.smarturi.SmartUriException) RyaType(org.apache.rya.api.domain.RyaType) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) ImmutableMap(com.google.common.collect.ImmutableMap) RyaURI(org.apache.rya.api.domain.RyaURI) Property(org.apache.rya.indexing.entity.model.Property)

Example 7 with Property

use of org.apache.rya.indexing.entity.model.Property in project incubator-rya by apache.

the class SmartUriAdapter method serializeUriEntity.

/**
 * Serializes an {@link Entity} into a Smart {@link URI}.
 * @param entity the {@link Entity} to serialize into a Smart URI.
 * @return the Smart {@link URI}.
 * @throws SmartUriException
 */
public static URI serializeUriEntity(final Entity entity) throws SmartUriException {
    final Map<URI, Value> objectMap = new LinkedHashMap<>();
    // Adds the entity's types to the Smart URI
    final List<RyaURI> typeIds = entity.getExplicitTypeIds();
    final Map<RyaURI, String> ryaTypeMap = createTypeMap(typeIds);
    final URI ryaTypeMapUri = createTypeMapUri(typeIds);
    final RyaType valueRyaType = new RyaType(XMLSchema.ANYURI, ryaTypeMapUri.stringValue());
    final Value typeValue = RyaToRdfConversions.convertValue(valueRyaType);
    objectMap.put(RYA_TYPES_URI, typeValue);
    final RyaURI subject = entity.getSubject();
    final Map<RyaURI, ImmutableMap<RyaURI, Property>> typeMap = entity.getProperties();
    for (final Entry<RyaURI, ImmutableMap<RyaURI, Property>> typeEntry : typeMap.entrySet()) {
        final RyaURI type = typeEntry.getKey();
        String typeShortName = ryaTypeMap.get(type);
        typeShortName = typeShortName != null ? typeShortName + "." : "";
        final ImmutableMap<RyaURI, Property> typeProperties = typeEntry.getValue();
        for (final Entry<RyaURI, Property> properties : typeProperties.entrySet()) {
            final RyaURI key = properties.getKey();
            final Property property = properties.getValue();
            final String valueString = property.getValue().getData();
            final RyaType ryaType = property.getValue();
            // final RyaType ryaType = new RyaType(new URIImpl(key.getData()), valueString);
            final Value value = RyaToRdfConversions.convertValue(ryaType);
            String formattedKey = key.getData();
            if (StringUtils.isNotBlank(typeShortName)) {
                formattedKey = addTypePrefixToUri(formattedKey, typeShortName);
            }
            final URI uri = new URIImpl(formattedKey);
            objectMap.put(uri, value);
        }
    }
    return serializeUri(subject, objectMap);
}
Also used : URIImpl(org.openrdf.model.impl.URIImpl) RyaType(org.apache.rya.api.domain.RyaType) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) RyaURI(org.apache.rya.api.domain.RyaURI) Value(org.openrdf.model.Value) Property(org.apache.rya.indexing.entity.model.Property)

Example 8 with Property

use of org.apache.rya.indexing.entity.model.Property in project incubator-rya by apache.

the class SmartUriAdapter method entityToValueMap.

public static Map<URI, Value> entityToValueMap(final Entity entity) {
    final Map<URI, Value> map = new LinkedHashMap<>();
    for (final Entry<RyaURI, ImmutableMap<RyaURI, Property>> entry : entity.getProperties().entrySet()) {
        for (final Entry<RyaURI, Property> property : entry.getValue().entrySet()) {
            final RyaURI propertyKey = property.getKey();
            final URI uri = new URIImpl(propertyKey.getData());
            final Property propertyValue = property.getValue();
            final Value value = RyaToRdfConversions.convertValue(propertyValue.getValue());
            map.put(uri, value);
        }
    }
    return map;
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI) Value(org.openrdf.model.Value) URIImpl(org.openrdf.model.impl.URIImpl) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) Property(org.apache.rya.indexing.entity.model.Property) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with Property

use of org.apache.rya.indexing.entity.model.Property in project incubator-rya by apache.

the class SmartUriAdapter method createTypePropertiesUri.

private static URI createTypePropertiesUri(final ImmutableMap<RyaURI, ImmutableMap<RyaURI, Property>> typeProperties) throws SmartUriException {
    final List<NameValuePair> nameValuePairs = new ArrayList<>();
    for (final Entry<RyaURI, ImmutableMap<RyaURI, Property>> typeProperty : typeProperties.entrySet()) {
        final RyaURI type = typeProperty.getKey();
        final Map<RyaURI, Property> propertyMap = typeProperty.getValue();
        final URI typeUri = createIndividualTypeWithPropertiesUri(type, propertyMap);
        final String keyString = type.getDataType().getLocalName();
        final String valueString = typeUri.getLocalName();
        nameValuePairs.add(new BasicNameValuePair(keyString, valueString));
    }
    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.addParameters(nameValuePairs);
    String uriString;
    try {
        final java.net.URI uri = uriBuilder.build();
        final String queryString = uri.getRawSchemeSpecificPart();
        uriString = "urn:test" + queryString;
    } catch (final URISyntaxException e) {
        throw new SmartUriException("Unable to create type properties for the Smart URI", e);
    }
    return new URIImpl(uriString);
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) URIImpl(org.openrdf.model.impl.URIImpl) URISyntaxException(java.net.URISyntaxException) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) ImmutableMap(com.google.common.collect.ImmutableMap) URIBuilder(org.apache.http.client.utils.URIBuilder) RyaURI(org.apache.rya.api.domain.RyaURI) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Property(org.apache.rya.indexing.entity.model.Property)

Example 10 with Property

use of org.apache.rya.indexing.entity.model.Property in project incubator-rya by apache.

the class SmartUriAdapter method convertMapToEntity.

private static Entity convertMapToEntity(final RyaURI subject, final Map<RyaURI, Map<URI, Value>> map) {
    final Entity.Builder entityBuilder = Entity.builder();
    entityBuilder.setSubject(subject);
    for (final Entry<RyaURI, Map<URI, Value>> typeEntry : map.entrySet()) {
        final RyaURI type = typeEntry.getKey();
        final Map<URI, Value> subMap = typeEntry.getValue();
        entityBuilder.setExplicitType(type);
        for (final Entry<URI, Value> entry : subMap.entrySet()) {
            final URI uri = entry.getKey();
            final Value value = entry.getValue();
            final RyaURI ryaUri = new RyaURI(uri.stringValue());
            final RyaURI ryaName = new RyaURI(uri.stringValue());
            final RyaType ryaType = new RyaType(value.stringValue());
            final Property property = new Property(ryaName, ryaType);
            entityBuilder.setProperty(ryaUri, property);
        }
    }
    final Entity entity = entityBuilder.build();
    return entity;
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity) RyaURI(org.apache.rya.api.domain.RyaURI) Value(org.openrdf.model.Value) RyaType(org.apache.rya.api.domain.RyaType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashBiMap(com.google.common.collect.HashBiMap) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) Property(org.apache.rya.indexing.entity.model.Property)

Aggregations

Property (org.apache.rya.indexing.entity.model.Property)35 RyaURI (org.apache.rya.api.domain.RyaURI)30 RyaType (org.apache.rya.api.domain.RyaType)25 Entity (org.apache.rya.indexing.entity.model.Entity)24 Test (org.junit.Test)21 EntityStorage (org.apache.rya.indexing.entity.storage.EntityStorage)18 TypedEntity (org.apache.rya.indexing.entity.model.TypedEntity)10 MongoEntityStorage (org.apache.rya.indexing.entity.storage.mongo.MongoEntityStorage)10 TypeStorage (org.apache.rya.indexing.entity.storage.TypeStorage)9 URI (org.openrdf.model.URI)8 URIImpl (org.openrdf.model.impl.URIImpl)8 MongoTypeStorage (org.apache.rya.indexing.entity.storage.mongo.MongoTypeStorage)7 Value (org.openrdf.model.Value)7 RyaStatement (org.apache.rya.api.domain.RyaStatement)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 Type (org.apache.rya.indexing.entity.model.Type)5 BindingSet (org.openrdf.query.BindingSet)5 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)5 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4