Search in sources :

Example 36 with RyaURI

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

the class StarQuery method init.

// called in constructor after nodes set
// assumes nodes and nodeColumnCond are same size
private void init() throws RyaTypeResolverException {
    commonVar = this.getCommonVar(nodes);
    if (!commonVar.isConstant()) {
        commonVarName = commonVar.getName();
    } else {
        commonVarName = commonVar.getName().substring(7);
    }
    if (hasContext()) {
        final RyaURI ctxtURI = (RyaURI) RdfToRyaConversions.convertValue(context.getValue());
        contextURI = ctxtURI.getData();
    }
    for (int i = 0; i < nodes.size(); i++) {
        nodeColumnCond[i] = nodeToTextColumn(nodes.get(i), i);
    }
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI)

Example 37 with RyaURI

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

the class StarQuery method nodeToTextColumn.

// assumes nodes forms valid star query with only one common variable
// assumes nodes and commonVar has been set
private TextColumn nodeToTextColumn(final StatementPattern node, final int i) throws RyaTypeResolverException {
    final RyaContext rc = RyaContext.getInstance();
    final Var subjVar = node.getSubjectVar();
    final Var predVar = node.getPredicateVar();
    final Var objVar = node.getObjectVar();
    final RyaURI predURI = (RyaURI) RdfToRyaConversions.convertValue(node.getPredicateVar().getValue());
    // assumes StatementPattern contains at least on variable
    if (subjVar.isConstant()) {
        if (commonVarConstant()) {
            varPos.put(objVar.getName(), i);
            return new TextColumn(new Text(predURI.getData()), new Text("object"));
        } else {
            return new TextColumn(new Text(predURI.getData()), new Text("subject" + "\u0000" + subjVar.getValue().stringValue()));
        }
    } else if (objVar.isConstant()) {
        if (commonVarConstant()) {
            varPos.put(subjVar.getName(), i);
            return new TextColumn(new Text(predURI.getData()), new Text("subject"));
        } else {
            isCommonVarURI = true;
            final RyaType objType = RdfToRyaConversions.convertValue(objVar.getValue());
            final byte[][] b1 = rc.serializeType(objType);
            final byte[] b2 = Bytes.concat("object".getBytes(StandardCharsets.UTF_8), "\u0000".getBytes(StandardCharsets.UTF_8), b1[0], b1[1]);
            return new TextColumn(new Text(predURI.getData()), new Text(b2));
        }
    } else {
        if (subjVar.getName().equals(commonVarName)) {
            isCommonVarURI = true;
            varPos.put(objVar.getName(), i);
            final TextColumn tc = new TextColumn(new Text(predURI.getData()), new Text("object"));
            tc.setIsPrefix(true);
            return tc;
        } else {
            varPos.put(subjVar.getName(), i);
            final TextColumn tc = new TextColumn(new Text(predURI.getData()), new Text("subject"));
            tc.setIsPrefix(true);
            return tc;
        }
    }
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI) RyaContext(org.apache.rya.api.resolver.RyaContext) Var(org.openrdf.query.algebra.Var) Text(org.apache.hadoop.io.Text) RyaType(org.apache.rya.api.domain.RyaType) TextColumn(org.apache.rya.accumulo.documentIndex.TextColumn)

Example 38 with RyaURI

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

the class EntityIndexSetProvider method discoverEntities.

private void discoverEntities(final StatementPattern pattern, final List<StatementPattern> unmatched) {
    final Var subj = pattern.getSubjectVar();
    final String subjStr = subj.getName();
    final RyaURI predURI = getPredURI(pattern);
    // check to see if current node is type
    if (new URIImpl(predURI.getData()).equals(RDF.TYPE)) {
        final Var obj = pattern.getObjectVar();
        final RyaURI objURI = new RyaURI(obj.getValue().stringValue());
        try {
            final Optional<Type> optType = typeStorage.get(objURI);
            // if is type, fetch type add to subject -> type map
            if (optType.isPresent()) {
                final Type type = optType.get();
                typeMap.put(type, pattern);
                subjectTypeMap.put(subjStr, type);
                // check unmatched properties, add matches
                for (final StatementPattern propertyPattern : unmatched) {
                    // store sps into the type -> property map
                    final RyaURI property = getPredURI(propertyPattern);
                    final Var typeSubVar = getTypeSubject(type);
                    final Var patternSubVar = propertyPattern.getSubjectVar();
                    if (type.getPropertyNames().contains(property) && typeSubVar.equals(patternSubVar)) {
                        typeMap.put(type, propertyPattern);
                    }
                }
            }
        } catch (final TypeStorageException e) {
            e.printStackTrace();
        }
    } else {
        // if not type, check to see if subject is in type map
        if (subjectTypeMap.containsKey(subjStr)) {
            // if is, check to see if pred is a property of type
            final Type type = subjectTypeMap.get(subjStr);
            if (type.getPropertyNames().contains(predURI)) {
                // if is, add sp to type -> sp map
                if (!typeMap.containsKey(type)) {
                    // each variable can only contain 1 type for now @see:Rya-235?
                    typeMap.put(type, pattern);
                }
            } else {
                // if not, add to unmatched type
                unmatched.add(pattern);
            }
        } else {
            // if not, add to unmatched
            unmatched.add(pattern);
        }
    }
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI) StatementPattern(org.openrdf.query.algebra.StatementPattern) Type(org.apache.rya.indexing.entity.model.Type) Var(org.openrdf.query.algebra.Var) URIImpl(org.openrdf.model.impl.URIImpl) TypeStorageException(org.apache.rya.indexing.entity.storage.TypeStorage.TypeStorageException)

