use of org.apache.clerezza.commons.rdf.IRI in project stanbol by apache.
the class SparqlSearcher method find.
@Override
public final QueryResultList<Representation> find(FieldQuery parsedQuery) throws IOException {
long start = System.currentTimeMillis();
final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
String sparqlQuery = query.toSparqlConstruct();
long initEnd = System.currentTimeMillis();
log.debug(" > InitTime: " + (initEnd - start));
log.debug(" > SPARQL query:\n" + sparqlQuery);
InputStream in = SparqlEndpointUtils.sendSparqlRequest(getQueryUri(), sparqlQuery, DEFAULT_RDF_CONTENT_TYPE);
long queryEnd = System.currentTimeMillis();
log.debug(" > QueryTime: " + (queryEnd - initEnd));
if (in != null) {
Graph graph;
Graph rdfData = parser.parse(in, DEFAULT_RDF_CONTENT_TYPE, new IRI(getBaseUri()));
if (rdfData instanceof Graph) {
graph = (Graph) rdfData;
} else {
graph = new IndexedGraph(rdfData);
}
long parseEnd = System.currentTimeMillis();
log.debug(" > ParseTime: " + (parseEnd - queryEnd));
return new RdfQueryResultList(query, graph);
} else {
return null;
}
}
use of org.apache.clerezza.commons.rdf.IRI in project stanbol by apache.
the class ClerezzaModelWriter method addEntityTriplesToGraph.
/**
* Adds the Triples that represent the Sign to the parsed graph. Note that
* this method does not add triples for the representation. However it adds
* the triple (sign,singRepresentation,representation)
*
* @param graph the graph to add the triples
* @param sign the sign
*/
private void addEntityTriplesToGraph(Graph graph, Entity sign) {
IRI id = new IRI(sign.getId());
IRI metaId = new IRI(sign.getMetadata().getId());
// add the FOAF triples between metadata and content
graph.add(new TripleImpl(id, FOAF_PRIMARY_TOPIC_OF, metaId));
graph.add(new TripleImpl(metaId, FOAF_PRIMARY_TOPIC, metaId));
graph.add(new TripleImpl(metaId, RDF.type, FOAF_DOCUMENT));
// add the site to the metadata
// TODO: this should be the HTTP URI and not the id of the referenced site
Literal siteName = literalFactory.createTypedLiteral(sign.getSite());
graph.add(new TripleImpl(metaId, SIGN_SITE, siteName));
}
use of org.apache.clerezza.commons.rdf.IRI in project stanbol by apache.
the class ClerezzaModelWriter method toRDF.
private Graph toRDF(QueryResultList<?> resultList) {
final Graph resultGraph;
Class<?> type = resultList.getType();
if (String.class.isAssignableFrom(type)) {
// create a new ImmutableGraph
resultGraph = new IndexedGraph();
for (Object result : resultList) {
// add a triple to each reference in the result set
resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, new IRI(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 RdfQueryResultList) {
resultGraph = ((RdfQueryResultList) resultList).getResultGraph();
if (isSignType) {
// if we build a ResultList for Signs, that we need to do more things
// first remove all triples representing results
Iterator<Triple> resultTripleIt = resultGraph.filter(QUERY_RESULT_LIST, QUERY_RESULT, null);
while (resultTripleIt.hasNext()) {
resultTripleIt.next();
resultTripleIt.remove();
}
// to the Sign IDs
for (Object result : resultList) {
IRI signId = new IRI(((Entity) result).getId());
addEntityTriplesToGraph(resultGraph, (Entity) result);
resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, signId));
}
}
} else {
// any other implementation of the QueryResultList interface
// create a new graph
resultGraph = new IndexedGraph();
if (Representation.class.isAssignableFrom(type)) {
for (Object result : resultList) {
IRI resultId;
if (!isSignType) {
addRDFTo(resultGraph, (Representation) result);
resultId = new IRI(((Representation) result).getId());
} else {
addRDFTo(resultGraph, (Entity) result);
resultId = new IRI(((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(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, resultId));
}
}
}
}
return resultGraph;
}
use of org.apache.clerezza.commons.rdf.IRI in project stanbol by apache.
the class ClerezzaYard 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);
final ResultSet result = executeSparqlFieldQuery(query);
// Note: An other possibility would be to first iterate over all results and add it to
// a list and create this Iterator than based on the List. This would
// be the preferenced way if changes in the graph could affect the
// Iteration over the SPARQL query results.
Iterator<Representation> representationIterator = new AdaptingIterator<SolutionMapping, Representation>(result, new AdaptingIterator.Adapter<SolutionMapping, Representation>() {
/**
* Adapter that gets the rootVariable of the Query (selecting the ID)
* and creates a Representation for it.
* @param solution a solution of the query
* @param type the type (no generics here)
* @return the representation or <code>null</code> if result is
* not an IRI or there is no Representation for the result.
*/
@Override
public Representation adapt(SolutionMapping solution, Class<Representation> type) {
RDFTerm resource = solution.get(query.getRootVariableName());
if (resource instanceof IRI) {
try {
return getRepresentation((IRI) resource, false);
} catch (IllegalArgumentException e) {
log.warn("Unable to create Representation for ID " + resource + "! -> ignore query result");
return null;
}
} else {
return null;
}
}
}, Representation.class);
// created before the method returns.
return new QueryResultListImpl<Representation>(query, representationIterator, Representation.class);
}
use of org.apache.clerezza.commons.rdf.IRI in project stanbol by apache.
the class ClerezzaYard method activate.
/**
* Internally used to activate the Yard. In case the Yard runs within a
* OSGI container it is called by the {@link #activate(ComponentContext)}
* Method. In case the Yard runs outside of an OSGI Container it is called
* by the Constructor taking the {@link YardConfig} as parameter
* @param config The configuration for the new Yard instance
* @throws IllegalArgumentException In case <code>null</code> is parsed as
* configuration or the configuration is invalid
*/
private final void activate(ClerezzaYardConfig config) throws IllegalArgumentException {
super.activate(RdfValueFactory.getInstance(), SparqlFieldQueryFactory.getInstance(), config);
if (tcManager == null) {
// this will be the case if we are not in an OSGI environment
// use the getInstance() method!
tcManager = TcManager.getInstance();
}
this.yardGraphUri = config.getGraphUri();
if (this.yardGraphUri == null) {
// use default
String yardUri = getUriPrefix();
// remove the "." at the last position of the prefix
this.yardGraphUri = new IRI(yardUri.substring(0, yardUri.length() - 2));
}
try {
try {
this.graph = tcManager.getImmutableGraph(yardGraphUri);
immutable = true;
} catch (NoSuchEntityException e) {
this.graph = tcManager.getGraph(yardGraphUri);
immutable = false;
}
log.info(" ... (re)use existing Graph {} for Yard {}", yardGraphUri, config.getName());
} catch (NoSuchEntityException e) {
log.info(" ... create new Graph {} for Yard {}", yardGraphUri, config.getName());
this.graph = tcManager.createGraph(yardGraphUri);
}
if (context != null) {
// within an OSGI environment
// Register the graph with the Stanbol SPARQL endpoint (STANBOL-677)
Dictionary<String, Object> graphRegProp = new Hashtable<String, Object>();
graphRegProp.put("graph.uri", yardGraphUri.getUnicodeString());
graphRegProp.put(Constants.SERVICE_RANKING, new Integer(-100));
graphRegProp.put("graph.name", getConfig().getName());
if (getConfig().getDescription() != null) {
graphRegProp.put("graph.description", getConfig().getDescription());
}
graphRegistration = context.getBundleContext().registerService(Graph.class.getName(), graph, graphRegProp);
}
// else do not register when running outside OSGI
}
Aggregations