use of org.apache.clerezza.commons.rdf.ImmutableGraph in project stanbol by apache.
the class ClerezzaYard method find.
@Override
public final QueryResultList<Representation> find(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
if (parsedQuery == null) {
throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
}
final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
Query sparqlQuery;
//NOTE:
// - use the endpoint type standard, because we do not know what type of
// SPARQL implementation is configured for Clerezza via OSGI
String sparqlQueryString = SparqlQueryUtils.createSparqlConstructQuery(query, limit, EndpointTypeEnum.Standard);
try {
sparqlQuery = QueryParser.getInstance().parse(sparqlQueryString);
} catch (ParseException e) {
log.error("ParseException for SPARQL Query in findRepresentation");
log.error("FieldQuery: " + query);
log.error("SPARQL Query: " + sparqlQueryString);
throw new YardException("Unable to parse SPARQL query generated for the parse FieldQuery", e);
}
Object resultObject = tcManager.executeSparqlQuery(sparqlQuery, graph);
final Graph resultGraph;
if (resultObject instanceof Graph) {
resultGraph = (Graph) resultObject;
} else if (resultObject instanceof ImmutableGraph) {
resultGraph = new IndexedGraph();
resultGraph.addAll((ImmutableGraph) resultObject);
} else {
log.error("Unable to create " + Graph.class + " instance for query reults of type " + resultObject.getClass() + " (this indicates that the used SPARQL Query was not of type CONSTRUCT)");
log.error("FieldQuery: " + query);
log.error("SPARQL Query: " + sparqlQueryString);
throw new YardException("Unable to process results of Query");
}
return new RdfQueryResultList(query, resultGraph);
}
use of org.apache.clerezza.commons.rdf.ImmutableGraph in project stanbol by apache.
the class XsltExtractor method extract.
public synchronized void extract(String id, Document doc, Map<String, Object> params, Graph result) throws ExtractorException {
if (params == null) {
params = new HashMap<String, Object>();
}
params.put(this.uriParameter, id);
initTransformerParameters(params);
Source source = new DOMSource(doc);
ByteArrayOutputStream writer = new ByteArrayOutputStream(8192);
StreamResult output = new StreamResult(writer);
try {
this.transformer.transform(source, output);
if (LOG.isDebugEnabled()) {
String rdf = writer.toString("UTF-8");
LOG.debug(rdf);
}
InputStream reader = new ByteArrayInputStream(writer.toByteArray());
Parser rdfParser = Parser.getInstance();
ImmutableGraph graph = rdfParser.parse(reader, this.syntax);
result.addAll(graph);
} catch (TransformerException e) {
throw new ExtractorException(e.getMessage(), e);
} catch (IOException e) {
throw new ExtractorException(e.getMessage(), e);
}
}
use of org.apache.clerezza.commons.rdf.ImmutableGraph in project stanbol by apache.
the class EventJobManagerImpl method getActiveEngines.
@Override
public List<EnhancementEngine> getActiveEngines() {
//This implementation return the list of active engined for the default
//Chain in the order they would be executed
Chain defaultChain = chainManager.getDefault();
if (defaultChain == null) {
throw new IllegalStateException("Currently no enhancement chain is " + "active. Please configure a Chain or enable the default chain");
}
ImmutableGraph ep;
try {
ep = defaultChain.getExecutionPlan();
} catch (ChainException e) {
throw new IllegalStateException("Unable to get Execution Plan for " + "default enhancement chain (name: '" + defaultChain.getName() + "'| class: '" + defaultChain.getClass() + "')!", e);
}
return ExecutionPlanHelper.getActiveEngines(engineManager, ep);
}
use of org.apache.clerezza.commons.rdf.ImmutableGraph in project stanbol by apache.
the class EnhancementPropertyTest method initExecutionMetadata.
/**
* Initialises the ExecutionMetadata based on the chain used by the test.
* This is typically done by the {@link EnhancementJobManager}, but as we do
* not use one for the tests we need to do this part manually
* @param chain
* @throws ChainException
*/
protected void initExecutionMetadata(Chain chain) throws ChainException {
//init the ExecutionMetadata ... this is normally done by the EnhancementJobManager
Graph em = ExecutionMetadataHelper.initExecutionMetadataContentPart(contentItem);
ImmutableGraph ep = chain.getExecutionPlan();
em.addAll(ep);
ExecutionMetadataHelper.initExecutionMetadata(em, ep, contentItem.getUri(), chain.getName(), false);
}
use of org.apache.clerezza.commons.rdf.ImmutableGraph in project stanbol by apache.
the class ExecutionPlanHelper method calculateExecutionPlan.
/**
* Creates an execution plan based on the
* {@link ServiceProperties#ENHANCEMENT_ENGINE_ORDERING} of the parsed
* EnhancementEngines. NOTE that the parsed list is modified as it is sorted by
* using the {@link EnhancementEngineHelper#EXECUTION_ORDER_COMPARATOR}.<p>
* A second parameter with the set of optional engines can be used to define
* what {@link ExecutionPlan#EXECUTION_NODE} in the execution plan should be
* marked as {@link ExecutionPlan#OPTIONAL}.
* @param chainName the name of the Chain to build the execution plan for
* @param availableEngines the list of engines
* @param optional the names of optional engines.
* @param missing the names of missing engines
* @param enhProps chain scoped enhancement properties. The key of the outer
* map are the name of the engine or <code>null</code> for the chain. The
* inner map uses the property as key and the value(s) as value. Multiple
* values can be parsed as {@link Collection}. Single values will be
* converted to RDF {@link TypedLiteral}s by using the {@link LiteralFactory}.
* For types not supported by the LiteralFactory the <code>toString()</code>
* method will be used. <code>null</code> can be parsed if no enhancement
* properties are present.
* @return the execution plan
* @since 0.12.1
*/
public static ImmutableGraph calculateExecutionPlan(String chainName, List<EnhancementEngine> availableEngines, Set<String> optional, Set<String> missing, Map<String, Map<String, Object>> enhProps) {
if (chainName == null || chainName.isEmpty()) {
throw new IllegalArgumentException("The parsed ChainName MUST NOT be empty!");
}
Collections.sort(availableEngines, EXECUTION_ORDER_COMPARATOR);
//now we have all required and possible also optional engines
// -> build the execution plan
Graph ep = new IndexedGraph();
BlankNodeOrIRI epNode = createExecutionPlan(ep, chainName, enhProps != null ? enhProps.get(null) : null);
Integer prevOrder = null;
Set<BlankNodeOrIRI> prev = null;
Set<BlankNodeOrIRI> current = new HashSet<BlankNodeOrIRI>();
for (String name : missing) {
boolean optionalMissing = optional.contains(name);
BlankNodeOrIRI node = writeExecutionNode(ep, epNode, name, optionalMissing, null, enhProps == null ? null : enhProps.get(name));
if (!optionalMissing) {
current.add(node);
}
// else add missing optional engines without any dependsOn restrictions
}
for (EnhancementEngine engine : availableEngines) {
String name = engine.getName();
Integer order = getEngineOrder(engine);
if (prevOrder == null || !prevOrder.equals(order)) {
prev = current;
current = new HashSet<BlankNodeOrIRI>();
prevOrder = order;
}
try {
BlankNodeOrIRI executionNode = writeExecutionNode(ep, epNode, name, optional.contains(name), prev, enhProps == null ? null : enhProps.get(name));
current.add(executionNode);
} catch (RuntimeException e) {
//add the engine and class to ease debugging in such cases
log.error("Exception while writing ExecutionNode for Enhancement Eninge: " + engine + "(class: " + engine.getClass() + ")", e);
//rethrow it
throw e;
}
}
return ep.getImmutableGraph();
}
Aggregations