#
# <one line to give the library's name and an idea of what it does.>
# Copyright (C) 2014  <copyright holder> <email>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
#

from grafon.mergers.transformation.maxcommonsubgraph import MaxCommonSubGraph
from grafon.mergers.transformation.mincommonsupergraph import MinCommonSuperGraph

import structuralsimilaritymeasure

class DMCSSimilarityMeasure(
  structuralsimilaritymeasure.StructuralSimilarityMeasure):
  """!
                       |mcs(G1, G2)|
  d_MCS(G1, G2) = 1 - ----------------
                       max(|G1|, |G2|)

  In this implementation the measure is reversed to measure the similarity.
  """

  def __init__(self, options, str_name = 'DMCSSimilarityMeasure'):
    """!
    Constructor
    @param options: a dictionary containing additional options for this measure
    @para str_name: default value same as the class name 
                     "DMCSSimilarityMeasure"
    """
    super(DMCSSimilarityMeasure, self).__init__(options, str_name)

  def calculate(self, g1, g2):
    """!
    Implementation of abstract calculate function from interface. 

    Calculates the  dmcs

    @param g1 first graph
    @type g1: networgx.DiGraph/networkx.MultiDiGraph

    @param g2 second graph
    @type g1: networgx.DiGraph/networkx.MultiDiGraph

    @return Count of same nodes normalized to <0,...,1>
    @rtype: double
    """
    if not g1 or not g2:
      return 0

    normf = 0.0 + max(len(g1.nodes()), len(g2.nodes()))
    mcs = MinCommonSuperGraph(None)
    gmcs = mcs.merge(g1, g2)
    return (len(gmcs.nodes()) / normf) if normf else 0.0

class DWGUSimilarityMeasure(
  structuralsimilaritymeasure.StructuralSimilarityMeasure):
  """!
                               |mcs(G1, G2)|
  d_WGU(G1, G2) = 1 - -----------------------------
                       |G1| + |G2| - |mcs(G1, G2)|

  In this implementation the measure is reversed to measure the similarity.
  """

  def __init__(self, options, str_name = 'DWGUSimilarityMeasure'):
    """!
    Constructor
    @param options: a dictionary containing additional options for this measure
    @para str_name: default value same as the class name 
                     "DWGUSimilarityMeasure"
    """
    super(DWGUSimilarityMeasure, self).__init__(options, str_name)

  def calculate(self, g1, g2):
    """!
    Implementation of abstract calculate function from interface. 

    Calculates the  dwgu

    @param g1: first rgaph
    @param g2: second rgaph
    @return Count of same nodes normalized to <0,...,1>
    """
    if not g1 or not g2:
      return 0

    mcs = MinCommonSuperGraph(None)
    gmcs = mcs.merge(g1, g2)
    mlen = len(gmcs.nodes())
    return (mlen / (0.0 + len(g1.nodes()) + len(g2.nodes()) - mlen)) \
      if mlen else 0.0


class DUGUSimilarityMeasure(
  structuralsimilaritymeasure.StructuralSimilarityMeasure):
  """!
  d_UGU(G1, G2) = |G1| + |G2| - 2 * |mcs(G1, G2)|

  WARNING: This measure can measure the disimilarity!
  The value equal to 0 means that the two graphs are the same!
  """

  def __init__(self, options, str_name = 'DUGUSimilarityMeasure'):
    """!
    Constructor
    @param options: a dictionary containing additional options for this measure
    @para str_name: default value same as the class name 
                     "DUGUSimilarityMeasure"
    """
    super(DUGUSimilarityMeasure, self).__init__(options, str_name)

  def calculate(self, g1, g2):
    """!
    Implementation of abstract calculate function from interface. 

    Calculates the  dwgu

    @param g1: first rgaph
    @param g2: second rgaph
    @return Count of same nodes normalized to <0,...,1>
    """
    if not g1 or not g2:
      return 0

    mcs = MinCommonSuperGraph(None)
    gmcs = mcs.merge(g1, g2)
    return len(g1.nodes()) + len(g2.nodes()) - (2 * len(gmcs.nodes()))


class DMMCSSimilarityMeasure(
  structuralsimilaritymeasure.StructuralSimilarityMeasure):
  """!
  d_MMCS(G1, G2) = |MCS(G1, G2)| - |mcs(G1, G2)|

  WARNING: This measure can measure the disimilarity!
  The value equal to 0 means that the two graphs are the same!
  """

  def __init__(self, options, str_name = 'DMMCSSimilarityMeasure'):
    """!
    Constructor
    @param options: a dictionary containing additional options for this measure
    @para str_name: default value same as the class name 
                     "DMMCSSimilarityMeasure"
    """
    super(DMMCSSimilarityMeasure, self).__init__(options, str_name)

  def calculate(self, g1, g2):
    """!
    Implementation of abstract calculate function from interface. 

    Calculates the  dwgu

    @param g1: first rgaph
    @param g2: second rgaph
    @return Count of same nodes normalized to <0,...,1>
    """
    if not g1 or not g2:
      return 0

    mcs = MinCommonSuperGraph(None)
    MCS = MaxCommonSubGraph(None)

    gmcs = mcs.merge(g1, g2)
    gMCS = MCS.merge(g1, g2)

    return len(gMCS.nodes()) - len(gmcs.nodes())


class DMMCSNSimilarityMeasure(
  structuralsimilaritymeasure.StructuralSimilarityMeasure):
  """!
                        |mcs(G1, G2)|
  d_MMCSN(G1, G2) = 1 - --------------
                        |MCS(G1, G2)| 

  In this implementation the measure is reversed to measure the similarity.
  """

  def __init__(self, options, str_name = 'DMMCSNSimilarityMeasure'):
    """!
    Constructor
    @param options: a dictionary containing additional options for this measure
    @para str_name: default value same as the class name 
                     "DMMCSNSimilarityMeasure"
    """
    super(DMMCSNSimilarityMeasure, self).__init__(options, str_name)

  def calculate(self, g1, g2):
    """!
    Implementation of abstract calculate function from interface. 

    Calculates the  dwgu

    @param g1: first rgaph
    @param g2: second rgaph
    @return Count of same nodes normalized to <0,...,1>
    """
    if not g1 or not g2:
      return 0

    mcs = MinCommonSuperGraph(None)
    MCS = MaxCommonSubGraph(None)

    gmcs = mcs.merge(g1, g2)
    gMCS = MCS.merge(g1, g2)
    normf = 0.0 +  len(gMCS.nodes())
    return (len(gmcs.nodes()) /  normf) if normf else 0.0