org.apache.jena.sparql.engine.http.QueryEngineHTTP Java Examples

The following examples show how to use org.apache.jena.sparql.engine.http.QueryEngineHTTP. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ARQFactory.java    From shacl with Apache License 2.0 6 votes vote down vote up
public QueryEngineHTTP createRemoteQueryExecution(
		String service,
		Query query, 
		List<String> defaultGraphURIs, 
		List<String> namedGraphURIs, 
		String user, 
		String password) {
    HttpClient httpClient = buildHttpClient(service, user, password);
	QueryEngineHTTP qexec = (QueryEngineHTTP) QueryExecutionFactoryFilter.get().sparqlService(service, query, httpClient);
	if( defaultGraphURIs.size() > 0 ) {
		qexec.setDefaultGraphURIs(defaultGraphURIs);
	}
	if( namedGraphURIs.size() > 0 ) {
		qexec.setNamedGraphURIs(namedGraphURIs);
	}
	return qexec;
}
 
Example #2
Source File: ExampleDBpedia1.java    From xcurator with Apache License 2.0 6 votes vote down vote up
static public void main(String...argv)
{
    String queryStr = "select distinct ?Concept where {[] a ?Concept} LIMIT 10";
    Query query = QueryFactory.create(queryStr);

    // Remote execution.
    try ( QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query) ) {
        // Set the DBpedia specific timeout.
        ((QueryEngineHTTP)qexec).addParam("timeout", "10000") ;

        // Execute.
        ResultSet rs = qexec.execSelect();
        ResultSetFormatter.out(System.out, rs, query);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: TripleIndexCreatorContext.java    From AGDISTIS with GNU Affero General Public License v3.0 6 votes vote down vote up
public String sparql(String subject) {

		// First query takes the most specific class from a given resource.
		String ontology_service = endpoint;

		String endpointsSparql = "select ?label where {<" + subject
				+ "> <http://www.w3.org/2000/01/rdf-schema#label> ?label FILTER (lang(?label) = 'en')} LIMIT 100";

		Query sparqlQuery = QueryFactory.create(endpointsSparql, Syntax.syntaxARQ);
		QueryEngineHTTP qexec = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(ontology_service, sparqlQuery);
		qexec.setModelContentType(WebContent.contentTypeRDFXML);
		ResultSet results = qexec.execSelect();
		String property = null;
		while (results.hasNext()) {
			QuerySolution qs = results.next();
			property = qs.getLiteral("?label").getLexicalForm();
		}
		return property;

	}
 
Example #4
Source File: SPARQL.java    From chatbot with Apache License 2.0 5 votes vote down vote up
public QueryExecution executeQuery(String queryString) {
    logger.info("SPARQL Query is:\n" + queryString);
    Query query = QueryFactory.create(queryString);
    QueryEngineHTTP queryEngine = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(ENDPOINT, query);
    queryEngine.addParam("timeout", String.valueOf(Constants.API_TIMEOUT));
    return queryEngine;
}
 
Example #5
Source File: SPARQLEndpointExecution.java    From hypergraphql with Apache License 2.0 5 votes vote down vote up
@Override
public SPARQLExecutionResult call() {
    Map<String, Set<String>> resultSet = new HashMap<>();

    markers.forEach(marker -> resultSet.put(marker, new HashSet<>()));

    Model unionModel = ModelFactory.createDefaultModel();

    SPARQLServiceConverter converter = new SPARQLServiceConverter(schema);
    String sparqlQuery = converter.getSelectQuery(query, inputSubset, rootType);
    logger.info(sparqlQuery);

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    Credentials credentials =
            new UsernamePasswordCredentials(this.sparqlEndpointService.getUser(), this.sparqlEndpointService.getPassword());
    credsProvider.setCredentials(AuthScope.ANY, credentials);
    HttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();
    HttpOp.setDefaultHttpClient(httpclient);

    Query jenaQuery = QueryFactory.create(sparqlQuery);

    QueryEngineHTTP qEngine = QueryExecutionFactory.createServiceRequest(this.sparqlEndpointService.getUrl(), jenaQuery);
    qEngine.setClient(httpclient);

    ResultSet results = qEngine.execSelect();

    results.forEachRemaining(solution -> {
        markers.stream().filter(solution::contains).forEach(marker ->
                resultSet.get(marker).add(solution.get(marker).asResource().getURI()));

        unionModel.add(this.sparqlEndpointService.getModelFromResults(query, solution, schema));
    });

    SPARQLExecutionResult sparqlExecutionResult = new SPARQLExecutionResult(resultSet, unionModel);
    logger.info(sparqlExecutionResult);

    return sparqlExecutionResult;
}
 
Example #6
Source File: DatasetGenerator.java    From NLIWOD with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
	// DBpedia as SPARQL endpoint
	long timeToLive = TimeUnit.DAYS.toMillis(30);
	CacheFrontend cacheFrontend = CacheUtilsH2.createCacheFrontend("/tmp/qald/sparql", true, timeToLive);
	  final QueryExecutionFactory qef  = FluentQueryExecutionFactory
			                                 .http("http://dbpedia.org/sparql", Lists.newArrayList("http://dbpedia.org"))
			                                 .config().withPostProcessor(qe -> ((QueryEngineHTTP) ((QueryExecutionHttpWrapper) qe).getDecoratee())
			                                                 .setModelContentType(WebContent.contentTypeRDFXML))
			                                 .withCache(cacheFrontend)
			                                 .end()
			                                 .create();
	List<IQuestion> questions = LoaderController.load(Dataset.QALD9_Train_Multilingual);
	// List<IQuestion> questions =
	// LoaderController.load(Dataset.QALD6_Train_Multilingual);
	questions.stream().filter(q -> q.getAnswerType().equals("resource")).collect(Collectors.toList());
	questions.forEach(q -> updateGoldenAnswers(qef, q));

	IRIShortFormProvider sfp = new SimpleIRIShortFormProvider();
	questions.forEach(q -> q.setGoldenAnswers(q.getGoldenAnswers().stream().map(a -> sfp.getShortForm(IRI.create(a))).collect(Collectors.toSet())));
	// questions = questions.stream()
	// .filter(q -> q.getLanguageToQuestion().get("en").equals("Where did
	// Super Bowl 50 take place?"))
	// .collect(Collectors.toList());

	DatasetGenerator stan = new DatasetGenerator(qef);
	stan.generate(questions);

	stan.writeCSVEvaluation();
}
 
