/*
 * Copyright 2018 Dmitry Ustalov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.nlpub.watset.util;

import org.jgrapht.Graph;
import org.jgrapht.Graphs;
import org.jgrapht.graph.SimpleWeightedGraph;
import org.jgrapht.graph.builder.GraphBuilder;

import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;

public interface Neighbors {
    static <V, E> Iterator<V> neighborIterator(Graph<V, E> graph, V node) {
        return graph.edgesOf(node).stream().
                map(e -> Graphs.getOppositeVertex(graph, e, node)).
                iterator();
    }

    static <V, E> Set<V> neighborSetOf(Graph<V, E> graph, V node) {
        return graph.edgesOf(node).stream().
                map(e -> Graphs.getOppositeVertex(graph, e, node)).
                collect(Collectors.toSet());
    }

    static <V, E> Graph<V, E> neighborhoodGraph(Graph<V, E> graph, V node) {
        final GraphBuilder<V, E, ? extends SimpleWeightedGraph<V, E>> builder = SimpleWeightedGraph.createBuilder(graph.getEdgeSupplier());

        final Set<V> neighborhood = neighborSetOf(graph, node);

        for (final V neighbor : neighborhood) {
            builder.addVertex(neighbor);

            for (final E edge : graph.edgesOf(neighbor)) {
                final V opposite = Graphs.getOppositeVertex(graph, edge, neighbor);

                if (neighborhood.contains(opposite)) {
                    builder.addVertex(opposite);
                    builder.addEdge(neighbor, opposite, edge, graph.getEdgeWeight(edge));
                }
            }
        }

        return builder.buildAsUnmodifiable();
    }
}

@@ -1336,32 +1336,280 @@
 oSet());%0A    %7D%0A%0A
+    /**%0A     * Extract a neighborhood graph for the given node from a graph.%0A     *%0A     * @param graph graph%0A     * @param node  target node%0A     * @param %3CV%3E   node class%0A     * @param %3CE%3E   edge class%0A     * @return a neighborhood graph%0A     */%0A
     static %3CV, E
@@ -1873,16 +1873,66 @@
 , node);
+%0A        neighborhood.forEach(builder::addVertex);
 %0A%0A      
@@ -1977,50 +1977,8 @@
 ) %7B%0A
-            builder.addVertex(neighbor);%0A%0A
     
@@ -2060,260 +2060,324 @@
 l V 
-opposit
+sourc
 e = 
-G
+g
 raph
-s
 .get
-OppositeVertex(graph, edge, neighbor);%0A%0A                if (neighborhood.contains(opposite)) %7B%0A                    builder.addVertex(opposite);%0A                    builder.addEdge(neighbor, opposite, edge, graph.getEdgeWeight(edge)
+EdgeSource(edge);%0A                final V target = graph.getEdgeTarget(edge);%0A%0A                if (neighborhood.contains(source) && neighborhood.contains(target)) %7B%0A                    final double weight = graph.getEdgeWeight(edge);%0A                    builder.addEdge(source, target, edge, weight
 );%0A 

Simplify neighborhoodGraph