Search in sources :

Example 11 with FixedStatementPattern

use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.

the class AbstractInferVisitor method meet.

@Override
public void meet(StatementPattern sp) throws Exception {
    if (!include) {
        return;
    }
    if (sp instanceof FixedStatementPattern || sp instanceof TransitivePropertySP || sp instanceof DoNotExpandSP) {
        // already inferred somewhere else
        return;
    }
    final Var predVar = sp.getPredicateVar();
    // we do not let timeRange preds be inferred, not good
    if (predVar == null || predVar.getValue() == null) // || RdfCloudTripleStoreUtils.getTtlValueConverter(conf, (URI) predVar.getValue()) != null
    {
        return;
    }
    meetSP(sp);
}
Also used : Var(org.openrdf.query.algebra.Var) TransitivePropertySP(org.apache.rya.rdftriplestore.utils.TransitivePropertySP) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern)

Example 12 with FixedStatementPattern

use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.

the class AllValuesFromVisitor method meetSP.

/**
 * Checks whether the StatementPattern is a type query whose solutions could be inferred
 * by allValuesFrom inference, and if so, replaces the node with a union of itself and any
 * possible inference.
 */
@Override
protected void meetSP(StatementPattern node) throws Exception {
    final Var subjVar = node.getSubjectVar();
    final Var predVar = node.getPredicateVar();
    final Var objVar = node.getObjectVar();
    // Only applies to type queries where the type is defined
    if (predVar != null && RDF.TYPE.equals(predVar.getValue()) && objVar != null && objVar.getValue() instanceof Resource) {
        final Resource typeToInfer = (Resource) objVar.getValue();
        Map<Resource, Set<URI>> relevantAvfRestrictions = inferenceEngine.getAllValuesFromByValueType(typeToInfer);
        if (!relevantAvfRestrictions.isEmpty()) {
            // We can infer the queried type if, for an allValuesFrom restriction type
            // associated  with the queried type, some anonymous neighboring node belongs to the
            // restriction type and has the node in question (subjVar) as a value for the
            // restriction's property.
            final Var avfTypeVar = new Var("t-" + UUID.randomUUID());
            final Var avfPredVar = new Var("p-" + UUID.randomUUID());
            final Var neighborVar = new Var("n-" + UUID.randomUUID());
            neighborVar.setAnonymous(true);
            final StatementPattern membershipPattern = new DoNotExpandSP(neighborVar, new Var(RDF.TYPE.stringValue(), RDF.TYPE), avfTypeVar);
            final StatementPattern valuePattern = new StatementPattern(neighborVar, avfPredVar, subjVar);
            final InferJoin avfPattern = new InferJoin(membershipPattern, valuePattern);
            // Use a FixedStatementPattern to contain the appropriate (restriction, predicate)
            // pairs, and check each one against the general pattern.
            final FixedStatementPattern avfPropertyTypes = new FixedStatementPattern(avfTypeVar, new Var(OWL.ONPROPERTY.stringValue(), OWL.ONPROPERTY), avfPredVar);
            for (Resource avfRestrictionType : relevantAvfRestrictions.keySet()) {
                for (URI avfProperty : relevantAvfRestrictions.get(avfRestrictionType)) {
                    avfPropertyTypes.statements.add(new NullableStatementImpl(avfRestrictionType, OWL.ONPROPERTY, avfProperty));
                }
            }
            final InferJoin avfInferenceQuery = new InferJoin(avfPropertyTypes, avfPattern);
            node.replaceWith(new InferUnion(node.clone(), avfInferenceQuery));
        }
    }
}
Also used : FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) StatementPattern(org.openrdf.query.algebra.StatementPattern) NullableStatementImpl(org.apache.rya.api.utils.NullableStatementImpl) Set(java.util.Set) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) URI(org.openrdf.model.URI) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern)

Example 13 with FixedStatementPattern

use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.

the class DomainRangeVisitor method meetSP.

/**
 * Checks whether this statement pattern might be inferred using domain and/or range knowledge,
 * and, if so, replaces the statement pattern with a union of itself and any possible
 * derivations.
 */
