org.openrdf.rio.RDFFormat Java Examples

The following examples show how to use org.openrdf.rio.RDFFormat. 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: SPARQLUpdateTestv2.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
     * Note: Overridden to turn off autocommit and commit after the data are
     * loaded.
     */
    protected void loadDataset(String datasetFile)
            throws RDFParseException, RepositoryException, IOException
        {
            logger.debug("loading dataset...");
            InputStream dataset = SPARQLUpdateTest.class.getResourceAsStream(datasetFile);
            try {
//                con.setAutoCommit(false);
                con.add(dataset, "", RDFFormat.forFileName(datasetFile));//RDFFormat.TRIG);
                con.commit();
            }
            finally {
                dataset.close();
            }
            logger.debug("dataset loaded.");
        }
 
Example #2
Source File: CustomSesameDataset.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
public void loadDataFromInputStream(InputStream is, RDFFormat format,
		Resource... contexts) throws RepositoryException,
		RDFParseException, IOException {
	RepositoryConnection con = null;
	try {
		con = currentRepository.getConnection();
		con.add(is, "http://geotriples.eu", format, contexts);
	} finally {
		try {
			con.close();
		} catch (RepositoryException e) {
			e.printStackTrace();
		}
	}

}
 
Example #3
Source File: ObjectParserTest.java    From anno4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJSONLD() throws UpdateExecutionException {

    try {
        URL url = new URL("http://example.com/");

        ObjectParser objectParser = new ObjectParser();
        List<Annotation> annotations = objectParser.parse(JSONLD, url, RDFFormat.JSONLD, true);

        for(Annotation anno : annotations) {
            System.out.println(anno.toString());
        }

        assertEquals(1, annotations.size());

        objectParser.shutdown();
    } catch (RepositoryException | MalformedURLException | RepositoryConfigException e) {
        e.printStackTrace();
    }
}
 
Example #4
Source File: LoadPdb.java    From database with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
		File file = new File("tmp-1Y26.jnl");

		Properties properties = new Properties();
		properties.setProperty(BigdataSail.Options.FILE, file.getAbsolutePath());

		BigdataSail sail = new BigdataSail(properties);
		Repository repo = new BigdataSailRepository(sail);
		repo.initialize();

        if(false) {
        sail.getDatabase().getDataLoader().loadData(
                "contrib/src/problems/alex/1Y26.rdf",
                new File("contrib/src/problems/alex/1Y26.rdf").toURI()
                        .toString(), RDFFormat.RDFXML);
        sail.getDatabase().commit();
        }
        
//		loadSomeDataFromADocument(repo, "contrib/src/problems/alex/1Y26.rdf");
		// readSomeData(repo);
		executeSelectQuery(repo, "SELECT * WHERE { ?s ?p ?o }");
	}
 
Example #5
Source File: GraphStore.java    From neo4j-sparql-extension with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Helper method for handleAdd.
 */
private void addToGraphstore(
		RepositoryConnection conn,
		InputStream in,
		String base,
		RDFFormat format,
		Resource dctx,
		boolean chunked) throws IOException, RDFParseException,
		RDFHandlerException, RepositoryException {
	if (chunked) {
		RDFParser parser = getRDFParser(format);
		parser.setRDFHandler(
				new ChunkedCommitHandler(conn, chunksize, dctx));
		parser.parse(in, base);
	} else {
		if (dctx != null) {
			conn.add(in, base, format, dctx);
		} else {
			conn.add(in, base, format);
		}
	}
}
 
Example #6
Source File: ObjectParserTest.java    From anno4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleInOneTurtle() throws UpdateExecutionException {
    try {
        URL url = new URL("http://example.com/");

        ObjectParser objectParser = new ObjectParser();
        List<Annotation> annotations = objectParser.parse(TURTLE_MULTIPLE, url, RDFFormat.TURTLE, true);

        assertEquals(3, annotations.size());

        for(Annotation anno : annotations) {
            System.out.println(anno.toString());
        }

        objectParser.shutdown();
    } catch (IOException | RepositoryException | RepositoryConfigException e) {
        e.printStackTrace();
    }
}
 
