Search in sources :

Example 1 with TupleQueryResultHandlerBase

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

the class SpinConstructRule method loadSpinRules.

/**
 * Load a set of SPIN rules from a data store.
 * @param conf Contains the connection information. Not null.
 * @return A map of rule identifiers to rule objects.
 * @throws ForwardChainException if connecting, querying for rules, or
 *  parsing rules fails.
 */
public static Ruleset loadSpinRules(RdfCloudTripleStoreConfiguration conf) throws ForwardChainException {
    Preconditions.checkNotNull(conf);
    Map<Resource, Rule> rules = new ConcurrentHashMap<>();
    // Connect to Rya
    SailRepository repository = null;
    SailRepositoryConnection conn = null;
    try {
        repository = new SailRepository(RyaSailFactory.getInstance(conf));
    } catch (Exception e) {
        throw new ForwardChainException("Couldn't initialize SAIL from configuration", e);
    }
    // Load and parse the individual SPIN rules from the data store
    String ruleQueryString = "SELECT ?type ?rule ?text WHERE {\n" + "  ?type <" + SPIN.RULE_PROPERTY.stringValue() + "> ?rule .\n" + "  {\n" + "    ?rule a <" + SP.CONSTRUCT_CLASS.stringValue() + "> .\n" + "    ?rule <" + SP.TEXT_PROPERTY.stringValue() + "> ?text .\n" + "  } UNION {\n" + "    ?rule a ?template .\n" + "    ?template <" + SPIN.BODY_PROPERTY + ">? ?body .\n" + "    ?body a <" + SP.CONSTRUCT_CLASS.stringValue() + "> .\n" + "    ?body <" + SP.TEXT_PROPERTY.stringValue() + "> ?text .\n" + "  }\n" + "}";
    SPARQLParser parser = new SPARQLParser();
    try {
        conn = repository.getConnection();
        TupleQuery ruleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, ruleQueryString);
        ruleQuery.evaluate(new TupleQueryResultHandlerBase() {

            @Override
            public void handleSolution(BindingSet bs) throws TupleQueryResultHandlerException {
                // For each rule identifier found, instantiate a SpinRule
                Value requiredType = bs.getValue("type");
                Value ruleIdentifier = bs.getValue("rule");
                Value ruleText = bs.getValue("text");
                if (requiredType instanceof Resource && ruleIdentifier instanceof Resource && ruleText instanceof Literal) {
                    ParsedQuery parsedRule;
                    try {
                        parsedRule = parser.parseQuery(ruleText.stringValue(), null);
                        if (parsedRule instanceof ParsedGraphQuery) {
                            SpinConstructRule rule = new SpinConstructRule((Resource) requiredType, (Resource) ruleIdentifier, (ParsedGraphQuery) parsedRule);
                            if (rule.hasAnonymousConsequent()) {
                                logger.error("Skipping unsupported rule " + ruleIdentifier + " -- consequent refers to bnode, which is not" + " currently supported (creating new bnodes at each" + " application could lead to infinite recursion).");
                            } else {
                                rules.put((Resource) ruleIdentifier, rule);
                            }
                        }
                    } catch (Exception e) {
                        throw new TupleQueryResultHandlerException(e);
                    }
                }
            }
        });
    } catch (TupleQueryResultHandlerException | QueryEvaluationException | MalformedQueryException | RepositoryException e) {
        throw new ForwardChainException("Couldn't retrieve SPIN rules", e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (RepositoryException e) {
                logger.warn("Error closing repository connection", e);
            }
        }
        if (repository.isInitialized()) {
            try {
                repository.shutDown();
            } catch (RepositoryException e) {
                logger.warn("Error shutting down repository", e);
            }
        }
    }
    return new Ruleset(rules.values());
}
Also used : TupleQueryResultHandlerBase(org.openrdf.query.TupleQueryResultHandlerBase) SailRepository(org.openrdf.repository.sail.SailRepository) ParsedQuery(org.openrdf.query.parser.ParsedQuery) TupleQuery(org.openrdf.query.TupleQuery) Literal(org.openrdf.model.Literal) MalformedQueryException(org.openrdf.query.MalformedQueryException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) TupleQueryResultHandlerException(org.openrdf.query.TupleQueryResultHandlerException) ParsedGraphQuery(org.openrdf.query.parser.ParsedGraphQuery) Resource(org.openrdf.model.Resource) RepositoryException(org.openrdf.repository.RepositoryException) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) RepositoryException(org.openrdf.repository.RepositoryException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) MalformedQueryException(org.openrdf.query.MalformedQueryException) TupleQueryResultHandlerException(org.openrdf.query.TupleQueryResultHandlerException) ForwardChainException(org.apache.rya.forwardchain.ForwardChainException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Value(org.openrdf.model.Value) ForwardChainException(org.apache.rya.forwardchain.ForwardChainException)

Example 2 with TupleQueryResultHandlerBase

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

the class CbSailProducer method performSelect.

protected Object performSelect(final String query, final String auth, final Boolean infer) throws RepositoryException, MalformedQueryException, QueryEvaluationException, TupleQueryResultHandlerException {
    final TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, query);
    if (auth != null && auth.length() > 0) {
        tupleQuery.setBinding(CONF_QUERY_AUTH, valueFactory.createLiteral(auth));
    }
    if (infer != null) {
        tupleQuery.setBinding(CONF_INFER, valueFactory.createLiteral(infer));
    }
    if (CbSailEndpoint.CbSailOutput.BINARY.equals(queryOutput)) {
        final List listOutput = new ArrayList();
        final TupleQueryResultHandlerBase handler = new TupleQueryResultHandlerBase() {

            @Override
            public void handleSolution(final BindingSet bindingSet) throws TupleQueryResultHandlerException {
                final Map<String, String> map = new HashMap<String, String>();
                for (final String s : bindingSet.getBindingNames()) {
                    map.put(s, bindingSet.getBinding(s).getValue().stringValue());
                }
                listOutput.add(map);
            }
        };
        tupleQuery.evaluate(handler);
        return listOutput;
    } else if (CbSailEndpoint.CbSailOutput.XML.equals(queryOutput)) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final SPARQLResultsXMLWriter sparqlWriter = new SPARQLResultsXMLWriter(baos);
        tupleQuery.evaluate(sparqlWriter);
        return new String(baos.toByteArray(), StandardCharsets.UTF_8);
    } else {
        throw new IllegalArgumentException("Query Output[" + queryOutput + "] is not recognized");
    }
}
Also used : BindingSet(org.openrdf.query.BindingSet) SPARQLResultsXMLWriter(org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter) TupleQueryResultHandlerBase(org.openrdf.query.TupleQueryResultHandlerBase) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TupleQuery(org.openrdf.query.TupleQuery) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with TupleQueryResultHandlerBase

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

