Search in sources :

Example 6 with SiteException

use of org.apache.stanbol.entityhub.servicesapi.site.SiteException in project stanbol by apache.

the class ReferencedSiteRootResource method deleteEntity.

@DELETE
@Path("entity/")
public Response deleteEntity(@PathParam(value = "site") String siteId, @QueryParam(value = "id") String id, @Context HttpHeaders headers) {
    MediaType accepted = getAcceptableMediaType(headers, JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES, MediaType.APPLICATION_JSON_TYPE);
    ManagedSite managedSite;
    Site site = getSite(siteId);
    if (site instanceof ManagedSite) {
        managedSite = (ManagedSite) site;
    } else {
        ResponseBuilder builder = Response.status(Status.FORBIDDEN).entity(String.format("The Site '%s' is not managed and does not support " + "create/update nor delete operations", site.getId())).header(HttpHeaders.ACCEPT, accepted);
        //addCORSOrigin(servletContext, builder, headers);
        return builder.build();
    }
    if (id == null || id.isEmpty()) {
        ResponseBuilder builder = Response.status(Status.BAD_REQUEST).entity("The Request does" + "not provide the id of the Entity to delete (parameter 'id').").header(HttpHeaders.ACCEPT, accepted);
        //addCORSOrigin(servletContext, builder, headers);
        return builder.build();
    }
    ResponseBuilder builder;
    try {
        if ("*".equals(id)) {
            managedSite.deleteAll();
            builder = Response.ok();
        } else {
            Entity entity = managedSite.getEntity(id);
            if (entity != null) {
                //delete the entity
                managedSite.delete(id);
                //return the deleted data
                final MediaType acceptedMediaType = getAcceptableMediaType(headers, new HashSet<String>(JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES), MediaType.APPLICATION_JSON_TYPE);
                builder = Response.ok(entity).header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
            } else {
                builder = Response.status(Status.NOT_FOUND).entity("No Entity with the parsed Id '" + id + "' is present on the ManagedSite '" + managedSite.getId() + "'!").header(HttpHeaders.ACCEPT, accepted);
            }
        }
    } catch (SiteException e) {
        String message = "Exception while deleting '" + id + "' from ManagedSite '" + managedSite.getId() + "'!";
        log.error(message, e);
        builder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(message + ' ' + e.getClass().getSimpleName() + ": " + e.getMessage()).header(HttpHeaders.ACCEPT, accepted);
    }
    //addCORSOrigin(servletContext, builder, headers);
    return builder.build();
}
Also used : ManagedSite(org.apache.stanbol.entityhub.servicesapi.site.ManagedSite) Site(org.apache.stanbol.entityhub.servicesapi.site.Site) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) ManagedSite(org.apache.stanbol.entityhub.servicesapi.site.ManagedSite) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) DELETE(javax.ws.rs.DELETE)

Example 7 with SiteException

use of org.apache.stanbol.entityhub.servicesapi.site.SiteException in project stanbol by apache.

the class DisambiguatorEngine method computeEnhancements.

/*
     * This function first evaluates all the possible ambiguations of each text annotation detected. the text
     * of all entities detected is used for making a Dbpedia query with all string for MLT that contain all
     * the other entities. The results obtained are used to calcualte new confidence values which are updated
     * in the metadata.
     */
