Search in sources :

Example 46 with RyaType

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);
}
Also used : ArrayList(java.util.ArrayList) Document(org.bson.Document) RyaType(org.apache.rya.api.domain.RyaType)

Example 47 with RyaType

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 {
        }
    };
}
Also used : CloseableIterator(org.apache.rya.api.utils.CloseableIterator) Value(org.openrdf.model.Value) URIImpl(org.openrdf.model.impl.URIImpl) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Document(org.bson.Document) RyaType(org.apache.rya.api.domain.RyaType) URI(org.openrdf.model.URI)

Example 48 with RyaType

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;
}
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 49 with RyaType

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);
}
Also used : ArrayList(java.util.ArrayList) Value(org.openrdf.model.Value) RyaType(org.apache.rya.api.domain.RyaType)

Example 50 with RyaType

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

Aggregations

RyaType (org.apache.rya.api.domain.RyaType)178 RyaURI (org.apache.rya.api.domain.RyaURI)146 RyaStatement (org.apache.rya.api.domain.RyaStatement)115 Test (org.junit.Test)109 BindingSet (org.openrdf.query.BindingSet)42 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)42 ArrayList (java.util.ArrayList)35 StatementPattern (org.openrdf.query.algebra.StatementPattern)35 ParsedQuery (org.openrdf.query.parser.ParsedQuery)35 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)35 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)34 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)34 RyaDAOException (org.apache.rya.api.persist.RyaDAOException)28 Property (org.apache.rya.indexing.entity.model.Property)23 URIImpl (org.openrdf.model.impl.URIImpl)23 HashSet (java.util.HashSet)22 Entity (org.apache.rya.indexing.entity.model.Entity)20 Value (org.openrdf.model.Value)19 LiteralImpl (org.openrdf.model.impl.LiteralImpl)19 BatchWriter (org.apache.accumulo.core.client.BatchWriter)18