#
# <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/>.
# 
#

import networkx
from grafon.mergers import mergerinterface

class MinCommonSuperGraph(mergerinterface.MergerInterface):
  """!
  Merger for any graphs. 
  
  Mergs the graphs based on the same node into both. Uses the compose mrthod
  from networkx to do it.
  """
  
  def __init__(self, bg):
    """!
    Store the base graph only as a member
    @param bg: base graph
    """
    self.bg = bg

  def merge(self, g1, g2):
    """!
    Merge two graphs into one. Uses compose method to merge both into one.
    
    @param g1: newtowrkx graph f.e. networkx.DiGraph()
    @param g2: newtowrkx graph f.e. networkx.DiGraph()
    @return new merged graph, also with networx type
    """
    n_g = networkx.MultiDiGraph()
    for e1 in g1.edges(data = True):
      if g2.has_edge(e1[0], e1[1]):
        n_g.add_edge(e1[0], e1[1], data = True)
    return n_g