def test_localize_two_ass_one_gua(self): """ forall(i,j) a_i_j -> forall(i) b_i replaced by forall(i,j) (a_i_j -> b_i) """ a_i_j_is_true, b_j_is_true = _get_is_true('a', 'i', 'j'), _get_is_true('b', 'j') b_i_is_true = _get_is_true('b', 'i') prop = SpecProperty([ForallExpr(['i', 'j'], a_i_j_is_true)], [ForallExpr(['j'], b_j_is_true)]) localized_prop = localize(prop) expected_prop_i_j1 = SpecProperty( [Bool(True)], [ForallExpr(['i', 'j'], BinOp('->', a_i_j_is_true, b_j_is_true))]) expected_prop_i_j2 = SpecProperty( [Bool(True)], [ForallExpr(['i', 'j'], BinOp('->', a_i_j_is_true, b_i_is_true))]) assert str(localized_prop) == str(expected_prop_i_j1) or \ str(localized_prop) == str(expected_prop_i_j2), \ str(localized_prop)
def test_instantiate_property_cutoff2(self): property = SpecProperty([Bool(True)], [parse_expr('Forall(j) b_j=1')]) result = inst_property(property, 2) expected = SpecProperty([Bool(True)], [parse_expr('b_0=1')]) result_data = _convert_conjunction_to_str(result) expected_data = _convert_conjunction_to_str(expected) self.assertEqual(expected_data, result_data)
def _assert_no_single_concrete(expressions): if len(expressions) != 1: return e = expressions[0] if e in [Bool(True), Bool(False)]: return if is_quantified_expr(e): return assert 0, 'single concrete assumptions/guarantees are not supported now: ' + str( e)
def test_instantiate_property_cutoff_another_4(self): property = SpecProperty([Bool(True)], [parse_expr('Forall(j,k) b_j=1 -> c_k=1')]) result = inst_property(property, 4) expected = SpecProperty([Bool(True)], [ parse_expr( '(b_0=1 -> c_1=1) * (b_0=1 -> c_2=1) * (b_0=1 -> c_3=1)') ]) result_data = _convert_conjunction_to_str(result) expected_data = _convert_conjunction_to_str(expected) self.assertEqual(expected_data, result_data)
def normalize_conjuncts(conjuncts:list) -> Expr: """ sound, complete forall(i,j) a_i_j and forall(i) b_i ----> forall(i,j) (a_i_j and b_i) forall(i) a_i and forall(j) b_j ----> forall(i) (a_i and b_i) """ if len(conjuncts) == 0: return Bool(True) if len(conjuncts) == 1 and isinstance(conjuncts[0], Bool): return conjuncts[0] for e in conjuncts: assert isinstance(e, ForallExpr), 'global non-parameterized properties are not supported' max_indices = max(conjuncts, key=lambda e: len(e.arg1)) new_indices = max_indices.arg1 normalized_underlying_expr = None for e in conjuncts: old_indices = e.arg1 new_by_old = dict((o, new_indices[i]) for i, o in enumerate(old_indices)) new_underlying_e = _replace_indices(new_by_old, e.arg2) if normalized_underlying_expr is None: normalized_underlying_expr = new_underlying_e else: normalized_underlying_expr = BinOp('*', normalized_underlying_expr, new_underlying_e) normalized_expr = ForallExpr(new_indices, normalized_underlying_expr) return normalized_expr
def and_properties(properties) -> SpecProperty: property_expressions = [ BinOp('->', and_expressions(p.assumptions), and_expressions(p.guarantees)) for p in properties ] return SpecProperty([Bool(True)], [and_expressions(property_expressions)])
def test_localize_zero_ass(self): """ true -> forall(i) b_i replaced by forall(i) (true -> b_i) """ b_i_is_true = _get_is_true('b', 'i') prop = SpecProperty([Bool(True)], [ForallExpr(['i'], b_i_is_true)]) localized_prop = localize(prop) expected_prop = SpecProperty( [Bool(True)], [ForallExpr(['i'], BinOp('->', Bool(True), b_i_is_true))]) assert str(localized_prop) == str(expected_prop), str(localized_prop)
def test_localize_one_ass_one_gua(self): """ forall(i) a_i -> forall(j) b_j replaced by forall(i) (a_i -> b_i) """ prop = SpecProperty([parse_expr('Forall (i) a_i=1')], [parse_expr('Forall (j) b_j=1')]) localized_prop = localize(prop) expected_prop_i = SpecProperty( [Bool(True)], [parse_expr('Forall (i) a_i=1 -> b_i=1')]) expected_prop_j = SpecProperty( [Bool(True)], [parse_expr('Forall (j) a_j=1 -> b_j=1')]) expected_prop_str_i = str(expected_prop_i) expected_prop_str_j = str(expected_prop_j) localized_prop_str = str(localized_prop) assert localized_prop_str == expected_prop_str_i \ or localized_prop_str == expected_prop_str_j, str(localized_prop_str)
def localize(property:SpecProperty): """ sound, but incomplete forall(i) a_i -> forall(j) g_j => forall(i) (a_i -> g_i) forall(i,j) a_i_j -> forall(k) g_k => forall(i,j) (a_i_j -> g_i) """ if not is_quantified_property(property): return property normalized_ass = normalize_conjuncts(property.assumptions) normalized_gua = normalize_conjuncts(property.guarantees) binding_indices_ass = _get_indices(normalized_ass) binding_indices_gua = _get_indices(normalized_gua) if len(binding_indices_ass) > len(binding_indices_gua): max_expr, other_expr = normalized_ass, normalized_gua else: max_expr, other_expr = normalized_gua, normalized_ass assert isinstance(max_expr, ForallExpr) max_binding_indices = max_expr.arg1 ass_newindex_by_old = dict((o, max_binding_indices[i]) for i, o in enumerate(binding_indices_ass)) gua_newindex_by_old = dict((o, max_binding_indices[i]) for i, o in enumerate(binding_indices_gua)) replaced_ass = _replace_indices(ass_newindex_by_old, normalized_ass) replaced_gua = _replace_indices(gua_newindex_by_old, normalized_gua) replaced_underlying_ass = replaced_ass.arg2 if is_quantified_expr(replaced_ass) else replaced_ass replaced_underlying_gua = replaced_gua.arg2 if is_quantified_expr(replaced_gua) else replaced_gua new_gua = ForallExpr(max_binding_indices, BinOp('->', replaced_underlying_ass, replaced_underlying_gua)) new_property = SpecProperty([Bool(True)], [new_gua]) return new_property
def _get_automatae(assumptions, guarantees, optimization, cutoff, ltl2ucw_converter: Ltl2UCW, logger): #TODO: check which optimizations are used properties = [SpecProperty(assumptions, [g]) for g in guarantees] logger.info('original properties:\n%s\n', '\n'.join(map(str, properties))) archi = TokRingArchitecture() archi_properties = [ SpecProperty(assumptions, [g]) for g in archi.guarantees() ] logger.info('architecture properties:\n%s\n', '\n'.join(map(str, archi_properties))) if OPTS[optimization] >= OPTS[STRENGTH]: # we don't need this in case of async_hub since its assumptions implies GF(tok), put it is here for simplicity properties = [ SpecProperty(p.assumptions + archi.implications(), p.guarantees) for p in properties ] properties = properties + archi_properties scheduler = InterleavingScheduler() properties = [ SpecProperty(p.assumptions + scheduler.assumptions, p.guarantees) for p in properties ] logger.info('after updating with scheduling assumptions:\n%s\n', '\n'.join(map(str, properties))) #TODO: add support of using other options without using strengthening if OPTS[optimization] >= OPTS[STRENGTH]: logger.info('strengthening properties..') pseudo_safety_properties, pseudo_liveness_properties = strengthen_many( properties, ltl2ucw_converter) properties = pseudo_safety_properties + pseudo_liveness_properties logger.info( 'strengthening resulted in pseudo_safety_properties (a_s -> g_s):\n%s\n', '\n'.join(map(str, pseudo_safety_properties))) logger.info( '..and in pseudo_liveness_properties (a_s&a_l -> g_l):\n%s\n', '\n'.join(map(str, pseudo_liveness_properties))) if OPTS[optimization] >= OPTS[STRENGTH]: properties = [localize(p) for p in properties] logger.info('properties after localizing:\n%s\n', '\n'.join(map(str, properties))) prop_real_cutoff_pairs = [(p, archi.get_cutoff(p)) for p in properties] par_global_property_pairs = [(p, c) for (p, c) in prop_real_cutoff_pairs if c != 2] par_local_property_pairs = [(p, c) for (p, c) in prop_real_cutoff_pairs if c == 2] for (p, c) in par_local_property_pairs: assert p.assumptions == [Bool(True)] assert c == 2 if optimization == SYNC_HUB: # removing GF(sch) from one-indexed properties par_local_property_pairs = [(_replace_sched_by_true(p, scheduler), c) for (p, c) in par_local_property_pairs] if optimization == SYNC_HUB: pass # TODO: should add sync_hub assumptions -- but currently they are added on SMT (Impl) level if optimization == ASYNC_HUB: # by definition async_hub_assumptions are one-indexed async_hub_assumptions = archi.get_async_hub_assumptions() par_local_property_pairs = [ (localize(SpecProperty(async_hub_assumptions, p.guarantees)), c) for (p, c) in par_local_property_pairs ] inst_property_cutoff_pairs = [] for (p, c) in par_global_property_pairs + par_local_property_pairs: inst_c = min(c, cutoff) inst_p = inst_property(p, inst_c) opt_inst_p = apply_log_bit_scheduler_optimization( inst_p, scheduler, SCHED_ID_PREFIX, inst_c) inst_property_cutoff_pairs.append((opt_inst_p, c)) local_properties = [p for (p, c) in inst_property_cutoff_pairs if c == 2] global_property_cutoff_pairs = [(p, min(c, cutoff)) for (p, c) in inst_property_cutoff_pairs if p not in local_properties] logger.info('instantiated local properties:\n%s\n', local_properties) logger.info('instantiated global properties:\n%s\n', global_property_cutoff_pairs) local_automaton = None if len(local_properties) > 0: local_property = and_properties(local_properties) local_automaton = ltl2ucw_converter.convert( expr_from_property(local_property)) glob_automatae_pairs = [] if len(global_property_cutoff_pairs) > 0: glob_automatae_pairs = [ (ltl2ucw_converter.convert(expr_from_property(p)), c) for (p, c) in global_property_cutoff_pairs ] if OPTS[optimization] < OPTS[SYNC_HUB] and local_automaton: if optimization == ASYNC_HUB: glob_automatae_pairs += [(local_automaton, 1)] else: glob_automatae_pairs += [(local_automaton, 2)] sync_automaton = None if OPTS[optimization] >= OPTS[SYNC_HUB]: sync_automaton = local_automaton return sync_automaton, glob_automatae_pairs
def visit_binary_op(self, binary_op:BinOp): if binary_op.name == '=': if _get_sched_signal(binary_op.arg1, self._scheduler) or _get_sched_signal(binary_op.arg2, self._scheduler): return Bool(True) return super().visit_binary_op(binary_op)