Java Code Examples for org.openrdf.rio.RDFFormat

The following examples show how to use org.openrdf.rio.RDFFormat. These examples are extracted from open source projects. 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 Project: cumulusrdf   Source File: QuadsTest.java    License: 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 2
Source Project: GeoTriples   Source File: CustomSesameDataset.java    License: 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 Project: anno4j   Source File: ObjectParserTest.java    License: 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 Project: anno4j   Source File: ObjectParserTest.java    License: 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 5
Source Project: anno4j   Source File: LiteralTest.java    License: 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 6
Source Project: ldp4j   Source File: RDFModelFormater.java    License: 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 7
Source Project: database   Source File: LoadPdb.java    License: 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 8
Source Project: database   Source File: SPARQLUpdateTestv2.java    License: 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 9
Source Project: cumulusrdf   Source File: LargeModifyStoreTest.java    License: 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 10
Source Project: database   Source File: LocalGOMTestCase.java    License: 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 11
/**
 * 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 12
private static RDFFormat guessFormatFromName(String name) {
    if (name.endsWith(".ttl")) {
        return RDFFormat.TURTLE;
    } else if (name.endsWith(".xml")) {
        return RDFFormat.RDFXML;
    }
    return RDFFormat.RDFXML;
}
 
Example 13
public static void loadDataFromResource(final RemoteRepositoryManager repo,
		final String namespace, final String resource) throws Exception {
	final InputStream is = SampleBlazegraphCustomFunctionRemote.class
			.getResourceAsStream(resource);
	if (is == null) {
		throw new IOException("Could not locate resource: " + resource);
	}
	try {
		repo.getRepositoryForNamespace(namespace).add(
				new RemoteRepository.AddOp(is, RDFFormat.N3));
	} finally {
		is.close();
	}
}
 
Example 14
Source Project: mustard   Source File: RDFURLDataSet.java    License: MIT License 5 votes vote down vote up
public void addFile(String url, RDFFormat fileFormat) {
	try {
		File tempFile = File.createTempFile("ducktape_data", "temp");
		
		org.apache.commons.io.FileUtils.copyURLToFile(new URL(url), tempFile);	
		
		this.rdfRep.getConnection().add(tempFile, null, fileFormat);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 15
public static void loadDataFromResources(final Repository repo, final String resource, final String baseURL)
		throws OpenRDFException, IOException {

	final RepositoryConnection cxn = repo.getConnection();
	
	try {
		cxn.begin();
		try {
			final InputStream is = SampleBlazegraphSesameEmbedded.class.getResourceAsStream(resource);
			if (is == null) {
				throw new IOException("Could not locate resource: " + resource);
			}
			final 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
private static void loadDataFromResource(final RemoteRepositoryManager repo,
		final String namespace, final String resource) throws Exception {
	final InputStream is = SampleBlazegraphSesameRemote.class
			.getResourceAsStream(resource);
	if (is == null) {
		throw new IOException("Could not locate resource: " + resource);
	}
	try {
		repo.getRepositoryForNamespace(namespace).add(
				new RemoteRepository.AddOp(is, RDFFormat.N3));
	} finally {
		is.close();
	}
}
 
Example 17
Source Project: blazegraph-samples   Source File: Utils.java    License: 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 18
Source Project: mustard   Source File: RDFFileDataSet.java    License: 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 19
Source Project: database   Source File: TestTicket2043b.java    License: 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 20
Source Project: database   Source File: TestFederatedQuery.java    License: 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 21
Source Project: database   Source File: TestTicket2043.java    License: 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 22
Source Project: GeoTriples   Source File: CustomSesameDataset.java    License: 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 23
Source Project: database   Source File: DataLoader.java    License: 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 24
Source Project: database   Source File: SPARQLUpdateTest.java    License: 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 25
Source Project: database   Source File: TestSparqlUpdate.java    License: 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 26
Source Project: anno4j   Source File: OntologyLoader.java    License: 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 27
Source Project: anno4j   Source File: OntologyLoader.java    License: 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 28
Source Project: database   Source File: LoadPdb.java    License: 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 29
Source Project: cumulusrdf   Source File: Util.java    License: 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 30
/**
 * 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;
	}
}