Search in sources :

Example 1 with FilterIterator

use of org.apache.commons.collections.iterators.FilterIterator in project stanbol by apache.

the class ProcessingState method initNextSentence.

/**
 * Correctly initialise {@link #sentence}, {@link #chunks}, {@link #chunk}
 * and {@link #tokenIndex} for the next element of {@link #sections}. If
 * no further sentences are to process it simple sets {@link #sentence},
 * {@link #chunks}, {@link #chunk} and {@link #tokenIndex} to <code>null</code>
 */
private boolean initNextSentence() {
    section = null;
    processableTokensIterator = null;
    consumedIndex = -1;
    boolean foundLinkableToken = false;
    while (!foundLinkableToken && sections.hasNext()) {
        section = sections.next();
        if (consumedSectionIndex > section.getStart()) {
            log.debug(" > skipping {} because an other section until Index {} " + "was already processed. This is not an error, but indicates that" + "multiple NLP framewords do contribute divergating Sentence annotations", section, consumedSectionIndex);
            // ignore this section
            continue;
        }
        consumedSectionIndex = section.getEnd();
        SectionData sectionData = new SectionData(tpc, section, enclosedSpanTypes, isUnicaseLanguage);
        // TODO: It would be better to use a SectionData field instead
        tokens = sectionData.getTokens();
        section = sectionData.section;
        foundLinkableToken = sectionData.hasLinkableToken();
    }
    processableTokensIterator = new FilterIterator(tokens.iterator(), PROCESSABLE_TOKEN_OREDICATE);
    return foundLinkableToken;
}
Also used : FilterIterator(org.apache.commons.collections.iterators.FilterIterator)

Example 2 with FilterIterator

use of org.apache.commons.collections.iterators.FilterIterator in project sling by apache.

the class DefaultConfigurationResourceResolvingStrategy method findConfigRefs.

/**
     * Searches the resource hierarchy upwards for all config references and returns them.
     * @param refs List to add found resources to
     * @param startResource Resource to start searching
     */
