Search in sources :

Example 6 with SPARQLRepository

use of org.eclipse.rdf4j.repository.sparql.SPARQLRepository in project com.inova8.intelligentgraph by peterjohnlawrence.

the class DegreeDistributionTupleFunction method evaluate.

/**
 * Evaluate.
 *
 * @param valueFactory the value factory
 * @param args the args
 * @return the closeable iteration{@code<? extends list<? extends value>, query evaluation exception>}
 * @throws QueryEvaluationException the query evaluation exception
 */
@Override
public CloseableIteration<? extends List<? extends Value>, QueryEvaluationException> evaluate(ValueFactory valueFactory, Value... args) throws QueryEvaluationException {
    org.eclipse.rdf4j.repository.Repository rep = new SPARQLRepository(args[0].stringValue());
    rep.init();
    conn = rep.getConnection();
    String queryString = "SELECT  ?degree (COUNT(?degree) AS ?count)\r\n" + "WHERE{\r\n" + "  {SELECT(COUNT(?n)AS?degree)\r\n" + "    WHERE{\r\n" + "      {?n?out_edge?out}\r\n" + "      UNION\r\n" + "      {?in?in_edge?n}\r\n" + "    }GROUP BY ?n}}\r\n" + "GROUP BY ?degree";
    TupleQuery query = conn.prepareTupleQuery(queryString);
    Collection<BindingSet> results = new ArrayList<BindingSet>();
    try (TupleQueryResult result = query.evaluate()) {
        while (result.hasNext()) {
            results.add(result.next());
        }
    }
    return new ConvertingIteration<BindingSet, List<Value>, QueryEvaluationException>(new CloseableIteratorIteration<>(results.iterator())) {

        @Override
        protected List<Value> convert(BindingSet bindings) throws QueryEvaluationException {
            List<Value> results = new ArrayList<>();
            for (String bindingName : bindings.getBindingNames()) {
                results.add(bindings.getValue(bindingName));
            }
            return results;
        }
    };
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository) ConvertingIteration(org.eclipse.rdf4j.common.iteration.ConvertingIteration) ArrayList(java.util.ArrayList) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) Value(org.eclipse.rdf4j.model.Value) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Example 7 with SPARQLRepository

use of org.eclipse.rdf4j.repository.sparql.SPARQLRepository in project lyo by eclipse.

the class SparqlUtil method getRepoConnection.

/**
 * return the repo connection object in order to be able to use the sesame
 * client libraries
 *
 * @param queryEndpoint
 *            the sparl query endpoint
 * @param user
 *            username for authentication if applicable
 * @param pwd
 *            password for authentication if applicable
 * @return
 */
public static RepositoryConnection getRepoConnection(String queryEndpoint, String user, String pwd) {
    SPARQLRepository repo = new SPARQLRepository(queryEndpoint);
    if (user != null && pwd != null && !user.isEmpty() && !pwd.isEmpty()) {
        repo.setUsernameAndPassword("okacimi", "nohheis4ae");
    }
    repo.initialize();
    try {
        RepositoryConnection conn = repo.getConnection();
        if (conn == null) {
            logger.error("error getting sparql repo connection !");
        }
        return conn;
    } catch (Exception e) {
        logger.error("error getting sparql repo connection !", e);
        return null;
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository) IOException(java.io.IOException)

Example 8 with SPARQLRepository

use of org.eclipse.rdf4j.repository.sparql.SPARQLRepository in project AJAN-service by aantakli.

the class BTUtil method getInitializedRepository.

public static Repository getInitializedRepository(final AgentTaskInformation metadata, final URI url) throws URISyntaxException, QueryEvaluationException {
    Repository repo;
    if (url.equals(new URI(AJANVocabulary.AGENT_KNOWLEDGE.toString()))) {
        repo = metadata.getAgentBeliefs().initialize();
    } else if (url.equals(new URI(AJANVocabulary.EXECUTION_KNOWLEDGE.toString()))) {
        repo = metadata.getExecutionBeliefs().initialize();
    } else if (url.equals(new URI(AJANVocabulary.BEHAVIOR_KNOWLEDGE.toString()))) {
        repo = metadata.getBehaviorTDB().getInitializedRepository();
    } else if (url.equals(new URI(AJANVocabulary.SERVICE_KNOWLEDGE.toString()))) {
        repo = metadata.getServiceTDB().getInitializedRepository();
    } else if (url.equals(new URI(AJANVocabulary.DOMAIN_KNOWLEDGE.toString()))) {
        repo = metadata.getDomainTDB().getInitializedRepository();
    } else {
        // -----------------------------------------------------------------
        // TODO: General Repository desription for SPARQL + Update Endpoints
        // -----------------------------------------------------------------
        repo = new SPARQLRepository(url.toString(), url.toString());
        repo.initialize();
    }
    return repo;
}
Also used : Repository(org.eclipse.rdf4j.repository.Repository) SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository) SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository) URI(java.net.URI)

Example 9 with SPARQLRepository

use of org.eclipse.rdf4j.repository.sparql.SPARQLRepository in project AJAN-service by aantakli.

the class StateLoader method getInitializedRepository.

private static Repository getInitializedRepository(final URI url, AgentTaskInformation taskInfos) throws URISyntaxException, QueryEvaluationException {
    Repository repo;
    if (url.equals(new URI(AJANVocabulary.AGENT_KNOWLEDGE.toString()))) {
        repo = taskInfos.getAgentBeliefs().getInitializedRepository();
    } else {
        repo = new SPARQLRepository(url.toString());
        repo.initialize();
    }
    return repo;
}
Also used : Repository(org.eclipse.rdf4j.repository.Repository) SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository) SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository) URI(java.net.URI)

Example 10 with SPARQLRepository

use of org.eclipse.rdf4j.repository.sparql.SPARQLRepository in project amazon-neptune-tools by awslabs.

the class NeptuneSparqlClient method executeGraphQuery.

public void executeGraphQuery(String sparql, RdfTargetConfig targetConfig) throws IOException {
    SPARQLRepository repository = chooseRepository();
    try (RepositoryConnection connection = repository.getConnection();
        OutputWriter outputWriter = targetConfig.createOutputWriter()) {
        RDFWriter writer = targetConfig.createRDFWriter(outputWriter);
        connection.prepareGraphQuery(sparql).evaluate(new GraphQueryHandler(writer));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter) OutputWriter(com.amazonaws.services.neptune.io.OutputWriter) IOException(java.io.IOException) NeptuneSigV4SignerException(com.amazonaws.neptune.auth.NeptuneSigV4SignerException)

Aggregations

SPARQLRepository (org.eclipse.rdf4j.repository.sparql.SPARQLRepository)17 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)8 Repository (org.eclipse.rdf4j.repository.Repository)6 IOException (java.io.IOException)4 TupleQuery (org.eclipse.rdf4j.query.TupleQuery)4 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)4 URI (java.net.URI)3 BindingSet (org.eclipse.rdf4j.query.BindingSet)3 NeptuneSigV4SignerException (com.amazonaws.neptune.auth.NeptuneSigV4SignerException)2 OutputWriter (com.amazonaws.services.neptune.io.OutputWriter)2 HashMap (java.util.HashMap)2 Resource (org.eclipse.rdf4j.model.Resource)2 Statement (org.eclipse.rdf4j.model.Statement)2 Value (org.eclipse.rdf4j.model.Value)2 SailRepository (org.eclipse.rdf4j.repository.sail.SailRepository)2 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)2 File (java.io.File)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1