Example #7
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 #8
Source File: QuadsTest.java    From cumulusrdf with Apache License 2.0 6 votes vote down vote up
/**
 * Test if the ttl feature for the triple store works.
 * @throws Exception never, otherwise the corresponding test will fail.
 */
@Ignore
@Test
public void testTTL() throws Exception {
	ProgrammableConfigurator progConf = new ProgrammableConfigurator(Collections.singletonMap("ttl-value", Integer.valueOf(5))); 
	QuadStore quadStore = new QuadStore("TestKS");
	quadStore.accept(progConf);
	quadStore.open();
	  
	quadStore.bulkLoad(DATA_NQ, RDFFormat.NQUADS);
	assertEquals((int) _query2numOfRes.get(_null_queries[0]), numOfRes(quadStore.query(_null_queries[0])));
	  
	Thread.sleep(7000);
	  
	assertEquals(0, numOfRes(quadStore.query(_null_queries[0])));
}
 
Example #9
Source File: LocalGOMTestCase.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Utility to load n3 statements from a resource
 */
public void load(final URL n3, final RDFFormat rdfFormat)
        throws IOException, RDFParseException, RepositoryException {

    final InputStream in = n3.openConnection().getInputStream();
    try {
        final Reader reader = new InputStreamReader(in);
        try {

            final BigdataSailRepositoryConnection cxn = m_repo
                    .getConnection();
            try {
                cxn.setAutoCommit(false);
                cxn.add(reader, "", rdfFormat);
                cxn.commit();
            } finally {
                cxn.close();
            }
        } finally {
            reader.close();
        }
    } finally {
        in.close();
    }
    
}
 
Example #10
Source File: LargeModifyStoreTest.java    From cumulusrdf with Apache License 2.0 6 votes vote down vote up
/**
 * Test if the quad store works with large iterators.
 * 
 * @throws Exception If something bad happens... 
 */
@Test
public void testLargeQuadOperations() throws Exception {
	_quadStore = newQuadStore();
	_quadStore.open();
	try {
		Random random = new Random(4986723946726L);
		List<Statement> statements = randomStatements(random, (int) (1000 * 3.1415));
		InputStream statementStream = statementIteratorToRdfStream(statements.iterator(), RDFFormat.NQUADS);

		_quadStore.bulkLoad(statementStream, RDFFormat.NQUADS);

		assertEquals(
				"Store contains different amount of quads than inserted!",
				statements.size(),
				numOfRes(_quadStore.query(SELECT_ALL_QUADS_PATTERN)));

		_quadStore.removeData(_quadStore.query(SELECT_ALL_QUADS_PATTERN));
		assertEquals(
				"Store should be empty after deleting everything!",
				0,
				numOfRes(_quadStore.query(SELECT_ALL_QUADS_PATTERN)));
	} finally {
		_quadStore.close();
	}
}
 
Example #11
Source File: LiteralTest.java    From anno4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setUp() throws Exception {
	module = new ObjectRepositoryConfig();
	module.addBehaviour(TestSupport.class);
	module.addConcept(TestConcept.class);
	module.addDatatype(SomeLiteral.class, new URIImpl("urn:SomeLiteral"));
	super.setUp();
	RepositoryConnection connection = repository.getConnection();
	// import RDF schema and datatype hierarchy
	connection.add(getClass().getResourceAsStream(
			"/testcases/schemas/rdfs-schema.rdf"), "", RDFFormat.RDFXML);
	connection.add(getClass().getResourceAsStream(
			"/testcases/schemas/owl-schema.rdf"), "", RDFFormat.RDFXML);
	connection.add(getClass().getResourceAsStream(
			"/testcases/schemas/xsd-datatypes.rdf"), "", RDFFormat.RDFXML);
	connection.close();
	factory = (ObjectRepository) repository;
	this.manager = factory.getConnection();
}
 