Example 39 with RyaURI

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

the class EntityQueryNode method findBindings.

private List<BindingSet> findBindings(final BindingSet bindingSet) throws QueryEvaluationException {
    final MapBindingSet resultSet = new MapBindingSet();
    try {
        final ConvertingCursor<TypedEntity> entitiesCursor;
        final String subj;
        // If the subject needs to be filled in, check if the subject variable is in the binding set.
        if (subjectIsConstant) {
            // if it is, fetch that value and then fetch the entity for the subject.
            subj = subjectConstant.get();
            entitiesCursor = entities.search(Optional.of(new RyaURI(subj)), type, properties);
        } else {
            entitiesCursor = entities.search(Optional.empty(), type, properties);
        }
        while (entitiesCursor.hasNext()) {
            final TypedEntity typedEntity = entitiesCursor.next();
            final ImmutableCollection<Property> properties = typedEntity.getProperties();
            // ensure properties match and only add properties that are in the statement patterns to the binding set
            for (final RyaURI key : objectVariables.keySet()) {
                final Optional<RyaType> prop = typedEntity.getPropertyValue(new RyaURI(key.getData()));
                if (prop.isPresent()) {
                    final RyaType type = prop.get();
                    final String bindingName = objectVariables.get(key).getName();
                    resultSet.addBinding(bindingName, ValueFactoryImpl.getInstance().createLiteral(type.getData()));
                }
            }
        }
    } catch (final EntityStorageException e) {
        throw new QueryEvaluationException("Failed to evaluate the binding set", e);
    }
    bindingSet.forEach(new Consumer<Binding>() {

        @Override
        public void accept(final Binding binding) {
            resultSet.addBinding(binding);
        }
    });
    final List<BindingSet> list = new ArrayList<>();
    list.add(resultSet);
    return list;
}
Also used : Binding(org.openrdf.query.Binding) MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) ArrayList(java.util.ArrayList) RyaType(org.apache.rya.api.domain.RyaType) RyaURI(org.apache.rya.api.domain.RyaURI) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) TypedEntity(org.apache.rya.indexing.entity.model.TypedEntity) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Property(org.apache.rya.indexing.entity.model.Property) EntityStorageException(org.apache.rya.indexing.entity.storage.EntityStorage.EntityStorageException)

Example 40 with RyaURI

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

the class EntityQueryNode method verifyAllPredicatesPartOfType.

/**
 * Verify all of the patterns have predicates that match one of the Type's property names.
 *
 * @param type - The Type the patterns match. (not null)
 * @param patterns - The patterns to check.
 * @throws IllegalStateException If any of the non-type defining Statement Patterns
 *   contain a predicate that does not match one of the Type's property names.
 */
private static void verifyAllPredicatesPartOfType(final Type type, final Collection<StatementPattern> patterns) throws IllegalStateException {
    requireNonNull(type);
    requireNonNull(patterns);
    for (final StatementPattern pattern : patterns) {
        // Skip TYPE patterns.
        final RyaURI predicate = new RyaURI(pattern.getPredicateVar().getValue().toString());
        if (predicate.equals(TYPE_ID_URI)) {
            continue;
        }
        if (!type.getPropertyNames().contains(predicate)) {
            throw new IllegalStateException("The Predicate of a Statement Pattern must be a property name for the Type. " + "Type ID: '" + type.getId().getData() + "' Pattern: " + pattern);
        }
    }
}
Also used : StatementPattern(org.openrdf.query.algebra.StatementPattern) RyaURI(org.apache.rya.api.domain.RyaURI)

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