Java Code Examples for org.openrdf.rio.Rio#createWriter()

The following examples show how to use org.openrdf.rio.Rio#createWriter() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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);
			}
		}
	}
}