Example #7
Source File: ARQFactory.java    From shacl with Apache License 2.0 5 votes vote down vote up
public QueryEngineHTTP createRemoteQueryExecution(Query query, List<String> graphURIs) {
	String service = graphURIs.get(0);
	String serviceAsURI = service;
	if(service.endsWith("/sparql")) {
		serviceAsURI = service.substring(0, service.lastIndexOf('/'));
	}
	return createRemoteQueryExecution(service, query, Collections.singletonList(serviceAsURI), graphURIs, null, null);
}
 
Example #8
Source File: SPARQLEndpointExecution.java    From hypergraphql with Apache License 2.0 4 votes vote down vote up
@Override
public SPARQLExecutionResult call() {
    Map<String, Set<String>> resultSet = new HashMap<>();

    markers.forEach(marker -> resultSet.put(marker, new HashSet<>()));

    Model unionModel = ModelFactory.createDefaultModel();

    SPARQLServiceConverter converter = new SPARQLServiceConverter(schema);
    String sparqlQuery = converter.getSelectQuery(query, inputSubset, rootType);
    logger.debug(sparqlQuery);

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    Credentials credentials =
            new UsernamePasswordCredentials(this.sparqlEndpointService.getUser(), this.sparqlEndpointService.getPassword());
    credsProvider.setCredentials(AuthScope.ANY, credentials);
    HttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();
    HttpOp.setDefaultHttpClient(httpclient);

    ARQ.init();
    Query jenaQuery = QueryFactory.create(sparqlQuery);

    QueryEngineHTTP qEngine = QueryExecutionFactory.createServiceRequest(this.sparqlEndpointService.getUrl(), jenaQuery);
    qEngine.setClient(httpclient);
    //qEngine.setSelectContentType(ResultsFormat.FMT_RS_XML.getSymbol());

    ResultSet results = qEngine.execSelect();

    results.forEachRemaining(solution -> {
        markers.stream().filter(solution::contains).forEach(marker ->
                resultSet.get(marker).add(solution.get(marker).asResource().getURI()));

        unionModel.add(this.sparqlEndpointService.getModelFromResults(query, solution, schema));
    });

    SPARQLExecutionResult sparqlExecutionResult = new SPARQLExecutionResult(resultSet, unionModel);
    logger.debug("Result: {}", sparqlExecutionResult);

    return sparqlExecutionResult;
}
 
