Java Code Examples for org.eclipse.rdf4j.repository.RepositoryConnection#clear()

The following examples show how to use org.eclipse.rdf4j.repository.RepositoryConnection#clear() . 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: SPARQLServiceEvaluationTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Load a dataset. Note: the repositories are cleared before loading data
 *
 * @param rep
 * @param datasetFile
 * @throws RDFParseException
 * @throws RepositoryException
 * @throws IOException
 */
protected void loadDataSet(Repository rep, String datasetFile)
		throws RDFParseException, RepositoryException, IOException {
	logger.debug("loading dataset...");
	InputStream dataset = SPARQLServiceEvaluationTest.class.getResourceAsStream(datasetFile);

	if (dataset == null) {
		throw new IllegalArgumentException("Datasetfile " + datasetFile + " not found.");
	}

	RepositoryConnection con = rep.getConnection();
	try {
		con.clear();
		con.add(dataset, "",
				Rio.getParserFormatForFileName(datasetFile).orElseThrow(Rio.unsupportedFormat(datasetFile)));
	} finally {
		dataset.close();
		con.close();
	}
	logger.debug("dataset loaded.");
}
 
Example 2
Source File: SeRQLQueryTestCase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected Repository createRepository(String entailment) throws Exception {
	Repository dataRep;
	if ("RDF".equals(entailment)) {
		dataRep = newRepository();
	} else {
		dataRep = newRepository(entailment);
	}
	dataRep.initialize();
	RepositoryConnection con = dataRep.getConnection();
	try {
		con.clear();
		con.clearNamespaces();
	} finally {
		con.close();
	}
	return dataRep;
}
 
Example 3
Source File: SPARQLServerBaseTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Load a dataset. Note: the repositories are cleared before loading data
 *
 * @param rep
 * @param datasetFile
 * @throws RDFParseException
 * @throws RepositoryException
 * @throws IOException
 */
protected void loadDataSet(Repository rep, String datasetFile)
		throws RDFParseException, RepositoryException, IOException {
	log.debug("loading dataset...");
	InputStream dataset = SPARQLServerBaseTest.class.getResourceAsStream(datasetFile);

	boolean needToShutdown = false;
	if (!rep.isInitialized()) {
		rep.init();
		needToShutdown = true;
	}
	RepositoryConnection con = rep.getConnection();
	try {
		con.clear();
		con.add(dataset, "", Rio.getParserFormatForFileName(datasetFile).get());
	} finally {
		dataset.close();
		con.close();
		if (needToShutdown) {
			rep.shutDown();
		}
	}
	log.debug("dataset loaded.");
}
 
Example 4
Source File: AbstractGenericLuceneTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testIndexWriterState() throws Exception {
	final String brokenTrig = "{ broken }";
	RepositoryConnection conn = repository.getConnection();
	try (StringReader sr = new StringReader(brokenTrig)) {
		conn.add(sr, "http://example.org/", RDFFormat.TRIG);
	} catch (Exception e) {
		// expected parse exception
		LOG.debug("Parse exception: {}", e.getMessage());
	}
	conn.close();
	conn = repository.getConnection();
	conn.clear(); // make sure this can be executed multiple times
	conn.add(FOAF.PERSON, RDFS.LABEL, SimpleValueFactory.getInstance().createLiteral("abc"));
	conn.close();
}
 
Example 5
Source File: RepositoryConfigUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Update the specified RepositoryConnection with the specified set of RepositoryConfigs. This will overwrite all
 * existing configurations in the Repository that have a Repository ID occurring in these RepositoryConfigs. Note:
 * this method does NOT commit the updates on the connection.
 *
 * @param con     the repository connection to perform the update on
 * @param configs The RepositoryConfigs that should be added to or updated in the Repository. The RepositoryConfig's
 *                ID may already occur in the Repository, in which case all previous configuration data for that
 *                Repository will be cleared before the RepositoryConfig is added.
 * @throws RepositoryException
 * @throws RepositoryConfigException
 */
@Deprecated
public static void updateRepositoryConfigs(RepositoryConnection con, RepositoryConfig... configs)
		throws RepositoryException, RepositoryConfigException {
	ValueFactory vf = con.getRepository().getValueFactory();

	con.begin();

	for (RepositoryConfig config : configs) {
		Resource context = getContext(con, config.getID());

		if (context != null) {
			con.clear(context);
		} else {
			context = vf.createBNode();
		}

		con.add(context, RDF.TYPE, REPOSITORY_CONTEXT);

		Model graph = new LinkedHashModel();
		config.export(graph);
		con.add(graph, context);
	}

	con.commit();
}
 
Example 6
Source File: SPARQLEmbeddedServer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void stop() throws Exception {
	Repository systemRepo = new HTTPRepository(Protocol.getRepositoryLocation(getServerUrl(), SystemRepository.ID));
	RepositoryConnection con = systemRepo.getConnection();
	try {
		con.clear();
	} finally {
		con.close();
		systemRepo.shutDown();
	}

	jetty.stop();
	System.clearProperty("org.mortbay.log.class");
}
 
Example 7
Source File: SPARQLQueryTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected final Repository createRepository() throws Exception {
	Repository repo = newRepository();
	repo.initialize();
	RepositoryConnection con = repo.getConnection();
	try {
		con.clear();
		con.clearNamespaces();
	} finally {
		con.close();
	}
	return repo;
}
 
Example 8
Source File: SPARQLUpdateTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates, initializes and clears a repository.
 *
 * @return an initialized empty repository.
 * @throws Exception
 */
