Java Code Examples for org.openrdf.rio.RDFWriter#handleStatement()

The following examples show how to use org.openrdf.rio.RDFWriter#handleStatement() . 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: ApplicationXmlTest.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
private void writeOutput( RDFWriter writer, Iterable<Statement> graph )
    throws RDFHandlerException
{
    writer.startRDF();
    writer.handleNamespace( "polygene", PolygeneRdf.POLYGENE_MODEL );
    writer.handleNamespace( "rdf", Rdfs.RDF );
    writer.handleNamespace( "rdfs", Rdfs.RDFS );
    for( Statement st : graph )
    {
        writer.handleStatement( st );
    }
    writer.endRDF();
}
 
Example 2
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 3
Source File: RemoteRepositoryBase.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Serialize an iteration of statements into a byte[] to send across the
 * wire.
 */
protected static byte[] serialize(final Iterable<? extends Statement> stmts,
        final RDFFormat format) throws Exception {
    
    final RDFWriterFactory writerFactory = 
        RDFWriterRegistry.getInstance().get(format);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    
    final RDFWriter writer = writerFactory.getWriter(baos);
    
    writer.startRDF();
    
    for (Statement stmt : stmts) {
    
        writer.handleStatement(stmt);
        
    }

    writer.endRDF();
    
    final byte[] data = baos.toByteArray();
    
    return data;

}
 
Example 4
Source File: ExportKB.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
     * Exports all told statements associated with the last commit point for the
     * KB.
     * 
     * @throws IOException
     * @throws SailException
     * @throws RDFHandlerException
     */
    public void exportData() throws IOException, SailException,
            RDFHandlerException {
        prepare();
//        final BigdataSail sail = new BigdataSail(kb);
//        try {
//            sail.initialize();
//            final SailConnection conn = sail.getReadOnlyConnection();
//            try {
                final CloseableIteration<? extends Statement, SailException> itr = conn
                        .getStatements(null/* s */, null/* p */, null/* o */,
                                includeInferred, new Resource[] {}/* contexts */);
                try {
                    final File file = new File(kbdir, "data."
                            + format.getDefaultFileExtension()+".gz");
                    System.out.println("Writing " + file);
                    final OutputStream os = new GZIPOutputStream(
                            new FileOutputStream(file));
                    try {
                        final RDFWriter writer = RDFWriterRegistry
                                .getInstance().get(format).getWriter(os);
                        writer.startRDF();
                        while (itr.hasNext()) {
                            final Statement stmt = itr.next();
                            writer.handleStatement(stmt);
                        }
                        writer.endRDF();
                    } finally {
                        os.close();
                    }
                } finally {
                    itr.close();
                }
//            } finally {
//                conn.close();
//            }
//        } finally {
//            sail.shutDown();
//        }

    }
 
Example 5
Source File: QueryServlet.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Void call() throws Exception {

  BigdataSailRepositoryConnection conn = null;
  
  try {

      conn = getQueryConnection();

      String mimeType = null;
      RDFFormat format = null;
      if (mimeTypes!=null) {
          mimeTypesLoop:
      	while(mimeTypes.hasMoreElements()) {
          	for (String mt:mimeTypes.nextElement().split(",")) {
          		mt = mt.trim();
               RDFFormat fmt = RDFWriterRegistry.getInstance()
                   .getFileFormatForMIMEType(mt);
               if (conn.getTripleStore().isQuads() && (mt.equals(RDFFormat.NQUADS.getDefaultMIMEType()) || mt.equals(RDFFormat.TURTLE.getDefaultMIMEType())) || !conn.getTripleStore().isQuads() && fmt != null) {
                   mimeType = mt;
                   format = fmt;
                   break mimeTypesLoop;
               }
          	}
          }
      }
      if (format==null) {
          if(conn.getTripleStore().isQuads()){
              mimeType = RDFFormat.NQUADS.getDefaultMIMEType();
          } else {
              mimeType = RDFFormat.NTRIPLES.getDefaultMIMEType();
          }
          format = RDFWriterRegistry.getInstance()
              .getFileFormatForMIMEType(mimeType);
      }
      resp.setContentType(mimeType);

      final OutputStream os = resp.getOutputStream();

      final RDFWriter w = RDFWriterRegistry.getInstance().get(format)
          .getWriter(os);

      RepositoryResult<Statement> stmts = null;

      try {
          w.startRDF();
          stmts = conn.getStatements(s, p, o, includeInferred, c);
          while(stmts.hasNext()){
              w.handleStatement(stmts.next());
          }
          w.endRDF();
      } finally {
          if (stmts != null) {
              stmts.close();
          }
          os.flush();
          os.close();
      }

      return null;
  } finally {
      if (conn != null) {
          conn.close();
      }
  }
}
 
