def execute_query(dlv_executable: str,q, db): query = DatalogQuery(Atom("CERTAINTY", [])) for atom in q.get_atoms(): query.add_atom(atom) program = DatalogProgram([query]) return execute_program(dlv_executable, program, db) #execute_program()
def read_datalog_file(file: str) -> DatalogProgram: f = open(file, "r") names_regex = "[A-Za-z][A-Za-z0-9_]*" atom_regex = names_regex + "(\(" + "(" + names_regex + ",)*" + names_regex + "\))?" other_regex = names_regex + "(=|!=|>|<)" + names_regex query_element_regex = "(" + other_regex + "|" + "(not )?" + atom_regex + ")" query_regex = "^" + atom_regex + " :- " + "(" + query_element_regex + ", )*" + query_element_regex + "\.$" queries = [] line_index = 0 try: for line in f: if regex.match(query_regex, line): elements = regex.findall(query_element_regex, line) head = parse_datalog_atom(elements[0][0]) q = DatalogQuery(head) for element in elements[1:]: atom_element = element[0] if "not " in atom_element: q.add_atom(parse_datalog_atom(atom_element[4:]), True) elif "=" in atom_element: q.add_atom(parse_datalog_equality_atom(atom_element)) elif ">" in atom_element or "<" in atom_element: q.add_atom(parse_datalog_compare_atom(atom_element)) else: q.add_atom(parse_datalog_atom(atom_element)) queries.append(q) line_index += 1 else: f.close() raise MalformedQuery(line, "DatalogQuery") except MalformedQuery: f.close() raise f.close() return DatalogProgram(queries)