
import decisiondiagram_core

DecisionDiagram = decisiondiagram_core.DecisionDiagram

class DecisionDiagramFactory:
	dd = 0
	trace = []
	trsfun = 0
	var_list = []

	def __init__(self):
		pass

	def produceDD(self, in_vardict, in_varlist, in_trsfun):
		self.trace = []
		self.dd = DecisionDiagram()
		self.dd.var_dict = in_vardict
		self.trsfun = in_trsfun
		self.var_list = in_varlist
		id = self.run(0)
		self.dd.setRoot(id)
		return self.dd
		
	def run(self, level):
		if level == len(self.var_list):
			sig = (self.trsfun(self.trace),),
			sig_str = (None, sig)
			if sig_str in self.dd.sigdict:
				id = self.dd.sigdict[sig_str]
			else:
				id = self.dd.addNewNode(None, sig, None)
				self.dd.sigdict[sig_str] = id
			return id
		else:
			sig = list()
			for i in range(self.dd.var_dict[self.var_list[level]]):
				self.trace.append(i) 
				sig.append(self.run(level+1)) 
				self.trace.pop()
			sig = tuple(sig)
			sig_str = (self.var_list[level], sig)
			if sig_str in self.dd.sigdict:
				id = self.dd.sigdict[sig_str]
			else:
				id = self.dd.addNewNode(self.var_list[level], sig, self.dd.var_dict[self.var_list[level]])
				self.dd.sigdict[sig_str] = id
			return id

GlobalDecisionDiagramFactory = DecisionDiagramFactory()