Example #12
Source File: DataLoader.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Load a set of RDF resources into the associated triple store and commit.
 * 
 * @param resource
 *            An array of resources to be loaded (required).
 * @param baseURL
 *            An array baseURL to use for those resources (required and must
 *            be 1:1 with the array of resources).
 * @param rdfFormat
 *            An array of {@link RDFFormat} values to use as a fall back for
 *            each resource (required and must be 1:1 with the array of
 *            resources).
 * 
 * @return Statistics about the load.
 * 
 * @throws IOException
 */
final public LoadStats loadData(final String[] resource, final String[] baseURL, final RDFFormat[] rdfFormat)
		throws IOException {

	if (resource.length != baseURL.length)
		throw new IllegalArgumentException();

	if (resource.length != rdfFormat.length)
		throw new IllegalArgumentException();

       if (log.isInfoEnabled())
           log.info("commit=" + commitEnum + ", closure=" + closureEnum
                   + ", resource=" + Arrays.toString(resource));

       final MyLoadStats totals = newLoadStats();

       for (int i = 0; i < resource.length; i++) {

           final boolean endOfBatch = i + 1 == resource.length;

           loadData2(//
                   totals,//
                   resource[i],//
                   baseURL[i],//
                   rdfFormat[i],//
                   endOfBatch//
                   );
           
       }

       doCommit(totals);
       
	if (log.isInfoEnabled())
		log.info("Loaded " + resource.length + " resources: " + totals);

       return totals;
       
   }
 
Example #13
Source File: TestBackupServlet.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Load the test data set.
 * 
 * @throws Exception
 */
private void doLoadFile() throws Exception {
       /*
	 * Only for testing. Clients should use AddOp(File, RDFFormat) or SPARQL
	 * UPDATE "LOAD".
	 */
       loadFile(
       		this.getClass().getResource("sample-data.ttl")
			.getFile(),
               RDFFormat.TURTLE);
}
 
Example #14
Source File: TestTicket2043b.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public void testBug() throws Exception {

		final BigdataSail sail = getSail();
		try {
			BigdataSailRepository repo = new BigdataSailRepository(sail);
			try {
				repo.initialize();
				final RepositoryConnection conn = repo.getConnection();
				conn.setAutoCommit(false);
				try {
					conn.add(getClass().getResourceAsStream("TestTicket2043.n3"), "",
							RDFFormat.TURTLE);
					conn.commit();
					// Check existing int literal 
					executeQuery(conn, "1", 2);
					// Check not existing int literal
					executeQuery(conn, "2", 0);
					// Check not existing plain literal
					executeQuery(conn, "\"3\"", 0);
					// Check not existing boolean literal
					executeQuery(conn, "true", 0);
					// Check not existing datetime literal
					executeQuery(conn, "\"2000-01-01T00:00:00Z\"^^xsd:dateTime", 0);
				} finally {
					conn.close();
				}
			} finally {
				repo.shutDown();
			}
		} finally {
			sail.__tearDownUnitTest();
		}
	}
 
Example #15
Source File: Utils.java    From blazegraph-samples with GNU General Public License v2.0 5 votes vote down vote up
public static void loadDataFromResources(Repository repo, String resource, String baseURL)
		throws OpenRDFException, IOException {

	RepositoryConnection cxn = repo.getConnection();
	
	try {
		cxn.begin();
		try {
			InputStream is = SampleBlazegraphCustomFunctionEmbedded.class.getClassLoader().getResourceAsStream(resource);
			if (is == null) {
				throw new IOException("Could not locate resource: " + resource);
			}
			Reader reader = new InputStreamReader(new BufferedInputStream(is));
			try {
				cxn.add(reader, baseURL, RDFFormat.N3);
			} finally {
				reader.close();
			}
			cxn.commit();
		} catch (OpenRDFException ex) {
			cxn.rollback();
			throw ex;
		}
	} finally {
		// close the repository connection
		cxn.close();
	}
}
 
