예제 #1
0
    def convert2python(self):
        self.__declaration_content.append(
            f"@context.valasp(validate_predicate={self.__validate_predicate}, with_fun=valasp.domain.primitive_types.Fun.{self.__with_fun}, auto_blacklist={self.__auto_blacklist})"
        )
        self.__declaration_content.append(
            f"class {self.__name.to_class().value}:")
        for term in self.__terms:
            self.__declaration_content.append(
                f"\t{term.term_name}: {term.term_type}")

        self.__post_init_content.append("\tdef __post_init__(self):")
        for term in self.__terms:
            term.convert2python()
            for i in term.post_init_content:
                self.__post_init_content.append(f"\t\t{i}")

        for having in self.__having:
            m = YamlValidation.match_having(having)
            assert m
            having = m['first'], m['op'], m['second']
            assert len(having) == 3
            self.__post_init_content.append(
                '\t\tif not self.%s %s self.%s: raise ValueError("%s")' %
                (having[0], having[1], having[2],
                 f'Expected {having[0]} {having[1]} {having[2]}'))

        if self.__after_init is not None:
            afi = self.__after_init.split('\n')
            for j in afi:
                self.__post_init_content.append(f"\t\t{j}")

        for term in self.__terms:
            for i in term.other_methods_content:
                self.__other_methods_content.append(f"\t{i}")

        if self.__before_grounding is not None:
            self.__other_methods_content.append('\t@classmethod')
            self.__other_methods_content.append(
                f'\tdef before_grounding_{self.__name}(cls):')
            afg = self.__before_grounding.split('\n')
            for j in afg:
                self.__other_methods_content.append('\t\t%s' % j)
        if self.__after_grounding is not None:
            self.__other_methods_content.append('\t@classmethod')
            self.__other_methods_content.append(
                f'\tdef after_grounding_{self.__name}(cls):')
            afg = self.__after_grounding.split('\n')
            for j in afg:
                self.__other_methods_content.append('\t\t%s' % j)

        output = []
        output.extend(self.__declaration_content)
        if len(self.__post_init_content) > 1:
            output.extend(self.__post_init_content)
        output.extend(self.__other_methods_content)
        return output
예제 #2
0
 def __parse_having(self):
     for i in self.__having:
         m = YamlValidation.match_having(i)
         assert m
         list_of_comparisons = m['first'], m['op'], m['second']
         assert len(list_of_comparisons) == 3
         if not self.__exists_term(list_of_comparisons[0]):
             raise ValueError(
                 f'{self.__name}: having: {i}: {list_of_comparisons[0]} is not a term name'
             )
         if not self.__exists_term(list_of_comparisons[2]):
             raise ValueError(
                 f'{self.__name}: having: {i}: {list_of_comparisons[2]} is not a term name'
             )