Example 6
Source File: BigdataRDFServlet.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
     * Send an RDF Graph as a response using content negotiation.
     * 
     * @param req
     * @param resp
     */
    static public void sendGraph(final HttpServletRequest req,
            final HttpServletResponse resp, final Graph g) throws IOException {
        /*
         * CONNEG for the MIME type.
         */
        final List<String> acceptHeaders = Collections.list(req.getHeaders("Accept"));
		final String acceptStr = ConnegUtil.getMimeTypeForQueryParameterQueryRequest(req
				.getParameter(BigdataRDFServlet.OUTPUT_FORMAT_QUERY_PARAMETER),
				acceptHeaders.toArray(new String[acceptHeaders.size()])); 
		
        final ConnegUtil util = new ConnegUtil(acceptStr);

        // The best RDFFormat for that Accept header.
        RDFFormat format = util.getRDFFormat();

        if (format == null)
            format = RDFFormat.RDFXML;

		RDFWriterFactory writerFactory = RDFWriterRegistry.getInstance().get(
				format);

		if (writerFactory == null) {

			if (log.isDebugEnabled()) {
				log.debug("No writer for format: format=" + format
						+ ", Accept=\"" + acceptStr + "\"");
			}

			format = RDFFormat.RDFXML;
			
			writerFactory = RDFWriterRegistry.getInstance().get(format);
			
		}

//        if (writerFactory == null) {
//
//			buildResponse(resp, HTTP_BADREQUEST, MIME_TEXT_PLAIN,
//					"No writer for format: Accept=\"" + acceptStr
//							+ "\", format=" + format);
//			
//			return;
//
//		}
		
        resp.setStatus(HTTP_OK);

        resp.setContentType(format.getDefaultMIMEType());

        final OutputStream os = resp.getOutputStream();
        try {
            final RDFWriter writer = writerFactory.getWriter(os);
            writer.startRDF();
            final Iterator<Statement> itr = g.iterator();
            while (itr.hasNext()) {
                final Statement stmt = itr.next();
                writer.handleStatement(stmt);
            }
            writer.endRDF();
            os.flush();
        } catch (RDFHandlerException e) {
        	// wrap and rethrow. will be handled by launderThrowable().
            throw new IOException(e);
        } finally {
            os.close();
        }
    }
 
Example 7
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 8
Source File: AbstractTestNanoSparqlClient.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generates some statements and serializes them using the specified
 * {@link RDFFormat}.
 * 
 * @param ntriples
 *            The #of statements to generate.
 * @param format
 *            The format.
 * 
 * @return the serialized statements.
 */
protected byte[] genNTRIPLES(final int ntriples, final RDFFormat format)
        throws RDFHandlerException {

    final Graph g = genNTRIPLES2(ntriples);
    
    final RDFWriterFactory writerFactory = RDFWriterRegistry.getInstance()
            .get(format);

    if (writerFactory == null)
        fail("RDFWriterFactory not found: format=" + format);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    final RDFWriter writer = writerFactory.getWriter(baos);

    writer.startRDF();

    for (Statement stmt : g) {

        writer.handleStatement(stmt);

    }

    writer.endRDF();

    return baos.toByteArray();
    
}
 
Example 9
Source File: AbstractTestNanoSparqlClient.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Write a graph on a buffer suitable for sending as an HTTP request body.
 * 
 * @param format
 *            The RDF Format to use.
 * @param g
 *            The graph.
 *            
 * @return The serialized data.
 * 
 * @throws RDFHandlerException
 */
static protected byte[] writeOnBuffer(final RDFFormat format, final Graph g)
        throws RDFHandlerException {

    final RDFWriterFactory writerFactory = RDFWriterRegistry.getInstance()
            .get(format);

    if (writerFactory == null)
        fail("RDFWriterFactory not found: format=" + format);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    final RDFWriter writer = writerFactory.getWriter(baos);

    writer.startRDF();

    for (Statement stmt : g) {

        writer.handleStatement(stmt);

    }

    writer.endRDF();

    return baos.toByteArray();

}