Java Code Examples for com.hp.hpl.jena.query.QueryExecution#setTimeout()

The following examples show how to use com.hp.hpl.jena.query.QueryExecution#setTimeout() . 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: AliadaRDFStoreDAO.java    From aliada-tool with GNU General Public License v3.0 6 votes vote down vote up
public String crm2AliadaClass(final String crmClass) {
	final Query query = QueryFactory.create(CRM_TO_ALIADA_CLASS_P1 + crmClass + CRM_TO_ALIADA_CLASS_P2);
	ARQ.getContext().setTrue(ARQ.useSAX);
       
	QueryExecution execution = null;
	try {
		execution = QueryExecutionFactory.sparqlService("http://172.25.5.15:8890/sparql", query);
		execution.setTimeout(2000, 5000);
		final ResultSet results = execution.execSelect();
		//Iterating over the SPARQL Query results
		while (results.hasNext()) {
			QuerySolution soln = results.nextSolution();
			//Printing DBpedia entries' abstract.
			System.out.println(soln.get("?abstract"));   
			return soln.get("?abstract").asResource().getURI();
		}
		return "NULL";
       } finally {
       	try {
       		execution.close();
		} catch (Exception exception) {
			// TODO: handle exception
		}
       }

}
 
Example 2
Source File: RDFStoreDAO.java    From aliada-tool with GNU General Public License v3.0 6 votes vote down vote up
/**
 * It executes a SELECT SPARQL query on the SPARQL endpoint, 
 * to get the number of resources specified in the query argument.
 *
 * @param query					the query to execute to get the number of resources.  
 * @param sparqlEndpointURI		the SPARQL endpoint URI.  
 * @param user					the user name for the SPARQl endpoint.
 * @param password				the password for the SPARQl endpoint.
 * @return the number of resources.
 * @since 2.0
 */
public int getNumResources(final String query, final String sparqlEndpointURI, final String user, final String password) {
	int numRes = 0;
	try {
        // Execute the query and obtain results
        final QueryExecution qexec = QueryExecutionFactory.sparqlService(
        		sparqlEndpointURI, 
        		QueryFactory.create(query), 
				auth(sparqlEndpointURI, user, password));
        qexec.setTimeout(2000, 5000);
           final ResultSet results = qexec.execSelect() ;
           while (results.hasNext())
           {
           	final QuerySolution soln = results.nextSolution() ;
           	numRes = soln.getLiteral("count").getInt();
           }
        qexec.close() ;
      } catch (Exception exception) {
		LOGGER.error(MessageCatalog._00035_SPARQL_FAILED, exception, query);
	}
	return numRes;
}
 
Example 3
Source File: RDFStoreDAO.java    From aliada-tool with GNU General Public License v3.0 6 votes vote down vote up
/**
 * It executes a SELECT SPARQL query on the SPARQL endpoint.
 *
 * @param sparqlEndpointURI		the SPARQL endpoint URI.  
 * @param user					the user name for the SPARQl endpoint.
 * @param password				the password for the SPARQl endpoint.
 * @param query					the query to use to look for the resources.
 * @return the {@link com.hp.hpl.jena.query.ResultSet} of the SELECT SPARQL query.
 * @since 2.0
 */
public ResultSet executeSelect(final String sparqlEndpointURI, final String user, final String password, final String query) {
	ResultSet results = null;
 	try {
        // Execute the query and obtain results
        final QueryExecution qexec = QueryExecutionFactory.sparqlService(
        		sparqlEndpointURI, 
        		QueryFactory.create(query), 
				auth(sparqlEndpointURI, user, password));
        qexec.setTimeout(2000, 5000);
           results = qexec.execSelect() ;
        qexec.close() ;
      } catch (Exception exception) {
		LOGGER.error(MessageCatalog._00035_SPARQL_FAILED, exception, query);
	}
 	return results;
}
 
Example 4
Source File: RDFStoreDAO.java    From aliada-tool with GNU General Public License v3.0 5 votes vote down vote up
/**
 * It executes a SELECT SPARQL query on the SPARQL endpoint, 
 * to get the discovered links.
 *
 * @param sparqlEndpointURI		the SPARQL endpoint URI.  
 * @param graphName 			the graphName, null in case of default graph.
 * @param user					the user name for the SPARQl endpoint.
 * @param password				the password for the SPARQl endpoint.
 * @param offset				causes the solutions generated to start after 
 *                              the specified number of solutions.
 * @param limit					upper bound on the number of solutions returned.
 * @return a list of triples with the discovered links.
 * @since 2.0
 */
