import youGen_NG

# define logging types
gen_tree = 1
parameter_trees = 2

class Gentree:
	'''
	Design decisions
		count_stack is used to keep track of the counts rooted at each
		 rule.
	'''
	def __init__(self,file_name):
		'''
		Purpose
			create data structures for calculating the counts.
		Precondition
			must be called before language generation.
		'''
		self.count_stack = []

	def destructor(self):
		'''
		Purpose
			destroy data structures for calculating the counts.
		Precondition
			must be called after language generation.
		'''
		self.count_stack = None

	def get_count(self,i):
		'''
		Purpose
			return the counts rooted at rule of index i in the rule
			 stack.
		Precondition
			i is between 0 and size of rule stack-1
		'''
		if i < len(self.count_stack):
			return self.count_stack[i]
		# log_start is not called for next rule yet
		return 0

	def log_start(self,logging_type,R,S,is_ground):
		'''
		Purpose
			log the start of the application of rule R.
		Precondition
			must be called after application of rule R.
			is_ground is either True or False.
		'''
		self.count_stack.append(0)

		if is_ground:
			# increments the counts
			for i in range(len(self.count_stack)):
				self.count_stack[i] += 1

	def log_end(self,logging_type):
		'''
		Purpose
			log the end of the application of rule R.
		Precondition
			must be called before application of alternative rule.
		'''
		global parameter_trees

		count = self.count_stack.pop()

		if logging_type == parameter_trees:
			# want number of gentree instead of parameter values
			for i in range(len(self.count_stack)):
				self.count_stack[i] -= count
