use of org.apache.stanbol.enhancer.engines.dereference.DereferenceException in project stanbol by apache.
the class TrackingDereferencerBase method dereference.
@Override
public final boolean dereference(IRI uri, Graph graph, Lock writeLock, DereferenceContext dc) throws DereferenceException {
T service = getService();
if (service == null) {
throw new DereferenceException(uri, serviceClass.getClass().getSimpleName() + "service is currently not available");
}
EntityhubDereferenceContext derefContext = (EntityhubDereferenceContext) dc;
Representation rep;
try {
rep = getRepresentation(service, uri.getUnicodeString(), derefContext.isOfflineMode());
} catch (EntityhubException e) {
throw new DereferenceException(uri, e);
}
//we need the languages as strings
final Set<String> langs = derefContext.getLanguages();
final FieldMapper fieldMapper = derefContext.getFieldMapper();
final Program<Object> ldpathProgram = derefContext.getProgram();
if (rep != null) {
if (fieldMapper == null && ldpathProgram == null && (langs == null || langs.isEmpty())) {
copyAll(uri, rep, graph, writeLock);
} else {
//we need to apply some filters while dereferencing
if (fieldMapper != null || (langs != null && !langs.isEmpty())) {
//this considers speficied fields and included languages
copyMapped(uri, rep, fieldMapper, langs, graph, writeLock);
}
if (ldpathProgram != null) {
//this executes LDPath statements
copyLdPath(uri, getRdfBackend(service), ldpathProgram, langs, graph, writeLock);
}
}
return true;
} else {
return false;
}
}
use of org.apache.stanbol.enhancer.engines.dereference.DereferenceException in project stanbol by apache.
the class TrackingDereferencerBase method copyLdPath.
/**
* Executes the {@link #ldpathProgram} using the parsed URI as context and
* writes the the results to the parsed ImmutableGraph
* @param uri the context
* @param rdfBackend the RdfBackend the LDPath program is executed on
* @param ldpathProgram The {@link Program} parsed via the dereference context
* @param langs the set of languages to dereference
* @param graph the graph to store the results
* @param writeLock the write lock for the graph
* @throws DereferenceException on any {@link EntityhubException} while
* executing the LDPath program
*/
private void copyLdPath(IRI uri, RDFBackend<Object> rdfBackend, Program<Object> ldpathProgram, Set<String> langs, Graph graph, Lock writeLock) throws DereferenceException {
//A RdfReference needs to be used as context
RdfReference context = valueFactory.createReference(uri);
//create the representation that stores results in an intermediate
//graph (we do not want partial results on an error
Graph ldPathResults = new SimpleGraph();
RdfRepresentation result = valueFactory.createRdfRepresentation(uri, ldPathResults);
//execute the LDPath Program and write results to the RDF ImmutableGraph
try {
for (org.apache.marmotta.ldpath.model.fields.FieldMapping<?, Object> mapping : ldpathProgram.getFields()) {
Collection<?> values;
try {
values = mapping.getValues(rdfBackend, context);
} catch (RuntimeException e) {
throw new DereferenceException(uri, e);
}
if (values != null && !values.isEmpty()) {
String fieldName = mapping.getFieldName();
if (langs == null || langs.isEmpty()) {
result.add(fieldName, values);
} else {
//filter for languages
for (Object value : values) {
if ((!(value instanceof Text)) || langs.contains(((Text) value).getLanguage())) {
result.add(fieldName, value);
}
//else text with filtered language ... do not add
}
}
}
}
} catch (EntityhubException e) {
throw new DereferenceException(uri, e);
}
if (log.isTraceEnabled()) {
log.trace("dereferenced via LDPath {}", ModelUtils.getRepresentationInfo(result));
}
if (!ldPathResults.isEmpty()) {
//copy the results
writeLock.lock();
try {
graph.addAll(ldPathResults);
} finally {
writeLock.unlock();
}
}
}
Aggregations