use of org.openrdf.model.Model in project stanbol by apache.
the class RdfRepresentationTest method testBNodeFiltering.
/**
* Test for STANBOL-1301
*/
@Test
public void testBNodeFiltering() {
URI concept = new URIImpl("http://example.org/mySkos#Concept123");
Representation r = createRepresentation(concept.stringValue());
assertTrue(r instanceof RdfRepresentation);
RdfRepresentation rep = (RdfRepresentation) r;
//add the example as listed in STANBOL-1301 to directly to the
//Sesame Model backing the created Representation
Model m = rep.getModel();
m.add(concept, RDF.TYPE, SKOS.CONCEPT);
m.add(concept, DCTERMS.IDENTIFIER, new LiteralImpl("123"));
m.add(concept, SKOS.PREF_LABEL, new LiteralImpl("Concept123", "en"));
BNode note1 = new BNodeImpl("5d8580be71044a88bcfe9852d1e9cfb6node17c4j452vx19576");
m.add(concept, SKOS.SCOPE_NOTE, note1);
m.add(note1, DCTERMS.CREATOR, new LiteralImpl("User1"));
m.add(note1, DCTERMS.CREATED, new LiteralImpl("2013-03-03T02:02:02Z", XMLSchema.DATETIME));
m.add(note1, RDFS.COMMENT, new LiteralImpl("The scope of this example global", "en"));
BNode note2 = new BNodeImpl("5d8580be71044a88bcfe9852d1e9cfb6node17c4j452vx19634");
m.add(concept, SKOS.SCOPE_NOTE, note2);
m.add(note2, DCTERMS.CREATOR, new LiteralImpl("User2"));
m.add(note2, DCTERMS.CREATED, new LiteralImpl("2013-03-03T04:04:04Z", XMLSchema.DATETIME));
m.add(note2, RDFS.COMMENT, new LiteralImpl("Der Geltungsbereich ist Global", "de"));
//now assert that BNodes are not reported via the Representation API
Iterator<Object> scopeNotes = rep.get(SKOS.SCOPE_NOTE.stringValue());
assertFalse(scopeNotes.hasNext());
Iterator<Reference> scopeNoteRefs = rep.getReferences(SKOS.SCOPE_NOTE.stringValue());
assertFalse(scopeNoteRefs.hasNext());
}
use of org.openrdf.model.Model 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.openrdf.model.Model 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.openrdf.model.Model in project stanbol by apache.
the class SesameModelWriter method toRDF.
private Model toRDF(QueryResultList<?> resultList) {
final Model resultGraph;
Class<?> type = resultList.getType();
if (String.class.isAssignableFrom(type)) {
//create a new ImmutableGraph
resultGraph = new LinkedHashModel();
for (Object result : resultList) {
//add a triple to each reference in the result set
resultGraph.add(QUERY_RESULT_LIST, QUERY_RESULT, sesameFactory.createURI(result.toString()));
}
} else {
//first determine the type of the resultList
final boolean isSignType;
if (Representation.class.isAssignableFrom(type)) {
isSignType = false;
} else if (Representation.class.isAssignableFrom(type)) {
isSignType = true;
} else {
//incompatible type -> throw an Exception
throw new IllegalArgumentException("Parsed type " + type + " is not supported");
}
//special treatment for RdfQueryResultList for increased performance
if (resultList instanceof SesameQueryResultList) {
resultGraph = ((SesameQueryResultList) resultList).getModel();
if (isSignType) {
//if we build a ResultList for Signs, we need to do more things
//first remove all triples representing results
resultGraph.filter(null, QUERY_RESULT, null).clear();
//to the Sign IDs
for (Object result : resultList) {
URI signId = sesameFactory.createURI(((Entity) result).getId());
addEntityTriplesToGraph(resultGraph, (Entity) result);
resultGraph.add(QUERY_RESULT_LIST, QUERY_RESULT, signId);
}
}
} else {
//any other implementation of the QueryResultList interface
//create a new graph
resultGraph = new LinkedHashModel();
if (Representation.class.isAssignableFrom(type)) {
for (Object result : resultList) {
URI resultId;
if (!isSignType) {
addRDFTo(resultGraph, (Representation) result);
resultId = sesameFactory.createURI(((Representation) result).getId());
} else {
addRDFTo(resultGraph, (Entity) result);
resultId = sesameFactory.createURI(((Entity) result).getId());
}
//Note: In case of Representation this Triple points to
// the representation. In case of Signs it points to
// the sign.
resultGraph.add(QUERY_RESULT_LIST, QUERY_RESULT, resultId);
}
}
}
}
return resultGraph;
}
use of org.openrdf.model.Model in project stanbol by apache.
the class SesameModelWriter method write.
@Override
public void write(QueryResultList<?> result, OutputStream out, MediaType mediaType) throws WebApplicationException, IOException {
Model queryRdf = toRDF(result);
//we need also to the JSON formatted FieldQuery as a literal to the
//RDF data.
FieldQuery query = result.getQuery();
if (query != null) {
try {
JSONObject fieldQueryJson = FieldQueryToJsonUtils.toJSON(query, nsPrefixService);
if (fieldQueryJson != null) {
//add the triple with the fieldQuery
queryRdf.add(QUERY_RESULT_LIST, FIELD_QUERY, sesameFactory.createLiteral(fieldQueryJson.toString()));
}
} catch (JSONException e) {
log.warn(String.format("Unable to serialize Fieldquery '%s' to JSON! " + "Query response will not contain the serialized query.", query), e);
}
}
//now serialise the data
writeRdf(queryRdf, out, mediaType);
}
Aggregations