Example usage for org.apache.commons.collections4.iterators IteratorIterable IteratorIterable

List of usage examples for org.apache.commons.collections4.iterators IteratorIterable IteratorIterable

Introduction

In this page you can find the example usage for org.apache.commons.collections4.iterators IteratorIterable IteratorIterable.

Prototype

public IteratorIterable(final Iterator<? extends E> iterator) 

Source Link

Document

Constructs a new IteratorIterable that will use the given iterator.

Usage

From source file:org.apache.bcel.BCELBenchmark.java

private Iterable<JarEntry> getClasses(JarFile jar) {
    return new IteratorIterable<>(
            new FilterIterator<>(new EnumerationIterator<>(jar.entries()), new Predicate<JarEntry>() {
                @Override/*from   w  w  w  .j a v  a  2 s .  com*/
                public boolean evaluate(JarEntry entry) {
                    return entry.getName().endsWith(".class");
                }
            }));
}

From source file:org.dkpro.core.io.nif.internal.Nif2DKPro.java

public void convert(Statement aContext, JCas aJCas) {
    Model m = aContext.getModel();//from w w w  .j  a  v  a  2  s  .co m

    final Resource tSentence = m.createResource(NIF.TYPE_SENTENCE);
    final Resource tWord = m.createResource(NIF.TYPE_WORD);
    final Resource tTitle = m.createResource(NIF.TYPE_TITLE);
    final Resource tParagraph = m.createResource(NIF.TYPE_PARAGRAPH);

    final Property pReferenceContext = m.createProperty(NIF.PROP_REFERENCE_CONTEXT);
    final Property pIsString = m.createProperty(NIF.PROP_IS_STRING);
    final Property pBeginIndex = m.createProperty(NIF.PROP_BEGIN_INDEX);
    final Property pEndIndex = m.createProperty(NIF.PROP_END_INDEX);
    final Property pLemma = m.createProperty(NIF.PROP_LEMMA);
    final Property pStem = m.createProperty(NIF.PROP_STEM);
    final Property pPosTag = m.createProperty(NIF.PROP_POS_TAG);
    final Property pTaIdentRef = m.createProperty(ITS.PROP_TA_IDENT_REF);
    final Property pTaClassRef = m.createProperty(ITS.PROP_TA_CLASS_REF);

    // Convert context node -> document text
    String text = m.getProperty(aContext.getSubject(), pIsString).getString();
    aJCas.setDocumentText(text);

    // Convert headings/titles
    Iterator<Resource> headingIterator = m.listResourcesWithProperty(RDF.type, tTitle)
            .filterKeep(res -> res.getProperty(pReferenceContext).getResource().equals(aContext.getSubject()));
    for (Resource nifTitle : new IteratorIterable<Resource>(headingIterator)) {
        int begin = nifTitle.getProperty(pBeginIndex).getInt();
        int end = nifTitle.getProperty(pEndIndex).getInt();
        Heading uimaHeading = new Heading(aJCas, begin, end);
        uimaHeading.addToIndexes();

        assert assertSanity(nifTitle, uimaHeading);
    }

    // Convert paragraphs
    Iterator<Resource> paragraphIterator = m.listResourcesWithProperty(RDF.type, tParagraph)
            .filterKeep(res -> res.getProperty(pReferenceContext).getResource().equals(aContext.getSubject()));
    for (Resource nifParagraph : new IteratorIterable<Resource>(paragraphIterator)) {
        int begin = nifParagraph.getProperty(pBeginIndex).getInt();
        int end = nifParagraph.getProperty(pEndIndex).getInt();
        Paragraph uimaParagraph = new Paragraph(aJCas, begin, end);
        uimaParagraph.addToIndexes();

        assert assertSanity(nifParagraph, uimaParagraph);
    }

    // Convert sentences
    List<Resource> nifSentences = m.listResourcesWithProperty(RDF.type, tSentence)
            .filterKeep(res -> res.getProperty(pReferenceContext).getResource().equals(aContext.getSubject()))
            .toList();
    nifSentences.sort((a, b) -> a.getProperty(pBeginIndex).getInt() - b.getProperty(pBeginIndex).getInt());
    for (Resource nifSentence : nifSentences) {
        int begin = nifSentence.getProperty(pBeginIndex).getInt();
        int end = nifSentence.getProperty(pEndIndex).getInt();
        Sentence uimaSentence = new Sentence(aJCas, begin, end);
        uimaSentence.addToIndexes();

        assert assertSanity(nifSentence, uimaSentence);
    }

    // Convert tokens
    Iterator<Resource> tokenIterator = m.listResourcesWithProperty(RDF.type, tWord)
            .filterKeep(res -> res.getProperty(pReferenceContext).getResource().equals(aContext.getSubject()));
    for (Resource nifWord : new IteratorIterable<Resource>(tokenIterator)) {
        int begin = nifWord.getProperty(pBeginIndex).getInt();
        int end = nifWord.getProperty(pEndIndex).getInt();
        Token uimaToken = new Token(aJCas, begin, end);
        uimaToken.addToIndexes();

        assert assertSanity(nifWord, uimaToken);

        // Convert lemma
        if (nifWord.hasProperty(pLemma)) {
            Lemma uimaLemma = new Lemma(aJCas, uimaToken.getBegin(), uimaToken.getEnd());
            uimaLemma.setValue(nifWord.getProperty(pLemma).getString());
            uimaLemma.addToIndexes();
            uimaToken.setLemma(uimaLemma);
        }

        // Convert stem
        if (nifWord.hasProperty(pLemma)) {
            Stem uimaStem = new Stem(aJCas, uimaToken.getBegin(), uimaToken.getEnd());
            uimaStem.setValue(nifWord.getProperty(pStem).getString());
            uimaStem.addToIndexes();
            uimaToken.setStem(uimaStem);
        }

        // Convert posTag (this is discouraged, the better alternative should be oliaLink)
        if (nifWord.hasProperty(pPosTag)) {
            String tag = nifWord.getProperty(pStem).getString();
            Type posTag = posMappingProvider.getTagType(tag);
            POS uimaPos = (POS) aJCas.getCas().createAnnotation(posTag, uimaToken.getBegin(),
                    uimaToken.getEnd());
            uimaPos.setPosValue(tag.intern());
            uimaPos.setCoarseValue(
                    uimaPos.getClass().equals(POS.class) ? null : uimaPos.getType().getShortName().intern());
            uimaPos.addToIndexes();
            uimaToken.setPos(uimaPos);
        }
    }

    // Convert named entities
    //
    // NIF uses taIdentRef to link to a unique instance of an entity and taClassRef to identify
    // the category of the entity. Named entity recognizers in DKPro Core just categorizes the
    // entity, e.g. as a person, location, or whatnot. For what NIF uses, we'd need a named
    // entity linker, not just a recognizer. Furthermore, the DKPro Core named entity
    // recognizers are not mapped to a common tag set (unlike e.g. POS which is mapped to 
    // the universal POS tags).
    // 
    // So, what we do here is treating the URI of the taClassRef in NIF simply as the
    // named entity category and store it. 
    //
    // Here we use duck-typing, i.e. it has a taClassRef property then it is likely a named
    // entity. NIF 2.1 [1] appears to introduce a representation of named entities using the
    // class "EntityOccurrence", but e.g. kore50 [2] doesn't seem to use that - it uses "Phrase"
    // instead.
    //
    // [1] http://nif.readthedocs.io/en/2.1-rc/prov-and-conf.html
    // [2] https://datahub.io/dataset/kore-50-nif-ner-corpus
    Set<Resource> nifNamedEntities1 = m.listResourcesWithProperty(pTaIdentRef)
            .filterKeep(res -> res.getProperty(pReferenceContext).getResource().equals(aContext.getSubject()))
            .toSet();
    Set<Resource> nifNamedEntities2 = m.listResourcesWithProperty(pTaIdentRef)
            .filterKeep(res -> res.getProperty(pReferenceContext).getResource().equals(aContext.getSubject()))
            .toSet();
    Set<Resource> nifNamedEntities = new HashSet<Resource>();
    nifNamedEntities.addAll(nifNamedEntities1);
    nifNamedEntities.addAll(nifNamedEntities2);
    for (Resource nifNamedEntity : nifNamedEntities) {
        int begin = nifNamedEntity.getProperty(pBeginIndex).getInt();
        int end = nifNamedEntity.getProperty(pEndIndex).getInt();
        NamedEntity uimaNamedEntity = new NamedEntity(aJCas, begin, end);
        if (nifNamedEntity.hasProperty(pTaClassRef)) {
            uimaNamedEntity.setValue(nifNamedEntity.getProperty(pTaClassRef).getResource().getURI());
        }
        if (nifNamedEntity.hasProperty(pTaIdentRef)) {
            uimaNamedEntity.setIdentifier(nifNamedEntity.getProperty(pTaIdentRef).getResource().getURI());
        }
        uimaNamedEntity.addToIndexes();

        assert assertSanity(nifNamedEntity, uimaNamedEntity);
    }
}

