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);
}
}
}
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");
}
}
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.)");
}
}
Aggregations