@Override
public void computeEnhancements(ContentItem ci) throws EngineException {
    String textContent;
    Entry<IRI, Blob> textBlob = ContentItemHelper.getBlob(ci, SUPPORTED_MIMETYPES);
    if (textBlob != null) {
        try {
            textContent = ContentItemHelper.getText(textBlob.getValue());
        } catch (IOException e) {
            log.warn("Unable to retieve plain text content for ContentItem " + ci.getUri(), e);
            textContent = null;
        }
    } else {
        textContent = null;
    }
    Graph graph = ci.getMetadata();
    // (1) read the data from the content item
    String contentLangauge;
    DisambiguationData disData;
    ci.getLock().readLock().lock();
    try {
        contentLangauge = EnhancementEngineHelper.getLanguage(ci);
        // NOTE (rwesten): moved the parsing of the information from the
        // contentItem to static method of the Class holding those information
        // (similar as it already was for SavedEntity)
        // readEntities(loseConfidence, allEntities, textAnnotations, graph);
        disData = DisambiguationData.createFromContentItem(ci);
    } finally {
        ci.getLock().readLock().unlock();
    }
    // (2) Disambiguate the SavedEntities
    for (SavedEntity savedEntity : disData.textAnnotations.values()) {
        if (savedEntity.getSuggestions().size() <= 1) {
            // we need not to disambiguate if only one suggestion is present
            continue;
        }
        // NOTE: the site is determined from the
        // fise:TextAnnotation <-- dc:relation --
        // fise:EntityAnnotation -- entityhub:ste --> "{siteName}"^^xsd:string
        // data.
        // TODO: add configuration to include/exclude Sites by name
        Site site = siteManager.getSite(savedEntity.getSite());
        // potential types of entities
        Collection<String> types = null;
        // TODO: make configurable
        boolean casesensitive = false;
        String savedEntityLabel = casesensitive ? savedEntity.getName() : savedEntity.getName().toLowerCase();
        // Determine the context used for disambiguation
        // TODO: make this configurable options
        String disambiguationContext;
        // (0.a) The easiest way is to just use the selection context
        // disambiguationContext = savedEntity.getContext();
        // (0.b) Calculate a context based on a moving window
        String window = getDisambiguationContext(textContent, savedEntity.getName(), savedEntity.getStart(), 100);
        log.info("Use Window: '{}' for '{}'", window, savedEntity.getName());
        // (1) The contextSelections:
        // All other selected text within the selection context
        List<String> contextSelections = getSelectionsInContext(savedEntity.getName(), disData.allSelectedTexts, window);
        // savedEntity.getContext());
        disambiguationContext = unionString(false, contextSelections);
        // (2) I do not understand this variant (see comment for the
        // EntitiesInRange(..) method
        // List<String> L = EntitiesInRange(disData.directoryTextAnotation,
        // (savedEntity.getStart() + savedEntity.getEnd()) / 2);
        // disambiguationContext = unionString(false,contextSelections);
        // (3) one can build a combination of the above
        // disambiguationContext = unionString(true, //unique adds
        // Collections.singleton(savedEntity.getName()), //the selected text
        // Collections.singleton(context), //the context
        // contextSelections); //other selected parsed in the context
        // or just the name of the entity AND the context
        // disambiguationContext = unionString(false,
        // Collections.singleton(savedEntity.getName()),
        // contextSelections);
        // (4) TODO: I would also like to have the possibility to disambiguate
        // using URIs of Entities suggested for other TextAnnotations
        // within the context.
        // make the similarity query on the Entityhub using the collected
        // information
        QueryResultList<Entity> results;
        log.info(" - Query '{}' for {}@{} with context '{}'", new Object[] { site.getId(), savedEntityLabel, contentLangauge, disambiguationContext });
        if (!StringUtils.isBlank(disambiguationContext)) {
            try {
                results = query(site, savedEntityLabel, contentLangauge, disambiguationContext);
            } catch (SiteException e) {
                // TODO we could also try to catch those errors ...
                throw new EngineException("Unable to disambiguate Mention of '" + savedEntity.getName() + "' on Entityhub Site '" + site.getId() + "!", e);
            }
            log.debug(" - {} results returned by query {}", results.size(), results.getQuery());
            // match the results with the suggestions
            disambiguateSuggestions(results, savedEntity);
        } else {
            log.debug(" - not disambiguated because of empty context!");
        }
    }
    // (3) Write back the Results of the Disambiguation process
    // NOTE (rwesten): In the original version of Kritarth this was done as
    // part of (2) - disambiguation. This is now changed as in (2) the
    // disambiguation results are stored in the Suggestions and only
    // applied to the EnhancementStructure in (3). This allows to reduce the
    // coverage of the wirte lock needed to be applied to the ContentItem.
    ci.getLock().writeLock().lock();
    try {
        applyDisambiguationResults(graph, disData);
    } finally {
        ci.getLock().writeLock().unlock();
    }
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) IRI(org.apache.clerezza.commons.rdf.IRI) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) Blob(org.apache.stanbol.enhancer.servicesapi.Blob) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) IOException(java.io.IOException) Graph(org.apache.clerezza.commons.rdf.Graph) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException)

Example 8 with SiteException

use of org.apache.stanbol.entityhub.servicesapi.site.SiteException in project stanbol by apache.

the class ReferencedSiteSearcher method get.

@Override
public Entity get(IRI id, Set<IRI> fields, String... languages) {
    if (id == null || id.getUnicodeString().isEmpty()) {
        return null;
    }
    org.apache.stanbol.entityhub.servicesapi.model.Entity entity;
    Site site = getSearchService();
    if (site == null) {
        throw new IllegalStateException("ReferencedSite " + siteId + " is currently not available");
    }
    try {
        entity = site.getEntity(id.getUnicodeString());
    } catch (SiteException e) {
        throw new IllegalStateException("Exception while getting " + id + " from the ReferencedSite " + site.getId(), e);
    }
    if (entity != null) {
        Set<String> languageSet;
        if (languages == null || languages.length < 1) {
            languageSet = null;
        } else if (languages.length == 1) {
            languageSet = Collections.singleton(languages[0]);
        } else {
            languageSet = new HashSet<String>(Arrays.asList(languages));
        }
        return new EntityhubEntity(entity.getRepresentation(), fields, languageSet);
    } else {
        return null;
    }
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) HashSet(java.util.HashSet)

