## Creature Project ## CSC323 ## 4/17/10 ## conflict.py: A class governing conflict resolution. from Instruction import Instruction from Species import Species from SpeciesDatabase import * class ConflictResolutionPolicy(): """This class defines what should happen when two or more instructions are given to a single cell.""" def __init__ (self, sd = None): if sd == None: sd = SpeciesDatabase.getInstance() self.strengthDict = sd.strengthDict self.vicDict = {} """StrengthDict has tuples for keys and ints for values These tuples are of the form (species1, species2). A value > 0 indicates species 1 is stronger A value == 0 indicates the species are equal in strength A value < 0 indicates species 1 is weaker The magnitude of the value is irrelevant If species1 is stronger the dictionary will return 1. If species1 is equal the dictionary will return 0. If species1 is weaker the dictionary will return -1.""" def victor(self, instrucList): """Determines and returns the species from the cell's possible changes that has the highest combined strength in the strength dict.""" self.speList = [] """Fills in specDict with every species that appears as a possible candidate from the given instructions (as keys).""" for instruc in instrucList: if instruc != None: """If the Species List does not contain the species, add the species""" if not self.speList.__contains__(instruc.species): self.speList.append(instruc.species) """If specDict is empty, then no changes were sent for this cell.""" if self.speList.__len__() == 0: return None self.vicDict.clear() """Initializes vicDict with all candidate species (as keys) and a score of 0 (as values).""" for spe in self.speList: """If spe is a string, make it a species instead""" if isinstance(spe, str): spe = sd.getSpeciesfromName(spe) self.vicDict[spe] = 0 """vicDict compares each of its species with all of its other species If the strength dictionary shows that the first species is stronger, it adds 1 to that species's score in vicDict. If it is shown as weaker, it subtracts 1 from its score in vicDict. If it is shown as equal in strength, its score is unaffected. This only checks forward relations, and can cause unplanned, but maybe faster results. Assumes reflexive relationships.""" for spec1 in self.speList: self.speList = self.speList[1:] for spec2 in self.speList: if self.strengthDict[(spec1,spec2)] > 0: self.vicDict[spec1] += 1 elif self.strengthDict[(spec1,spec2)] < 0: self.vicDict[spec1] -= 1 """Find which species has the highest score in vicDict. It also needs to check if two (or more) species tie for the high score. If this happens (after all species have been checked), then the species destroy each other and None is returned""" self.highScore = [] for spe in self.vicDict: if self.highScore == []: self.highScore.append((spe, self.vicDict[spe])) elif self.vicDict[spe] > self.highScore[0][1]: self.highScore = [] self.highScore.append((spe, self.vicDict[spe])) elif self.vicDict[spe] == self.highScore[0][1]: self.highScore.append((spe, self.vicDict[spe])) if self.highScore.__len__() == 1: return self.highScore[0][0] else: return None class StupidConflictResolver(ConflictResolutionPolicy): """This is the stupidConflictResolver. It returns the first species from instrucList as the winning species. Used for testing purposes?""" def victor(self, instrucList): if instrucList == None or instrucList == [] or instrucList[0] == None: raise Exception("Nonetype caught!") return None else: print(type(instrucList[0]) + " !!!!!!!!!!!!!!!!!!!!!!!!!!!") return instrucList[0].species