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 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 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 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 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_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 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 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 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 __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 __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 __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 __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 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 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
def detect_systems(self): """Read the active provers from the configuration file.""" # local variables provers = filemgt.read_config('active', 'provers').split(',') finders = filemgt.read_config('active', 'modelfinders').split(',') provers = [s.strip() for s in provers] finders = [s.strip() for s in finders] provers = [x for x in provers if len(x) > 0] finders = [x for x in finders if len(x) > 0] self.extend([Reasoner(r) for r in provers]) self.extend([ Reasoner(r, reasoner_type=Reasoner.MODEL_FINDER) for r in finders ]) logging.getLogger(__name__).debug("REASONER SET: " + str(provers + finders)) return True
def constructCommand(self, ontology): import os """Return the command (includes constructing it if necessary) to invoke the reasoner.""" self.args = commands.get_system_command(self.name, ontology) ending = None if ontology.resolve: ending = Filemgt.read_config(self.name, 'all_ending') if ending is None: ending = "" ending = ending + Filemgt.read_config(self.name, 'ending') self.output_file = Filemgt.get_full_path(ontology.name, folder=Filemgt.read_config( 'output', 'folder'), ending=ending) self.ontology = ontology logging.getLogger(__name__).debug('Reasoner command: ' + str(self.args)) return self.args
def convert_all_clif_files(folder, output, resolve, loc=default_dir, prefix=default_prefix): 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("Found CL file " + file) ontology = Parser.parse_file(file, prefix, loc, resolve) convert_single_clif_file(ontology, output, resolve, loc, prefix)
def extract_p9_predicates_and_functions(self): #print 'extract predicates and functions' prover9args = 'prover9 -t 0 -f ' for f in self.imports: prover9args += f.p9_file_name + ' ' options_file = commands.get_p9_empty_optionsfile( self.get_p9_file_name(), verbose=False) prover9args += ' ' + options_file + ' ' # would be better to create a temporary file or read the output stream directly temp_file = self.get_module_name() + '_order' + filemgt.read_config( 'ladr', 'ending') prover9args += ' > ' + temp_file logging.getLogger(__name__).debug(prover9args) process.executeSubprocess(prover9args) order_file = open(temp_file, 'r') line = order_file.readline() predicates = None functions = None while line: if line.find('predicate_order') > -1: predicateline = line[line.find('predicate_order([') + len('predicate_order([') + 1:-4] predicates = predicateline.split() for i in range(len(predicates)): predicates[i] = predicates[i].replace(',', '') line = order_file.readline() functionline = line[line.find('function_order([') + len('function_order([') + 1:-4] functions = functionline.split() for i in range(len(functions)): functions[i] = functions[i].replace(',', '') break line = order_file.readline() order_file.close() #print 'temp file : ' + temp_file #print 'options file : ' + options_file os.remove(temp_file) os.remove(options_file) if predicates and functions: return (predicates, functions) else: return ([], [])
def reformat_urls(lines): """Delete URL prefixes from all import declarations.""" lines = list(lines) prefixes = filemgt.read_config('cl', 'prefix').split(',') prefixes = [p.strip().strip('"') for p in prefixes] prefixes = sorted([p.strip() for p in prefixes], key=lambda s: len(s), reverse=True) for i in range(0, len(lines)): for prefix in prefixes: if prefix in lines[i]: if not prefix.endswith('/'): prefix = prefix + '/' # print "replacing prefix: " + prefix + " in " + lines[i] lines[i] = lines[i].replace(prefix, '') # print lines[i] return lines
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 enforce_limits(self, pid): limit = int(filemgt.read_config('system', 'memory_limit') ) # read custom memory limit from configuration file if limit is None: logging.getLogger(__name__).debug( "USING DEFAULT MEMORY LIMIT OF 2GB FOR EACH REASONER") limit = 2048 # default memory limit for each reasoner is 2GB memory = get_memory(pid) #enforce memory limit if memory > limit: logging.getLogger(__name__).info("MEMORY EXCEEDED: " + self.name + ", command = " + self.args[0]) self.status = 'ALLOTED MEMORY EXCEEDED: ' + str(limit) + 'GB' self.shutdown() # enforce time limit if self.cputime > self.timeout: logging.getLogger(__name__).info("TIME EXCEEDED: " + self.name + ", command = " + self.args[0]) self.status = 'ALLOTED TIME EXCEEDED: ' + str( self.timeout) + ' seconds' self.shutdown()
def add_lemmas (self): # first get LADR translation of ClifModule # logging.getLogger(__name__).debug("CREATING LADR TRANSLATION OF LEMMA: " + self.module.module_name) #self.module.get_p9_file_name() (lemma_names, ladr_files) = ladr.get_ladr_goal_files(self.module.get_p9_file_name(), self.module.module_name) logging.getLogger(__name__).debug("CREATED LADR TRANSLATION OF LEMMA: " + self.module.get_p9_file_name()) # translate to tptp as goal tptp_sentences = clif.translate_sentences([self.module.clif_processed_file_name], "TPTP", axiom=False) logging.getLogger(__name__).debug("Goal created with " + str(len(tptp_sentences))+ " sentences") # for t in tptp_sentences: # print t # populate the list of lemmas for i in range(0,len(ladr_files)): name = os.path.join(os.path.dirname(lemma_names[i]), os.path.basename(ladr_files[i].replace(filemgt.read_config('ladr','ending'),''))) #print "NAME = " + name m = LemmaModule(name,ladr_files[i],tptp_sentences[i]) self.lemmas.append(m)
''' Created on 2013-03-19 @author: Torsten Hahmann ''' import os, sys, argparse, logging sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../") import macleod.scripts.licence import macleod.Filemgt as Filemgt import macleod.parsing.Parser as Parser #from macleod.Filemgt import Filemgt # defaults for the ontology directory and basepath default_dir = Filemgt.read_config('system', 'path') default_prefix = Filemgt.read_config('cl', 'prefix') def consistent(filename, m, options=[]): if '-module' in options: results = m.run_consistency_check_by_subset( abort=True, abort_signal=ClifModuleSet.CONSISTENT) elif '-depth' in options: results = m.run_consistency_check_by_depth( abort=True, abort_signal=ClifModuleSet.CONSISTENT) elif '-simple' in options: results = m.run_simple_consistency_check() else: results = m.run_full_consistency_check(