Example 9 with SiteException

use of org.apache.stanbol.entityhub.servicesapi.site.SiteException in project stanbol by apache.

the class ReferencedSiteSearcher method get.

@Override
public Representation get(String id, Set<String> includeFields) {
    if (id == null || id.isEmpty()) {
        return null;
    }
    Entity entity;
    Site site = getSearchService();
    if (site == null) {
        throw new IllegalStateException("ReferencedSite " + siteId + " is currently not available");
    }
    try {
        entity = site.getEntity(id);
    } catch (SiteException e) {
        throw new IllegalStateException("Exception while getting " + id + " from the ReferencedSite " + site.getId(), e);
    }
    return entity == null ? null : entity.getRepresentation();
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException)

Example 10 with SiteException

use of org.apache.stanbol.entityhub.servicesapi.site.SiteException in project stanbol by apache.

the class ReferencedSiteImpl method findEntities.

@Override
public QueryResultList<Entity> findEntities(FieldQuery query) throws SiteException {
    List<Entity> results;
    if (siteConfiguration.getCacheStrategy() == CacheStrategy.all) {
        try {
            // When using the Cache, directly get the representations!
            QueryResultList<Representation> representations = cache.findRepresentation((query));
            results = new ArrayList<Entity>(representations.size());
            for (Representation result : representations) {
                Entity entity = new EntityImpl(getId(), result, null);
                results.add(entity);
                initEntityMetadata(entity, siteMetadata, singletonMap(RdfResourceEnum.isChached.getUri(), (Object) Boolean.TRUE));
            }
            return new QueryResultListImpl<Entity>(query, results, Entity.class);
        } catch (YardException e) {
            if (entitySearcher == null) {
                throw new SiteException("Unable to execute query on Cache " + siteConfiguration.getCacheId(), e);
            } else {
                log.warn(String.format("Error while performing query on Cache %s! Try to use remote site %s as fallback!", siteConfiguration.getCacheId(), siteConfiguration.getQueryUri()), e);
            }
        }
    }
    QueryResultList<String> entityIds;
    if (entitySearcher == null) {
        throw new SiteException(String.format("The ReferencedSite %s does not support queries!", getId()));
    }
    try {
        entityIds = entitySearcher.findEntities(query);
    } catch (IOException e) {
        throw new SiteException(String.format("Unable to execute query on remote site %s with entitySearcher %s!", siteConfiguration.getQueryUri(), siteConfiguration.getEntitySearcherType()), e);
    }
    int numResults = entityIds.size();
    List<Entity> entities = new ArrayList<Entity>(numResults);
    int errors = 0;
    SiteException lastError = null;
    for (String id : entityIds) {
        Entity entity;
        try {
            entity = getEntity(id);
            if (entity == null) {
                log.warn("Unable to create Entity for ID that was selected by an FieldQuery (id=" + id + ")");
            }
            entities.add(entity);
            // use the position in the list as resultSocre
            entity.getRepresentation().set(RdfResourceEnum.resultScore.getUri(), Float.valueOf((float) numResults));
        } catch (SiteException e) {
            lastError = e;
            errors++;
            log.warn(String.format("Unable to get Representation for Entity " + "%s. -> %d Error%s for %d Entities in QueryResult (Reason:%s)", id, errors, errors > 1 ? "s" : "", entityIds.size(), e.getMessage()));
        }
        // decrease numResults because it is used as resultScore for
        // entities
        numResults--;
    }
    if (lastError != null) {
        if (entities.isEmpty()) {
            throw new SiteException("Unable to get anly Representations for " + "Entities selected by the parsed Query (Root-Cause is the " + "last Exception trown)", lastError);
        } else {
            log.warn(String.format("Unable to get %d/%d Represetnations for selected Entities.", errors, entityIds.size()));
            log.warn("Stack trace of the last Exception:", lastError);
        }
    }
    return new QueryResultListImpl<Entity>(query, entities, Entity.class);
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) ArrayList(java.util.ArrayList) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) IOException(java.io.IOException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException)

Aggregations

SiteException (org.apache.stanbol.entityhub.servicesapi.site.SiteException)17 Site (org.apache.stanbol.entityhub.servicesapi.site.Site)12 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)9 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)8 HashSet (java.util.HashSet)7 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)5 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)5 ManagedSiteException (org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException)5 MediaType (javax.ws.rs.core.MediaType)4 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)4 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)4 IOException (java.io.IOException)3 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)3 RdfRepresentation (org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)3 ManagedSite (org.apache.stanbol.entityhub.servicesapi.site.ManagedSite)3 ArrayList (java.util.ArrayList)2 Path (javax.ws.rs.Path)2 EntityImpl (org.apache.stanbol.entityhub.core.model.EntityImpl)2 LDPathSelect (org.apache.stanbol.entityhub.ldpath.query.LDPathSelect)2 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)2