def test_utils(self): words = ['(', 'a', '(', 'b', 'c', ')', '(', 'd', ')', ')'] self.assertEquals(9, utils.index_of_closing_parenthesis(words, 0)) self.assertEquals(5, utils.index_of_closing_parenthesis(words, 2)) self.assertEquals(8, utils.index_of_closing_parenthesis(words, 6)) self.assertEquals(-1, utils.index_of_closing_parenthesis(words, 9)) self.assertEquals(-1, utils.index_of_opening_parenthesis(words, 0)) self.assertEquals(2, utils.index_of_opening_parenthesis(words, 5)) self.assertEquals(6, utils.index_of_opening_parenthesis(words, 8)) self.assertEquals(0, utils.index_of_opening_parenthesis(words, 9)) self.assertEquals(0, utils.index_closest_left(words, 2, '(')) self.assertEquals(1, utils.index_closest_left(words, 5, 'a')) self.assertEquals(-1, utils.index_closest_left(words, 5, 'z')) self.assertEquals(6, utils.index_closest_right(words, 3, '(')) self.assertEquals(-1, utils.index_closest_right(words, 3, 'a')) self.assertEquals(7, utils.index_closest_right(words, 3, 'd')) self.assertEquals(['(', 'a', '(', 'b', 'c', ')', '(', 'd', ')', ')'], utils.parenthesis_enclosure(words, 1)) self.assertEquals(['(', 'b', 'c', ')'], utils.parenthesis_enclosure(words, 3)) self.assertEquals(['(', 'b', 'c', ')'], utils.parenthesis_enclosure(words, 4)) self.assertEquals(['(', 'd', ')'], utils.parenthesis_enclosure(words, 7)) self.assertEquals(['(', 'd', ')'], utils.parenthesis_enclosure(words, 8)) self.assertEquals(['(', 'a', '(', 'b', 'c', ')', '(', 'd', ')', ')'], utils.parenthesis_enclosure(words, 9))
def get_unsat_core(text, interactive_mode, produce_assignments): """Returns unsat-core or empty list if it was not obtainable (e.g. decision was sat).""" unsat_core = [] words = utils.str_to_wlist(text) # skip model - NOTE: in the interactive mode there is no need to skip the model if not interactive_mode: j = utils.index_of_closing_parenthesis(words, 1) + 1 # omit model if produce_assignments: x = words[j:] j = utils.index_of_closing_parenthesis( words, j) + 1 # omit assignments x = words[j:] if j + 1 >= len(words): return [] else: j = 1 prefix = words[j] + words[j + 1] if prefix == '(error': return [] else: while j < len(words): if words[j] == '(': pass elif words[j] == ')': break else: unsat_core.append(words[j]) j += 1 return unsat_core
def get_assignments(text, interactive_mode): """Returns dictionary of assignments if decision was sat.""" words = utils.str_to_wlist(text) j = 1 # On the j=0 is a decision. prefix = words[j] + words[j + 1] if prefix == '(objectives': j = utils.index_of_closing_parenthesis(words, j) + 1 prefix = words[j] + words[j + 1] if prefix == '(model' or prefix == '((': # skip model - assignments are printed after it. j = utils.index_of_closing_parenthesis(words, j) + 1 prefix = words[j] + words[j + 1] if j + 1 >= len(words): return [] elif prefix == '(error': return {} else: return utils.wlist_to_dict_parenthesis(words[j:])
def get_model_explicit(words): model_values = {} i = 2 while i < len(words): if words[i] == 'define-fun': name = words[i + 1] # Usually i+2 and i+3 contains parenthesis, but in some scenarios there may be some values # in between. They are ignored here. i = utils.index_of_closing_parenthesis(words, i + 2) # i+1 is a type of the function if words[i + 2] == '(': j = utils.index_of_closing_parenthesis(words, i + 2) value = ' '.join(words[i + 2:j + 1]) i = j + 1 # j+1 is closing parenthesis of define-fun else: value = words[i + 2] i += 3 # i+3 is closing parenthesis of define-fun model_values[name] = value assert words[ i] == ')' # i should point to the last parenthesis of define-fun i += 1 return model_values
def get_model_simplified(words): # raise Exception('Loading model in the simplified form is not supported yet!') model_values = {} i = 1 while i < len(words): if words[i] == '(': name = words[i + 1] # i+1 is a type of the function if words[i + 2] == '(': j = utils.index_of_closing_parenthesis(words, i + 2) value = ' '.join(words[i + 2:j + 1]) i = j + 1 # j+1 is closing parenthesis else: value = words[i + 2] i += 3 # i+3 is closing parenthesis model_values[name] = value assert words[ i] == ')' # i should point to the last parenthesis of value definition i += 1 return model_values
def get_model_values(text): """Returns dictionary with values assigned to variables.""" words = utils.str_to_wlist(text) if len(words) <= 1: raise Exception( 'Trying to get model while presumably only check-sat was invoked!' ) start = 1 prefix = words[start] + words[start + 1] # On the i=0 always is a decision. if prefix == '(objectives': start = utils.index_of_closing_parenthesis(words, start) + 1 prefix = words[start] + words[start + 1] if prefix == '(error' or prefix not in ('(model', '(('): return {} elif prefix == '(model': return SolverResult.get_model_explicit(words[start:]) elif prefix == '((': return SolverResult.get_model_simplified(words[start:])