public Triple[] getDiscoveredLinks(final String sparqlEndpointURI, final String graphName, final String user, final String password, final int offset, final int limit) {
	final String query = "select * FROM <" + graphName + "> " + 
					"where {?source ?rel ?target }" +
					" ORDER BY ?source ?target" +
					" OFFSET " + offset + " LIMIT " + limit;
  	ArrayList<Triple> linksList = new ArrayList<Triple>();
 	try {
        // Execute the query and obtain results
        final QueryExecution qexec = QueryExecutionFactory.sparqlService(
        		sparqlEndpointURI, 
        		QueryFactory.create(query), 
				auth(sparqlEndpointURI, user, password));
        qexec.setTimeout(2000, 5000);
           final ResultSet results = qexec.execSelect() ;
           while (results.hasNext())
           {
           	final QuerySolution soln = results.nextSolution() ;
           	final Resource sourceResType = soln.getResource("source");
           	final Resource targetResType = soln.getResource("target");
           	final Resource relResType = soln.getResource("rel");
       		final Triple triple = new Triple(sourceResType.asNode(), relResType.asNode(), targetResType.asNode());
       		linksList.add(triple);
           }
        qexec.close() ;
      } catch (Exception exception) {
		LOGGER.error(MessageCatalog._00035_SPARQL_FAILED, exception, query);
	}
	if (linksList.isEmpty()) {
		return new Triple[0];
	}
	return (Triple[]) linksList.toArray(new Triple[linksList.size()]);
}
 
Example 5
Source File: RDFStoreDAO.java    From aliada-tool with GNU General Public License v3.0 5 votes vote down vote up
/**
 * It executes a SELECT SPARQL query on the SPARQL endpoint,
 * to get the ambiguous discovered links.
 *
 * @param sparqlEndpointURI		the SPARQL endpoint URI.  
 * @param graphName 			the graphName, null in case of default graph.
 * @param user					the user name for the SPARQl endpoint.
 * @param password				the password for the SPARQl endpoint.
 * @param offset				causes the solutions generated to start after 
 *                              the specified number of solutions.
 * @param limit					upper bound on the number of solutions returned.
 * @return a list of {@link eu.aliada.shared.rdfstore.AmbiguousLink} with the ambiguous discovered links.
 * @since 2.0
 */
public AmbiguousLink[] getAmbiguousDiscoveredLinks(final String sparqlEndpointURI, final String graphName, final String user, final String password, final int offset, final int limit) {
	final String query = "SELECT ?localRes ?extResBegin (COUNT(?localRes) AS ?count) FROM <" + graphName + "> " + 
			" WHERE {?localRes ?rel ?extRes ." +
			" BIND( str(?extRes) as ?extResStr )." +
			" BIND( SUBSTR(?extResStr, 1,14) AS ?extResBegin)" +
			" }" +
			" GROUP BY ?localRes ?extResBegin" +
			" HAVING (COUNT(?localRes) > 1)" +
			" ORDER BY ?localRes" +
			" OFFSET " + offset + " LIMIT " + limit;
	
	ArrayList<AmbiguousLink> ambiguousLinksList = new ArrayList<AmbiguousLink>();
	try {
		// Execute the query and obtain results
		final QueryExecution qexec = QueryExecutionFactory.sparqlService(
				sparqlEndpointURI, 
				QueryFactory.create(query), 
				auth(sparqlEndpointURI, user, password));
		qexec.setTimeout(2000, 5000);
		final ResultSet results = qexec.execSelect() ;
		while (results.hasNext())
		{
			final QuerySolution soln = results.nextSolution() ;
	    	final Resource localRes = soln.getResource("localRes");
	    	final String extResBegin = soln.getLiteral("extResBegin").getString();
	    	final AmbiguousLink ambiguousLink = new AmbiguousLink();
	    	ambiguousLink.setSourceURI(localRes.getURI());
	    	getSourceURIAmbiguousLinks(ambiguousLink, localRes, extResBegin, sparqlEndpointURI, graphName, user, password);
	    	ambiguousLinksList.add(ambiguousLink);
	    }
	    qexec.close() ;
	  } catch (Exception exception) {
		LOGGER.error(MessageCatalog._00035_SPARQL_FAILED, exception, query);
	}
	if (ambiguousLinksList.isEmpty()) {
		return new AmbiguousLink[0];
	}
	return (AmbiguousLink[]) ambiguousLinksList.toArray(new AmbiguousLink[ambiguousLinksList.size()]);
}
 