Example #16
Source File: LinkedDataServletTest.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
@Test
public void get() throws IOException, Exception {

	for (String mime_type : MimeTypes.RDF_SERIALIZATIONS) {

		when(_request.getHeader(Parameters.ACCEPT)).thenReturn(mime_type);

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

			/*
			 * prepare mock ...
			 */
			File tmp = tmpFile();
			when(_response.getOutputStream()).thenReturn(new StubServletOutputStream(tmp));
			when(_request.getRequestURL()).thenReturn(new StringBuffer(URIS.get(i)));

			/*
			 * get data
			 */
			_ld_servlet.doGet(_request, _response);

			/*
			 * verify ...
			 */
			verify(_response, atLeastOnce()).setStatus(HttpServletResponse.SC_OK);
			assertTrue(parseAsList(new FileInputStream(tmp), RDFFormat.forMIMEType(mime_type)).size() > 0);
		}
	}
}
 
Example #17
Source File: RDFFileDataSet.java    From mustard with MIT License 5 votes vote down vote up
public void addDir(File dir, RDFFormat fileFormat) {
	for (File file : dir.listFiles(new RDFFileFilter(fileFormat))) {
		// System.out.println("Adding: " + file.getName());
		addFile(file, fileFormat);
		
	}
}
 
Example #18
Source File: RDFDataSetModule.java    From mustard with MIT License 5 votes vote down vote up
/**
 * RDFDataSet based on a single file
 * 
 * @param filename
 * @param mimetype
 */
public RDFDataSetModule(
		@In(name="filename") String filename, 
		@In(name="mimetype") String mimetype,
		@In(name="isUrl") Boolean isUrl) {
	super();
	this.filename = filename;
	this.format = RDFFormat.forMIMEType(mimetype);
	this.isUrl = isUrl;
}
 
