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;
}
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 */
}
}
}
}
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;
}
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());
}
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());
}
Aggregations