com.hp.hpl.jena.query.ResultSetFormatter Java Examples

The following examples show how to use com.hp.hpl.jena.query.ResultSetFormatter. 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: SearchTestCase.java    From SolRDF with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	try {
		SolRDF solrdf = SolRDF.newBuilder()
				.withEndpoint("http://127.0.0.1:8080/solr/store")
				.withGraphStoreProtocolEndpointPath("/rdf-graph-store")
				.withSPARQLEndpointPath("/sparql")
				.build();
		
		solrdf.add(new FileReader("/work/workspaces/solrdf/solrdf/solrdf/solrdf-integration-tests/src/test/resources/sample_data/faceting_test_dataset.nt"), "N-TRIPLES");
		
		solrdf.commit();
		
		CloseableResultSet rs = null;
		try {
			rs = solrdf.select("SELECT * WHERE {?s ?p ?o}");
			System.out.println(ResultSetFormatter.asText(rs));
			
			Model m = solrdf.construct("DESCRIBE <http://example.org/book4>");
			System.out.println(m);
		} finally {
			rs.close();
		}
	} catch (final UnableToBuildSolRDFClientException exception) {
		exception.printStackTrace();
	}
}
 
Example #2
Source File: FromAndFromNamedClauses_ITCase.java    From SolRDF with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	Dataset memoryDataset = DatasetFactory.createMem();
	Model memoryModel = ModelFactory.createDefaultModel();
	memoryModel.read(new FileReader("/work/workspaces/rdf/SolRDF/solrdf/src/test/resources/sample_data/one_triple_1.ttl"), "http://e.org", "TTL");
	memoryDataset.addNamedModel("http://grapha.com", memoryModel);
	
	memoryModel = ModelFactory.createDefaultModel();
	memoryModel.read(new FileReader("/work/workspaces/rdf/SolRDF/solrdf/src/test/resources/sample_data/one_triple_2.ttl"), "http://e.org", "TTL");
	memoryDataset.addNamedModel("http://graphb.com", memoryModel);
	
	memoryModel = ModelFactory.createDefaultModel();
	memoryModel.read(new FileReader("/work/workspaces/rdf/SolRDF/solrdf/src/test/resources/sample_data/one_triple_3.ttl"), "http://e.org", "TTL");
	memoryDataset.addNamedModel("http://graphc.com", memoryModel);
	
	memoryModel = ModelFactory.createDefaultModel();
	memoryModel.read(new FileReader("/work/workspaces/rdf/SolRDF/solrdf/src/test/resources/sample_data/one_triple_4.ttl"), "http://e.org", "TTL");
	memoryDataset.addNamedModel("http://graphd.com", memoryModel);
	
	final Query query = QueryFactory.create(q2());//"SELECT ?s FROM <http://grapha.com> WHERE { ?s <http://example.org/title> ?o }");
	
	System.out.println(ResultSetFormatter.asText(QueryExecutionFactory.create(query, memoryDataset).execSelect()));
}
 
Example #3
Source File: IntegrationTestSupertypeLayer.java    From SolRDF with Apache License 2.0 6 votes vote down vote up
/**
 * Executes a given SELECT query against a given dataset.
 * 
 * @param data the mistery guest containing test data (query and dataset)
 * @throws Exception never, otherwise the test fails.
 */
protected void selectTest(final MisteryGuest data) throws Exception {
	load(data);
	
	try {
		assertTrue(
				Arrays.toString(data.datasets) + ", " + data.query,
				ResultSetCompare.isomorphic(
						SOLRDF_CLIENT.select(queryString(data.query)),
						(inMemoryExecution = QueryExecutionFactory.create(
								QueryFactory.create(queryString(data.query)), 
								memoryDataset)).execSelect()));
	} catch (final Throwable error) {
		log.debug("JNS\n" + ResultSetFormatter.asText(SOLRDF_CLIENT.select(queryString(data.query))));
		
		QueryExecution debugExecution = null;
		log.debug("MEM\n" + ResultSetFormatter.asText(
				(debugExecution = (QueryExecutionFactory.create(
						QueryFactory.create(queryString(data.query)), 
						memoryDataset))).execSelect()));
		
		debugExecution.close();
		throw error;
	} 
}
 
