def run(self): # We need to capture the print statements from the parser backup = sys.stdout sys.stdout = self.error # Create a place to read the text from buffer = tempfile.mkstemp(".macleod") with open(buffer[1], 'w') as f: f.write(self.text) try: self.ontology = Parser.parse_file(buffer[1], filemgt.read_config('cl', 'prefix'), os.path.abspath(filemgt.read_config('system', 'path')), self.resolve, self.path) except Exception as e: print(e) # If it's not a CL error we need to find the problem in Python if not isinstance(e, TypeError): # This prints to the Python console, not to the console in the window traceback.print_exc() self.ontology = None # return to the previous output sys.stdout = backup # leave no trace of the buffer os.close(buffer[0]) os.remove(buffer[1])
def convert_single_clif_file(ontology, output, resolve, loc=default_dir, prefix=default_prefix): logging.getLogger(__name__).info("Converting " + ontology.name + " to " + output + " format") if (output == tptp_output): results = ontology.to_tptp(resolve) elif (output == ladr_output): results = ontology.to_ladr(resolve) # the following assumes that the names of the configuration sections are the same as the names of the output (tptp/ladr) if resolve: ending = Filemgt.read_config(output, 'all_ending') else: ending = "" ending = ending + Filemgt.read_config(output, 'ending') output_file_name = Filemgt.get_full_path(ontology.name, folder=Filemgt.read_config( 'tptp', 'folder'), ending=ending) #ontology.get_all_modules() with open(output_file_name, "w") as f: for sentence in results: print(sentence) f.write(sentence + "\n") f.close() return output_file_name
def get_lemma_files_from_sentences(lemmas_name, sentences): sentences_files = [] sentences_names = [] import math # determine maximal number of digits digits = int(math.ceil(math.log10(len(sentences)))) i = 1 for lemma in sentences: name = lemmas_name + '_goal' + ('{0:0' + str(digits) + 'd}').format(i) filename = filemgt.get_full_path( name, folder=filemgt.read_config('ladr', 'folder'), ending=filemgt.read_config('ladr', 'ending')) output_file = open(filename, 'w') output_file.write('formulas(goals).\n') output_file.write(lemma + '\n') output_file.write('end_of_list.\n') output_file.close() sentences_files.append(filename) sentences_names.append(name) i += 1 return (sentences_names, sentences_files)
def preprocess_clif_file(self): # add the standard ending for CLIF files to the module name self.clif_file_name = filemgt.get_full_path(self.module_name, ending=filemgt.read_config( 'cl', 'ending')) #print self.clif_file_name self.clif_processed_file_name = filemgt.get_full_path( self.module_name, folder=filemgt.read_config('converters', 'tempfolder'), ending=filemgt.read_config('cl', 'ending')) logging.getLogger(__name__).debug("Clif file name = " + self.clif_file_name) logging.getLogger(__name__).debug("Clif preprocessed file name = " + self.clif_processed_file_name) clif.remove_all_comments(self.clif_file_name, self.clif_processed_file_name) self.imports = clif.get_imports(self.clif_processed_file_name) logging.getLogger(__name__).debug("imports detected in " + self.module_name + ": " + str(self.imports)) self.nonlogical_symbols = clif.get_all_nonlogical_symbols( self.clif_processed_file_name)
def __check_and_update_root_path(self): """Attempt to update in the conf file, returns False if the path is invalid""" if not os.path.isdir(self.root_dir_edit.text()): self.root_dir_edit.clear() self.root_dir_edit.setPlaceholderText("Invalid Path") return False filemgt.edit_config('system', 'path', self.root_dir_edit.text(), filemgt.config_file) return True
def get_p9_file_name(self): """get the filename of the LADR translation of the module and translate the ClifModule if not yet done so.""" if not self.p9_file_name: # do the translation self.p9_file_name = filemgt.get_full_path( self.module_name, folder=filemgt.read_config('ladr', 'folder'), ending=filemgt.read_config('ladr', 'ending')) self.get_translated_file(self.p9_file_name, "LADR") logging.getLogger(__name__).info("CREATED LADR TRANSLATION: " + self.p9_file_name) return self.p9_file_name
def get_tptp_file_name(self): """get the filename of the TPTP translation of the module and translate the ClifModule if not yet done so. This version does not rely on the clif to ladr translation, but does a direct translation.""" if not self.tptp_file_name: self.tptp_file_name = filemgt.get_full_path( self.module_name, folder=filemgt.read_config('tptp', 'folder'), ending=filemgt.read_config('tptp', 'ending')) self.get_translated_file(self.tptp_file_name, "TPTP") logging.getLogger(__name__).info("CREATED TPTP TRANSLATION: " + self.tptp_file_name) return self.tptp_file_name
def get_p9_cmd(imports, output_stem, option_files=None): """get a formatted command to run Prover9 with options (timeout, etc.) set in the class instance.""" args = [] args.append(filemgt.read_config('prover9', 'command')) args.append('-t' + filemgt.read_config('prover9', 'timeout')) args.append('-f') # append all ladr input files for m in imports: args.append(m.get_p9_file_name()) if option_files: for f in option_files: args.append(f) return (args, [])
def get_p9_empty_optionsfile(p9_file_name, verbose=True): # currently one option file for all!! #print 'OPTIONS FILE - P9 file name = ' + p9_file_name options_file_name = os.path.join( os.path.dirname(p9_file_name), os.path.splitext(os.path.basename(p9_file_name))[0] + filemgt.read_config('prover9', 'options_ending')) #print 'OPTIONS FILE = ' + options_file_name ladr.options_files.append(options_file_name) if os.path.isfile(options_file_name): return options_file_name else: # options_file_name = module_p9_file + '.options' options_file = open(options_file_name, 'w') options_file.write('clear(auto_denials).\n') if not verbose: options_file.write('clear(print_initial_clauses).\n') options_file.write('clear(print_kept).\n') options_file.write('clear(print_given).\n') #options_file.write('set(quiet).') options_file.close() return options_file_name
def get_paradox_cmd(imports, output_stem): """ we only care about the first element in the list of imports, which will we use as base name to obtain a single tptp file of the imports, which is the input for paradox.""" args = [] args.append(filemgt.read_config('paradox', 'command')) args.append('--time') args.append(filemgt.read_config('paradox', 'timeout')) args.append('--verbose') args.append('2') args.append('--model') args.append('--tstp') # append all tptp input files args.append( list(imports)[0].get_module_set(imports).get_single_tptp_file(imports)) return (args, [])
def get_m4_cmd(imports, output_stem): """get a formatted command to run Mace4 with options (timeout, etc.) set in the class instance.""" args = [] args.append(filemgt.read_config('mace4', 'command')) args.append('-v0') args.append('-t' + filemgt.read_config('mace4', 'timeout')) args.append('-s' + filemgt.read_config('mace4', 'timeout_per')) args.append('-n' + filemgt.read_config('mace4', 'start_size')) args.append('-N' + filemgt.read_config('mace4', 'end_size')) args.append('-f') # append all ladr input files for m in imports: args.append(m.get_p9_file_name()) return (args, [])
def __init__(self, name, reasoner_type=None, reasoner_id=None): logging.getLogger(__name__).debug('Initializing ' + name) self.identifier = '' self.type = Reasoner.PROVER self.args = [] self.modules = [] self.input_files = '' self.output_file = '' self.ontology = '' self.time = -1 self.output = None self.name = name if reasoner_type: self.type = reasoner_type if reasoner_id: self.identifier = reasoner_id else: self.identifier = name self.timeout = Filemgt.read_config(self.name, 'timeout') logging.getLogger(__name__).debug('Finished initializing ' + name)
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
def get_output_filename(ontology, resolve, output_type): # the following assumes that the names of the configuration sections # are the same as the names of the output (tptp/ladr/owl) if resolve: ending = Filemgt.read_config(output_type, 'all_ending') else: ending = "" ending = ending + Filemgt.read_config(output_type, 'ending') output_filename = Filemgt.get_full_path(ontology.name, folder=Filemgt.read_config(output_type,'folder'), ending=ending) return output_filename
def main(args): ''' :param args: arguments passed from customized entry points :return: ''' # Parse out the ontology object then print it nicely default_basepath = Filemgt.get_ontology_basepath() if args.sub is None: args.sub = default_basepath[0] if args.base is None: args.base = default_basepath[1] if (args.tptp or args.ladr or args.latex) and args.nocond is False: Parser.conditionals = True # TODO need to substitute base path full_path = args.file if os.path.isfile(full_path): logging.getLogger(__name__).info("Starting to parse " + args.file) convert_file(full_path, args=args) elif os.path.isdir(full_path): logging.getLogger(__name__).info("Starting to parse all CLIF files in folder " + args.file) convert_folder(full_path, args=args) else: logging.getLogger(__name__).error("Attempted to parse non-existent file or directory: " + full_path)
def convert_folder(folder, args): tempfolder = Filemgt.read_config('converters', 'tempfolder') ignores = [tempfolder] cl_ending = Filemgt.read_config('cl', 'ending') #logging.getLogger(__name__).info("Traversing folder " + folder) for directory, subdirs, files in os.walk(folder): if any(ignore in directory for ignore in ignores): pass else: for single_file in files: if single_file.endswith(cl_ending): file = os.path.join(directory, single_file) logging.getLogger(__name__).info("Parsing CLIF file " + file) convert_file(file, args=args)
def cumulate_ladr_files(input_files, output_file): """write all axioms from a set of p9 files to a single file without any change in the content itself except for the replacement of certain symbols""" special_symbols = filemgt.get_tptp_symbols() logging.getLogger(__name__).debug('Special symbols: ' + str(special_symbols)) text = [] for f in input_files: in_file = open(f, 'r') line = in_file.readline() while line: if len(special_symbols) > 0: for key in special_symbols: line = line.replace(' ' + key + '(', ' ' + special_symbols[key] + '(') line = line.replace('(' + key + '(', '(' + special_symbols[key] + '(') text.append(line) line = in_file.readline() in_file.close() text = strip_inner_commands(text) # store the location of all "<-" to be able to replace them back later on: out_file = open(output_file, 'w+') out_file.write('%axioms from module ' + f + ' and all its imports \n') out_file.write('%----------------------------------\n') out_file.write('\n') out_file.writelines(text) out_file.close() return output_file
def __init__(self, parent=None): super(MacleodSettings, self).__init__(parent) self.resize(WINDOW_WIDTH, WINDOW_HEIGHT) self.setWindowTitle("Macleod Settings") root_dir_path = filemgt.read_config('system', 'path') sp = QSizePolicy() sp.setHorizontalPolicy(QSizePolicy.Expanding) sp.setVerticalPolicy(QSizePolicy.Expanding) self.setSizePolicy(sp) # root directory settings root_dir_label = QLabel("Project Root Directory") self.root_dir_edit = QLineEdit() self.root_dir_edit.setText(root_dir_path) # color settings self._color_equals = self.__create_color_button("color_equals") self._color_predicate = self.__create_color_button("color_predicate") self._color_function = self.__create_color_button("color_function") self._color_connective = self.__create_color_button("color_connective") self._color_not = self.__create_color_button("color_not") self._color_quantifier = self.__create_color_button("color_quantifier") self._color_parentheses = self.__create_color_button("color_parentheses") self._color_find = self.__create_color_button("color_find") # exit buttons exit_buttons = QDialogButtonBox(self) exit_buttons.addButton(QDialogButtonBox.Ok) exit_buttons.addButton(QDialogButtonBox.Cancel) exit_buttons.button(QDialogButtonBox.Ok).clicked.connect(self.ok_event) exit_buttons.button(QDialogButtonBox.Cancel).clicked.connect(self.close) # layout & tabs tab_controller = QTabWidget() main_layout = QVBoxLayout(self) main_layout.addWidget(tab_controller) highlighter_layout = QFormLayout(self) highlighter_layout.addRow("Equals: ", self._color_equals) highlighter_layout.addRow("Predicates: ", self._color_predicate) highlighter_layout.addRow("Functions: ", self._color_function) highlighter_layout.addRow("Connectives: ", self._color_connective) highlighter_layout.addRow("Negation: ", self._color_not) highlighter_layout.addRow("Quantifiers: ", self._color_quantifier) highlighter_layout.addRow("Parentheses: ", self._color_parentheses) highlighter_layout.addRow("Find (Highlight): ", self._color_find) parser_layout = QFormLayout(self) parser_layout.addWidget(root_dir_label) parser_layout.addWidget(self.root_dir_edit) main_layout.addWidget(exit_buttons) parser_tab = QWidget(self) parser_tab.setLayout(parser_layout) highlighter_tab = QWidget(self) highlighter_tab.setLayout(highlighter_layout) tab_controller.addTab(parser_tab, "Parser Settings") tab_controller.addTab(highlighter_tab, "Highlighter Settings") self.setLayout(main_layout)
def get_m4_cmd(ontology): """get a formatted command to run Mace4 with options (timeout, etc.) set in the class instance.""" args = [] args.append(filemgt.read_config('mace4', 'command')) args.append('-v0') args.append('-t' + filemgt.read_config('mace4', 'timeout')) args.append('-s' + filemgt.read_config('mace4', 'timeout_per')) args.append('-n' + filemgt.read_config('mace4', 'start_size')) args.append('-N' + filemgt.read_config('mace4', 'end_size')) args.append('-f') args.append( clif_converter.convert_single_clif_file(ontology, clif_converter.ladr_output, True)) return args
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)
def shutdown(self): record = filemgt.format( logging.LogRecord( self.__class__.__name__, logging.DEBUG, None, None, "RECEIVED ABORT SIGNAL: " + self.name + ", command = " + self.args[0], None, None)) self.log_queue.put(record) self.exit.set()
def __init__(self, name, depth=None): ''' Constructor ''' self.module_set = None self.module_name = '' self.hierarchy_name = '' """set of all imported module names """ self.imports = set([]) """ set of all parent module names""" self.parents = None self.ancestors = None self.clif_file_name = '' """Location of the clif input file that has undergone preprocessing""" self.clif_processed_file_name = '' self.p9_file_name = None self.tptp_file_name = None # the distinction between nonlogical_symbols and nonlogical_variables assumes that a single symbol is not used as both in different sentences self.nonlogical_symbols = set([]) self.import_closure_nonlogical_symbols = None self.properly_defined_symbols = None #self.parents_nonlogical_symbols = set([]) # stores the depth of the import hierarchy self.depth = depth self.set_module_name(name) logging.getLogger(__name__).info('processing module: ' + self.module_name) # remove any obsolete URL ending as specified in the configuration file if self.module_name.endswith(filemgt.read_config('cl', 'ending')): self.module_name = os.path.normpath( self.module_name.replace(filemgt.read_config('cl', 'ending'), '')) self.hierarchy_name = filemgt.get_hierarchy_name(self.module_name) self.preprocess_clif_file()
def get_vampire_cmd(imports, ouput_stem): args = [] args.append(filemgt.read_config('vampire', 'command')) args.append('--mode') args.append('casc') args.append('--proof') args.append('tptp') args.append('-t') args.append(filemgt.read_config('vampire', 'timeout')) # needed for Windows args.append('--input_file') args.append( list(imports)[0].get_module_set(imports).get_single_tptp_file(imports)) logging.getLogger(__name__).debug("COMMAND FOR vampire IS " + str(args)) # works for linux, not for Windows #return (args, [list(imports)[0].get_module_set(imports).get_single_tptp_file(imports)]) return (args, [])
def run_module_consistency_check(self, module): """check a single module for consistency.""" outfile_stem = filemgt.get_full_path(module.module_name, folder=filemgt.read_config( 'output', 'folder')) reasoners = ReasonerSet() reasoners.constructAllCommands([module]) logging.getLogger(__name__).info("USING " + str(len(reasoners)) + " REASONERS: " + str([r.name for r in reasoners])) # run provers and modelfinders simultaneously and wait until one returns reasoners = process.raceProcesses(reasoners) (return_value, _) = self.consolidate_results(reasoners) self.pretty_print_result(module.module_name, return_value) return return_value
def __create_color_button(self, name): '''Generate a color button for a particular setting, returns the button.''' new_button = QColorButton(name, self) try : new_button.set_color(filemgt.read_config("gui", name)) except configparser.NoOptionError: pass # This will be a blank color if the setting doesn't exist return new_button
def __init__(self, parent=None): super(MacleodWindow, self).__init__(parent) # store the project path self.root_path = filemgt.read_config('system', 'path') # key: CodeEditor object, value: ontology object self.ontologies = dict() self.setup_widgets() self.setup_layout()
def terminate(self): record = filemgt.format( logging.LogRecord( self.__class__.__name__, logging.INFO, None, None, "TERMINATING: " + self.name + ", command = " + self.args[0], None, None)) self.log_queue.put(record) self.shutdown() time.sleep(0.2) multiprocessing.Process.terminate(self)
def get_p9_cmd(ontology): """get a formatted command to run Prover9 with options (timeout, etc.) set in the class instance.""" args = [] args.append(filemgt.read_config('prover9', 'command')) args.append('-t' + filemgt.read_config('prover9', 'timeout')) args.append('-f') args.append( clif_converter.convert_single_clif_file(ontology, clif_converter.ladr_output, True)) # check for possible options file (to change predicate order or other parameters) options_file = filemgt.read_config('prover9', 'options') if options_file is not None: options_file = os.path.abspath(options_file) args.append(options_file) return args
def enforce_limits(self, pid): limit = 1536 # each reasoning process is not allowed to use up more than 1.5GB of memory memory = get_memory(pid) #enforce memory limit if memory > limit: record = filemgt.format( logging.LogRecord( self.__class__.__name__, logging.INFO, None, None, "MEMORY EXCEEDED: " + self.name + ", command = " + self.args[0], None, None)) self.log_queue.put(record) self.shutdown() # enforce time limit if self.cputime > self.timeout: record = filemgt.format( logging.LogRecord( self.__class__.__name__, logging.INFO, None, None, "TIME EXCEEDED: " + self.name + ", command = " + self.args[0], None, None)) self.log_queue.put(record) self.shutdown()
def get_paradox_cmd(ontology): """ we only care about the first element in the list of imports, which will we use as base name to obtain a single tptp file of the imports, which is the input for paradox.""" args = [] args.append(filemgt.read_config('paradox', 'command')) # this option is needed for linux or mac to run paradox using wine, where "wine" is the command, but the path to paradox is the first argument, stored in the key "options" option = filemgt.read_config('paradox', 'options') if option is not None: args.append(option) args.append('--time') args.append(filemgt.read_config('paradox', 'timeout')) args.append('--verbose') args.append('2') args.append('--model') args.append('--tstp') args.append( clif_converter.convert_single_clif_file(ontology, clif_converter.tptp_output, True)) return args