use of org.apache.rya.api.domain.RyaType in project incubator-rya by apache.
the class MongoPcjDocuments method getResults.
/**
* Retrieves the stored {@link BindingSet} results for the provided pcjId.
*
* @param pcjId - The Id of the PCJ to retrieve results from.
* @param restrictionBindings - The collection of {@link BindingSet}s to restrict results.
* <p>
* Note: the result restrictions from {@link BindingSet}s are an OR
* over ANDS in that: <code>
* [
* bindingset: binding AND binding AND binding,
* OR
* bindingset: binding AND binding AND binding,
* .
* .
* .
* OR
* bindingset: binding
* ]
* </code>
* @return
*/
public CloseableIterator<BindingSet> getResults(final String pcjId, final Collection<BindingSet> restrictionBindings) {
// empty bindings return all results.
if (restrictionBindings.size() == 1 && restrictionBindings.iterator().next().size() == 0) {
return listResults(pcjId);
}
final Document query = new Document(PCJ_ID, pcjId);
final Document bindingSetDoc = new Document();
final List<Document> bindingSetList = new ArrayList<>();
restrictionBindings.forEach(bindingSet -> {
final Document bindingDoc = new Document();
final List<Document> bindings = new ArrayList<>();
bindingSet.forEach(binding -> {
final RyaType type = RdfToRyaConversions.convertValue(binding.getValue());
final Document typeDoc = new Document().append(BINDING_TYPE, type.getDataType().stringValue()).append(BINDING_VALUE, type.getData());
final Document bind = new Document(binding.getName(), typeDoc);
bindings.add(bind);
});
bindingDoc.append("$and", bindings);
bindingSetList.add(bindingDoc);
});
bindingSetDoc.append("$or", bindingSetList);
return queryForBindings(query);
}
use of org.apache.rya.api.domain.RyaType in project incubator-rya by apache.
the class MongoPcjDocuments method queryForBindings.
private CloseableIterator<BindingSet> queryForBindings(final Document query) {
final FindIterable<Document> rez = pcjCollection.find(query);
final Iterator<Document> resultsIter = rez.iterator();
return new CloseableIterator<BindingSet>() {
@Override
public boolean hasNext() {
return resultsIter.hasNext();
}
@Override
public BindingSet next() {
final Document bs = resultsIter.next();
final MapBindingSet binding = new MapBindingSet();
for (final String key : bs.keySet()) {
if (key.equals(VISIBILITIES_FIELD)) {
// has auths, is a visibility binding set.
} else if (!key.equals("_id") && !key.equals(PCJ_ID)) {
// is the binding value.
final Document typeDoc = (Document) bs.get(key);
final URI dataType = new URIImpl(typeDoc.getString(BINDING_TYPE));
final RyaType type = new RyaType(dataType, typeDoc.getString(BINDING_VALUE));
final Value value = RyaToRdfConversions.convertValue(type);
binding.addBinding(key, value);
}
}
return binding;
}
@Override
public void close() throws Exception {
}
};
}
use of org.apache.rya.api.domain.RyaType 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.api.domain.RyaType in project incubator-rya by apache.
the class BindingSetStringConverter method convert.
@Override
public String convert(final BindingSet bindingSet, final VariableOrder varOrder) {
requireNonNull(bindingSet);
requireNonNull(varOrder);
// Convert each Binding to a String.
final List<String> bindingStrings = new ArrayList<>();
for (final String varName : varOrder) {
if (bindingSet.hasBinding(varName)) {
// Add a value to the binding set.
final Value value = bindingSet.getBinding(varName).getValue();
final RyaType ryaValue = RdfToRyaConversions.convertValue(value);
final String bindingString = ryaValue.getData() + TYPE_DELIM + ryaValue.getDataType();
bindingStrings.add(bindingString);
} else {
// Add a null value to the binding set.
bindingStrings.add(NULL_VALUE_STRING);
}
}
// Join the bindings using the binding delim.
return Joiner.on(BINDING_DELIM).join(bindingStrings);
}
use of org.apache.rya.api.domain.RyaType 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);
}
Aggregations