# Antigen.py
# Author: Juliette Zerick (jnzerick@math.ucdavis.edu)
# EAD 221 + PHY 250 (Spring 2009)
# Purpose: The Antigen class encapsulates an Antigen object, part of an
# 	Immune System Algorithm. Note that the functionality of the class
#	is independent of the specific problem; distance functions and
# 	such are passed in as function handles.

from UtilsNConstants import *

# a class that represents a single Antigen in an idiotypic network
class Antigen:
	# get the antigen-antibody affinity
	def AgAb_aff(self,Ab):
		return self.fh_AgAb_aff(self,Ag)
	
	# calculate the affinity between two antigens
	def Ag_aff(self,Ag):
		return self.fh_Ag_aff(self,Ag)
	
	# print the Antigen
	def printAntigen(self):
		self.fh_printfull(self)

	# Antigen constructor--does a DEEP copy of chromosome
	def __init__(self,fh_mut,fh_AbAg_aff,fh_Ag_aff,fh_printfull,fh_tostr,\
		C, G):
		self.fh_mutate = fh_mut
		self.fh_AbAg_aff = fh_AbAg_aff
		self.fh_Ag_aff = fh_Ag_aff
		self.fh_printfull = fh_printfull
		self.fh_tostr = fh_tostr

		self.chromosome = copylist(C)
		self.gen = G

	# returns a DEEP copy of this Antigen
	def copy(self):
		return Antigen(self.fh_mutate, 
			self.fh_AbAg_aff, self.fh_Ag_aff, self.fh_printfull,
			self.fh_tostr,self.chromosome, self.gen)

	# mutate this Antigen
	def mutate(self,prob_mut):
		self.fh_mutate(self,prob_mut)

	# "destructor"--free up some memory
	def __del__(self):
		del self.chromosome

	# get a (deep) copy of the chromosome
	def get_chromcopy(self):
		return copylist(self.chromosome)

	# overloaded == operator--NOT consistent with the other
	# overloaded operators! used in the list.remove() method
	def __eq__(self,other):
		for i in xrange(len(self.chromosome)):
			if self.chromosome[i] != other.chromosome[i]:
				return False

		return True

	# overloaded str() typecast operator; also used in print
	def __str__(self):
		return self.fh_tostr(self)
