Search in sources :

Example 11 with SPARQLRepository

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

the class TestFixtures method isReachable.

public static boolean isReachable(String aUrl) {
    if (UNREACHABLE_ENDPOINTS.contains(aUrl)) {
        return false;
    }
    try {
        URL url = new URL(aUrl + "?query=" + new URLCodec().encode("SELECT ?v WHERE { BIND (true AS ?v)}"));
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("HEAD");
        con.setConnectTimeout(7_500);
        con.setReadTimeout(7_500);
        con.setRequestProperty("Content-Type", "application/sparql-query");
        int status = con.getResponseCode();
        if (status == HTTP_MOVED_TEMP || status == HTTP_MOVED_PERM) {
            String location = con.getHeaderField("Location");
            return isReachable(location);
        }
    } catch (Exception e) {
        System.out.printf("[%s] Network-level check: %s%n", aUrl, e.getMessage());
        UNREACHABLE_ENDPOINTS.add(aUrl);
        return false;
    }
    SPARQLRepository r = new SPARQLRepository(aUrl);
    r.setHttpClient(newPerThreadSslCheckingHttpClientBuilder().build());
    r.init();
    try (RepositoryConnection conn = r.getConnection()) {
        TupleQuery query = conn.prepareTupleQuery("SELECT ?v WHERE { BIND (true AS ?v)}");
        query.setMaxExecutionTime(5);
        try (TupleQueryResult result = query.evaluate()) {
            return true;
        }
    } catch (Exception e) {
        System.out.printf("[%s] Repository-level check: %s%n", aUrl, e.getMessage());
        // e.printStackTrace();
        UNREACHABLE_ENDPOINTS.add(aUrl);
        return false;
    }
}
Also used : URLCodec(org.apache.commons.codec.net.URLCodec) RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) HttpURLConnection(java.net.HttpURLConnection) SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult) URL(java.net.URL) CompressorException(org.apache.commons.compress.compressors.CompressorException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 12 with SPARQLRepository

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

the class Remote_PathQL_GetFactTests method setUpBeforeClass.

/**
 * Sets the up before class.
 *
 * @throws Exception the exception
 */
@BeforeAll
static void setUpBeforeClass() throws Exception {
    // org.eclipse.rdf4j.repository.Repository workingRep = new SPARQLRepository("http://localhost:8080/rdf4j-server/repositories/calc2graph");
    SPARQLRepository workingRep = new SPARQLRepository("http://localhost:8080/rdf4j-server/repositories/calc2graph");
    // org.eclipse.rdf4j.repository.Repository workingRep = new HTTPRepository("http://localhost:8080/rdf4j-server","calc2graph");
    // HTTPRepository workingRep = new HTTPRepository("http://localhost:8080/rdf4j-server","calc2graph");
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Accept", "text/plain");
    workingRep.setAdditionalHttpHeaders(headers);
    source = IntelligentGraphRepository.create(workingRep);
    source.prefix("<http://inova8.com/calc2graph/def/>");
    source.prefix("rdfs", "<http://www.w3.org/2000/01/rdf-schema#>");
}
Also used : SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository) HashMap(java.util.HashMap) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 13 with SPARQLRepository

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

the class TrianglesFunction method evaluate.

/**
 * Evaluate.
 *
 * @param valueFactory the value factory
 * @param args the args
 * @return the value
 * @throws ValueExprEvaluationException the value expr evaluation exception
 */
@Override
public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
    org.eclipse.rdf4j.repository.Repository rep = new SPARQLRepository(args[0].stringValue());
    rep.init();
    try (RepositoryConnection workingConn = workingRep.getConnection();
        RepositoryConnection conn = rep.getConnection()) {
        // SPARQLRepository updaterepository = new SPARQLRepository(args[0].stringValue()+"/statements");
        // Update update =updaterepository.getConnection().prepareUpdate(QueryLanguage.SPARQL,"insert data{graph <http://test> {<http://exampleSub> <http://examplePred> <http://exampleObj> }}");
        // update.execute();
        String graphQueryString = "CONSTRUCT{?vertex <label:connect> ?neighbor.}\r\n" + "WHERE{\r\n" + "SELECT ?vertex ?neighbor\r\n" + "WHERE{{?vertex ?p ?neighbor.}\r\n" + "    UNION{?neighbor ?p ?vertex.}FILTER(STR(?vertex) <STR(?neighbor))}\r\n" + "}";
        GraphQueryResult graphResult = conn.prepareGraphQuery(graphQueryString).evaluate();
        Resource graph = iri("ng:temp");
        while (graphResult.hasNext()) {
            Statement st = graphResult.next();
            // ... do something with the resulting statement here.
            workingConn.add(st, graph);
        }
        String queryString = "SELECT(COUNT(DISTINCT*) AS ?triangles)\r\n" + "WHERE{\r\n" + "  {\r\n" + "GRAPH<ng:temp>   {\r\n" + "      ?x?p?y.\r\n" + "      ?y?p?z.\r\n" + "      ?x?p?z.\r\n" + "    }\r\n" + "  }\r\n" + "}";
        TupleQuery query = workingConn.prepareTupleQuery(queryString);
        try (TupleQueryResult result = query.evaluate()) {
            // we just iterate over all solutions in the result...
            while (result.hasNext()) {
                BindingSet solution = result.next();
                return valueFactory.createLiteral(((SimpleLiteral) solution.getValue("triangles")).intValue());
            }
        }
    }
    return null;
}
Also used : Repository(org.eclipse.rdf4j.repository.Repository) RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) BindingSet(org.eclipse.rdf4j.query.BindingSet) SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult) GraphQueryResult(org.eclipse.rdf4j.query.GraphQueryResult)

Example 14 with SPARQLRepository

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

the class SparqlUtil method processQuery_sesame.

/**
 * Send the given sparql update to the sparql update service using the
 * sesame libraries
 *
 * @param query
 *            sparql update to be processeda
 * @param serviceUrl
 *            sparql update endpoint for processing the sparql update
 * @param user
 *            username for authentication if applicable
 * @param pwd
 *            password for authentication if applicable
 */
public static void processQuery_sesame(String query, String serviceUrl, String user, String pwd) {
    SPARQLRepository repo = new SPARQLRepository(serviceUrl);
    repo.setUsernameAndPassword(user, pwd);
    repo.initialize();
    RepositoryConnection rc = repo.getConnection();
    processQuery_sesame(query, rc);
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository)

Example 15 with SPARQLRepository

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

the class SparqlTripleDataBase method getInitializedRepository.

@Override
public Repository getInitializedRepository() {
    Repository repository = new SPARQLRepository(sparqlEndpoint.toString(), sparqlUpdateEndpoint.toString());
    repository.initialize();
    return repository;
}
Also used : Repository(org.eclipse.rdf4j.repository.Repository) SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository) SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository)

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