/* ==========================================
 * JGraphT : a free Java graph-theory library
 * ==========================================
 *
 * Project Info:  http://jgrapht.sourceforge.net/
 * Project Creator:  Barak Naveh (http://sourceforge.net/users/barak_naveh)
 *
 * (C) Copyright 2003-2008, by Barak Naveh and Contributors.
 *
 * This program and the accompanying materials are dual-licensed under
 * either
 *
 * (a) the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation, or (at your option) any
 * later version.
 *
 * or (per the licensee's choosing)
 *
 * (b) the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation.
 */
/* -------------------------
 * IsomorphicGraphMapping.java
 * -------------------------
 * (C) Copyright 2015, by Fabian Späh and Contributors.
 *
 * Original Author:  Fabian Späh
 * Contributor(s):   Rita Dobler
 *
 * $Id$
 *
 * Changes
 * -------
 * 20-Jun-2015 : Initial revision (FS);
 *
 */
package org.jgrapht.alg.isomorphism;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.jgrapht.GraphMapping;


/**
 * @author Fabian Späh
 *
 * @param <V> the type of the vertices
 * @param <E> the type of the edges
 */
public class IsomorphicGraphMapping<V, E>
    implements GraphMapping<V, E>
{

    GraphOrdering<V, E> g1,
                        g2;

    int[] core1,
          core2;

    /**
     * @param g1 the first graph
     * @param g2 the second graph which is a possible subgraph of g1
     * @param core1
     * @param core2
     */
    public IsomorphicGraphMapping(
                    GraphOrdering<V, E> g1,
                    GraphOrdering<V, E> g2,
                    int[] core1,
                    int[] core2)
    {
        this.g1    = g1;
        this.g2    = g2;
        this.core1 = core1.clone();
        this.core2 = core2.clone();
    }

    @Override
    public V getVertexCorrespondence(V v, boolean forward) {
        GraphOrdering<V, E> firstGraph, secondGraph;
        int[] core;

        if (forward) {
            firstGraph  = g1;
            secondGraph = g2;
            core        = core1;
        } else {
            firstGraph  = g2;
            secondGraph = g1;
            core        = core2;
        }

        int vNumber = firstGraph.getVertexNumber(v),
            uNumber = core[vNumber];

        if (uNumber == VF2State.NULL_NODE)
            return null;

        return secondGraph.getVertex(uNumber);
    }

    @Override
    public E getEdgeCorrespondence(E e, boolean forward) {
        GraphOrdering<V, E> firstGraph, secondGraph;
        int[] core;

        if (forward) {
            firstGraph  = g1;
            secondGraph = g2;
            core        = core1;
        } else {
            firstGraph  = g2;
            secondGraph = g1;
            core        = core2;
        }

        int[] eNumbers = firstGraph.getEdgeNumbers(e);
        if (core[eNumbers[0]] == VF2State.NULL_NODE ||
                        core[eNumbers[1]] == VF2State.NULL_NODE)
            return null;

        return secondGraph.getEdge(core[eNumbers[0]], core[eNumbers[1]]);
    }

    /**
     * Checks if a vertex v from the first graph has a corresponding vertex in 
     * the second graph
     * @param v
     * @return is there a corresponding vertex to v in the subgraph
     */
    public boolean hasVertexCorrespondence(V v) {
        return getVertexCorrespondence(v, true) != null;
    }

    /**
     * Checks if a edge e from the first graph has a corresponding edge in the 
     * second graph
     * @param e
     * @return is there a corresponding edge to e in the subgraph
     */
    public boolean hasEdgeCorrespondence(E e) {
        return getEdgeCorrespondence(e, true) != null;
    }

    @Override
    public String toString() {
        String str = "[";
        Set<V> vertexSet = g1.getGraph().vertexSet();
        Map<String, V> vertexMap = new TreeMap<String, V>();

        for (V v : vertexSet)
            vertexMap.put(v.toString(), v);

        int i = 0;
        for (Map.Entry<String, V> entry : vertexMap.entrySet())   {
            V u = getVertexCorrespondence(entry.getValue(), true);
            str += (i++ == 0 ? "" : " ") + entry.getKey() + "=" +
                            (u == null ? "~~" : u);
        }

        return str + "]";
    }

    /**
     * Checks for equality. Assuming both are mappings on the same graphs.
     * 
     * @param rel the corresponding SubgraphIsomorphismRelation
     * @return do both relations map to the same vertices
     */
    public boolean equals(GraphMapping<V, E> rel) {
        for (V v : g2.getGraph().vertexSet()) {
            if (!getVertexCorrespondence(v, false).equals(
                            rel.getVertexCorrespondence(v, false)))
                return false;
        }

        return true;
    }

}

@@ -1165,27 +1165,185 @@
 %0A * 
-@author Fabian Sp%C3%A4h
+This class represents a GraphMapping between two (subgraph)isomorphic graphs.%0A * In the subgraph isomorphic case, the second one is assumed to be a subgraph%0A * of the first one.
 %0A *%0A
@@ -4675,35 +4675,15 @@
 ing 
-SubgraphIsomorphismRelation
+mapping
 %0A   
@@ -4764,22 +4764,30 @@
 boolean 
-e
+isE
 qual
-s
+Mapping
 (GraphMa

refactor GraphMapping