@SuppressWarnings("unchecked")
private Iterator<String> findConfigRefs(final Resource startResource, final Collection<String> bucketNames) {
    // collect all context path resources (but filter out those without config reference)
    final Iterator<ContextResource> contextResources = new FilterIterator(contextPathStrategy.findContextResources(startResource), new Predicate() {

        @Override
        public boolean evaluate(Object object) {
            ContextResource contextResource = (ContextResource) object;
            return StringUtils.isNotBlank(contextResource.getConfigRef());
        }
    });
    // get config resource path for each context resource, filter out items where not reference could be resolved
    final Iterator<String> configPaths = new Iterator<String>() {

        private final List<ContextResource> relativePaths = new ArrayList<>();

        private String next = seek();

        private String useFromRelativePathsWith;

        private String seek() {
            String val = null;
            while (val == null && (useFromRelativePathsWith != null || contextResources.hasNext())) {
                if (useFromRelativePathsWith != null) {
                    final ContextResource contextResource = relativePaths.remove(relativePaths.size() - 1);
                    val = checkPath(contextResource, useFromRelativePathsWith + "/" + contextResource.getConfigRef(), bucketNames);
                    if (val != null) {
                        log.trace("+ Found reference for context path {}: {}", contextResource.getResource().getPath(), val);
                    }
                    if (relativePaths.isEmpty()) {
                        useFromRelativePathsWith = null;
                    }
                } else {
                    final ContextResource contextResource = contextResources.next();
                    val = contextResource.getConfigRef();
                    // if absolute path found we are (probably) done
                    if (val != null && val.startsWith("/")) {
                        val = checkPath(contextResource, val, bucketNames);
                    }
                    if (val != null) {
                        final boolean isAbsolute = val.startsWith("/");
                        if (isAbsolute && !relativePaths.isEmpty()) {
                            useFromRelativePathsWith = val;
                            val = null;
                        } else if (!isAbsolute) {
                            relativePaths.add(0, contextResource);
                            val = null;
                        }
                    }
                    if (val != null) {
                        log.trace("+ Found reference for context path {}: {}", contextResource.getResource().getPath(), val);
                    }
                }
            }
            if (val == null && !relativePaths.isEmpty()) {
                log.error("Relative references not used as no absolute reference was found: {}", relativePaths);
            }
            return val;
        }

        @Override
        public boolean hasNext() {
            return next != null;
        }

        @Override
        public String next() {
            if (next == null) {
                throw new NoSuchElementException();
            }
            final String result = next;
            next = seek();
            return result;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    // expand paths and eliminate duplicates
    return new PathEliminateDuplicatesIterator(new PathParentExpandIterator(config.configPath(), configPaths));
}
Also used : Predicate(org.apache.commons.collections.Predicate) ContextResource(org.apache.sling.caconfig.resource.spi.ContextResource) PathEliminateDuplicatesIterator(org.apache.sling.caconfig.resource.impl.util.PathEliminateDuplicatesIterator) PathEliminateDuplicatesIterator(org.apache.sling.caconfig.resource.impl.util.PathEliminateDuplicatesIterator) Iterator(java.util.Iterator) ArrayIterator(org.apache.commons.collections.iterators.ArrayIterator) PathParentExpandIterator(org.apache.sling.caconfig.resource.impl.util.PathParentExpandIterator) FilterIterator(org.apache.commons.collections.iterators.FilterIterator) FilterIterator(org.apache.commons.collections.iterators.FilterIterator) ArrayList(java.util.ArrayList) List(java.util.List) PathParentExpandIterator(org.apache.sling.caconfig.resource.impl.util.PathParentExpandIterator) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with FilterIterator

use of org.apache.commons.collections.iterators.FilterIterator in project eclipse-integration-commons by spring-projects.

the class ValidProjectFilter method iterator.

@SuppressWarnings("unchecked")
public Iterator<Entry> iterator() {
    final Set<String> validProjects = new HashSet<String>();
    IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    for (IProject project : projects) {
        // Test if the selected project has the given nature
        if (project.isAccessible() && SpringCoreUtils.hasNature(project, natureId)) {
            validProjects.add(project.getName());
        }
    }
    return new FilterIterator(target.iterator(), new Predicate() {

        public boolean evaluate(Object _entry) {
            Entry entry = (Entry) _entry;
            return validProjects.contains(entry.getProject());
        }
    });
}
Also used : Entry(org.springsource.ide.eclipse.commons.core.Entry) FilterIterator(org.apache.commons.collections.iterators.FilterIterator) IProject(org.eclipse.core.resources.IProject) HashSet(java.util.HashSet) Predicate(org.apache.commons.collections.Predicate)

Example 4 with FilterIterator

use of org.apache.commons.collections.iterators.FilterIterator in project stanbol by apache.

the class NamedEntityTokenFilter method reset.

@SuppressWarnings("unchecked")
@Override
public void reset() throws IOException {
    super.reset();
    nePhrases = new LinkedList<Chunk>();
    neChunks = new FilterIterator(at.getChunks(), new NamedEntityPredicate());
}
Also used : FilterIterator(org.apache.commons.collections.iterators.FilterIterator) Chunk(org.apache.stanbol.enhancer.nlp.model.Chunk)

Aggregations

FilterIterator (org.apache.commons.collections.iterators.FilterIterator)4 Predicate (org.apache.commons.collections.Predicate)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 ArrayIterator (org.apache.commons.collections.iterators.ArrayIterator)1 PathEliminateDuplicatesIterator (org.apache.sling.caconfig.resource.impl.util.PathEliminateDuplicatesIterator)1 PathParentExpandIterator (org.apache.sling.caconfig.resource.impl.util.PathParentExpandIterator)1 ContextResource (org.apache.sling.caconfig.resource.spi.ContextResource)1 Chunk (org.apache.stanbol.enhancer.nlp.model.Chunk)1 IProject (org.eclipse.core.resources.IProject)1 Entry (org.springsource.ide.eclipse.commons.core.Entry)1