the class MongoBatchUpdatePCJ method updatePCJResults.

private void updatePCJResults(final String ryaInstanceName, final String pcjId, final MongoClient client) throws InstanceDoesNotExistException, PCJDoesNotExistException, RyaClientException {
    // Things that have to be closed before we exit.
    Sail sail = null;
    SailConnection sailConn = null;
    try (final PrecomputedJoinStorage pcjStorage = new MongoPcjStorage(client, ryaInstanceName)) {
        // Create an instance of Sail backed by the Rya instance.
        sail = connectToRya(ryaInstanceName);
        final SailRepository sailRepo = new SailRepository(sail);
        final SailRepositoryConnection sailRepoConn = sailRepo.getConnection();
        // Purge the old results from the PCJ.
        try {
            pcjStorage.purge(pcjId);
        } catch (final PCJStorageException e) {
            throw new RyaClientException("Could not batch update PCJ with ID '" + pcjId + "' because the old " + "results could not be purged from it.", e);
        }
        // Parse the PCJ's SPARQL query.
        final PcjMetadata metadata = pcjStorage.getPcjMetadata(pcjId);
        final String sparql = metadata.getSparql();
        sailConn = sail.getConnection();
        final TupleQuery tupleQuery = sailRepoConn.prepareTupleQuery(QueryLanguage.SPARQL, sparql);
        // Execute the query.
        final List<VisibilityBindingSet> batch = new ArrayList<>(1000);
        tupleQuery.evaluate(new TupleQueryResultHandlerBase() {

            @Override
            public void handleSolution(final BindingSet bindingSet) throws TupleQueryResultHandlerException {
                final VisibilityBindingSet result = new VisibilityBindingSet(bindingSet, "");
                log.warn("Visibility information on the binding set is lost during a batch update." + "  This can create data leaks.");
                batch.add(result);
                if (batch.size() == 1000) {
                    try {
                        pcjStorage.addResults(pcjId, batch);
                    } catch (final PCJStorageException e) {
                        throw new TupleQueryResultHandlerException("Fail to batch load new results into the PCJ with ID '" + pcjId + "'.", e);
                    }
                    batch.clear();
                }
            }
        });
        if (!batch.isEmpty()) {
            pcjStorage.addResults(pcjId, batch);
            batch.clear();
        }
    } catch (final MalformedQueryException | PCJStorageException | SailException | QueryEvaluationException | RepositoryException | TupleQueryResultHandlerException e) {
        throw new RyaClientException("Fail to batch load new results into the PCJ with ID '" + pcjId + "'.", e);
    } finally {
        if (sailConn != null) {
            try {
                sailConn.close();
            } catch (final SailException e) {
                log.warn(e.getMessage(), e);
            }
        }
        if (sail != null) {
            try {
                sail.shutDown();
            } catch (final SailException e) {
                log.warn(e.getMessage(), e);
            }
        }
    }
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) RyaClientException(org.apache.rya.api.client.RyaClientException) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) TupleQueryResultHandlerBase(org.openrdf.query.TupleQueryResultHandlerBase) TupleQueryResultHandlerException(org.openrdf.query.TupleQueryResultHandlerException) SailRepository(org.openrdf.repository.sail.SailRepository) ArrayList(java.util.ArrayList) TupleQuery(org.openrdf.query.TupleQuery) RepositoryException(org.openrdf.repository.RepositoryException) RyaDetailsRepositoryException(org.apache.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException) SailException(org.openrdf.sail.SailException) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) MongoPcjStorage(org.apache.rya.indexing.pcj.storage.mongo.MongoPcjStorage) SailConnection(org.openrdf.sail.SailConnection) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Sail(org.openrdf.sail.Sail) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) MalformedQueryException(org.openrdf.query.MalformedQueryException) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException)

Aggregations

BindingSet (org.openrdf.query.BindingSet)3 TupleQuery (org.openrdf.query.TupleQuery)3 TupleQueryResultHandlerBase (org.openrdf.query.TupleQueryResultHandlerBase)3 ArrayList (java.util.ArrayList)2 MalformedQueryException (org.openrdf.query.MalformedQueryException)2 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)2 TupleQueryResultHandlerException (org.openrdf.query.TupleQueryResultHandlerException)2 RepositoryException (org.openrdf.repository.RepositoryException)2 SailRepository (org.openrdf.repository.sail.SailRepository)2 SailRepositoryConnection (org.openrdf.repository.sail.SailRepositoryConnection)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 RyaClientException (org.apache.rya.api.client.RyaClientException)1 RyaDetailsRepositoryException (org.apache.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException)1 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)1 ForwardChainException (org.apache.rya.forwardchain.ForwardChainException)1 PcjMetadata (org.apache.rya.indexing.pcj.storage.PcjMetadata)1 PrecomputedJoinStorage (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage)1