Exemple #1
0
    def detect_faulty_definitions(self):
        if self.properly_defined_symbols:
            return False

        faulty = False
        if filemgt.module_is_definition_set(self.module_name):
            self.properly_defined_symbols = set([])
            new_symbols = []
            i = 0
            sentences = clif.get_logical_sentences_from_file(
                self.clif_processed_file_name)
            if len(sentences) == 0:
                if len(self.imports) == 0:
                    logging.getLogger(__name__).warn(
                        "Empty definition file: " + self.module_name)
            else:
                #print "PARENT's IMPORT CLOSURE SYMBOLS: " + str(self.get_irreflexive_import_closure_nonlogical_symbols())
                #print "SENTENCE SYMBOLS: " + str(clif.get_nonlogical_symbols(sentences[0]))
                new_symbols = [
                    clif.get_nonlogical_symbols(sentence) -
                    self.get_irreflexive_import_closure_nonlogical_symbols()
                    for sentence in sentences
                ]
                #new_symbols = [clif.get_nonlogical_symbols(sentence) - self.get_irreflexive_import_closure_nonlogical_symbols() for sentence in sentences]
                #print new_symbols

                # check for definitions that introduce no new symbols
                for i in range(0, len(new_symbols)):
                    if len(new_symbols[i]) == 0:
                        logging.getLogger(__name__).error(
                            "No new symbol seen in definition " + str(i + 1) +
                            " in: " + self.module_name)
                        faulty = True

                while True:
                    # filter the definitions that have exactly one defined symbol
                    #print "NEW SYMBOLS = " + str(new_symbols)
                    new_symbols = [
                        x - self.properly_defined_symbols for x in new_symbols
                    ]
                    new_single_symbols = [
                        sym.pop() for sym in new_symbols if len(sym) == 1
                    ]
                    #                    new_single_symbols = [sym.pop() for sym in filter(lambda x:len(x)==1, new_symbols)]
                    if len(new_single_symbols
                           ) == 0:  # stable set of single symbols
                        break
                    self.properly_defined_symbols.update(new_single_symbols)

                # the remaining ones have two or more newly introduced symbols
                for i in range(0, len(new_symbols)):
                    if len(new_symbols[i]) > 0:
                        logging.getLogger(__name__).error(
                            "More than one new symbol (" +
                            str(new_symbols[i]) +
                            ") found in a definition in: " + self.module_name)
                        faulty = True
                #print "PROPERLY DEFINED SYMBOLS = " + str(self.properly_defined_symbols)

        return faulty
Exemple #2
0
    def deep_clean(self):
        """ Fix the tree at last """

        removes = []
        for name in self.nodes:
            if filemgt.module_is_definition_set(name):
                print(name)
                removes.append(name)

        for name, node in self.nodes.items():
            if 'definition' not in name:
                # TODO refactor these comprehensions
                node.children = [
                    c for c in node.children
                    if not filemgt.module_is_definition_set(c.name)
                ]
                node.definitions += [
                    p for p in node.parents
                    if filemgt.module_is_definition_set(p.name)
                ]
                node.parents = [
                    p for p in node.parents
                    if not filemgt.module_is_definition_set(p.name)
                ]

                for c in node.children:
                    if 'definitions' in c.name:
                        logging.getLogger(__name__).debug(
                            'Did not remove definition in ' + node.name)
                for p in node.parents:
                    if 'definitions' in p.name:
                        logging.getLogger(__name__).debug(
                            'Did not remove definition in ' + node.name)

        visited = self.traverse(self.tree)

        for name, node in self.nodes.items():
            if node not in visited:
                removes.append(name)

        for name in removes:
            try:
                self.nodes.pop(name)
            except KeyError:
                logging.getLogger(__name__).debug(
                    'Already removed definition ' + name)
Exemple #3
0
 def get_defined_symbols(self):
     # get all definitions from this module if it is a definition
     defined_symbols = []
     if filemgt.module_is_definition_set(self.module_name):
         if not self.detect_faulty_definitions():
             #print "DEFINED SYMBOLS = " + str(self.properly_defined_symbols)
             #logging.getLogger(__name__).info("Determining arity for symbols in " + self.module_name)
             for symbol in self.properly_defined_symbols:
                 #logging.getLogger(__name__).info("Determining arity for symbol: " + symbol)
                 arity = clif.get_nonlogical_symbol_arity_from_file(
                     self.clif_processed_file_name, symbol)
                 defined_symbols.append((symbol, arity))
     return defined_symbols
Exemple #4
0
    def __str__(self):
        long_repr = (self.module_name + ' (depth=' + str(self.get_depth()) +
                     ', parents: ' + str(self.get_parents()))

        long_repr += ')'
        long_repr += '\n'

        if self.module_set.completely_processed:
            if filemgt.module_is_definition_set(
                    self.module_name) and not self.detect_faulty_definitions():
                long_repr += '| '
                for _ in range(self.get_depth()):
                    long_repr += '    '
                long_repr += '  + Defines symbols:'
                for (symbol, arity) in self.get_defined_symbols():
                    long_repr += ' ' + str(symbol) + '(' + str(arity) + ')'
                long_repr += '\n'
        long_repr += '| '
        for _ in range(self.get_depth()):
            long_repr += '    '
        long_repr += '  + Uses symbols: '
        for (symbol, arity) in self.get_nonlogical_symbols():
            long_repr += ' ' + str(symbol) + '(' + str(arity) + ')'
        return long_repr
Exemple #5
0
 def is_simple_definition(self):
     if filemgt.module_is_definition_set(
             self.module_name) and not self.detect_faulty_definitions():
         return True
     else:
         return False