Search in sources :

Example 76 with RyaURI

use of org.apache.rya.api.domain.RyaURI in project incubator-rya by apache.

the class StatementMetadataNode method verifyHasCorrectTypePattern.

/**
 * Verifies StatementPatterns define a reified pattern with associated
 * metadata properties.
 *
 * @param patterns
 *            - The patterns to check. (not null)
 * @throws IllegalStateException
 *             No Type or the wrong Type is specified by the patterns.
 */
public static boolean verifyHasCorrectTypePattern(final Collection<StatementPattern> patterns) throws IllegalStateException {
    requireNonNull(patterns);
    boolean subjFound = false;
    boolean objFound = false;
    boolean predFound = false;
    boolean statementFound = false;
    boolean valid = true;
    boolean contextSet = false;
    Var context = null;
    for (final StatementPattern pattern : patterns) {
        final RyaURI predicate = new RyaURI(pattern.getPredicateVar().getValue().toString());
        if (!contextSet) {
            context = pattern.getContextVar();
            contextSet = true;
        } else {
            if (context != null && !context.equals(pattern.getContextVar())) {
                return false;
            }
        }
        if (predicate.equals(TYPE_ID_URI)) {
            final RyaURI statementID = new RyaURI(pattern.getObjectVar().getValue().stringValue());
            if (statementID.equals(STATEMENT_ID_URI)) {
                statementFound = true;
            } else {
                // contains more than one Statement containing TYPE_ID_URI
                // as Predicate
                // and STATEMENT_ID_URI as Object
                valid = false;
            }
        }
        if (predicate.equals(SUBJ_ID_URI)) {
            if (!subjFound) {
                subjFound = true;
            } else {
                // contains more than Subject SP
                valid = false;
            }
        }
        if (predicate.equals(PRED_ID_URI)) {
            if (!predFound) {
                predFound = true;
            } else {
                // contains more than one Predicate SP
                valid = false;
            }
        }
        if (predicate.equals(OBJ_ID_URI)) {
            if (!objFound) {
                objFound = true;
            } else {
                // contains more than one Object SP
                valid = false;
            }
        }
    }
    return valid && statementFound && subjFound && predFound && objFound;
}
Also used : StatementPattern(org.openrdf.query.algebra.StatementPattern) RyaURI(org.apache.rya.api.domain.RyaURI) Var(org.openrdf.query.algebra.Var)

Example 77 with RyaURI

use of org.apache.rya.api.domain.RyaURI in project incubator-rya by apache.

the class StatementMetadataNode method setStatementPatternAndProperties.

/**
 * Constructs a {@link StatementPattern} from the StatementPatterns
 * representing a reified query. This StatementPattern has as a subject, the
 * object of the StatementPattern containing the predicate
 * {@link RDF#SUBJECT}. This StatementPattern has as predicate, the object
 * of the StatementPattern containing the predicate {@link RDF#PREDICATE}.
 * This StatementPattern has as an object, the object of the
 * StatementPattern containing the predicate {@link RDF#OBJECT}. This method
 * also builds a map between all predicates that are not of the above type
 * and the object {@link Var}s they are associated with. This map contains
 * the user specified metadata properties and is used for comparison with
 * the metadata properties extracted from RyaStatements passed back by the
 * {@link RyaQueryEngine}.
 *
 * @param patterns
 *            - collection of patterns representing a reified query
 */
private void setStatementPatternAndProperties(Collection<StatementPattern> patterns) {
    StatementPattern sp = new StatementPattern();
    Map<RyaURI, Var> properties = new HashMap<>();
    for (final StatementPattern pattern : patterns) {
        final RyaURI predicate = new RyaURI(pattern.getPredicateVar().getValue().toString());
        if (!uriList.contains(predicate)) {
            Var objVar = pattern.getObjectVar();
            properties.put(predicate, objVar);
            continue;
        }
        if (predicate.equals(SUBJ_ID_URI)) {
            sp.setContextVar(pattern.getContextVar());
            sp.setSubjectVar(pattern.getObjectVar());
        }
        if (predicate.equals(PRED_ID_URI)) {
            sp.setPredicateVar(pattern.getObjectVar());
        }
        if (predicate.equals(OBJ_ID_URI)) {
            sp.setObjectVar(pattern.getObjectVar());
        }
    }
    this.statement = sp;
    this.properties = properties;
}
Also used : StatementPattern(org.openrdf.query.algebra.StatementPattern) RyaURI(org.apache.rya.api.domain.RyaURI) HashMap(java.util.HashMap) Var(org.openrdf.query.algebra.Var)

Example 78 with RyaURI

use of org.apache.rya.api.domain.RyaURI 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 79 with RyaURI

use of org.apache.rya.api.domain.RyaURI in project incubator-rya by apache.

the class SmartUriAdapter method convertUriToTypeMap.

private static Map<RyaURI, String> convertUriToTypeMap(final URI typeMapUri) throws SmartUriException {
    final Map<RyaURI, String> map = new HashMap<>();
    java.net.URI uri;
    try {
        final URIBuilder uriBuilder = new URIBuilder(typeMapUri.stringValue());
        uri = uriBuilder.build();
    } catch (final URISyntaxException e) {
        throw new SmartUriException("Unable to parse Rya type map in Smart URI", e);
    }
    final List<NameValuePair> params = URLEncodedUtils.parse(uri, Charsets.UTF_8.name());
    for (final NameValuePair param : params) {
        final String name = param.getName();
        final String value = param.getValue();
        final RyaURI type = new RyaURI(name);
        map.put(type, value);
    }
    return map;
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) URISyntaxException(java.net.URISyntaxException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 80 with RyaURI

use of org.apache.rya.api.domain.RyaURI 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)

Aggregations

RyaURI (org.apache.rya.api.domain.RyaURI)287 Test (org.junit.Test)190 RyaStatement (org.apache.rya.api.domain.RyaStatement)183 RyaType (org.apache.rya.api.domain.RyaType)146 BindingSet (org.openrdf.query.BindingSet)56 ArrayList (java.util.ArrayList)52 StatementPattern (org.openrdf.query.algebra.StatementPattern)50 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)49 HashSet (java.util.HashSet)43 RyaDAOException (org.apache.rya.api.persist.RyaDAOException)43 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)42 ParsedQuery (org.openrdf.query.parser.ParsedQuery)42 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)42 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)35 Entity (org.apache.rya.indexing.entity.model.Entity)30 Property (org.apache.rya.indexing.entity.model.Property)28 URIImpl (org.openrdf.model.impl.URIImpl)25 EntityStorage (org.apache.rya.indexing.entity.storage.EntityStorage)22 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)21 TripleRow (org.apache.rya.api.resolver.triple.TripleRow)21