@Override
protected void meetSP(StatementPattern node) throws Exception {
    final Var subjVar = node.getSubjectVar();
    final Var predVar = node.getPredicateVar();
    final Var objVar = node.getObjectVar();
    final Var contextVar = node.getContextVar();
    // Only applies to statement patterns that query for members of a defined type.
    if (predVar != null && RDF.TYPE.equals(predVar.getValue()) && objVar != null && objVar.getValue() instanceof URI) {
        final URI inferredType = (URI) objVar.getValue();
        // Preserve the original node so explicit type assertions are still matched:
        TupleExpr currentNode = node.clone();
        // If there are any properties with this type as domain, check for appropriate triples:
        final Set<URI> domainProperties = inferenceEngine.getPropertiesWithDomain(inferredType);
        if (!domainProperties.isEmpty()) {
            Var domainPredVar = new Var("p-" + UUID.randomUUID());
            Var domainObjVar = new Var("o-" + UUID.randomUUID());
            domainObjVar.setAnonymous(true);
            Var domainVar = new Var(RDFS.DOMAIN.stringValue(), RDFS.DOMAIN);
            StatementPattern domainSP = new DoNotExpandSP(subjVar, domainPredVar, domainObjVar, contextVar);
            // Enumerate predicates having this type as domain
            FixedStatementPattern domainFSP = new FixedStatementPattern(domainPredVar, domainVar, objVar);
            for (URI property : domainProperties) {
                domainFSP.statements.add(new NullableStatementImpl(property, RDFS.DOMAIN, inferredType));
            }
            // For each such predicate, any triple <subjVar predicate _:any> implies the type
            currentNode = new InferUnion(currentNode, new InferJoin(domainFSP, domainSP));
        }
        // If there are any properties with this type as range, check for appropriate triples:
        final Set<URI> rangeProperties = inferenceEngine.getPropertiesWithRange(inferredType);
        if (!rangeProperties.isEmpty()) {
            Var rangePredVar = new Var("p-" + UUID.randomUUID());
            Var rangeSubjVar = new Var("s-" + UUID.randomUUID());
            rangeSubjVar.setAnonymous(true);
            Var rangeVar = new Var(RDFS.RANGE.stringValue(), RDFS.RANGE);
            StatementPattern rangeSP = new DoNotExpandSP(rangeSubjVar, rangePredVar, subjVar, contextVar);
            // Enumerate predicates having this type as range
            FixedStatementPattern rangeFSP = new FixedStatementPattern(rangePredVar, rangeVar, objVar);
            for (URI property : rangeProperties) {
                rangeFSP.statements.add(new NullableStatementImpl(property, RDFS.RANGE, inferredType));
            }
            // For each such predicate, any triple <_:any predicate subjVar> implies the type
            currentNode = new InferUnion(currentNode, new InferJoin(rangeFSP, rangeSP));
        }
        node.replaceWith(currentNode);
    }
}
Also used : FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) StatementPattern(org.openrdf.query.algebra.StatementPattern) NullableStatementImpl(org.apache.rya.api.utils.NullableStatementImpl) Var(org.openrdf.query.algebra.Var) URI(org.openrdf.model.URI) TupleExpr(org.openrdf.query.algebra.TupleExpr) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern)

Example 14 with FixedStatementPattern

use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.

the class HasValueVisitor method meetSP.

/**
 * Checks whether facts matching the StatementPattern could be derived using
 * has-value inference, and if so, replaces the StatementPattern node with a
 * union of itself and any such possible derivations.
 */
