use of org.apache.stanbol.enhancer.servicesapi.Chain in project stanbol by apache.
the class AbstractEnhancerResource method getExecutionPlan.
/*@OPTIONS
public Response handleCorsPreflight(@Context HttpHeaders headers) {
ResponseBuilder res = Response.ok();
enableCORS(servletContext, res, headers);
return res.build();
}
@OPTIONS
@Path("/ep")
public Response handleEpCorsPreflight(@Context HttpHeaders headers) {
ResponseBuilder res = Response.ok();
enableCORS(servletContext, res, headers,HttpMethod.OPTIONS,HttpMethod.GET);
return res.build();
}*/
@GET
@Path("/ep")
@Produces(value = { JSON_LD, APPLICATION_JSON, N3, N_TRIPLE, RDF_JSON, RDF_XML, TURTLE, X_TURTLE })
public Response getExecutionPlan(@Context HttpHeaders headers) {
ResponseBuilder res;
Chain chain = null;
try {
chain = getChain();
res = Response.ok(chain.getExecutionPlan());
} catch (ChainException e) {
String chainName = chain == null ? "" : ("'" + chain.getName() + "' ");
res = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("The Enhancement Chain " + chainName + "is currently" + "not executeable (message: " + e.getMessage() + ")!");
}
//addCORSOrigin(servletContext, res, headers);
return res.build();
}
use of org.apache.stanbol.enhancer.servicesapi.Chain in project stanbol by apache.
the class EnhancerUtils method buildChainsMap.
/**
* Uses the parsed {@link ChainManager} to build a Map
* representing the current snapshot of the active enhancement chains.
*
* @param chainManager The chain manager used to build the snapshot
* @return the map with the names as key and an Entry with the {@link ServiceReference}
* and the {@link Chain} instance as value.
*/
public static Map<String, Map.Entry<ServiceReference, Chain>> buildChainsMap(ChainManager chainManager) {
Map<String, Map.Entry<ServiceReference, Chain>> chains = new HashMap<String, Map.Entry<ServiceReference, Chain>>();
for (String chainName : chainManager.getActiveChainNames()) {
ServiceReference chainRef = chainManager.getReference(chainName);
if (chainRef != null) {
Chain chain = chainManager.getChain(chainRef);
if (chain != null) {
Map<ServiceReference, Chain> m = Collections.singletonMap(chainRef, chain);
chains.put(chainName, m.entrySet().iterator().next());
}
}
}
return chains;
}
use of org.apache.stanbol.enhancer.servicesapi.Chain in project stanbol by apache.
the class BenchmarkServlet method doPost.
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
final String content = request.getParameter(PARAM_CONTENT);
if (content == null) {
throw new ServletException("Missing " + PARAM_CONTENT + " parameter");
}
String chainName = request.getParameter(PARAM_CHAIN);
final Template t = AccessController.doPrivileged(new PrivilegedAction<Template>() {
@Override
public Template run() {
return getTemplate("/velocity/benchmark-results.html");
}
});
final VelocityContext ctx = getVelocityContext(request, "Benchmark Results");
ctx.put("contentItemFactory", ciFactory);
ctx.put("jobManager", jobManager);
List<? extends Benchmark> benchmarks = parser.parse(new StringReader(content));
if (chainName != null && !chainName.isEmpty()) {
Chain chain = chainManager.getChain(chainName);
if (chain == null) {
response.setStatus(404);
PrintWriter w = response.getWriter();
w.println("Unable to perform benchmark on EnhancementChain '" + StringEscapeUtils.escapeHtml(chainName) + "' because no chain with that name is active!");
IOUtils.closeQuietly(w);
return;
}
for (Benchmark benchmark : benchmarks) {
benchmark.setChain(chain);
}
}
ctx.put("benchmarks", benchmarks);
ctx.put("graphFormatter", new GraphFormatter(graphSerializer));
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws IOException {
t.merge(ctx, response.getWriter());
return null;
}
});
} catch (PrivilegedActionException pae) {
Exception e = pae.getException();
if (e instanceof IOException) {
throw (IOException) e;
} else {
throw RuntimeException.class.cast(e);
}
}
}
use of org.apache.stanbol.enhancer.servicesapi.Chain in project stanbol by apache.
the class TopicClassificationEngine method getChainNames.
@Override
public List<String> getChainNames() throws InvalidSyntaxException {
List<String> chainNames = new ArrayList<String>();
BundleContext bundleContext = context.getBundleContext();
ServiceReference[] references = bundleContext.getServiceReferences(Chain.class.getName(), null);
if (references != null) {
for (ServiceReference ref : references) {
Chain chain = (Chain) bundleContext.getService(ref);
try {
if (chain.getEngines().contains(getName())) {
chainNames.add(chain.getName());
}
} catch (ChainException e) {
// This chain is currently not active ... ignore
}
}
}
return chainNames;
}
use of org.apache.stanbol.enhancer.servicesapi.Chain in project stanbol by apache.
the class EnhancerUtils method addActiveChains.
/**
* Create the RDF data for the currently active Enhancement {@link Chain}s.<p>
* Note the the parsed rootUrl MUST already consider offsets configured
* for the Stanbol RESTful service. When called from within a
* {@link BaseStanbolResource} the following code segment should be used:<p>
* <code><pre>
* String rootUrl = uriInfo.getBaseUriBuilder().path(getRootUrl()).build().toString();
* </pre></code>
* @param activeChains the active enhancement chains as {@link Entry entries}.
* @param defaultChain the default chain
* @param graph the RDF graph to add the triples
* @param rootUrl the root URL used by the current request
*/
public static void addActiveChains(Iterable<Entry<ServiceReference, Chain>> activeChains, Chain defaultChain, Graph graph, String rootUrl) {
IRI enhancer = new IRI(rootUrl + "enhancer");
graph.add(new TripleImpl(enhancer, RDF.type, Enhancer.ENHANCER));
for (Entry<ServiceReference, Chain> entry : activeChains) {
IRI chainResource = new IRI(rootUrl + "enhancer/chain/" + entry.getValue().getName());
graph.add(new TripleImpl(enhancer, Enhancer.HAS_CHAIN, chainResource));
if (entry.getValue().equals(defaultChain)) {
graph.add(new TripleImpl(enhancer, Enhancer.HAS_DEFAULT_CHAIN, chainResource));
}
graph.add(new TripleImpl(chainResource, RDF.type, Enhancer.ENHANCEMENT_CHAIN));
graph.add(new TripleImpl(chainResource, RDFS.label, new PlainLiteralImpl(entry.getValue().getName())));
}
}
Aggregations