Search in sources :

Example 1 with Chain

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();
}
Also used : Chain(org.apache.stanbol.enhancer.servicesapi.Chain) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with Chain

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;
}
Also used : Chain(org.apache.stanbol.enhancer.servicesapi.Chain) Entry(java.util.Map.Entry) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with Chain

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);
        }
    }
}
Also used : Chain(org.apache.stanbol.enhancer.servicesapi.Chain) PrivilegedActionException(java.security.PrivilegedActionException) VelocityContext(org.apache.velocity.VelocityContext) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) NamespaceException(org.osgi.service.http.NamespaceException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Template(org.apache.velocity.Template) ServletException(javax.servlet.ServletException) StringReader(java.io.StringReader) Benchmark(org.apache.stanbol.enhancer.benchmark.Benchmark) PrintWriter(java.io.PrintWriter)

Example 4 with Chain

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;
}
Also used : Chain(org.apache.stanbol.enhancer.servicesapi.Chain) ArrayList(java.util.ArrayList) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with Chain

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())));
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Chain(org.apache.stanbol.enhancer.servicesapi.Chain) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

Chain (org.apache.stanbol.enhancer.servicesapi.Chain)7 ChainException (org.apache.stanbol.enhancer.servicesapi.ChainException)4 ServiceReference (org.osgi.framework.ServiceReference)3 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ServletException (javax.servlet.ServletException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)1 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)1