@Override
protected void meetSP(StatementPattern node) throws Exception {
    final Var subjVar = node.getSubjectVar();
    final Var predVar = node.getPredicateVar();
    final Var objVar = node.getObjectVar();
    // Both require defined predicate
    if (predVar != null && predVar.getValue() != null) {
        final URI predURI = (URI) predVar.getValue();
        if (RDF.TYPE.equals(predURI) && objVar != null && objVar.getValue() != null && objVar.getValue() instanceof Resource) {
            // If the predicate is rdf:type and the type is specified, check whether it can be
            // inferred using any hasValue restriction(s)
            final Resource objType = (Resource) objVar.getValue();
            final Map<URI, Set<Value>> sufficientValues = inferenceEngine.getHasValueByType(objType);
            if (sufficientValues.size() > 0) {
                final Var valueVar = new Var("v-" + UUID.randomUUID());
                TupleExpr currentNode = node.clone();
                for (URI property : sufficientValues.keySet()) {
                    final Var propVar = new Var(property.toString(), property);
                    final TupleExpr valueSP = new DoNotExpandSP(subjVar, propVar, valueVar);
                    final FixedStatementPattern relevantValues = new FixedStatementPattern(objVar, propVar, valueVar);
                    for (Value value : sufficientValues.get(property)) {
                        relevantValues.statements.add(new NullableStatementImpl(objType, property, value));
                    }
                    currentNode = new InferUnion(currentNode, new InferJoin(relevantValues, valueSP));
                }
                node.replaceWith(currentNode);
            }
        } else {
            // If the predicate has some hasValue restriction associated with it, then finding
            // that the object belongs to the appropriate type implies a value.
            final Map<Resource, Set<Value>> impliedValues = inferenceEngine.getHasValueByProperty(predURI);
            if (impliedValues.size() > 0) {
                final Var rdfTypeVar = new Var(RDF.TYPE.stringValue(), RDF.TYPE);
                final Var typeVar = new Var("t-" + UUID.randomUUID());
                final Var hasValueVar = new Var(OWL.HASVALUE.stringValue(), OWL.HASVALUE);
                final TupleExpr typeSP = new DoNotExpandSP(subjVar, rdfTypeVar, typeVar);
                final FixedStatementPattern typeToValue = new FixedStatementPattern(typeVar, hasValueVar, objVar);
                final TupleExpr directValueSP = node.clone();
                for (Resource type : impliedValues.keySet()) {
                    // { ?var rdf:type :type } implies { ?var :property :val } for certain (:type, :val) pairs
                    for (Value impliedValue : impliedValues.get(type)) {
                        typeToValue.statements.add(new NullableStatementImpl(type, OWL.HASVALUE, impliedValue));
                    }
                }
                node.replaceWith(new InferUnion(new InferJoin(typeToValue, typeSP), directValueSP));
            }
        }
    }
}
Also used : Set(java.util.Set) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) URI(org.openrdf.model.URI) TupleExpr(org.openrdf.query.algebra.TupleExpr) NullableStatementImpl(org.apache.rya.api.utils.NullableStatementImpl) Value(org.openrdf.model.Value) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern)

Example 15 with FixedStatementPattern

use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.

the class ParallelEvaluationStrategyImpl method evaluate.

