#
# <one line to give the library's name and an idea of what it does.>
# Copyright (C) 2015  <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/>.
# 

import sidddescriptormeasure
import grafon.constants as constants

class SIDDSumDescriptorsMeasure(
  sidddescriptormeasure.SIDDDescriptorMeasure):
  """!
  Calcualtes the similarity based on the nodes decriptors.
  
  As a similarity is returned the sum of all values of nodes 
  descriptors normalized with the number of same nodes into two graphs
  """

  def __init__(self, options, str_name = 'SIDDSumDescriptorsMeasure'):
    """!
    Constructor

    @param options: a dictionary containing options for this measure
    @para str_name: default value same as the class name 
                    "SIDDSumDescriptorsMeasure"
    """
    super(SIDDSumDescriptorsMeasure, self).__init__(options, str_name)

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

    For each node which is into g1 and g2 calculates the SIDD and 
    returns the sum of its.

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

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

    @return Sum of SIDD
    @rtype: double
    """
    if not g1 or not g2:
      return 0
    dict_node_descr = self.prepare_node_descriptor_dictionary(g1, g2)
    if not len(dict_node_descr):
      return 0
    norm = len(set(g1.nodes() + g2.nodes()))
    return sum(dict_node_descr.values()) / norm