Example 6
Source File: RDFStoreDAO.java    From aliada-tool with GNU General Public License v3.0 5 votes vote down vote up
/**
 * It executes a SELECT SPARQL query on the SPARQL endpoint, 
 * to get the number of ambiguous discovered links.
 *
 * @param sparqlEndpointURI		the SPARQL endpoint URI.  
 * @param graphName 			the graphName, null in case of default graph.
 * @param user					the user name for the SPARQl endpoint.
 * @param password				the password for the SPARQl endpoint.
 * @return the number of the ambiguous discovered links.
 * @since 2.0
 */
public int getNumAmbiguousDiscoveredLinks (final String sparqlEndpointURI, final String graphName, final String user, final String password) {
	final String query = "SELECT (COUNT(?localRes) AS ?count) FROM <" + graphName + "> " + 
					" WHERE {?localRes ?rel ?extRes ." +
					" BIND( str(?extRes) as ?extResStr )." +
					" BIND( SUBSTR(?extResStr, 1,14) AS ?extResBegin)" +
					" }" +
					" GROUP BY ?localRes ?extResBegin" +
					" HAVING (COUNT(?localRes) > 1)";

	int numLinks = 0;
	try {
        // Execute the query and obtain results
        final QueryExecution qexec = QueryExecutionFactory.sparqlService(
        		sparqlEndpointURI, 
        		QueryFactory.create(query), 
				auth(sparqlEndpointURI, user, password));
        qexec.setTimeout(2000, 5000);
           final ResultSet results = qexec.execSelect() ;
           while (results.hasNext())
           {
           	final QuerySolution soln = results.nextSolution() ;
           	numLinks = numLinks + soln.getLiteral("count").getInt();
           }
        qexec.close() ;
      } catch (Exception exception) {
		LOGGER.error(MessageCatalog._00035_SPARQL_FAILED, exception, query);
	}
	return numLinks;
}
 
Example 7
Source File: RDFStoreDAO.java    From aliada-tool with GNU General Public License v3.0 5 votes vote down vote up
/**
 * It executes a SELECT SPARQL query on the SPARQL endpoint, 
 * to get the ambiguous discovered links of a source URI.
 *
 * @param ambiguousLink			a {@link eu.aliada.shared.rdfstore.AmbiguousLink} that contains the source URI.  
 * @param localRes				the source resource of the link.  
 * @param extResBegin			the beginning string of the target link.  
 * @param sparqlEndpointURI		the SPARQL endpoint URI.  
 * @param graphName 			the graphName, null in case of default graph.
 * @param user					the user name for the SPARQl endpoint.
 * @param password				the password for the SPARQl endpoint.
 * @param offset				causes the solutions generated to start after 
 *                              the specified number of solutions.
 * @param limit					upper bound on the number of solutions returned.
 * @since 2.0
 */
public void getSourceURIAmbiguousLinks(final AmbiguousLink ambiguousLink, final Resource localRes, final String extResBegin, final String sparqlEndpointURI, final String graphName, final String user, final String password) {
	final String query = "SELECT ?rel ?extRes FROM <" + graphName + "> " + 
			" WHERE {<" + ambiguousLink.getSourceURI() + "> ?rel ?extRes ." +
			" FILTER regex(?extRes, \"^" + extResBegin + "\", \"i\")" +
			" }";
	try {
		// Execute the query and obtain results
		final QueryExecution qexec = QueryExecutionFactory.sparqlService(
				sparqlEndpointURI, 
				QueryFactory.create(query), 
				auth(sparqlEndpointURI, user, password));
		qexec.setTimeout(2000, 5000);
		final ResultSet results = qexec.execSelect() ;
		while (results.hasNext())
		{
			final QuerySolution soln = results.nextSolution() ;
	    	final Resource extRes = soln.getResource("extRes");
           	final Resource relResType = soln.getResource("rel");
           	final Triple triple = new Triple(localRes.asNode(), relResType.asNode(), extRes.asNode());
	    	ambiguousLink.addLink(triple);
	    }
	    qexec.close() ;
	  } catch (Exception exception) {
		LOGGER.error(MessageCatalog._00035_SPARQL_FAILED, exception, query);
	}
}
 
Example 8
Source File: RDFStoreDAO.java    From aliada-tool with GNU General Public License v3.0 5 votes vote down vote up
/**
 * It executes a SELECT SPARQL query on the SPARQL endpoint, 
 * to get the resources specified in the query argument.
 *
 * @param query					the query to execute to get the resources.  
 * @param sparqlEndpointURI		the SPARQL endpoint URI.  
 * @param user					the user name for the SPARQl endpoint.
 * @param password				the password for the SPARQl endpoint.
 * @param offset				causes the solutions generated to start after 
 *                              the specified number of solutions.
 * @param limit					upper bound on the number of solutions returned.
 * @return a list of {@link eu.aliada.shared.rdfstore.RetrievedResource} with the resources.
 * @since 2.0
 */
