Search in sources :

Example 31 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class SPARQLConnection method createDeleteDataCommand.

private String createDeleteDataCommand(Iterable<? extends Statement> statements, Resource... contexts) {
    StringBuilder qb = new StringBuilder();
    qb.append("DELETE DATA \n");
    qb.append("{ \n");
    if (contexts.length > 0) {
        for (Resource context : contexts) {
            if (context != null) {
                String namedGraph = context.stringValue();
                if (context instanceof BNode) {
                    // SPARQL does not allow blank nodes as named graph
                    // identifiers, so we need to skolemize
                    // the blank node id.
                    namedGraph = "urn:nodeid:" + context.stringValue();
                }
                qb.append("    GRAPH <" + namedGraph + "> { \n");
            }
            createDataBody(qb, statements, true);
            if (context != null) {
                qb.append(" } \n");
            }
        }
    } else {
        createDataBody(qb, statements, false);
    }
    qb.append("}");
    return qb.toString();
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) Resource(org.eclipse.rdf4j.model.Resource)

Example 32 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class SPARQLConnection method setBindings.

/* protected/private methods */
private void setBindings(Query query, Resource subj, IRI pred, Value obj, Resource... contexts) throws RepositoryException {
    if (subj != null) {
        query.setBinding("s", subj);
    }
    if (pred != null) {
        query.setBinding("p", pred);
    }
    if (obj != null) {
        query.setBinding("o", obj);
    }
    if (contexts != null && contexts.length > 0) {
        SimpleDataset dataset = new SimpleDataset();
        for (Resource ctx : contexts) {
            if (ctx == null || ctx instanceof IRI) {
                dataset.addDefaultGraph((IRI) ctx);
            } else {
                throw new RepositoryException("Contexts must be URIs");
            }
        }
        query.setDataset(dataset);
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Resource(org.eclipse.rdf4j.model.Resource) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset)

Example 33 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class SPARQLRepositoryConfig method export.

@Override
public Resource export(Model m) {
    Resource implNode = super.export(m);
    m.setNamespace("sparql", NAMESPACE);
    if (getQueryEndpointUrl() != null) {
        m.add(implNode, QUERY_ENDPOINT, vf.createIRI(getQueryEndpointUrl()));
    }
    if (getUpdateEndpointUrl() != null) {
        m.add(implNode, UPDATE_ENDPOINT, vf.createIRI(getUpdateEndpointUrl()));
    }
    return implNode;
}
Also used : Resource(org.eclipse.rdf4j.model.Resource)

Example 34 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class HTTPRepositoryConfig method export.

@Override
public Resource export(Model graph) {
    Resource implNode = super.export(graph);
    if (url != null) {
        graph.setNamespace("http", NAMESPACE);
        graph.add(implNode, REPOSITORYURL, SimpleValueFactory.getInstance().createIRI(url));
    }
    return implNode;
}
Also used : Resource(org.eclipse.rdf4j.model.Resource)

Example 35 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class RemoteRepositoryManager method addRepositoryConfig.

@Override
public void addRepositoryConfig(RepositoryConfig config) throws RepositoryException, RepositoryConfigException {
    try (RDF4JProtocolSession httpClient = getSesameClient().createRDF4JProtocolSession(serverURL)) {
        String baseURI = Protocol.getRepositoryLocation(serverURL, config.getID());
        Resource ctx = SimpleValueFactory.getInstance().createIRI(baseURI + "#" + config.getID());
        httpClient.setUsernameAndPassword(username, password);
        httpClient.setRepository(Protocol.getRepositoryLocation(serverURL, SystemRepository.ID));
        Model model = new LinkedHashModel();
        config.export(model, ctx);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Rio.write(model, baos, httpClient.getPreferredRDFFormat());
        removeRepository(config.getID());
        try (InputStream contents = new ByteArrayInputStream(baos.toByteArray())) {
            httpClient.upload(contents, baseURI, httpClient.getPreferredRDFFormat(), false, true, ctx);
        }
    } catch (IOException | QueryEvaluationException | UnauthorizedException ue) {
        throw new RepositoryException(ue);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Resource(org.eclipse.rdf4j.model.Resource) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) UnauthorizedException(org.eclipse.rdf4j.http.protocol.UnauthorizedException) RDF4JProtocolSession(org.eclipse.rdf4j.http.client.RDF4JProtocolSession) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel)

Aggregations

Resource (org.eclipse.rdf4j.model.Resource)90 IRI (org.eclipse.rdf4j.model.IRI)37 Value (org.eclipse.rdf4j.model.Value)30 Test (org.junit.Test)16 Statement (org.eclipse.rdf4j.model.Statement)15 Model (org.eclipse.rdf4j.model.Model)12 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)12 BNode (org.eclipse.rdf4j.model.BNode)11 IOException (java.io.IOException)9 Literal (org.eclipse.rdf4j.model.Literal)9 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)7 StringWriter (java.io.StringWriter)6 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 TreeModel (org.eclipse.rdf4j.model.impl.TreeModel)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)6 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)5 ArrayList (java.util.ArrayList)4