Example #4
Source File: SparqlExtractor.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
public void handleResultSet(ResultSet results, String method, TopicMap tm) throws Exception {
    if(results != null) {
        if("RESULTSET-RDF-TOPICMAP".equals(method)) {
            Model model = ResultSetFormatter.toModel(results);
            RDF2TopicMap(model, tm);
        }
        else {
            resultSet2TopicMap(results, tm);
        }
    }
    else {
        log("Warning: no result set available!");
    }
}
 
Example #5
Source File: SPARQLService.java    From semanticMDR with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Runs SPARQL queries
 * 
 * @param type
 *            Result Format: N3, N-TRIPLE, RDF/XML, RDF/XML-ABBREV, TURTLE
 * @param query
 *            Sparql for the query
 * @return
 */
@GET
@Produces({ WebUtil.MEDIA_TYPE_APPLICATION_NTRIPLE,
		WebUtil.MEDIA_TYPE_APPLICATION_RDFJSON,
		WebUtil.MEDIA_TYPE_APPLICATION_RDFXML, MediaType.TEXT_PLAIN,
		WebUtil.MEDIA_TYPE_TEXT_N3, WebUtil.MEDIA_TYPE_TEXT_TURTLE })
public Response selectQuery(@QueryParam("sparql") String query,
		@Context Request request) {

	Variant variant = request.selectVariant(WebUtil.VARIANTS);
	MediaType mediaType = variant.getMediaType();

	Repository repository = RepositoryManager.getInstance().getRepository();
	OntModel ontModel = repository.getMDRDatabase().getOntModel();
	Query q = null;

	try {
		query = URLDecoder.decode(query, "UTF-8");
		q = QueryFactory.create(query);
	} catch (Exception exc) {
		logger.error("Error during the creation of the SPARQL query", exc);
		return Response.serverError().build();
	}

	QueryExecution qexec = QueryExecutionFactory.create(q, ontModel);
	Model resultModel = null;
	if (q.isSelectType()) {
		ResultSet resultSet = qexec.execSelect();
		resultModel = ResultSetFormatter.toModel(resultSet);
	} else {
		throw new WebApplicationException(Status.UNAUTHORIZED);
	}
	qexec.close();

	graphStream.setModel(resultModel);
	graphStream.setLanguage(WebUtil.getSerializationLanguage(mediaType
			.toString()));

	return Response.ok(graphStream).build();
}
 
Example #6
Source File: Sparql.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Object execute(Map<String, EdmLiteral> parameters) throws ODataException
{
   EdmLiteral query_lit = parameters.remove("query");
   // Olingo2 checks for presence of non-nullable parameters for us!
   String query_s = query_lit.getLiteral();
   Query query = QueryFactory.create(query_s);
   if (!(query.isSelectType() || query.isDescribeType()))
   {
      throw new InvalidOperationException(query.getQueryType());
   }

   DrbCortexModel cortexmodel;
   try
   {
      cortexmodel = DrbCortexModel.getDefaultModel();
   }
   catch (IOException ex)
   {
      throw new RuntimeException(ex);
   }

   Model model = cortexmodel.getCortexModel().getOntModel();

   QueryExecution qexec = null;
   // FIXME: QueryExecution in newer versions of Jena (post apache incubation) implement AutoClosable.
   try
   {
      qexec = QueryExecutionFactory.create(query, model);
      if (query.isSelectType())
      {
         ResultSet results = qexec.execSelect();
         return ResultSetFormatter.asXMLString(results);
      }
      else
      {
         Model description = qexec.execDescribe();
         // newer version of Jena have the RIOT package for I/O
         StringWriter strwrt = new StringWriter();
         Abbreviated abb = new Abbreviated();
         abb.write(description, strwrt, null);
         return strwrt.toString();
      }
   }
   finally
   {
      if (qexec != null)
      {
         qexec.close();
      }
   }
}