Search in sources :

Example 66 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.

the class SesameYard method getRepresentation.

@Override
public Representation getRepresentation(String id) throws YardException {
    if (id == null) {
        throw new IllegalArgumentException("The parsed representation id MUST NOT be NULL!");
    }
    if (id.isEmpty()) {
        throw new IllegalArgumentException("The parsed representation id MUST NOT be EMTPY!");
    }
    RepositoryConnection con = null;
    try {
        con = repository.getConnection();
        con.begin();
        Representation rep = getRepresentation(con, sesameFactory.createURI(id), true);
        con.commit();
        return rep;
    } catch (RepositoryException e) {
        throw new YardException("Unable to get Representation " + id, e);
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RepositoryException(org.openrdf.repository.RepositoryException)

Example 67 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.

the class SesameYard method find.

@Override
public final QueryResultList<Representation> find(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
    if (parsedQuery == null) {
        throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
    }
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    RepositoryConnection con = null;
    TupleQueryResult results = null;
    try {
        con = repository.getConnection();
        con.begin();
        // execute the query
        int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
        results = executeSparqlFieldQuery(con, query, limit, true);
        // parse the results and generate the Representations
        // create an own valueFactors so that all the data of the query results
        // are added to the same Sesame Model
        Model model = new TreeModel();
        RdfValueFactory valueFactory = new RdfValueFactory(model, sesameFactory);
        List<Representation> representations = limit > 0 ? new ArrayList<Representation>(limit) : new ArrayList<Representation>();
        Map<String, URI> bindings = new HashMap<String, URI>(query.getFieldVariableMappings().size());
        for (Entry<String, String> mapping : query.getFieldVariableMappings().entrySet()) {
            bindings.put(mapping.getValue(), sesameFactory.createURI(mapping.getKey()));
        }
        while (results.hasNext()) {
            BindingSet result = results.next();
            Value value = result.getValue(query.getRootVariableName());
            if (value instanceof URI) {
                URI subject = (URI) value;
                // link the result with the query result
                model.add(queryRoot, queryResult, subject);
                // now copy over the other selected data
                for (String binding : result.getBindingNames()) {
                    URI property = bindings.get(binding);
                    if (property != null) {
                        model.add(subject, property, result.getValue(binding));
                    }
                // else no mapping for the query.getRootVariableName()
                }
                // create a representation and add it to the results
                representations.add(valueFactory.createRdfRepresentation(subject));
            }
        // ignore non URI results
        }
        con.commit();
        return new SesameQueryResultList(model, query, representations);
    } catch (RepositoryException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } catch (QueryEvaluationException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } finally {
        if (results != null) {
            // close the result if present
            try {
                results.close();
            } catch (QueryEvaluationException ignore) {
            /* ignore */
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            /* ignore */
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) BindingSet(org.openrdf.query.BindingSet) HashMap(java.util.HashMap) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RepositoryException(org.openrdf.repository.RepositoryException) URI(org.openrdf.model.URI) TreeModel(org.openrdf.model.impl.TreeModel) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) Model(org.openrdf.model.Model) TreeModel(org.openrdf.model.impl.TreeModel) Value(org.openrdf.model.Value) TupleQueryResult(org.openrdf.query.TupleQueryResult) RdfValueFactory(org.apache.stanbol.entityhub.model.sesame.RdfValueFactory)

Example 68 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.

the class SesameYardTest method testBNodeSupport.

@Test
public void testBNodeSupport() throws YardException, RepositoryException {
    RepositoryConnection con = repo.getConnection();
    org.openrdf.model.ValueFactory sesameFactory = con.getValueFactory();
    URI subject = sesameFactory.createURI("urn:test.sesameyard:bnodesupport.subject");
    URI property = sesameFactory.createURI("urn:test.sesameyard:bnodesupport.property");
    URI value = sesameFactory.createURI("urn:test.sesameyard:bnodesupport.value");
    URI property2 = sesameFactory.createURI("urn:test.sesameyard:bnodesupport.property2");
    URI loop1 = sesameFactory.createURI("urn:test.sesameyard:bnodesupport.loop1");
    URI loop2 = sesameFactory.createURI("urn:test.sesameyard:bnodesupport.loop2");
    BNode bnode1 = sesameFactory.createBNode();
    BNode bnode2 = sesameFactory.createBNode();
    con.add(subject, property, bnode1);
    con.add(bnode1, property2, value);
    con.add(bnode1, loop1, bnode2);
    con.add(bnode2, loop2, bnode1);
    con.commit();
    con.close();
    Yard yard = getYard();
    Representation rep = yard.getRepresentation(subject.stringValue());
    Assert.assertTrue(rep instanceof RdfRepresentation);
    Model model = ((RdfRepresentation) rep).getModel();
    // Assert for the indirect statements to be present in the model
    Assert.assertFalse(model.filter(null, property2, null).isEmpty());
    Assert.assertFalse(model.filter(null, loop1, null).isEmpty());
    Assert.assertFalse(model.filter(null, loop2, null).isEmpty());
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) BNode(org.openrdf.model.BNode) SesameYard(org.apache.stanbol.entityhub.yard.sesame.SesameYard) Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Model(org.openrdf.model.Model) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) URI(org.openrdf.model.URI) Test(org.junit.Test) YardTest(org.apache.stanbol.entityhub.test.yard.YardTest)

Example 69 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project qi4j-sdk by Qi4j.

the class MemoryRepositoryTest method testMemoryRepository.

@Test
public void testMemoryRepository() throws RepositoryException {
    module.injectTo(this);
    RepositoryConnection conn = repository.getConnection();
    Assert.assertThat("repository is open", conn.isOpen(), CoreMatchers.equalTo(true));
    conn.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 70 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project qi4j-sdk by Qi4j.

the class NativeRepositoryTest method testNativeRepository.

@Test
public void testNativeRepository() throws RepositoryException {
    module.injectTo(this);
    RepositoryConnection conn = repository.getConnection();
    Assert.assertThat("repository is open", conn.isOpen(), CoreMatchers.equalTo(true));
    conn.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Aggregations

RepositoryConnection (org.openrdf.repository.RepositoryConnection)71 TupleQuery (org.openrdf.query.TupleQuery)27 URI (org.openrdf.model.URI)24 RepositoryException (org.openrdf.repository.RepositoryException)20 Test (org.junit.Test)15 Statement (org.openrdf.model.Statement)11 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)10 Literal (org.openrdf.model.Literal)9 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)6 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)6 StatementImpl (org.openrdf.model.impl.StatementImpl)6 Update (org.openrdf.query.Update)6 InputStream (java.io.InputStream)5 Value (org.openrdf.model.Value)5 Repository (org.openrdf.repository.Repository)5 SailRepository (org.openrdf.repository.sail.SailRepository)5 RyaSailRepository (org.apache.rya.rdftriplestore.RyaSailRepository)4 Resource (org.openrdf.model.Resource)4 InputStreamReader (java.io.InputStreamReader)3 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)3