public RetrievedResource[] getResources(final String query, final String sparqlEndpointURI, final String user, final String password, final int offset, final int limit) {
	final ArrayList<RetrievedResource> resList = new ArrayList<RetrievedResource>();
 	try {
        // Execute the query and obtain results
        final QueryExecution qexec = QueryExecutionFactory.sparqlService(
        		sparqlEndpointURI, 
        		QueryFactory.create(query), 
				auth(sparqlEndpointURI, user, password));
        qexec.setTimeout(2000, 5000);
           final ResultSet results = qexec.execSelect() ;
           while (results.hasNext())
           {
           	final QuerySolution soln = results.nextSolution() ;
           	final Resource res = soln.getResource("res");
       		String name = "";
           	if(soln.contains("name")) {
           		name = soln.getLiteral("name").getString();
           	}
       		final RetrievedResource retrievedRes = new RetrievedResource(res.getURI(), name);
       		resList.add(retrievedRes);
           }
        qexec.close() ;
      } catch (Exception exception) {
		LOGGER.error(MessageCatalog._00035_SPARQL_FAILED, exception, query);
	}
	if (resList.isEmpty()) {
		return new RetrievedResource[0];
	}
	return (RetrievedResource[]) resList.toArray(new RetrievedResource[resList.size()]);
}
 
Example 9
Source File: ResourceDescriptionServlet.java    From GeoTriples with Apache License 2.0 4 votes vote down vote up
protected void doGet(HttpServletRequest request,
		HttpServletResponse response) throws IOException, ServletException {
	D2RServer server = D2RServer.fromServletContext(getServletContext());
	server.checkMappingFileChanged();
	String relativeResourceURI = request.getRequestURI().substring(
			request.getContextPath().length()
					+ request.getServletPath().length());
	// Some servlet containers keep the leading slash, some don't
	if (!"".equals(relativeResourceURI)
			&& "/".equals(relativeResourceURI.substring(0, 1))) {
		relativeResourceURI = relativeResourceURI.substring(1);
	}
	if (request.getQueryString() != null) {
		relativeResourceURI = relativeResourceURI + "?"
				+ request.getQueryString();
	}

	/* Determine service stem, i.e. vocab/ in /[vocab/]data */
	int servicePos;
	if (-1 == (servicePos = request.getServletPath().indexOf(
			"/" + D2RServer.getDataServiceName())))
		throw new ServletException("Expected to find service path /"
				+ D2RServer.getDataServiceName());
	String serviceStem = request.getServletPath().substring(1,
			servicePos + 1);

	String resourceURI = RequestParamHandler
			.removeOutputRequestParam(server.resourceBaseURI(serviceStem)
					+ relativeResourceURI);
	String documentURL = server.dataURL(serviceStem, relativeResourceURI);

	String pageURL = server.pageURL(serviceStem, relativeResourceURI);

	String sparqlQuery = "DESCRIBE <" + resourceURI + ">";
	QueryExecution qe = QueryExecutionFactory.create(sparqlQuery,
			server.dataset());
	if (server.getConfig().getPageTimeout() > 0) {
		qe.setTimeout(Math.round(server.getConfig().getPageTimeout() * 1000));
	}
	Model description = qe.execDescribe();
	qe.close();
	
	if (description.size() == 0) {
		response.sendError(404);
	}
	if (description.qnameFor(FOAF.primaryTopic.getURI()) == null
			&& description.getNsPrefixURI("foaf") == null) {
		description.setNsPrefix("foaf", FOAF.NS);
	}
	Resource resource = description.getResource(resourceURI);

	Resource document = description.getResource(documentURL);
	document.addProperty(FOAF.primaryTopic, resource);

	Statement label = resource.getProperty(RDFS.label);
	if (label != null) {
		document.addProperty(RDFS.label,
				"RDF Description of " + label.getString());
	}
	server.addDocumentMetadata(description, document);
	if (server.getConfig().serveMetadata()) {
		// add document metadata from template
		Model resourceMetadataTemplate = server.getConfig().getResourceMetadataTemplate(
				server, getServletContext());
		MetadataCreator resourceMetadataCreator = new MetadataCreator(
				server, resourceMetadataTemplate);
		description.add(resourceMetadataCreator.addMetadataFromTemplate(
				resourceURI, documentURL, pageURL));
		
		Map<String, String> descPrefixes = description.getNsPrefixMap();
		descPrefixes.putAll(resourceMetadataTemplate.getNsPrefixMap());
		description.setNsPrefixes(descPrefixes);
	}
	// TODO: Add a Content-Location header
	new ModelResponse(description, request, response).serve();
}
 