From source file:org.dkpro.core.io.rdf.internal.Rdf2Uima.java

public static void convert(Statement aContext, JCas aJCas) throws CASException {
    Model m = aContext.getModel();/*  w  w  w. j  a v a  2s.co m*/

    // Set up names
    Resource tView = m.createResource(RdfCas.TYPE_VIEW);
    Resource tFeatureStructure = m.createResource(RdfCas.TYPE_FEATURE_STRUCTURE);
    Property pIndexedIn = m.createProperty(RdfCas.PROP_INDEXED_IN);

    Map<Resource, FeatureStructure> fsIndex = new HashMap<>();

    // Convert the views/SofAs
    Map<Resource, JCas> viewIndex = new HashMap<>();
    Iterator<Resource> viewIter = m.listSubjectsWithProperty(RDF.type, tView);
    for (Resource view : new IteratorIterable<Resource>(viewIter)) {
        JCas viewJCas = convertView(view, aJCas);
        viewIndex.put(view, viewJCas);
        fsIndex.put(view, viewJCas.getSofa());
    }

    // Convert the FSes but without setting their feature values yet - we cannot fill
    // the feature values just set because some of them may point to FSes not yet created
    List<Resource> fses = m.listSubjectsWithProperty(RDF.type, tFeatureStructure).toList();
    for (Resource fs : fses) {
        FeatureStructure uimaFS = initFS(fs.as(OntResource.class), aJCas);
        fsIndex.put(fs, uimaFS);
    }

    // Now fill the FSes with their feature values
    for (Resource fs : fses) {
        convertFS(fs.as(OntResource.class), aJCas, fsIndex);
    }

    // Finally add the FSes to the indexes of the respective views
    for (Resource fs : fses) {
        Iterator<Statement> indexedInIter = fs.listProperties(pIndexedIn);
        for (Statement indexedIn : new IteratorIterable<Statement>(indexedInIter)) {
            JCas viewJCas = viewIndex.get(indexedIn.getResource());
            viewJCas.addFsToIndexes(fsIndex.get(fs));
        }
    }
}

