private Optional<ImmutableSubstitution<ImmutableTerm>> unifyPairs(ImmutableList<TermPair> originalPairs) {

        /*
         * Some pairs will be removed, some others will be added.
         */
        List<TermPair> allPairs = new LinkedList<>(originalPairs);
        Queue<TermPair> pairsToVisit = new LinkedList<>(originalPairs);

        try {
            /*
             * TODO: explain
             */
            while (!pairsToVisit.isEmpty()) {
                TermPair currentPair = pairsToVisit.poll();
                if (currentPair.canBeRemoved()) {
                    continue;
                }
                ImmutableSubstitution<ImmutableTerm> substitution = currentPair.getSubstitution();

                List<TermPair> additionalPairs = new ArrayList<>();
                /*
                 * TODO: explain
                 */
                Iterator<TermPair> it = allPairs.iterator();
                while (it.hasNext()) {
                    TermPair pairToUpdate = it.next();
                    if (pairToUpdate == currentPair) {
                        continue;
                    } else {
                        additionalPairs.addAll(pairToUpdate.applySubstitution(substitution));
                        if (pairToUpdate.canBeRemoved()) {
                            it.remove();
                        }
                    }
                }
                allPairs.addAll(additionalPairs);
                pairsToVisit.addAll(additionalPairs);
            }
            return Optional.of(convertPairs2Substitution(allPairs));
        }
        catch (UnificationException e) {
            return Optional.empty();
        }
    }
private Optional < ImmutableSubstitution < ImmutableTerm > > unifyPairs ( ImmutableList < TermPair > originalPairs ) { List < TermPair > allPairs = new LinkedList < > ( originalPairs ) ; Queue < TermPair > pairsToVisit = new LinkedList < > ( originalPairs ) ; try { while ( ! pairsToVisit . isEmpty ( ) ) { TermPair currentPair = pairsToVisit . poll ( ) ; if ( currentPair . canBeRemoved ( ) ) { continue ; } ImmutableSubstitution < ImmutableTerm > substitution = currentPair . getSubstitution ( ) ; List < TermPair > additionalPairs = new ArrayList < > ( ) ; Iterator < TermPair > it = allPairs . iterator ( ) ; while ( it . hasNext ( ) ) { TermPair pairToUpdate = it . next ( ) ; if ( pairToUpdate == currentPair ) { continue ; } else { additionalPairs . addAll ( pairToUpdate . applySubstitution ( substitution ) ) ; if ( pairToUpdate . canBeRemoved ( ) ) { it . remove ( ) ; } } } allPairs . addAll ( additionalPairs ) ; pairsToVisit . addAll ( additionalPairs ) ; } return Optional . of ( convertPairs2Substitution ( allPairs ) ) ; } catch ( UnificationException e ) { return Optional . empty ( ) ; } }
TODO: explain