org.openrdf.rio.Rio Java Examples

The following examples show how to use org.openrdf.rio.Rio. 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: QueryEvaluation.java    From CostFed with GNU Affero General Public License v3.0 7 votes vote down vote up
protected Graph parseConfig(File file) throws SailConfigException, IOException {

        RDFFormat format = Rio.getParserFormatForFileName(file.getAbsolutePath());
        if (format==null)
            throw new SailConfigException("Unsupported file format: " + file.getAbsolutePath());
        RDFParser parser = Rio.createParser(format);
        Graph model = new GraphImpl();
        parser.setRDFHandler(new StatementCollector(model));
        InputStream stream = new FileInputStream(file);

        try {
            parser.parse(stream, file.getAbsolutePath());
        } catch (Exception e) {
            throw new SailConfigException("Error parsing file!");
        }

        stream.close();
        return model;
    }
 
Example #2
Source File: RDFModelParser.java    From ldp4j with Apache License 2.0 6 votes vote down vote up
private TripleProducer getProducer(String content) {
	RDFFormat sesameFormat =
		Rio.
			getParserFormatForMIMEType(
				this.format.getMime(),
				RDFFormat.TURTLE);
	TripleProducer producer=null;
	switch(unmarshallStyle) {
		case PARSER_BASED:
			producer=new ParserBasedTripleProducer(content,sesameFormat,this.baseURI.toString());
			break;
		case REPOSITORY_BASED:
			producer=new RepositoryBasedTripleProducer(content,sesameFormat,this.baseURI.toString());
			break;
		default:
			throw new AssertionError("Unsupported unmarshalling style '"+unmarshallStyle+"'");
	}
	return producer;
}
 
Example #3
Source File: RDFModelFormater.java    From ldp4j with Apache License 2.0 6 votes vote down vote up
private RDFWriter createWriter(StringWriter writer) {
	RDFWriter result=null;
	if(format.equals(Format.TURTLE)) {
		result=new TurtlePrettyPrinter(new MemValueFactory().createURI(baseURI.toString()),writer);
	} else {
		RDFWriterRegistry registry=RDFWriterRegistry.getInstance();
		RDFFormat rawFormat=Rio.getWriterFormatForMIMEType(format.getMime(),RDFFormat.RDFXML);
		RDFWriterFactory factory=registry.get(rawFormat);
		result=factory.getWriter(writer);
		if(format.equals(Format.JSON_LD)) {
			result.getWriterConfig().set(JSONLDSettings.JSONLD_MODE,JSONLDMode.FLATTEN);
			result.getWriterConfig().set(BasicWriterSettings.PRETTY_PRINT,true);
		}
	}
	return result;
}
 
Example #4
Source File: CustomSesameDataset.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
/**
 * Execute a CONSTRUCT/DESCRIBE SPARQL query against the graphs
 * 
 * @param qs
 *            CONSTRUCT or DESCRIBE SPARQL query
 * @param format
 *            the serialization format for the returned graph
 * @return serialized graph of results
 */