From source file:org.dkpro.core.io.rdf.internal.Rdf2Uima.java

public static FeatureStructure convertFS(OntResource aFS, JCas aJCas,
        Map<Resource, FeatureStructure> aFsIndex) {
    FeatureStructure fs = aFsIndex.get(aFS);

    Iterator<Statement> stmtIter = aFS.listProperties();
    for (Statement stmt : new IteratorIterable<Statement>(stmtIter)) {
        // Skip all non-features
        if (!stmt.getPredicate().getURI().startsWith("uima:")) {
            // System.out.println("Skipping: " + stmt);
            continue;
        }//from w  w  w.  j  a  v a 2  s .  c  o m

        String featureName = StringUtils.substringAfterLast(stmt.getPredicate().getURI(), "-");
        Feature uimaFeat = fs.getType().getFeatureByBaseName(featureName);

        // Cannot update start/end of document annotation because that FS is already indexed, so
        // we skip those
        if (fs == aJCas.getDocumentAnnotationFs() && (CAS.FEATURE_BASE_NAME_BEGIN.equals(featureName)
                || CAS.FEATURE_BASE_NAME_END.equals(featureName))) {
            System.out.println("Skipping: " + stmt);
            continue;
        }

        if (uimaFeat.getRange().isPrimitive()) {
            switch (uimaFeat.getRange().getName()) {
            case CAS.TYPE_NAME_BOOLEAN:
                fs.setBooleanValue(uimaFeat, stmt.getObject().asLiteral().getBoolean());
                break;
            case CAS.TYPE_NAME_BYTE:
                fs.setByteValue(uimaFeat, stmt.getObject().asLiteral().getByte());
                break;
            case CAS.TYPE_NAME_DOUBLE:
                fs.setDoubleValue(uimaFeat, stmt.getObject().asLiteral().getDouble());
                break;
            case CAS.TYPE_NAME_FLOAT:
                fs.setFloatValue(uimaFeat, stmt.getObject().asLiteral().getFloat());
                break;
            case CAS.TYPE_NAME_INTEGER:
                fs.setIntValue(uimaFeat, stmt.getObject().asLiteral().getInt());
                break;
            case CAS.TYPE_NAME_LONG:
                fs.setLongValue(uimaFeat, stmt.getObject().asLiteral().getLong());
                break;
            case CAS.TYPE_NAME_SHORT:
                fs.setShortValue(uimaFeat, stmt.getObject().asLiteral().getShort());
                break;
            case CAS.TYPE_NAME_STRING: {
                fs.setStringValue(uimaFeat, stmt.getObject().asLiteral().getString());
                break;
            }
            default:
                throw new IllegalArgumentException("Feature [" + uimaFeat.getName()
                        + "] has unsupported primitive type [" + uimaFeat.getRange().getName() + "]");
            }
        } else {
            FeatureStructure targetUimaFS = aFsIndex.get(stmt.getObject().asResource());
            if (targetUimaFS == null) {
                throw new IllegalStateException(
                        "No UIMA FS found for [" + stmt.getObject().asResource().getURI() + "]");
            }
            fs.setFeatureValue(uimaFeat, targetUimaFS);
        }
    }

    return fs;
}