protected Repository createRepository() throws Exception {
	Repository repository = newRepository();
	repository.initialize();
	RepositoryConnection con = repository.getConnection();
	con.clear();
	con.clearNamespaces();
	con.close();
	return repository;
}
 
Example 9
Source File: RemoveServlet.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void remove(RepositoryConnection con, Resource subj, IRI pred, Value obj, WorkbenchRequest req)
		throws BadRequestException, RepositoryException {
	if (req.isParameterPresent(CONTEXT)) {
		Resource ctx = req.getResource(CONTEXT);
		if (subj == null && pred == null && obj == null) {
			con.clear(ctx);
		} else {
			con.remove(subj, pred, obj, ctx);
		}
	} else {
		con.remove(subj, pred, obj);
	}
}
 
Example 10
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 5 votes vote down vote up
public void testClearAllGraph() throws Exception {
    RepositoryConnection conn = repository.getConnection();

    String insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "PREFIX ex: <http://example/addresses#>\n" +
            "INSERT DATA\n" +
            "{ GRAPH ex:G1 {\n" +
            "<http://example/book3> dc:title    \"A new book\" ;\n" +
            "                         dc:creator  \"A.N.Other\" .\n" +
            "}\n" +
            "}";
    Update update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();

    insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "PREFIX ex: <http://example/addresses#>\n" +
            "INSERT DATA\n" +
            "{ GRAPH ex:G2 {\n" +
            "<http://example/book3> dc:title    \"A new book\" ;\n" +
            "                         dc:creator  \"A.N.Other\" .\n" +
            "}\n" +
            "}";
    update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();

    String query = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "select * where { <http://example/book3> ?p ?o. }";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(4, tupleHandler.getCount());

    tupleHandler = new CountTupleHandler();
    conn.clear();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(0, tupleHandler.getCount());

    conn.close();
}
 
Example 11
Source File: CustomGraphQueryInferencerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected void runTest(final CustomGraphQueryInferencer inferencer) throws RepositoryException, RDFParseException,
		IOException, MalformedQueryException, UpdateExecutionException {
	// Initialize
	Repository sail = new SailRepository(inferencer);
	sail.initialize();
	RepositoryConnection connection = sail.getConnection();
	try {
		connection.begin();
		connection.clear();
		connection.add(new StringReader(initial), BASE, RDFFormat.TURTLE);

		// Test initial inferencer state
		Collection<Value> watchPredicates = inferencer.getWatchPredicates();
		assertThat(watchPredicates).hasSize(testData.predCount);
		Collection<Value> watchObjects = inferencer.getWatchObjects();
		assertThat(watchObjects).hasSize(testData.objCount);
		Collection<Value> watchSubjects = inferencer.getWatchSubjects();
		assertThat(watchSubjects).hasSize(testData.subjCount);
		ValueFactory factory = connection.getValueFactory();
		if (resourceFolder.startsWith(PREDICATE)) {
			assertThat(watchPredicates.contains(factory.createIRI(BASE, "brotherOf"))).isTrue();
			assertThat(watchPredicates.contains(factory.createIRI(BASE, "parentOf"))).isTrue();
		} else {
			IRI bob = factory.createIRI(BASE, "Bob");
			IRI alice = factory.createIRI(BASE, "Alice");
			assertThat(watchSubjects).contains(bob, alice);
			assertThat(watchObjects).contains(bob, alice);
		}

		// Test initial inferencing results
		assertThat(Iterations.asSet(connection.getStatements(null, null, null, true)))
				.hasSize(testData.initialCount);

		// Test results after removing some statements
		connection.prepareUpdate(QueryLanguage.SPARQL, delete).execute();
		assertThat(Iterations.asSet(connection.getStatements(null, null, null, true)))
				.hasSize(testData.countAfterRemove);

		// Tidy up. Storage gets re-used for subsequent tests, so must clear here,
		// in order to properly clear out any inferred statements.
		connection.clear();
		connection.commit();
	} finally {
		connection.close();
	}
	sail.shutDown();
}
 
Example 12
Source File: ClearOperation.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void execute(RepositoryConnection con) throws RepositoryException {
	con.clear(getContexts());
}
 
Example 13
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 4 votes vote down vote up
public void testClearGraph() throws Exception {
    RepositoryConnection conn = repository.getConnection();

    String insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "PREFIX ex: <http://example/addresses#>\n" +
            "INSERT DATA\n" +
            "{ GRAPH ex:G1 {\n" +
            "<http://example/book3> dc:title    \"A new book\" ;\n" +
            "                         dc:creator  \"A.N.Other\" .\n" +
            "}\n" +
            "}";
    Update update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();

    insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "PREFIX ex: <http://example/addresses#>\n" +
            "INSERT DATA\n" +
            "{ GRAPH ex:G2 {\n" +
            "<http://example/book3> dc:title    \"A new book\" ;\n" +
            "                         dc:creator  \"A.N.Other\" .\n" +
            "}\n" +
            "}";
    update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();

    String query = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "select * where { <http://example/book3> ?p ?o. }";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(4, tupleHandler.getCount());

    tupleHandler = new CountTupleHandler();
    conn.clear(VF.createIRI("http://example/addresses#G2"));
    tupleQuery.evaluate(tupleHandler);
    assertEquals(2, tupleHandler.getCount());

    tupleHandler = new CountTupleHandler();
    conn.clear(VF.createIRI("http://example/addresses#G1"));
    tupleQuery.evaluate(tupleHandler);
    assertEquals(0, tupleHandler.getCount());

    conn.close();
}