## Creature Project ## CSC323 ## 4/17/10 ## rule.py: Contains methods associated with the class Rule. #Add Import Statement Here class Rule(): """Holds a predicate and a list of actions to execute if the predicate holds true""" def __init__(self, pred, instructions): self.predicate = pred self.instructions = instructions def holds(self, world, coordinates): """If the predicate resolves as true, returns true, otherwise returns false.""" if self.predicate.holds(world, coordinates): return True else: return False def getInstructions(self, coordinates): """Returns a list of packed instruction objects. These are all the instructions that are to used if the rule's predicate holds.""" packedInstructions = [] for i in self.instructions: packedInstructions.append(i.pack(coordinates)) return packedInstructions def __str__(self): """Overwrites the to string method for a rule.""" ruleString = "Predicate: " + str(self.predicate)+ "\n Instructions: { " for i in self.instructions: ruleString += str(i) + " " ruleString += "}" return ruleString def save_the_rule(self): """A method for saving rules""" ruleString = "R{P" + str(self.predicate) for i in self.instructions: ruleString += "\n" + i.save_the_instruction() ruleString += "}" return ruleString