Search in sources :

Example 1 with RdfRepresentation

use of org.apache.stanbol.entityhub.model.sesame.RdfRepresentation in project stanbol by apache.

the class SesameYard method createRepresentationGraph.

/**
     * Extracts the triples that belong to the {@link Representation} with the
     * parsed id from the Sesame repository.
     * @param con the repository connection
     * @param valueFactory the {@link RdfValueFactory} to use
     * @param uri the subject of the Representation to extract
     * @return the representation with the extracted data.
     * @throws RepositoryException 
     */
protected RdfRepresentation createRepresentationGraph(RepositoryConnection con, RdfValueFactory valueFactory, URI uri) throws RepositoryException {
    RdfRepresentation rep = valueFactory.createRdfRepresentation(uri);
    Model model = rep.getModel();
    extractRepresentation(con, model, uri, new HashSet<BNode>());
    return rep;
}
Also used : BNode(org.openrdf.model.BNode) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Model(org.openrdf.model.Model) TreeModel(org.openrdf.model.impl.TreeModel)

Example 2 with RdfRepresentation

use of org.apache.stanbol.entityhub.model.sesame.RdfRepresentation in project stanbol by apache.

the class SesameYard method findRepresentation.

@Override
public QueryResultList<Representation> findRepresentation(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, false);
        //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>();
        while (results.hasNext()) {
            BindingSet result = results.next();
            Value value = result.getValue(query.getRootVariableName());
            if (value instanceof URI) {
                //copy all data to the model and create the representation
                RdfRepresentation rep = createRepresentationGraph(con, valueFactory, (URI) value);
                //link the result with the query result
                model.add(queryRoot, queryResult, value);
                representations.add(rep);
            }
        //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) 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) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) 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 3 with RdfRepresentation

use of org.apache.stanbol.entityhub.model.sesame.RdfRepresentation in project stanbol by apache.

the class SesameYard method store.

protected final Representation store(RepositoryConnection con, Representation representation, boolean allowCreate, boolean canNotCreateIsError) throws IllegalArgumentException, RepositoryException {
    if (representation == null) {
        return null;
    }
    log.debug("store Representation " + representation.getId());
    URI subject = sesameFactory.createURI(representation.getId());
    boolean contains = con.hasStatement(subject, null, null, includeInferred, contexts);
    con.remove(subject, null, null, contexts);
    if (!contains && !allowCreate) {
        if (canNotCreateIsError) {
            throw new IllegalArgumentException("Parsed Representation " + representation.getId() + " in not managed by this Yard " + getName() + "(id=" + getId() + ")");
        } else {
            return null;
        }
    }
    //get the graph for the Representation and add it to the store
    RdfRepresentation toAdd = valueFactory.toRdfRepresentation(representation);
    if (toAdd.getModel().isEmpty()) {
        con.add(toAdd.getURI(), managedRepresentation, managedRepresentationState, contexts);
    } else {
        con.add(toAdd.getModel(), contexts);
    }
    return toAdd;
}
Also used : RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) URI(org.openrdf.model.URI)

Example 4 with RdfRepresentation

use of org.apache.stanbol.entityhub.model.sesame.RdfRepresentation in project stanbol by apache.

the class SesameContextTest method validateEntity.

/**
     * Used by {@link #testRetrival()} to validate that an Entity is correctly
     * retrieved by the tested {@link SesameYard}s.
     * @param entity key - URI; value - expected RDF data
     * @throws YardException 
     */
private void validateEntity(Yard yard, URI subject) throws YardException {
    Representation rep = yard.getRepresentation(subject.stringValue());
    assertNotNull("The Representation for " + subject + "is missing in the " + yard.getId(), rep);
    assertTrue("RdfRepresentation expected", rep instanceof RdfRepresentation);
    //check the RDF type to validate that some data are present
    assertEquals(skosConcept.stringValue(), rep.getFirstReference(rdfType.stringValue()).getReference());
}
Also used : RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation)

Example 5 with RdfRepresentation

use of org.apache.stanbol.entityhub.model.sesame.RdfRepresentation 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)

Aggregations

RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)6 Model (org.openrdf.model.Model)4 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)3 URI (org.openrdf.model.URI)3 BNode (org.openrdf.model.BNode)2 TreeModel (org.openrdf.model.impl.TreeModel)2 RepositoryConnection (org.openrdf.repository.RepositoryConnection)2 HashSet (java.util.HashSet)1 RdfValueFactory (org.apache.stanbol.entityhub.model.sesame.RdfValueFactory)1 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)1 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)1 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)1 YardTest (org.apache.stanbol.entityhub.test.yard.YardTest)1 SesameYard (org.apache.stanbol.entityhub.yard.sesame.SesameYard)1 Test (org.junit.Test)1 Value (org.openrdf.model.Value)1 BindingSet (org.openrdf.query.BindingSet)1 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)1 TupleQueryResult (org.openrdf.query.TupleQueryResult)1 RepositoryException (org.openrdf.repository.RepositoryException)1