def strip_unused_symbols(gff_automaton_text: str) -> str: # GOAL may produce automaton # whose alphabet contains symbols # not used in any transition labels # here we remove unused propositions from the alphabet lines = gff_automaton_text.splitlines() alphabet_start = find(lambda l: '<Alphabet' in l, lines) alphabet_end = find(lambda l: '</Alphabet' in l, lines) if alphabet_start == -1 or alphabet_end == -1: return gff_automaton_text trans_start = find(lambda l: '<TransitionSet' in l, lines) trans_end = find(lambda l: '</TransitionsSet' in l, lines) lbl_lines = list(filter(lambda l: '<Label' in l, lines[trans_start + 1:trans_end])) used_labels = set() for lbl_line in lbl_lines: # <Label>~g ~r</Label> #: :type: str lbls = lbl_line[lbl_line.find('>') + 1: lbl_line.find('<', lbl_line.find('>'))] lbls = stripped_non_empty(lbls.replace('~', '').split()) used_labels.update(lbls) # now construct the result result = lines[:alphabet_start + 1] for lbl in filter(lambda l: l != 'True', used_labels): result.append(" <Proposition>%s</Proposition>" % lbl) result += lines[alphabet_end:] return '\n'.join(result)
def main(smv_spec_file_name, verbose_level): logger = logging.getLogger(__name__) scripts_dir = os.path.dirname(os.path.abspath(__file__)) spec_2_smv_path = scripts_dir + '/spec_2_smv.py' verbosity = '-{}'.format('v' * verbose_level) if verbose_level else '' latches_2_output_path = scripts_dir + '/latches_2_output.py' logger.debug('calling to %s' % spec_2_smv_path) # i could not fix the problem with unicode encodings when using execute_shell (with separate checks of exit statuses), # so use piping here instead aig = str(subprocess.check_output( '{spec_2_smv} {verbosity} {spec_file} | smvflatten | smvtoaig | aigtoaig -a | {latches_2_output}' .format(spec_2_smv=spec_2_smv_path, verbosity=verbosity, spec_file=smv_spec_file_name, latches_2_output=latches_2_output_path), shell=True), encoding=sys.getdefaultencoding()) outputs = get_controllable(readfile(smv_spec_file_name).splitlines()) assert outputs, 'there are no controllable signals, abort' lines = aig.splitlines() symbol_table_starts = find(lambda l: l.startswith('i'), lines) result = lines[:symbol_table_starts] i = symbol_table_starts while True: l = lines[i] if not l.startswith('i'): break result.append(rename(l, outputs)) i += 1 result += lines[i:] print('\n'.join(result)) return 0
def main(smv_spec_file_name, verbose_level): logger = logging.getLogger(__name__) scripts_dir = os.path.dirname(os.path.abspath(__file__)) spec_2_smv_path = scripts_dir + '/spec_2_smv.py' verbosity = '-{}'.format('v'*verbose_level) if verbose_level else '' latches_2_output_path = scripts_dir + '/latches_2_output.py' logger.debug('calling to %s' % spec_2_smv_path) # i could not fix the problem with unicode encodings when using execute_shell (with separate checks of exit statuses), # so use piping here instead aig = str(subprocess.check_output('{spec_2_smv} {verbosity} {spec_file} | smvflatten | smvtoaig | aigtoaig -a | {latches_2_output}' .format(spec_2_smv=spec_2_smv_path, verbosity=verbosity, spec_file=smv_spec_file_name, latches_2_output=latches_2_output_path), shell=True), encoding=sys.getdefaultencoding()) outputs = get_controllable(readfile(smv_spec_file_name).splitlines()) assert outputs, 'there are no controllable signals, abort' lines = aig.splitlines() symbol_table_starts = find(lambda l: l.startswith('i'), lines) result = lines[:symbol_table_starts] i = symbol_table_starts while True: l = lines[i] if not l.startswith('i'): break result.append(rename(l, outputs)) i += 1 result += lines[i:] print('\n'.join(result)) return 0
def compose_smv(non_spec_modules, asmpt_modules, grnt_modules, clean_main_module: SmvModule, counting_fairness_module: SmvModule, counting_justice_module: SmvModule) -> StrAwareList: smv = StrAwareList() for m in non_spec_modules: smv += m.module_str smv.sep() for am in asmpt_modules: smv += am.module_str smv.sep() for gm in grnt_modules: smv += gm.module_str smv.sep() if counting_fairness_module: smv += counting_fairness_module.module_str smv.sep() if counting_justice_module: smv += counting_justice_module.module_str smv.sep() # the main module smv += clean_main_module.module_str if asmpt_modules: smv += "VAR --assumptions modules" for m in asmpt_modules: smv += 'env_prop_%s : %s(%s);' % (m.name, m.name, ','.join(m.module_inputs)) if counting_fairness_module: fair_signals = ['env_prop_%s.fair' % m.name for m in filter(lambda m: m.has_fair, asmpt_modules)] smv += 'env_prop_%s : %s(%s);' % \ (counting_fairness_module.name, counting_fairness_module.name, ','.join(fair_signals)) smv.sep() smv += "VAR --guarantees modules" for m in grnt_modules: smv += 'sys_prop_%s : %s(%s);' % (m.name, m.name, ','.join(m.module_inputs)) assert m.has_bad or m.has_fair, str(m) if counting_justice_module: just_signals = ['sys_prop_%s.fair' % m.name for m in filter(lambda m: m.has_fair, grnt_modules)] smv += 'sys_prop_%s : %s(%s);' % \ (counting_justice_module.name, counting_justice_module.name, ','.join(just_signals)) has_bad_sys = find(lambda m: m.has_bad, grnt_modules) != -1 has_bad_env = find(lambda m: m.has_bad, asmpt_modules) != -1 if any([has_bad_sys, has_bad_env, counting_fairness_module, counting_justice_module]): smv.sep() smv += "VAR --special properties" if has_bad_sys: smv += "sys_prop_bad_variable: boolean;" if has_bad_env: smv += "env_prop_constr_variable: boolean;" if counting_justice_module: smv += "sys_prop_just_variable: boolean;" if counting_fairness_module: smv += "env_prop_fair_variable: boolean;" smv.sep() smv += "ASSIGN" if has_bad_sys: smv += "next(sys_prop_bad_variable) := %s;" % ' | '.join(['sys_prop_%s.bad' % m.name for m in filter(lambda m: m.has_bad, grnt_modules)]) smv += "init(sys_prop_bad_variable) := FALSE;" if has_bad_env: smv += "next(env_prop_constr_variable) := !(%s);" % ' | '.join('env_prop_%s.bad' % m.name for m in filter(lambda m: m.has_bad, asmpt_modules)) smv += "init(env_prop_constr_variable) := TRUE;" if counting_fairness_module: smv += "next(env_prop_fair_variable) := env_prop_%s.fair;" % counting_fairness_module.name smv += "init(env_prop_fair_variable) := TRUE;" if counting_justice_module: smv += "next(sys_prop_just_variable) := sys_prop_%s.fair;" % counting_justice_module.name smv += "init(sys_prop_just_variable) := FALSE;" return smv