def test_get_ground_spec_ts(self): def _encode_error(accepting, final): f_error = FALSE() for f in accepting: f_error = Or(f, f_error) f_error = And(f_error, final) return f_error spec_list = Spec.get_specs_from_string( "SPEC [CB] [ENTRY] [l] void m1() |- [CI] [ENTRY] [l] void m2()") assert spec_list is not None ctrace = CTrace() cb = CCallback(1, 1, "", "void m1()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) ctrace.add_msg(cb) ci = CCallin(1, 1, "", "void m2()", [TestGrounding._get_obj("1", "string")], None) cb.add_msg(ci) ground_s = Spec.get_specs_from_string( "SPEC [CB] [ENTRY] [1] void m1() |- [CI] [ENTRY] [1] void m2()")[0] ts_enc = TSEncoder(ctrace, []) ts_var = ts_enc._encode_vars() accepting = [] gs_ts = ts_enc._get_ground_spec_ts(ground_s, 0, accepting) gs_ts.product(ts_var) error = _encode_error(accepting, TRUE()) self.assertTrue( self._accept_word(ts_enc, gs_ts, ["[CB]_[ENTRY]_void m1()(1)"], error)) self.assertFalse( self._accept_word(ts_enc, gs_ts, ["[CI]_[ENTRY]_void m2()(1)"], error)) # check the disable error = _encode_error( accepting, TSEncoder._get_state_var("[CI]_[ENTRY]_void m2()(1)")) self.assertFalse( self._accept_word(ts_enc, gs_ts, ["[CB]_[ENTRY]_void m1()(1)"], error)) self.assertFalse( self._accept_word(ts_enc, gs_ts, ["[CI]_[ENTRY]_void m2()(1)"], error))
def test_encode_vars(self): fmwk_over = TestGrounding._get_fmwkov("", "cb", False) trace = CTrace() fmwk_over_cb = TestGrounding._get_fmwkov("", "cb", False) fmwk_over_cb1 = TestGrounding._get_fmwkov("", "cb1", False) cb = CCallback(1, 1, "", "cb", [], None, [fmwk_over_cb]) cb1 = CCallback(1, 1, "", "cb1", [], None, [fmwk_over_cb1]) ci = CCallin(1, 1, "", "ci", [], None) cb.add_msg(cb1) cb1.add_msg(ci) trace.add_msg(cb) cb = CCallback(1, 1, "", "cb", [], None, [fmwk_over_cb]) cb1 = CCallback(1, 1, "", "cb1", [], None, [fmwk_over_cb1]) ci = CCallin(1, 1, "", "ci", [], None) cb.add_msg(cb1) cb1.add_msg(ci) trace.add_msg(cb) ts_enc = TSEncoder(trace, []) ts_var = ts_enc._encode_vars() self.assertTrue(len(ts_var.state_vars) == 6) trans = TRUE() l = [cb, cb1, ci] for m in l: for entry in [TSEncoder.ENTRY, TSEncoder.EXIT]: var = TSEncoder._get_state_var( TSEncoder.get_key_from_msg(m, entry)) ivar = ts_enc.r2a.get_msg_eq( TSEncoder.get_key_from_msg(m, entry)) trans = And(trans, Implies(ivar, var)) self.assertTrue(is_valid(Iff(ts_var.init, TRUE()))) self.assertTrue(is_valid(Iff(ts_var.trans, trans)))
def test_encode_ground_specs(self): ts_enc = self._get_sample_trace() vars_ts = ts_enc._encode_vars() (ts, disabled_ci, accepting) = ts_enc._encode_ground_specs() ts.product(vars_ts) accepting_states = FALSE() for k, v in accepting.iteritems(): for state in v: accepting_states = Or(accepting_states, state) assert (disabled_ci == set(["[CI]_[ENTRY]_void m2()(1)"])) self.assertTrue( self._accept_word(ts_enc, ts, ["[CB]_[ENTRY]_void m1()(1)"], accepting_states)) self.assertFalse( self._accept_word(ts_enc, ts, ["[CI]_[ENTRY]_void m2()(1)"], accepting_states)) error = And(accepting_states, TSEncoder._get_state_var("[CI]_[ENTRY]_void m2()(1)")) self.assertFalse( self._accept_word(ts_enc, ts, ["[CB]_[ENTRY]_void m1()(1)"], error))
def test_encode_cbs(self): def cb(name): fmwk_over = TestGrounding._get_fmwkov("", name, False) cb = CCallback(1, 1, "", name, [], None, [fmwk_over]) return cb def ci(name): ci = CCallin(1, 1, "", name, [], None) return ci def new_trace(tree_trace_list): def add_rec(parent, children): for (child, lchildren) in children: parent.add_msg(child) add_rec(child, lchildren) ctrace = CTrace() add_rec(ctrace, tree_trace_list) return ctrace trace_tree = [(cb("cb1"), [(ci("ci1"), [])]), (cb("cb2"), [(ci("ci2"), [])])] ctrace = new_trace(trace_tree) ctrace.print_trace(sys.stdout) # ctrace.print_trace(sys.stdout) ts_enc = TSEncoder(ctrace, []) vars_ts = ts_enc._encode_vars() (ts, errors) = ts_enc._encode_cbs(set()) ts.product(vars_ts) self.assertTrue(len(errors) == 0) cb_1_seq = [ "[CB]_[ENTRY]_cb1()", "[CI]_[ENTRY]_ci1()", "[CI]_[EXIT]_ci1()", "[CB]_[EXIT]_cb1()" ] cb_2_seq = [ "[CB]_[ENTRY]_cb2()", "[CI]_[ENTRY]_ci2()", "[CI]_[EXIT]_ci2()", "[CB]_[EXIT]_cb2()" ] cb_11 = [ "[CB]_[ENTRY]_cb1()", "[CI]_[ENTRY]_ci1()", "[CI]_[EXIT]_ci1()", "[CB]_[EXIT]_cb1()", "[CB]_[ENTRY]_cb1()", "[CI]_[ENTRY]_ci1()", "[CI]_[EXIT]_ci1()", "[CB]_[EXIT]_cb1()" ] cb_12 = [ "[CB]_[ENTRY]_cb1()", "[CI]_[ENTRY]_ci1()", "[CI]_[EXIT]_ci1()", "[CB]_[EXIT]_cb1()", "[CB]_[ENTRY]_cb2()", "[CI]_[ENTRY]_ci2()", "[CI]_[EXIT]_ci2()", "[CB]_[EXIT]_cb2()" ] cb_22 = [ "[CB]_[ENTRY]_cb2()", "[CI]_[ENTRY]_ci2()", "[CI]_[EXIT]_ci2()", "[CB]_[EXIT]_cb2()", "[CB]_[ENTRY]_cb2()", "[CI]_[ENTRY]_ci2()", "[CI]_[EXIT]_ci2()", "[CB]_[EXIT]_cb2()" ] accepting_traces = [cb_1_seq, cb_2_seq, cb_11, cb_12, cb_22] for seq in accepting_traces: self.assertTrue(self._accept_word(ts_enc, ts, seq, TRUE())) deadlock_traces = [["[CI]_[ENTRY]_ci1()"], ["[CI]_[ENTRY]_ci2()"], [ "[CB]_[ENTRY]_cb1()", "[CI]_[ENTRY]_ci1()", "[CI]_[EXIT]_ci1()", "[CB]_[EXIT]_cb1()", "[CI]_[ENTRY]_ci2()", "[CI]_[EXIT]_ci2()" ], [ "[CB]_[ENTRY]_cb2()", "[CI]_[ENTRY]_ci1()", "[CB]_[EXIT]_cb1()" ]] for seq in deadlock_traces: self.assertFalse(self._accept_word(ts_enc, ts, seq, TRUE())) ts_enc = TSEncoder(ctrace, []) vars_ts = ts_enc._encode_vars() (ts, errors) = ts_enc._encode_cbs(set(["[CI]_[ENTRY]_ci1()"])) ts.product(vars_ts) self.assertTrue(len(errors) == 1) self.assertTrue( is_sat( And(errors[0], Not(TSEncoder._get_state_var("[CI]_[ENTRY_]ci1()"))))) ts_enc = TSEncoder(ctrace, []) vars_ts = ts_enc._encode_vars() (ts, errors) = ts_enc._encode_cbs(set(["[CB]_[EXIT]_cb1()"])) ts.product(vars_ts) self.assertTrue(len(errors) == 1) self.assertTrue( is_sat( And(errors[0], Not(TSEncoder._get_state_var("[CB]_[EXIT]_cb1()"))))) ts_enc = TSEncoder(ctrace, []) vars_ts = ts_enc._encode_vars() (ts, errors) = ts_enc._encode_cbs( set(["[CI]_[ENTRY]_ci1()", "[CI]_[ENTRY]_ci2()"])) ts.product(vars_ts) self.assertTrue(len(errors) == 1) self.assertTrue( is_sat( And([ errors[0], Not(TSEncoder._get_state_var("[CI]_[ENTRY]_ci1()")), Not(TSEncoder._get_state_var("[CI]_[ENTRY]_ci2()")) ])))