Search in sources :

Example 1 with QueryRulesetException

use of org.apache.rya.accumulo.mr.merge.util.QueryRuleset.QueryRulesetException 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)

Example 2 with QueryRulesetException

use of org.apache.rya.accumulo.mr.merge.util.QueryRuleset.QueryRulesetException in project incubator-rya by apache.

the class QueryRuleset method setQuery.

/**
 * Set this ruleset's defining query based on the configuration. Query can be
 * specified directly or using a file; if it's read from a file, the query
 * text will also be added to the configuration.
 * @return SPARQL query
 * @throws QueryRulesetException if there is no configuration, or if the query can't be found or read
 */
private void setQuery() throws QueryRulesetException {
    if (conf == null) {
        throw new QueryRulesetException("No Configuration given");
    }
    query = conf.get(CopyTool.QUERY_STRING_PROP);
    final String queryFile = conf.get(CopyTool.QUERY_FILE_PROP);
    if (query == null && queryFile != null) {
        try {
            query = FileUtils.readFileToString(new File(queryFile), StandardCharsets.UTF_8);
            conf.set(CopyTool.QUERY_STRING_PROP, query);
        } catch (final IOException e) {
            throw new QueryRulesetException("Error loading query from file: " + queryFile, e);
        }
    } else if (query == null) {
        throw new QueryRulesetException("No query string or query file provided");
    }
}
Also used : QueryRulesetException(org.apache.rya.accumulo.mr.merge.util.QueryRuleset.QueryRulesetException) IOException(java.io.IOException) File(java.io.File)

Example 3 with QueryRulesetException

use of org.apache.rya.accumulo.mr.merge.util.QueryRuleset.QueryRulesetException in project incubator-rya by apache.

the class BaseRuleMapper method setup.

@Override
protected void setup(final Context context) throws IOException, InterruptedException {
    final Configuration conf = context.getConfiguration();
    split = (RangeInputSplit) context.getInputSplit();
    final Range range = split.getRange();
    // Determine the table and table layout we're scanning
    parentTableName = split.getTableName();
    parentTablePrefix = conf.get(MRUtils.TABLE_PREFIX_PROPERTY);
    for (final TABLE_LAYOUT layout : TABLE_LAYOUT.values()) {
        final String tableName = RdfCloudTripleStoreUtils.layoutPrefixToTable(layout, parentTablePrefix);
        if (tableName.equals(parentTableName)) {
            parentLayout = layout;
        }
    }
    conf.set(MergeTool.TABLE_NAME_PROP, parentTableName);
    // Set up connections and parent/child table information, if necessary
    super.setup(context);
    // If we're working at the statement level, get the relevant rules and conditions:
    if (parentLayout != null) {
        AccumuloQueryRuleset ruleset;
        try {
            ruleset = new AccumuloQueryRuleset(new AccumuloRdfConfiguration(conf));
        } catch (final QueryRulesetException e) {
            throw new IOException("Error parsing the input query", e);
        }
        final List<CopyRule> rules = ruleset.getRules(parentLayout, range);
        for (final CopyRule rule : rules) {
            log.info("Mapper applies to rule:");
            for (final String line : rule.toString().split("\n")) {
                log.info("\t" + line);
            }
        }
        // this input split will receive, so if any condition is true we'll want to copy the statement.
        for (final CopyRule rule : rules) {
            // (even if there are redundant rules with conditions)
            if (rule.getCondition() == null) {
                condition = null;
                break;
            } else // If there is a set of conditions, matching it means we should accept the statement.
            if (condition == null) {
                condition = rule.getCondition();
            } else // If there are more than one rules that match, satisfying any conditions means we should accept.
            {
                condition = new Or(condition, rule.getCondition());
            }
        }
        // Set up the strategy to evaluate those conditions
        strategy = new ParallelEvaluationStrategyImpl(null, null, null, childAccumuloRdfConfiguration);
        // Log info about the split and combined condition
        log.info("Table: " + parentTableName);
        log.info("Range:");
        log.info("\tfrom " + keyToString(range.getStartKey(), Integer.MAX_VALUE));
        log.info("\tto " + keyToString(range.getEndKey(), Integer.MAX_VALUE));
        if (condition == null) {
            log.info("Condition: none");
        } else {
            log.info("Condition:");
            for (final String line : condition.toString().split("\n")) {
                log.info("\t" + line);
            }
        }
    } else {
        log.info("(Copying all rows from " + parentTableName + " directly.)");
    }
}
Also used : CopyRule(org.apache.rya.accumulo.mr.merge.util.CopyRule) TABLE_LAYOUT(org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT) AccumuloQueryRuleset(org.apache.rya.accumulo.mr.merge.util.AccumuloQueryRuleset) Or(org.openrdf.query.algebra.Or) Configuration(org.apache.hadoop.conf.Configuration) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) ParallelEvaluationStrategyImpl(org.apache.rya.rdftriplestore.evaluation.ParallelEvaluationStrategyImpl) QueryRulesetException(org.apache.rya.accumulo.mr.merge.util.QueryRuleset.QueryRulesetException) IOException(java.io.IOException) Range(org.apache.accumulo.core.data.Range) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration)

Aggregations

IOException (java.io.IOException)3 QueryRulesetException (org.apache.rya.accumulo.mr.merge.util.QueryRuleset.QueryRulesetException)3 File (java.io.File)1 Range (org.apache.accumulo.core.data.Range)1 Configuration (org.apache.hadoop.conf.Configuration)1 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)1 AccumuloQueryRuleset (org.apache.rya.accumulo.mr.merge.util.AccumuloQueryRuleset)1 CopyRule (org.apache.rya.accumulo.mr.merge.util.CopyRule)1 TABLE_LAYOUT (org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT)1 RdfCloudTripleStore (org.apache.rya.rdftriplestore.RdfCloudTripleStore)1 ParallelEvaluationStrategyImpl (org.apache.rya.rdftriplestore.evaluation.ParallelEvaluationStrategyImpl)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