public String runSPARQL(String qs, RDFFormat format) {
	try {
		RepositoryConnection con = currentRepository.getConnection();
		try {
			GraphQuery query = con.prepareGraphQuery(
					org.openrdf.query.QueryLanguage.SPARQL, qs);
			StringWriter stringout = new StringWriter();
			RDFWriter w = Rio.createWriter(format, stringout);
			query.evaluate(w);
			return stringout.toString();
		} finally {
			con.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}
 
Example #5
Source File: CustomSesameDataset.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
public String printRDF(RDFFormat outform) {
	try {
		RepositoryConnection con = currentRepository.getConnection();
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			RDFWriter w = Rio.createWriter(outform, out);

			con.export(w);
			String result = new String(out.toByteArray(), "UTF-8");
			return result;
		} finally {
			con.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}
 
Example #6
Source File: TestTicket276.java    From database with GNU General Public License v2.0 6 votes vote down vote up
private void addData(final RepositoryConnection conn) throws IOException,
		RDFParseException, RepositoryException, RDFHandlerException {

    final RDFParser rdfParser = Rio.createParser(RDFFormat.NTRIPLES,
			conn.getValueFactory());
	rdfParser.setVerifyData(true);
	rdfParser.setStopAtFirstError(true);
	rdfParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
	rdfParser.setRDFHandler(new RDFHandlerBase() {
		@Override
		public void handleStatement(Statement st)
				throws RDFHandlerException {
			try {
				conn.add(st);
			} catch (OpenRDFException e) {
				throw new RDFHandlerException(e);
			}
		}
	});
	rdfParser.parse(getClass().getResourceAsStream("TestTicket276.n3"), "");
}
 
Example #7
Source File: TestUtils.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a statement iterator to an input stream with serialized RDF data.
 * 
 * @param statements The statement iterator.
 * @param format The RDF format to use for serialization.
 * @return The serialized RDF data.
 * @throws RDFHandlerException in case of operation failure. 
 */
public static InputStream statementIteratorToRdfStream(final Iterator<Statement> statements, final RDFFormat format) throws RDFHandlerException {
	ByteArrayOutputStream stream = new ByteArrayOutputStream();
	RDFWriter rdfWriter = Rio.createWriter(format, stream);

	rdfWriter.startRDF();

	while (statements.hasNext()) {
		rdfWriter.handleStatement(statements.next());
	}

	rdfWriter.endRDF();
	return new ByteArrayInputStream(stream.toByteArray());
}
 
Example #8
Source File: TripleStoreBlazegraph.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
private static void write(Model statements, OutputStream out) {
    try (PrintStream pout = new PrintStream(out)) {
        RDFWriter w = new RDFXMLPrettyWriter(pout);
        Rio.write(statements, w);
    } catch (Exception x) {
        throw new TripleStoreException("Writing model statements", x);
    }
}
 
Example #9
Source File: RDFModelParser.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
@Override
public void injectTriples(TripleSink sink) throws IOException {
	try {
		Collector collector = new Collector();
		RDFParser parser =Rio.createParser(this.format);
		parser.setRDFHandler(collector);
		parser.parse(new StringReader(this.content), this.base);
		SesameModelParser tripleParser=new SesameModelParser(collector.getNamespaces());
		for(Statement st:collector.getStatements()) {
			sink.addTriple(tripleParser.parseStatement(st));
		}
	} catch (OpenRDFException e) {
		throw new IOException(e);
	}
}
 
Example #10
Source File: RdfIndexExporter.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public void exportReadableToStream( PrintStream out )
    throws IOException
{
    RDFWriter rdfWriter = Rio.createWriter( RDFFormat.TRIG, out );
    exportToWriter( rdfWriter );
}
 
Example #11
Source File: RDFModelFormater.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
protected String exportRepository(RepositoryConnection connection) throws RepositoryException, RDFHandlerException {
	StringWriter writer=new StringWriter();
	RDFWriter rdfWriter=Rio.createWriter(getFormat(),writer);
	if(rdfWriter instanceof TurtleWriter) {
		rdfWriter=new RebasingTurtleWriter(writer);
	}
	connection.export(rdfWriter);
	return writer.toString();
}
 
Example #12
Source File: RepositoryModel.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void writeTo(OutputStream stream, Syntax syntax) throws
// interface allows it
        IOException, ModelRuntimeException {
	RDFWriter rdfWriter = Rio.createWriter(getRDFFormat(syntax), stream);
	writeTo(rdfWriter);
}
 
Example #13
Source File: SPARQLQueryTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
protected final Set<Statement> readExpectedGraphQueryResult()
	throws Exception
{
	RDFFormat rdfFormat = Rio.getParserFormatForFileName(resultFileURL);

	if (rdfFormat != null) {
		RDFParser parser = Rio.createParser(rdfFormat);
		parser.setDatatypeHandling(DatatypeHandling.IGNORE);
		parser.setPreserveBNodeIDs(true);
		parser.setValueFactory(dataRep.getValueFactory());

		Set<Statement> result = new LinkedHashSet<Statement>();
		parser.setRDFHandler(new StatementCollector(result));

		InputStream in = new URL(resultFileURL).openStream();
		try {
			parser.parse(in, resultFileURL);
		}
		finally {
			in.close();
		}

		return result;
	}
	else {
		throw new RuntimeException("Unable to determine file type of results file");
	}
}
 
Example #14
Source File: Util.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the incoming RDF stream according with a given format.
 * 
 * @param stream
 *            the RDF (input) stream.
 * @param format
 *            the {@link RDFFormat} that will be used for parsing.
 * @return a {@link List} with collected statements.
 */
public static List<Statement> parseAsList(final InputStream stream, final RDFFormat format) {
	final List<Statement> statements = new ArrayList<Statement>();
	try {
		final RDFParser parser = Rio.createParser(format, VALUE_FACTORY);
		parser.setRDFHandler(new StatementCollector(statements));
		parser.parse(stream, "");
	} catch (final Exception exception) {
		LOGGER.error(MessageCatalog._00029_RDF_PARSE_FAILURE, exception);
	}
	return statements;
}
 
Example #15
Source File: SesameHTTPRepositoryTest.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
/**
 * test get Statemetns.
 * @throws Exception The test would fails if Exception occurs
 */
@Test
public void testGetStatements() throws Exception {
	//insert some statments first
	for (final String tripleAsString : _data) {
		final Statement triple = parseNX(tripleAsString).iterator().next();
		TRIPLE_STORE.addData(triple);
	}
	//test get the statements
	String uri = BASE_URI + Protocol.REPOSITORIES + "/" + REPO_ID + "/" + Protocol.STATEMENTS;
	File tmp = WebTestUtils.tmpFile();
	final ServletOutputStream servletOutputStream = new StubServletOutputStream(tmp);
	final HttpServletRequest request = mockHttpRequest(uri, METHOD_GET);
	when(request.getParameter(Protocol.SUBJECT_PARAM_NAME)).thenReturn(DEVICE);
	when(request.getParameter(Protocol.ACCEPT_PARAM_NAME)).thenReturn(MIMETYPE_RDF_XML);
	when(request.getCharacterEncoding()).thenReturn(UTF_8);
	final HttpServletResponse response = mock(HttpServletResponse.class);
	when(response.getOutputStream()).thenReturn(servletOutputStream);

	GetMethod method = new GetMethod(uri);
	RDFFormat format = RDFFormat.RDFXML;
	RDFParser parser = Rio.createParser(format, valueFactory);
	parser.setParserConfig(parser.getParserConfig());
	StatementCollector collector = new StatementCollector();
	parser.setRDFHandler(collector);
	_classUnderTest.service(request, response);

	parser.parse(new FileInputStream(tmp), method.getURI().getURI());
	assertTrue(!collector.getStatements().isEmpty());
	verify(response).setStatus(HttpServletResponse.SC_OK);

	servletOutputStream.close();
}
 
Example #16
Source File: RdfIndexExporter.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public void exportFormalToWriter( PrintWriter out )
    throws IOException
{
    RDFWriter rdfWriter = Rio.createWriter( RDFFormat.RDFXML, out );
    exportToWriter( rdfWriter );
}
 
Example #17
Source File: CustomSesameDataset.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
/**
 * Dump RDF graph
 * 
 * @param out
 *            output stream for the serialization
 * @param outform
 *            the RDF serialization format for the dump
 * @return
 */
public void dumpRDF(OutputStream out, RDFFormat outform) {
	try {
		RepositoryConnection con = currentRepository.getConnection();
		try {
			RDFWriter w = Rio.createWriter(outform, out);
			con.export(w);
		} finally {
			con.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example #18
Source File: SPARQLQueryTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
protected void upload(URI graphURI, Resource context)
	throws Exception
{
	RepositoryConnection con = dataRep.getConnection();

	try {
		con.begin();
		RDFFormat rdfFormat = Rio.getParserFormatForFileName(graphURI.toString(), RDFFormat.TURTLE);
		RDFParser rdfParser = Rio.createParser(rdfFormat, dataRep.getValueFactory());
		rdfParser.setVerifyData(false);
		rdfParser.setDatatypeHandling(DatatypeHandling.IGNORE);
		// rdfParser.setPreserveBNodeIDs(true);

		RDFInserter rdfInserter = new RDFInserter(con);
		rdfInserter.enforceContext(context);
		rdfParser.setRDFHandler(rdfInserter);

		URL graphURL = new URL(graphURI.toString());
		InputStream in = graphURL.openStream();
		try {
			rdfParser.parse(in, graphURI.toString());
		}
		finally {
			in.close();
		}

		con.commit();
	}
	catch (Exception e) {
		if (con.isActive()) {
			con.rollback();
		}
		throw e;
	}
	finally {
		con.close();
	}
}
 
Example #19
Source File: TestFederatedQuery.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
  * Read the expected graph query result from the specified resource
  * 
  * @param resultFile
  * @return
  * @throws Exception
  */
 private Set<Statement> readExpectedGraphQueryResult(String resultFile) throws Exception
 {
     final RDFFormat rdfFormat = Rio.getParserFormatForFileName(resultFile);
 
     if (rdfFormat != null) {
         final RDFParser parser = Rio.createParser(rdfFormat);
         parser.setDatatypeHandling(DatatypeHandling.IGNORE);
         parser.setPreserveBNodeIDs(true);
         parser.setValueFactory(ValueFactoryImpl.getInstance());
 
         final Set<Statement> result = new LinkedHashSet<Statement>();
         parser.setRDFHandler(new StatementCollector(result));
 
final InputStream in = TestFederatedQuery.class
		.getResourceAsStream(TEST_RESOURCE_PATH + resultFile);
         try {
             parser.parse(in, null);     // TODO check
         }
         finally {
             in.close();
         }
 
         return result;
     }
     else {
         throw new RuntimeException("Unable to determine file type of results file");
     }
 }
 
Example #20
Source File: LinkedDataServletTest.java    From cumulusrdf with Apache License 2.0 4 votes vote down vote up
@Test
public void post() throws IOException, CumulusStoreException, RDFHandlerException, ServletException {

	for (int i = 0; i < TRIPLES_NT.size(); i++) {

		for (String mime_type : MimeTypes.RDF_SERIALIZATIONS) {

			/*
			 * clear data ...
			 */
			TRIPLE_STORE.clear();
			assertTrue("store should be empty", !TRIPLE_STORE.query(new Value[] { null, null, null }).hasNext());

			/*
			 * prepare data in desired RDF serialization
			 */

			Model model = new LinkedHashModel(parseNX(TRIPLES_NT.get(i)));
			final ByteArrayOutputStream out = new ByteArrayOutputStream();
			Rio.write(model, out, RDFFormat.forMIMEType(mime_type));

			/*
			 * prepare mock ...
			 */
			when(_request.getHeader(Headers.CONTENT_TYPE)).thenReturn(mime_type);
			when(_request.getInputStream()).thenReturn(new ServletInputStream() {
				final InputStream _inputStream = new ByteArrayInputStream(out.toByteArray());

				@Override
				public int read() throws IOException {
					return _inputStream.read();
				}
			});

			/*
			 * HTTP POST 
			 */
			_ld_servlet.doPost(_request, _response);

			/*
			 * verify the HTTP POST ...
			 */
			verify(_response, atLeastOnce()).setStatus(HttpServletResponse.SC_CREATED);

			for (Statement stmt : model) {
				assertTrue("statement '" + stmt + "' has not been been added correctly for serialization '" + mime_type + "'", TRIPLE_STORE.query(Util.toValueArray(stmt))
						.hasNext());
			}
		}
	}
}
 
Example #21
Source File: RepositoryModel.java    From semweb4j with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void writeTo(Writer writer, Syntax syntax) throws ModelRuntimeException {
	assertModel();
	RDFWriter rdfWriter = Rio.createWriter(getRDFFormat(syntax), writer);
	writeTo(rdfWriter);
}
 
Example #22
Source File: RepositoryModelSet.java    From semweb4j with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Writes the whole ModelSet to the OutputStream. Depending on the Syntax
 * the context URIs might or might not be serialized. TriX should be able to
 * serialize contexts.
 */
@Override
public void writeTo(OutputStream out, Syntax syntax) throws IOException, ModelRuntimeException {
	RDFWriter rdfWriter = Rio.createWriter(getRDFFormat(syntax), out);
	this.writeTo(rdfWriter);
}
 
Example #23
Source File: RepositoryModelSet.java    From semweb4j with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Writes the whole ModelSet to the Writer. Depending on the Syntax the
 * context URIs might or might not be serialized. TriX should be able to
 * serialize contexts.
 */
@Override
public void writeTo(Writer writer, Syntax syntax) throws IOException, ModelRuntimeException {
	RDFWriter rdfWriter = Rio.createWriter(getRDFFormat(syntax), writer);
	this.writeTo(rdfWriter);
}
 
Example #24
Source File: SubSetCreator.java    From mustard with MIT License 4 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
	boolean[] inference = {false, true};
	long[] seeds = {1,2,3,4,5,6,7,8,9,10};
	double fraction = 0.05;
	int minSize = 0;
	int maxClasses = 3;
	int depth = 3;
	
	///*
	String saveDir = "datasets/BGS_small/BGSsubset";
	String loadDir = BGS_FOLDER;
	RDFFormat format = RDFFormat.NTRIPLES;
	//*/

	/*
	String saveDir = "datasets/AMsubset";
	String loadDir = AM_FOLDER;
	RDFFormat format = RDFFormat.TURTLE;
	//*/

	
	RDFDataSet tripleStore = new RDFFileDataSet(loadDir, format);

	LargeClassificationDataSet ds = new BGSDataSet(tripleStore, "http://data.bgs.ac.uk/ref/Lexicon/hasTheme", 10, 0.05, 5, 3);

	//LargeClassificationDataSet ds = new AMDataSet(tripleStore,  10, 0.05, 5, 3, true);

	
	for (long seed : seeds) {
		for (boolean inf : inference) {
			ds.createSubSet(seed, fraction, minSize, maxClasses);

			System.out.println("Getting Statements...");
			Set<Statement> stmts = RDFUtils.getStatements4Depth(tripleStore, ds.getRDFData().getInstances(), depth, inf);
			System.out.println("# Statements: " + stmts.size());
			stmts.removeAll(new HashSet<Statement>(ds.getRDFData().getBlackList()));
			System.out.println("# Statements: " + stmts.size() + ", after blackList");


			File dir = new File(saveDir + seed + inf);
			dir.mkdirs();

			File file = new File(saveDir + seed + inf, "subset.ttl");
			File instFile = new File(saveDir + seed + inf, "instances.txt");
			File targetFile = new File(saveDir + seed + inf, "target.txt");
			
			try {
				RDFWriter writer = Rio.createWriter(RDFFormat.TURTLE, new FileWriter(file));
				FileWriter instWriter = new FileWriter(instFile);
				FileWriter targetWriter = new FileWriter(targetFile);
				
				
				writer.startRDF();
				for (Statement stmt : stmts) {
					writer.handleStatement(stmt);
				}
				writer.endRDF();
				
				for (Resource inst : ds.getRDFData().getInstances()) {
					instWriter.write(inst.toString() + "\n");
				}
				instWriter.close();
				
				for (Double target : ds.getTarget()) {
					targetWriter.write(target.toString() + "\n");
				}
				targetWriter.close();

			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	}
}
 
Example #25
Source File: FileSesameDataset.java    From GeoTriples with Apache License 2.0 4 votes vote down vote up
private void init(String target,RDFFormat format) {
	this.target = new File(target);

    try {
        //fw = new BufferedWriter(new FileWriter(target));
    	
    	fw = new BufferedWriter
    		    (new OutputStreamWriter(new FileOutputStream(target),"iso-8859-7"));
        
        
        writer = Rio.createWriter(this.format, fw);
        writer.startRDF();

    } catch (IOException | RDFHandlerException ex) {
        log.error("", ex);
    } 

}
 
Example #26
Source File: RDFModelFormater.java    From ldp4j with Apache License 2.0 4 votes vote down vote up
private RDFFormat getFormat() {
	return Rio.getWriterFormatForMIMEType(format.getMime(), RDFFormat.TURTLE);
}
 
Example #27
Source File: BasicRioLoader.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Choose the parser based on the {@link RDFFormat} specified to the
 * constructor.
 * 
 * @return The parser.
 */
final protected RDFParser getParser(final RDFFormat rdfFormat) {

    final RDFParser parser = Rio.createParser(rdfFormat, valueFactory);
    
    parser.setValueFactory(valueFactory);
    
    return parser;

}
 
Example #28
Source File: AbstractDataDrivenSPARQLTestCase.java    From database with GNU General Public License v2.0 2 votes vote down vote up
public Set<Statement> readExpectedGraphQueryResult() throws Exception {

            if (expectedGraphQueryResult != null)
                return expectedGraphQueryResult;
            
            final RDFFormat rdfFormat = Rio
                    .getParserFormatForFileName(resultFileURL);

            if (rdfFormat != null) {

                final RDFParser parser = Rio.createParser(rdfFormat);
                parser.setDatatypeHandling(DatatypeHandling.IGNORE);
                parser.setPreserveBNodeIDs(true);
                parser.setValueFactory(store.getValueFactory());

                final Set<Statement> result = new LinkedHashSet<Statement>();
                
                parser.setRDFHandler(new StatementCollector(result));

                final InputStream in = getResourceAsStream(resultFileURL);
                
                try {
                
                    parser.parse(in, resultFileURL);
                    
                } finally {
                    
                    in.close();
                    
                }
                
                expectedGraphQueryResult = result;

                return result;
                
            } else {

                throw new RuntimeException(
                        "Unable to determine file type of results file: "
                                + resultFileURL);

            }
            
        }