Search in sources :

Example 51 with Statement

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

the class SPARQLConnection method getStatementsQuadMode.

private RepositoryResult<Statement> getStatementsQuadMode(Resource subj, IRI pred, Value obj, boolean includeInferred, Resource... contexts) throws MalformedQueryException, RepositoryException, QueryEvaluationException {
    TupleQueryResult qRes = null;
    RepositoryResult<Statement> result = null;
    boolean allGood = false;
    try {
        TupleQuery tupleQuery = prepareTupleQuery(SPARQL, EVERYTHING_WITH_GRAPH);
        setBindings(tupleQuery, subj, pred, obj, contexts);
        tupleQuery.setIncludeInferred(includeInferred);
        qRes = tupleQuery.evaluate();
        result = new RepositoryResult<Statement>(new ExceptionConvertingIteration<Statement, RepositoryException>(toStatementIteration(qRes, subj, pred, obj)) {

            @Override
            protected RepositoryException convert(Exception e) {
                return new RepositoryException(e);
            }
        });
        allGood = true;
        return result;
    } finally {
        if (!allGood) {
            try {
                if (result != null) {
                    result.close();
                }
            } finally {
                if (qRes != null) {
                    qRes.close();
                }
            }
        }
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) ExceptionConvertingIteration(org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) SPARQLTupleQuery(org.eclipse.rdf4j.repository.sparql.query.SPARQLTupleQuery) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) UpdateExecutionException(org.eclipse.rdf4j.query.UpdateExecutionException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) UnknownTransactionStateException(org.eclipse.rdf4j.repository.UnknownTransactionStateException) UnsupportedQueryLanguageException(org.eclipse.rdf4j.query.UnsupportedQueryLanguageException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException)

Example 52 with Statement

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

the class SPARQLConnection method getStatementsSingleTriple.

private RepositoryResult<Statement> getStatementsSingleTriple(Resource subj, IRI pred, Value obj, boolean includeInferred, Resource... contexts) throws RepositoryException {
    if (hasStatement(subj, pred, obj, includeInferred, contexts)) {
        Statement st = getValueFactory().createStatement(subj, pred, obj);
        CloseableIteration<Statement, RepositoryException> cursor;
        cursor = new SingletonIteration<Statement, RepositoryException>(st);
        return new RepositoryResult<Statement>(cursor);
    } else {
        return new RepositoryResult<Statement>(new EmptyIteration<Statement, RepositoryException>());
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) RepositoryResult(org.eclipse.rdf4j.repository.RepositoryResult)

Example 53 with Statement

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

the class RepositoryConfigUtil method getRepositoryIDs.

@Deprecated
public static Set<String> getRepositoryIDs(Repository repository) throws RepositoryException {
    RepositoryConnection con = repository.getConnection();
    try {
        Set<String> idSet = new LinkedHashSet<String>();
        RepositoryResult<Statement> idStatementIter = con.getStatements(null, REPOSITORYID, null, true);
        try {
            while (idStatementIter.hasNext()) {
                Statement idStatement = idStatementIter.next();
                if (idStatement.getObject() instanceof Literal) {
                    Literal idLiteral = (Literal) idStatement.getObject();
                    idSet.add(idLiteral.getLabel());
                }
            }
        } finally {
            idStatementIter.close();
        }
        return idSet;
    } finally {
        con.close();
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) LinkedHashSet(java.util.LinkedHashSet) Statement(org.eclipse.rdf4j.model.Statement) Literal(org.eclipse.rdf4j.model.Literal)

Example 54 with Statement

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

the class RepositoryConfigUtil method getIDStatement.

private static Statement getIDStatement(RepositoryConnection con, String repositoryID) throws RepositoryException, RepositoryConfigException {
    Literal idLiteral = con.getRepository().getValueFactory().createLiteral(repositoryID);
    List<Statement> idStatementList = Iterations.asList(con.getStatements(null, REPOSITORYID, idLiteral, true));
    if (idStatementList.size() == 1) {
        return idStatementList.get(0);
    } else if (idStatementList.isEmpty()) {
        return null;
    } else {
        throw new RepositoryConfigException("Multiple ID-statements for repository ID " + repositoryID);
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) Literal(org.eclipse.rdf4j.model.Literal)

Example 55 with Statement

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

the class RepositoryConfigUtil method getRepositoryConfig.

@Deprecated
public static RepositoryConfig getRepositoryConfig(Repository repository, String repositoryID) throws RepositoryConfigException, RepositoryException {
    RepositoryConnection con = repository.getConnection();
    try {
        Statement idStatement = getIDStatement(con, repositoryID);
        if (idStatement == null) {
            // No such config
            return null;
        }
        Resource repositoryNode = idStatement.getSubject();
        Resource context = idStatement.getContext();
        if (context == null) {
            throw new RepositoryException("No configuration context for repository " + repositoryID);
        }
        Model contextGraph = QueryResults.asModel(con.getStatements(null, null, null, true, context));
        return RepositoryConfig.create(contextGraph, repositoryNode);
    } finally {
        con.close();
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Model(org.eclipse.rdf4j.model.Model) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Aggregations

Statement (org.eclipse.rdf4j.model.Statement)74 IRI (org.eclipse.rdf4j.model.IRI)17 Test (org.junit.Test)17 Model (org.eclipse.rdf4j.model.Model)16 Literal (org.eclipse.rdf4j.model.Literal)15 Resource (org.eclipse.rdf4j.model.Resource)15 Value (org.eclipse.rdf4j.model.Value)13 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)10 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)8 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)8 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)8 StringWriter (java.io.StringWriter)7 BNode (org.eclipse.rdf4j.model.BNode)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)6 IOException (java.io.IOException)5