Search in sources :

Example 1 with AbstractTupleQueryResultHandler

use of org.eclipse.rdf4j.query.AbstractTupleQueryResultHandler 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 AbstractTupleQueryResultHandler() {

            @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 : SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) AbstractTupleQueryResultHandler(org.eclipse.rdf4j.query.AbstractTupleQueryResultHandler) Literal(org.eclipse.rdf4j.model.Literal) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BindingSet(org.eclipse.rdf4j.query.BindingSet) SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) Resource(org.eclipse.rdf4j.model.Resource) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) ForwardChainException(org.apache.rya.forwardchain.ForwardChainException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Value(org.eclipse.rdf4j.model.Value) ForwardChainException(org.apache.rya.forwardchain.ForwardChainException)

Example 2 with AbstractTupleQueryResultHandler

use of org.eclipse.rdf4j.query.AbstractTupleQueryResultHandler in project incubator-rya by apache.

the class MongoSpinIT method executeQuery.

private Set<BindingSet> executeQuery(final URL queryFile) throws Exception {
    final SailRepositoryConnection conn = repository.getConnection();
    try (final InputStream queryIS = queryFile.openStream();
        final BufferedReader br = new BufferedReader(new InputStreamReader(queryIS, StandardCharsets.UTF_8))) {
        final String query = br.lines().collect(Collectors.joining("\n"));
        final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
        final Set<BindingSet> solutions = new HashSet<>();
        tupleQuery.evaluate(new AbstractTupleQueryResultHandler() {

            @Override
            public void handleSolution(final BindingSet bindingSet) throws TupleQueryResultHandlerException {
                solutions.add(bindingSet);
            }
        });
        return solutions;
    } finally {
        closeQuietly(conn);
    }
}
Also used : ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) AbstractTupleQueryResultHandler(org.eclipse.rdf4j.query.AbstractTupleQueryResultHandler) InputStreamReader(java.io.InputStreamReader) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) HashSet(java.util.HashSet)

Example 3 with AbstractTupleQueryResultHandler

use of org.eclipse.rdf4j.query.AbstractTupleQueryResultHandler 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, VALUE_FACTORY.createLiteral(auth));
    }
    if (infer != null) {
        tupleQuery.setBinding(CONF_INFER, VALUE_FACTORY.createLiteral(infer));
    }
    if (CbSailEndpoint.CbSailOutput.BINARY.equals(queryOutput)) {
        final List listOutput = new ArrayList();
        final AbstractTupleQueryResultHandler handler = new AbstractTupleQueryResultHandler() {

            @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 : AbstractTupleQueryResultHandler(org.eclipse.rdf4j.query.AbstractTupleQueryResultHandler) BindingSet(org.eclipse.rdf4j.query.BindingSet) SPARQLResultsXMLWriter(org.eclipse.rdf4j.query.resultio.sparqlxml.SPARQLResultsXMLWriter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with AbstractTupleQueryResultHandler

use of org.eclipse.rdf4j.query.AbstractTupleQueryResultHandler 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 AbstractTupleQueryResultHandler() {

            @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.eclipse.rdf4j.query.BindingSet) RyaClientException(org.apache.rya.api.client.RyaClientException) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) ArrayList(java.util.ArrayList) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) RyaDetailsRepositoryException(org.apache.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException) SailException(org.eclipse.rdf4j.sail.SailException) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) MongoPcjStorage(org.apache.rya.indexing.pcj.storage.mongo.MongoPcjStorage) AbstractTupleQueryResultHandler(org.eclipse.rdf4j.query.AbstractTupleQueryResultHandler) SailConnection(org.eclipse.rdf4j.sail.SailConnection) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Sail(org.eclipse.rdf4j.sail.Sail) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException)

Aggregations

AbstractTupleQueryResultHandler (org.eclipse.rdf4j.query.AbstractTupleQueryResultHandler)4 BindingSet (org.eclipse.rdf4j.query.BindingSet)4 TupleQuery (org.eclipse.rdf4j.query.TupleQuery)4 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)3 SailRepositoryConnection (org.eclipse.rdf4j.repository.sail.SailRepositoryConnection)3 ArrayList (java.util.ArrayList)2 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)2 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)2 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)2 SailRepository (org.eclipse.rdf4j.repository.sail.SailRepository)2 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)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