from parlai.agents.programr.parser.template.nodes.base import TemplateNode
from parlai.agents.programr.utils.logging.ylogger import YLogger


class TemplateVocabularyNode(TemplateNode):

    def __init__(self):
        TemplateNode.__init__(self)

    def resolve_to_string(self, brain):
        set_words = brain.sets.count_words_in_sets()
        pattern_words = brain.aiml_parser.pattern_parser.count_words_in_patterns()
        resolved = "%d" % (set_words + pattern_words)
        YLogger.debug(brain, "[%s] resolved to [%s]", self.to_string(), resolved)
        return resolved

    def resolve(self, brain):
        try:
            return self.resolve_to_string(brain)
        except Exception as excep:
            YLogger.exception(brain, "Failed to resolve", excep)
            return ""

    def to_string(self):
        return "VOCABULARY"

    def to_xml(self, brain):
        xml = "<vocabulary>"
        xml += self.children_to_xml(brain)
        xml += "</vocabulary>"
        return xml

    #######################################################################################################
    # <vocabulary/> |

    def add_default_star(self):
        return True

    def parse_expression(self, graph, expression):
        self._parse_node(graph, expression)
