use of org.apache.stanbol.entityhub.ldpath.EntityhubLDPath in project stanbol by apache.
the class EntityhubDereferenceContext method initLdPath.
protected void initLdPath(String program) throws DereferenceConfigurationException {
TrackingDereferencerBase<?> dereferencer = getEntityhubDereferencer();
ValueFactory valueFactory = dereferencer.getValueFactory();
Program<Object> ldpathProgram;
if (!StringUtils.isBlank(program)) {
@SuppressWarnings("rawtypes") RDFBackend<Object> parseBackend = new ParseBackend<Object>(valueFactory);
EntityhubLDPath parseLdPath = new EntityhubLDPath(parseBackend, valueFactory);
try {
ldpathProgram = parseLdPath.parseProgram(new StringReader(program));
} catch (LDPathParseException e) {
log.error("Unable to parse Context LDPath pogram: \n {}", program);
throw new DereferenceConfigurationException("Unable to parse context LDPath program !", e, dereferencer.getClass(), DEREFERENCE_ENTITIES_LDPATH);
}
//finally validate if all mappings of the program do use a URI as key
//also store used fieldNames as we need them later
Set<String> contextFields = new HashSet<String>();
for (org.apache.marmotta.ldpath.model.fields.FieldMapping<?, Object> mapping : ldpathProgram.getFields()) {
try {
new URI(mapping.getFieldName());
contextFields.add(mapping.getFieldName());
} catch (URISyntaxException e) {
throw new DereferenceConfigurationException("Parsed LDPath MUST use valid URIs as field names (invalid field name: '" + mapping.getFieldName() + "' | selector: '" + mapping.getSelector().getPathExpression(parseBackend) + "')!", dereferencer.getClass(), DereferenceConstants.DEREFERENCE_ENTITIES_LDPATH);
}
}
//append the mappings configured for the engine
if (dereferencer.getLdPathProgram() != null) {
for (org.apache.marmotta.ldpath.model.fields.FieldMapping<?, Object> mapping : dereferencer.getLdPathProgram().getFields()) {
if (!contextFields.contains(mapping.getFieldName())) {
ldpathProgram.addMapping(mapping);
}
//else ignore mappings for fields specified in the context
}
}
} else {
//no context specific - use the one of the config
ldpathProgram = dereferencer.getLdPathProgram();
}
if (ldpathProgram != null && !ldpathProgram.getFields().isEmpty()) {
this.ldpathProgram = ldpathProgram;
} else {
this.ldpathProgram = null;
}
}
use of org.apache.stanbol.entityhub.ldpath.EntityhubLDPath in project stanbol by apache.
the class LDPathHelper method executeLDPath.
/**
* Executes the LDPath program on the contexts stored in the backend and
* returns the result as an RDF graph
* @param contexts the contexts to execute the program on
* @param ldpath the LDPath program to execute
* @param backend the {@link RDFBackend} to use
* @return The results stored within an RDF graph
* @throws LDPathParseException if the parsed LDPath program is invalid
*/
private static Graph executeLDPath(RDFBackend<Object> backend, String ldpath, Set<String> contexts) throws LDPathParseException {
Graph data = new IndexedGraph();
RdfValueFactory vf = new RdfValueFactory(data);
EntityhubLDPath ldPath = new EntityhubLDPath(backend, vf);
Program<Object> program = ldPath.parseProgram(getReader(ldpath));
if (log.isDebugEnabled()) {
log.debug("Execute on Context(s) '{}' LDPath program: \n{}", contexts, program.getPathExpression(backend));
}
/*
* NOTE: We do not need to process the Representations returned by
* EntityhubLDPath#exdecute, because the RdfValueFactory used uses
* the local variable "Graph data" to backup all created
* RdfRepresentation. Because of this all converted data will be
* automatically added the Graph. The only thing we need to do is to
* wrap the Graph in the response.
*/
for (String context : contexts) {
ldPath.execute(vf.createReference(context), program);
}
return data;
}
Aggregations