Java Code Examples for org.openrdf.repository.RepositoryConnection#begin()

The following examples show how to use org.openrdf.repository.RepositoryConnection#begin() . 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: TripleStoreBlazegraph.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void read(InputStream is, String base, String name) {
    RepositoryConnection cnx = null;
    try {
        cnx = repo.getConnection();
        cnx.begin();
        Resource context = context(cnx, name);
        read(is, base, name, cnx, context);
        cnx.commit();
        addNamespaceForBase(cnx, base);
    } catch (RepositoryException x) {
        throw new TripleStoreException(String.format("Reading. Repo problem %s %s", name, base), x);
    } finally {
        closeConnection(cnx, "Reading");
    }
}
 
Example 2
Source File: TripleStoreBlazegraph.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
private void copyFrom(RepositoryConnection sourceConn) {
    RepositoryConnection targetConn = null;
    try {
        targetConn = repo.getConnection();
        targetConn.begin();
        copyNamespaces(sourceConn, targetConn);
        copyStatements(sourceConn, targetConn);
        targetConn.commit();
    } catch (RepositoryException e) {
        LOG.error("Copying from source : {}", e.getMessage());
    } finally {
        if (targetConn != null) {
            closeConnection(targetConn, "Copying from source");
        }
    }
}
 
Example 3
Source File: TripleStoreBlazegraph.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
private void addStatements(RepositoryConnection cnx, String contextName, String objNs, String objType,
    PropertyBags statements)
    throws RepositoryException {

    cnx.begin();
    for (PropertyBag statement : statements) {
        createStatements(cnx, objNs, objType, statement, context(cnx, contextName));
    }
    cnx.commit();
}
 
Example 4
Source File: TripleStoreBlazegraph.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
private String addStatement(RepositoryConnection cnx, String contextName, String objNs, String objType,
    PropertyBag properties) throws RepositoryException {
    cnx.begin();
    String id = createStatements(cnx, objNs, objType, properties, context(cnx, contextName));
    cnx.commit();
    return id;
}
 
Example 5
Source File: SampleBlazegraphSesameEmbedded.java    From blazegraph-samples with GNU General Public License v2.0 5 votes vote down vote up
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 6
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 7
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 8
Source File: HelloBlazegraph.java    From blazegraph-samples with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws OpenRDFException {

		final Properties props = new Properties();
		
		/*
		 * For more configuration parameters see
		 * http://www.blazegraph.com/docs/api/index.html?com/bigdata/journal/BufferMode.html
		 */
		props.put(Options.BUFFER_MODE, BufferMode.DiskRW); // persistent file system located journal
		props.put(Options.FILE, "/tmp/blazegraph/test.jnl"); // journal file location

		final BigdataSail sail = new BigdataSail(props); // instantiate a sail
		final Repository repo = new BigdataSailRepository(sail); // create a Sesame repository

		repo.initialize();

		try {
			// prepare a statement
			final URIImpl subject = new URIImpl("http://blazegraph.com/Blazegraph");
			final URIImpl predicate = new URIImpl("http://blazegraph.com/says");
			final Literal object = new LiteralImpl("hello");
			final Statement stmt = new StatementImpl(subject, predicate, object);

			// open repository connection
			RepositoryConnection cxn = repo.getConnection();

			// upload data to repository
			try {
				cxn.begin();
				cxn.add(stmt);
				cxn.commit();
			} catch (OpenRDFException ex) {
				cxn.rollback();
				throw ex;
			} finally {
				// close the repository connection
				cxn.close();
			}

			// open connection
			if (repo instanceof BigdataSailRepository) {
				cxn = ((BigdataSailRepository) repo).getReadOnlyConnection();
			} else {
				cxn = repo.getConnection();
			}

			// evaluate sparql query
			try {

				final TupleQuery tupleQuery = cxn
						.prepareTupleQuery(QueryLanguage.SPARQL,
								"select ?p ?o where { <http://blazegraph.com/Blazegraph> ?p ?o . }");
				final TupleQueryResult result = tupleQuery.evaluate();
				try {
					while (result.hasNext()) {
						final BindingSet bindingSet = result.next();
						System.err.println(bindingSet);
					}
				} finally {
					result.close();
				}

			} finally {
				// close the repository connection
				cxn.close();
			}

		} finally {
			repo.shutDown();
		}
	}
 
Example 9
Source File: SPARQLUpdateConformanceTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Override
	protected void runTest()
		throws Exception
	{
		RepositoryConnection con = dataRep.getConnection();
		RepositoryConnection erCon = expectedResultRepo.getConnection();
		try {
			String updateString = readUpdateString();

			con.begin();
//			con.setReadContexts((URI)null);
			
			Update update = con.prepareUpdate(QueryLanguage.SPARQL, updateString, requestFileURL);
			if (this.dataset != null) {
				update.setDataset(this.dataset);
			}
			update.execute();

			con.commit();
			
			// check default graph
			logger.info("checking default graph");
			compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, (Resource)null)),
					Iterations.asList(erCon.getStatements(null, null, null, true, (Resource)null)));

			for (String namedGraph : inputNamedGraphs.keySet()) {
				logger.info("checking named graph {}", namedGraph);
				URI contextURI = con.getValueFactory().createURI(namedGraph.replaceAll("\"", ""));
				compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, contextURI)),
						Iterations.asList(erCon.getStatements(null, null, null, true, contextURI)));
			}
		}
		catch(Exception e) {
			if(con.isActive()) {
				con.rollback();
			}
			throw e;
		}
		finally {
			con.close();
			erCon.close();
		}
	}