From source file:org.xwiki.watchlist.internal.DefaultWatchListNotifier.java

@Override
public void sendNotification(Collection<String> subscribers, List<WatchListEvent> events,
        Map<String, Object> notificationData) throws WatchListException {
    try {// w  w  w .j  av a 2  s . c  o m
        // FIXME: Temporary, until we move to all references.
        List<DocumentReference> subscriberReferences = getSubscriberReferences(subscribers);

        // Source
        Map<String, Object> source = new HashMap<>();
        source.put(EventsAndSubscribersSource.SUBSCRIBERS_PARAMETER, subscriberReferences);
        source.put(EventsAndSubscribersSource.EVENTS_PARAMETER, events);

        // Parameters
        Map<String, Object> parameters = new HashMap<>();
        parameters.put(WatchListEventMimeMessageFactory.HINT_PARAMETER, "template");
        parameters.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER,
                notificationData.get(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER));
        parameters.put(WatchListEventMimeMessageFactory.SKIP_CONTEXT_USER_PARAMETER,
                notificationData.get(WatchListEventMimeMessageFactory.SKIP_CONTEXT_USER_PARAMETER));
        parameters.put(WatchListEventMimeMessageFactory.ATTACH_AUTHOR_AVATARS_PARAMETER,
                notificationData.get(WatchListEventMimeMessageFactory.ATTACH_AUTHOR_AVATARS_PARAMETER));
        Map<String, Object> templateFactoryParameters = getTemplateFactoryParameters(notificationData);
        parameters.put(WatchListEventMimeMessageFactory.PARAMETERS_PARAMETER, templateFactoryParameters);

        // Create the message iterator and the other mail sender parameters.
        Iterator<MimeMessage> messageIterator = messageFactory.createMessage(source, parameters);
        Session session = this.sessionFactory.create(Collections.<String, String>emptyMap());
        MailListener mailListener = mailListenerProvider.get();

        // Pass it to the message sender to send it asynchronously.
        // FIXME: !? There must be a better way instead of using IteratorIterable.
        mailSender.sendAsynchronously(new IteratorIterable<MimeMessage>(messageIterator), session,
                mailListener);
    } catch (Exception e) {
        throw new WatchListException(
                String.format("Failed to send notification to subscribers [%s]", subscribers), e);
    }
}