Example #9
Source File: KnowledgeCardCreator.java    From NLIWOD with GNU Affero General Public License v3.0 4 votes vote down vote up
private QueryExecution executeQuery(String queryString) {
	Query query = QueryFactory.create(queryString);
	QueryEngineHTTP queryEngine = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(ENDPOINT, query);
	queryEngine.addParam("timeout", String.valueOf(API_TIMEOUT));
	return queryEngine;
}
 
Example #10
Source File: QUEPY.java    From NLIWOD with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Overriding Original search method to implement Quepy's two step requests for
 * QA
 */
@Override
public void search(IQuestion question, String language) throws Exception {
	String questionString;
	if (!question.getLanguageToQuestion().containsKey(language)) {
		return;
	}
	questionString = question.getLanguageToQuestion().get(language);
	log.debug(this.getClass().getSimpleName() + ": " + questionString);
	this.getParamMap().put(this.getQueryKey(), questionString);
	if (this.setLangPar) {
		this.getParamMap().put(this.getLangKey(), language);
	}
	HttpResponse response = this.getIsPostReq() ? fetchPostResponse(getUrl(), getParamMap())
			: fetchGetResponse(getUrl(), getParamMap());
	// Test if error occured
	if (response.getStatusLine().getStatusCode() >= 400) {
		throw new Exception("QUEPY Server could not answer due to: " + response.getStatusLine());
	}
	// Fetch the SPARQL
	String sparqlStr = null;
	JSONParser parser = new JSONParser();

	JSONObject responsejson = (JSONObject) parser.parse(responseparser.responseToString(response));
	JSONArray queriesArr = (JSONArray) responsejson.get("queries");
	for (int i = 0; i < queriesArr.size(); i++) {
		JSONObject queryObj = (JSONObject) queriesArr.get(i);
		if (queryObj.get("language").toString().equalsIgnoreCase("sparql") && queryObj.get("query") != null) {
			sparqlStr = queryObj.get("query").toString();
			break;
		}
	}
	if (sparqlStr != null) {
		HashSet<String> result = new HashSet<String>();
		question.setSparqlQuery(sparqlStr);
		// Fetch results using sparql
		Query query = QueryFactory.create(sparqlStr);
		// Remote execution.
		QueryExecution qexec = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT, query);
		// Set the DBpedia specific timeout.
		((QueryEngineHTTP) qexec).addParam("timeout", "10000");
		// Execute.
		ResultSet rs = qexec.execSelect();
		// Get the values and push them to the question
		while (rs.hasNext()) {
			QuerySolution qs = rs.next();
			Iterator<String> varIt = qs.varNames();
			while (varIt.hasNext()) {
				RDFNode node = qs.get(varIt.next());
				result.add(node.asLiteral().getString());
			}
		}
		question.setGoldenAnswers(result);
	}
}
 
Example #11
Source File: ARQFactory.java    From shacl with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a remote QueryExecution on a given Query.
 * @param query  the Query to execute
 * @return a remote QueryExecution
 */
public QueryEngineHTTP createRemoteQueryExecution(Query query) {
	List<String> graphURIs = query.getGraphURIs();
	return createRemoteQueryExecution(query, graphURIs);
}