Example #19
Source File: RDFFileDataSet.java    From mustard with MIT License 5 votes vote down vote up
public void addFile(String filename, RDFFormat fileFormat) {
	try {
		this.rdfRep.getConnection().add(new File(filename), null, fileFormat);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example #20
Source File: CustomSesameDataset.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
/**
 * Import RDF data from a string
 * 
 * @param rdfstring
 *            string with RDF data
 * @param format
 *            RDF format of the string (used to select parser)
 */
public void addString(String rdfstring, RDFFormat format) {
	try {
		RepositoryConnection con = currentRepository.getConnection();
		try {
			StringReader sr = new StringReader(rdfstring);
			con.add(sr, "", format);
		} finally {
			con.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example #21
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 #22
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
protected void loadDataset(String datasetFile)
	throws RDFParseException, RepositoryException, IOException
{
	logger.debug("loading dataset...");
	InputStream dataset = SPARQLUpdateTest.class.getResourceAsStream(datasetFile);
	try {
		con.add(dataset, "", RDFFormat.TRIG);
	}
	finally {
		dataset.close();
	}
	logger.debug("dataset loaded.");
}
 
Example #23
Source File: TestSparqlUpdate.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Load the test data set.
 * 
 * @throws Exception
 */
private void doLoadFile() throws Exception {
       /*
	 * Only for testing. Clients should use AddOp(File, RDFFormat) or SPARQL
	 * UPDATE "LOAD".
	 */
       loadFile(
               "src/test/java/com/bigdata/rdf/sail/webapp/dataset-update.trig",
               RDFFormat.TRIG);
}
 
Example #24
Source File: OntologyLoader.java    From anno4j with Apache License 2.0 5 votes vote down vote up
private RDFFormat forFileName(String path, RDFFormat fallback) {
	RDFFormat format = RDFFormat.forFileName(path);
	RDFParserRegistry registry = RDFParserRegistry.getInstance();
	if (format != null && registry.has(format))
		return format;
	return fallback;
}
 
Example #25
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testAddZipFile()
	throws Exception
{
	testCon.add(RepositoryConnectionTest.class.getResourceAsStream(TEST_DIR_PREFIX + "graphs.zip"), "",
			RDFFormat.TURTLE);
	assertTrue(NEWLY_ADDED, testCon.hasStatement(null, publisher, nameBob, false));
	assertTrue(NEWLY_ADDED, testCon.hasStatement(null, publisher, nameAlice, false));
	assertTrue("alice should be known in the store", testCon.hasStatement(null, name, nameAlice, false));
	assertTrue("bob should be known in the store", testCon.hasStatement(null, name, nameBob, false));
}
 
Example #26
Source File: OntologyLoader.java    From anno4j with Apache License 2.0 5 votes vote down vote up
private String getAcceptHeader() {
	StringBuilder sb = new StringBuilder();
	String preferred = RDFFormat.RDFXML.getDefaultMIMEType();
	sb.append(preferred).append(";q=0.2");
	Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
	for (RDFFormat format : rdfFormats) {
		for (String type : format.getMIMETypes()) {
			if (!preferred.equals(type)) {
				sb.append(", ").append(type);
			}
		}
	}
	return sb.toString();
}
 
Example #27
Source File: LoadPdb.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public static void loadSomeDataFromADocument(Repository repo, String documentPath) throws Exception {
	int counter = 0;
	File file = new File(documentPath);
	StatementCollector collector = new StatementCollector();
	InputStream in = new FileInputStream(file);
	try {
           final RDFParser parser = RDFParserRegistry.getInstance()
                   .get(RDFFormat.RDFXML).getParser();
		parser.setRDFHandler(collector);
		parser.parse(in, file.toURI().toString());
	} finally {
		in.close();
	}

	RepositoryConnection cxn = repo.getConnection();
	cxn.setAutoCommit(false);

	Statement stmt = null;
	try {
		for (Iterator<Statement> i = collector.getStatements().iterator(); i.hasNext();) {
			stmt = i.next();
			cxn.add(stmt);
			counter++;
		}
		LOG.info("Loaded " + counter + " triples");
		cxn.commit();
	} catch (Exception e) {
		LOG.error("error inserting statement: " + stmt, e);
		cxn.rollback();
	} finally {
		cxn.close();
	}

}
 
Example #28
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 #29
Source File: AbstractSailResource.java    From neo4j-sparql-extension with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns an instance of {@link org.openrdf.rio.RDFFormat} for a
 * given MIME-Type string.
 *
 * @param mimetype the MIME-Type as string
 * @return the corresponding RDF-Format
 */
protected RDFFormat getRDFFormat(String mimetype) {
	switch (mimetype) {
		default:
		case RDFMediaType.RDF_TURTLE:
			return RDFFormat.TURTLE;
		case RDFMediaType.RDF_XML:
			return RDFFormat.RDFXML;
		case RDFMediaType.RDF_NTRIPLES:
			return RDFFormat.NTRIPLES;
		case RDFMediaType.RDF_JSON:
			return RDFFormat.RDFJSON;
	}
}
 
Example #30
Source File: TestTicket1681.java    From database with GNU General Public License v2.0 5 votes vote down vote up
private void executeQuery(final SailRepository repo)
		throws RepositoryException, MalformedQueryException,
		QueryEvaluationException, RDFParseException, IOException {
	try {
		repo.initialize();
		final RepositoryConnection conn = repo.getConnection();
		conn.setAutoCommit(false);
		try {
			conn.add(getClass().getResourceAsStream("TestTicket1681.nt"), "",
					RDFFormat.TURTLE);
			conn.commit();

			final String query = "SELECT * WHERE { ?s <http://p>  ?o . FILTER (?o IN (<http://o2>, <http://o3>) ) }";
			final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL,
					query);
			final TupleQueryResult tqr = q.evaluate();
			int cnt = 0;
			while (tqr.hasNext()) {
			    final Set<String> bindingNames = tqr.next().getBindingNames();
			    cnt++;
			    if(log.isInfoEnabled())
			        log.info("bindingNames="+bindingNames);
			}
			tqr.close();
			assertEquals(1, cnt);
		} finally {
			conn.close();
		}
	} finally {
		repo.shutDown();
	}
}