Example 10
Source File: RDFStoreDAO.java    From aliada-tool with GNU General Public License v3.0 4 votes vote down vote up
/**
 * It executes a SELECT SPARQL query on the SPARQL endpoint, 
 * to get the resources specified in the query argument.
 *
 * @param sparqlEndpointURI		the SPARQL endpoint URI.  
 * @param graphName 			the graphName, null in case of default graph.
 * @param user					the user name for the SPARQl endpoint.
 * @param password				the password for the SPARQl endpoint.
 * @param offset				causes the solutions generated to start after 
 *                              the specified number of solutions.
 * @param limit					upper bound on the number of solutions returned.
 * @return a list of {@link eu.aliada.shared.rdfstore.RetrievedResource} with the resources.
 * @since 2.0
 */
public RetrievedWork[] getWorks(final String sparqlEndpointURI, final String graphName, final String user, final String password, final int offset, final int limit) {
	final String query = " PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
	" PREFIX ecrm:   <http://erlangen-crm.org/current/>" +
	" PREFIX efrbroo: <http://erlangen-crm.org/efrbroo/>" +
	" SELECT DISTINCT ?work ?expr ?manif ?title FROM <" + graphName + "> " +
	" WHERE { ?work rdf:type efrbroo:F1_Work . "+
	" ?work efrbroo:R40_has_representative_expression ?expr ." +
	" ?expr efrbroo:R4_carriers_provided_by ?manif ." +
	" ?manif ecrm:P102_has_title ?apel ." +
	" ?apel ecrm:P3_has_note ?title ." +
	" }" +
	" ORDER BY ?work ?expr ?manif" +
	" OFFSET " + offset + " LIMIT " + limit;

	ArrayList<RetrievedWork> resList = new ArrayList<RetrievedWork>();
 	try {
        // Execute the query and obtain results
        final QueryExecution qexec = QueryExecutionFactory.sparqlService(
        		sparqlEndpointURI, 
        		QueryFactory.create(query), 
				auth(sparqlEndpointURI, user, password));
        qexec.setTimeout(2000, 5000);
           final ResultSet results = qexec.execSelect() ;
           while (results.hasNext())
           {
           	final QuerySolution soln = results.nextSolution() ;
	    	final RetrievedWork retrievedRes = new RetrievedWork();
	    	retrievedRes.setWorkURI(soln.getResource("work").getURI());
	    	retrievedRes.setExprURI(soln.getResource("expr").getURI());
	    	retrievedRes.setManifURI(soln.getResource("manif").getURI());
	    	retrievedRes.setTitle(soln.getLiteral("title").getString());
       		
	    	String dimensions = "";
           	if(soln.contains("dimensions")) {
           		dimensions = soln.getLiteral("dimensions").getString();
           	}
	    	retrievedRes.setDimensions(dimensions);
       		
	    	String extension = "";
           	if(soln.contains("extension")) {
           		extension = soln.getLiteral("extension").getString();
           	}
	    	retrievedRes.setExtension(extension);
       		
	    	String author = "";
           	if(soln.contains("author")) {
           		author = soln.getLiteral("author").getString();
           	}
	    	retrievedRes.setAuthor(author);

	    	String publicPlace = "";
           	if(soln.contains("place_publication")) {
           		publicPlace = soln.getLiteral("place_publication").getString();
           	}
	    	retrievedRes.setPublicPlace(publicPlace);

	    	String publicDate = "";
           	if(soln.contains("date_publication")) {
           		publicDate = soln.getLiteral("date_publication").getString();
           	}
	    	retrievedRes.setPublicDate(publicDate);

	    	String edition = "";
           	if(soln.contains("edition")) {
           		edition = soln.getLiteral("edition").getString();
           	}
	    	retrievedRes.setEdition(edition);
       		
	    	resList.add(retrievedRes);
           }
        qexec.close() ;
      } catch (Exception exception) {
		LOGGER.error(MessageCatalog._00035_SPARQL_FAILED, exception, query);
	}
	if (resList.isEmpty()) {
		return new RetrievedWork[0];
	}
	return (RetrievedWork[]) resList.toArray(new RetrievedWork[resList.size()]);
}