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;
}
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);
}
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;
}
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);
}
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;
}
Aggregations