Search in sources :

Example 1 with UnsupportedQueryLanguageException

use of org.openrdf.query.UnsupportedQueryLanguageException in project incubator-rya by apache.

the class QueryRuleset method setRules.

/**
 * Extract the rules from the query string, applying inference rules if configured to.
 * @throws QueryRulesetException if the parsed query can't be parsed and translated into valid rules.
 */
private void setRules() throws QueryRulesetException {
    final ParsedTupleQuery ptq;
    final TupleExpr te;
    try {
        ptq = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL, query, null);
    } catch (UnsupportedQueryLanguageException | MalformedQueryException e) {
        throw new QueryRulesetException("Error parsing query:\n" + query, e);
    }
    te = ptq.getTupleExpr();
    // Before converting to rules (and renaming variables), validate that no statement patterns
    // consist of only variables (this would result in a rule  that matches every triple).
    // Needs to be done before inference, since inference rules may create such statement patterns
    // that are OK because they won'd be converted to rules directly.
    te.visit(new QueryModelVisitorBase<QueryRulesetException>() {

        @Override
        public void meet(final StatementPattern node) throws QueryRulesetException {
            if (!(node.getSubjectVar().hasValue() || node.getPredicateVar().hasValue() || node.getObjectVar().hasValue())) {
                throw new QueryRulesetException("Statement pattern with no constants would match every statement:\n" + node + "\nFrom parsed query:\n" + te);
            }
        }
    });
    // Apply inference, if applicable
    if (conf != null && conf.isInfer()) {
        RdfCloudTripleStore store = null;
        try {
            log.info("Applying inference rules");
            store = (RdfCloudTripleStore) RyaSailFactory.getInstance(conf);
            final InferenceEngine inferenceEngine = store.getInferenceEngine();
            // Apply in same order as query evaluation:
            te.visit(new TransitivePropertyVisitor(conf, inferenceEngine));
            te.visit(new SymmetricPropertyVisitor(conf, inferenceEngine));
            te.visit(new InverseOfVisitor(conf, inferenceEngine));
            te.visit(new SubPropertyOfVisitor(conf, inferenceEngine));
            te.visit(new SubClassOfVisitor(conf, inferenceEngine));
            te.visit(new SameAsVisitor(conf, inferenceEngine));
            log.info("Query after inference:\n");
            for (final String line : te.toString().split("\n")) {
                log.info("\t" + line);
            }
        } catch (final Exception e) {
            throw new QueryRulesetException("Error applying inference to parsed query:\n" + te, e);
        } finally {
            if (store != null) {
                try {
                    store.shutDown();
                } catch (final SailException e) {
                    log.error("Error shutting down Sail after applying inference", e);
                }
            }
        }
    }
    // Extract the StatementPatterns and Filters and turn them into rules:
    final RulesetVisitor rv = new RulesetVisitor();
    try {
        te.visit(rv);
        rv.addSchema();
    } catch (final QueryRulesetException e) {
        throw new QueryRulesetException("Error extracting rules from parsed query:\n" + te, e);
    }
    for (final CopyRule candidateRule : rv.rules) {
        boolean unique = true;
        for (final CopyRule otherRule : rv.rules) {
            if (!candidateRule.equals(otherRule) && otherRule.isGeneralizationOf(candidateRule)) {
                unique = false;
                break;
            }
        }
        if (unique) {
            rules.add(candidateRule);
        }
    }
}
Also used : SameAsVisitor(org.apache.rya.rdftriplestore.inference.SameAsVisitor) RdfCloudTripleStore(org.apache.rya.rdftriplestore.RdfCloudTripleStore) SymmetricPropertyVisitor(org.apache.rya.rdftriplestore.inference.SymmetricPropertyVisitor) SubClassOfVisitor(org.apache.rya.rdftriplestore.inference.SubClassOfVisitor) SubPropertyOfVisitor(org.apache.rya.rdftriplestore.inference.SubPropertyOfVisitor) SailException(org.openrdf.sail.SailException) TupleExpr(org.openrdf.query.algebra.TupleExpr) SailException(org.openrdf.sail.SailException) UnsupportedQueryLanguageException(org.openrdf.query.UnsupportedQueryLanguageException) QueryRulesetException(org.apache.rya.accumulo.mr.merge.util.QueryRuleset.QueryRulesetException) MalformedQueryException(org.openrdf.query.MalformedQueryException) IOException(java.io.IOException) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) StatementPattern(org.openrdf.query.algebra.StatementPattern) InferenceEngine(org.apache.rya.rdftriplestore.inference.InferenceEngine) MalformedQueryException(org.openrdf.query.MalformedQueryException) TransitivePropertyVisitor(org.apache.rya.rdftriplestore.inference.TransitivePropertyVisitor) InverseOfVisitor(org.apache.rya.rdftriplestore.inference.InverseOfVisitor) QueryRulesetException(org.apache.rya.accumulo.mr.merge.util.QueryRuleset.QueryRulesetException) UnsupportedQueryLanguageException(org.openrdf.query.UnsupportedQueryLanguageException) ParsedTupleQuery(org.openrdf.query.parser.ParsedTupleQuery)

Aggregations

IOException (java.io.IOException)1 QueryRulesetException (org.apache.rya.accumulo.mr.merge.util.QueryRuleset.QueryRulesetException)1 RdfCloudTripleStore (org.apache.rya.rdftriplestore.RdfCloudTripleStore)1 InferenceEngine (org.apache.rya.rdftriplestore.inference.InferenceEngine)1 InverseOfVisitor (org.apache.rya.rdftriplestore.inference.InverseOfVisitor)1 SameAsVisitor (org.apache.rya.rdftriplestore.inference.SameAsVisitor)1 SubClassOfVisitor (org.apache.rya.rdftriplestore.inference.SubClassOfVisitor)1 SubPropertyOfVisitor (org.apache.rya.rdftriplestore.inference.SubPropertyOfVisitor)1 SymmetricPropertyVisitor (org.apache.rya.rdftriplestore.inference.SymmetricPropertyVisitor)1 TransitivePropertyVisitor (org.apache.rya.rdftriplestore.inference.TransitivePropertyVisitor)1 FixedStatementPattern (org.apache.rya.rdftriplestore.utils.FixedStatementPattern)1 MalformedQueryException (org.openrdf.query.MalformedQueryException)1 UnsupportedQueryLanguageException (org.openrdf.query.UnsupportedQueryLanguageException)1 StatementPattern (org.openrdf.query.algebra.StatementPattern)1 TupleExpr (org.openrdf.query.algebra.TupleExpr)1 ParsedTupleQuery (org.openrdf.query.parser.ParsedTupleQuery)1 SailException (org.openrdf.sail.SailException)1