public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final StatementPattern sp, Collection<BindingSet> bindings) throws QueryEvaluationException {
    final Var subjVar = sp.getSubjectVar();
    final Var predVar = sp.getPredicateVar();
    final Var objVar = sp.getObjectVar();
    final Var cntxtVar = sp.getContextVar();
    List<Map.Entry<Statement, BindingSet>> stmts = new ArrayList<Map.Entry<Statement, BindingSet>>();
    Iteration<? extends Map.Entry<Statement, BindingSet>, QueryEvaluationException> iter;
    if (sp instanceof FixedStatementPattern) {
        Collection<Map.Entry<Statement, BindingSet>> coll = Lists.newArrayList();
        for (BindingSet binding : bindings) {
            Value subjValue = getVarValue(subjVar, binding);
            Value predValue = getVarValue(predVar, binding);
            Value objValue = getVarValue(objVar, binding);
            Resource contxtValue = (Resource) getVarValue(cntxtVar, binding);
            for (Statement st : ((FixedStatementPattern) sp).statements) {
                if (!((subjValue != null && !subjValue.equals(st.getSubject())) || (predValue != null && !predValue.equals(st.getPredicate())) || (objValue != null && !objValue.equals(st.getObject())))) {
                    coll.add(new RdfCloudTripleStoreUtils.CustomEntry<Statement, BindingSet>(st, binding));
                }
            }
        }
        iter = new IteratorIteration(coll.iterator());
    } else if (sp instanceof TransitivePropertySP && ((subjVar != null && subjVar.getValue() != null) || (objVar != null && objVar.getValue() != null)) && sp.getPredicateVar() != null) {
        // if this is a transitive prop ref, we need to make sure that either the subj or obj is not null
        // TODO: Cannot handle a open ended transitive property where subj and obj are null
        // TODO: Should one day handle filling in the subj or obj with bindings and working this
        // TODO: a lot of assumptions, and might be a large set returned causing an OME
        Set<Statement> sts = null;
        try {
            sts = inferenceEngine.findTransitiveProperty((Resource) getVarValue(subjVar), (URI) getVarValue(predVar), getVarValue(objVar), (Resource) getVarValue(cntxtVar));
        } catch (InferenceEngineException e) {
            throw new QueryEvaluationException(e);
        }
        Collection<Map.Entry<Statement, BindingSet>> coll = new ArrayList();
        for (BindingSet binding : bindings) {
            for (Statement st : sts) {
                coll.add(new RdfCloudTripleStoreUtils.CustomEntry<Statement, BindingSet>(st, binding));
            }
        }
        iter = new IteratorIteration(coll.iterator());
    } else {
        for (BindingSet binding : bindings) {
            Value subjValue = getVarValue(subjVar, binding);
            Value predValue = getVarValue(predVar, binding);
            Value objValue = getVarValue(objVar, binding);
            Resource contxtValue = (Resource) getVarValue(cntxtVar, binding);
            if ((subjValue != null && !(subjValue instanceof Resource)) || (predValue != null && !(predValue instanceof URI))) {
                continue;
            }
            stmts.add(new RdfCloudTripleStoreUtils.CustomEntry<Statement, BindingSet>(new NullableStatementImpl((Resource) subjValue, (URI) predValue, objValue, contxtValue), binding));
        }
        if (stmts.size() == 0) {
            return new EmptyIteration();
        }
        iter = ((RdfCloudTripleStoreConnection.StoreTripleSource) tripleSource).getStatements(stmts);
    }
    return new ConvertingIteration<Map.Entry<Statement, BindingSet>, BindingSet, QueryEvaluationException>(iter) {

        @Override
        protected BindingSet convert(Map.Entry<Statement, BindingSet> stbs) throws QueryEvaluationException {
            Statement st = stbs.getKey();
            BindingSet bs = stbs.getValue();
            QueryBindingSet result = new QueryBindingSet(bs);
            // contain a Value for that Var name
            if (subjVar != null && !subjVar.isConstant() && !result.hasBinding(subjVar.getName())) {
                result.addBinding(subjVar.getName(), st.getSubject());
            }
            if (predVar != null && !predVar.isConstant() && !result.hasBinding(predVar.getName())) {
                result.addBinding(predVar.getName(), st.getPredicate());
            }
            if (objVar != null && !objVar.isConstant() && !result.hasBinding(objVar.getName())) {
                result.addBinding(objVar.getName(), st.getObject());
            }
            if (cntxtVar != null && !cntxtVar.isConstant() && !result.hasBinding(cntxtVar.getName()) && st.getContext() != null) {
                result.addBinding(cntxtVar.getName(), st.getContext());
            }
            return result;
        }
    };
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) Set(java.util.Set) StoreTripleSource(org.apache.rya.rdftriplestore.RdfCloudTripleStoreConnection.StoreTripleSource) ConvertingIteration(info.aduna.iteration.ConvertingIteration) Var(org.openrdf.query.algebra.Var) ArrayList(java.util.ArrayList) URI(org.openrdf.model.URI) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) Statement(org.openrdf.model.Statement) EmptyIteration(info.aduna.iteration.EmptyIteration) Resource(org.openrdf.model.Resource) InferenceEngineException(org.apache.rya.rdftriplestore.inference.InferenceEngineException) TransitivePropertySP(org.apache.rya.rdftriplestore.utils.TransitivePropertySP) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) RdfCloudTripleStoreUtils(org.apache.rya.api.RdfCloudTripleStoreUtils) NullableStatementImpl(org.apache.rya.api.utils.NullableStatementImpl) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Value(org.openrdf.model.Value) IteratorIteration(info.aduna.iteration.IteratorIteration) Collection(java.util.Collection) Map(java.util.Map)

Aggregations

FixedStatementPattern (org.apache.rya.rdftriplestore.utils.FixedStatementPattern)19 Var (org.openrdf.query.algebra.Var)16 StatementPattern (org.openrdf.query.algebra.StatementPattern)12 NullableStatementImpl (org.apache.rya.api.utils.NullableStatementImpl)11 URI (org.openrdf.model.URI)11 Resource (org.openrdf.model.Resource)10 Set (java.util.Set)9 Join (org.openrdf.query.algebra.Join)8 TupleExpr (org.openrdf.query.algebra.TupleExpr)6 HashSet (java.util.HashSet)5 Test (org.junit.Test)5 Projection (org.openrdf.query.algebra.Projection)5 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)5 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)5 Union (org.openrdf.query.algebra.Union)5 HashMap (java.util.HashMap)4 Value (org.openrdf.model.Value)4 DoNotExpandSP (org.apache.rya.rdftriplestore.inference.DoNotExpandSP)3 TransitivePropertySP (org.apache.rya.rdftriplestore.utils.TransitivePropertySP)3 Statement (org.openrdf.model.Statement)3