def test_dfs(self): pta = PTA(["IDLE", "TX"]) pta.add_transition("UNINITIALIZED", "IDLE", "init") pta.add_transition("IDLE", "TX", "send") pta.add_transition("TX", "IDLE", "txComplete") self.assertEqual(dfs_tran_to_name(pta.dfs(0), False), [["init"]]) self.assertEqual(dfs_tran_to_name(pta.dfs(1), False), [["init", "send"]]) self.assertEqual(dfs_tran_to_name(pta.dfs(2), False), [["init", "send", "txComplete"]]) self.assertEqual( dfs_tran_to_name(pta.dfs(3), False), [["init", "send", "txComplete", "send"]], ) pta = PTA(["IDLE"]) pta.add_transition("UNINITIALIZED", "IDLE", "init") pta.add_transition("IDLE", "IDLE", "set1") pta.add_transition("IDLE", "IDLE", "set2") self.assertEqual(dfs_tran_to_name(pta.dfs(0), False), [["init"]]) self.assertEqual( sorted(dfs_tran_to_name(pta.dfs(1), False)), [["init", "set1"], ["init", "set2"]], ) self.assertEqual( sorted(dfs_tran_to_name(pta.dfs(2), False)), [ ["init", "set1", "set1"], ["init", "set1", "set2"], ["init", "set2", "set1"], ["init", "set2", "set2"], ], )
def test_simulation_arg_to_param_map(self): pta = PTA(parameters=["txpower", "length"]) pta.add_state("IDLE", power=5) pta.add_state("TX", power=100) pta.add_transition("UNINITIALIZED", "IDLE", "init", energy=500000, duration=50000) pta.add_transition( "IDLE", "IDLE", "setTxPower", energy=10000, duration=120, arg_to_param_map={0: "txpower"}, ) pta.add_transition("IDLE", "TX", "send", energy=3, duration=10) pta.add_transition("TX", "IDLE", "txComplete", timeout=2000, is_interrupt=True) trace = [["init"], ["setTxPower", 10]] expected_energy = 510000 expected_duration = 50120 result = pta.simulate(trace) self.assertAlmostEqual(result.energy, expected_energy * 1e-12, places=12) self.assertAlmostEqual(result.duration, expected_duration * 1e-6, places=6) self.assertEqual(result.end_state.name, "IDLE") self.assertEqual(result.parameters, {"txpower": 10, "length": None})
def test_simulation(self): pta = PTA() pta.add_state("IDLE", power=5) pta.add_state("TX", power=100) pta.add_transition("UNINITIALIZED", "IDLE", "init", duration=50000) pta.add_transition("IDLE", "TX", "send", energy=3, duration=10) pta.add_transition("TX", "IDLE", "txComplete", timeout=2000, is_interrupt=True) trace = [ ["init"], [None, 10000000], ["send", "foo", 3], [None, 5000000], ["send", "foo", 3], ] expected_energy = 5.0 * 10000000 + 3 + 100 * 2000 + 5 * 5000000 + 3 + 100 * 2000 expected_duration = 50000 + 10000000 + 10 + 2000 + 5000000 + 10 + 2000 result = pta.simulate(trace) self.assertAlmostEqual(result.energy, expected_energy * 1e-12, places=12) self.assertAlmostEqual(result.duration, expected_duration * 1e-6, places=6) self.assertEqual(result.end_state.name, "IDLE") self.assertEqual(result.parameters, {})
def test_dfs_trace_filter(self): pta = PTA(["IDLE"]) pta.add_transition("UNINITIALIZED", "IDLE", "init") pta.add_transition("IDLE", "IDLE", "set1") pta.add_transition("IDLE", "IDLE", "set2") self.assertEqual( sorted( dfs_tran_to_name( pta.dfs( 2, trace_filter=[ ["init", "set1", "set2"], ["init", "set2", "set1"], ], ), False, )), [["init", "set1", "set2"], ["init", "set2", "set1"]], ) self.assertEqual( sorted( dfs_tran_to_name( pta.dfs(2, trace_filter=[["init", "set1", "$"], ["init", "set2", "$"]]), False, )), [["init", "set1"], ["init", "set2"]], )
def test_dfs_accepting(self): pta = PTA(["IDLE", "TX"], accepting_states=["IDLE"]) pta.add_transition("UNINITIALIZED", "IDLE", "init") pta.add_transition("IDLE", "TX", "send") pta.add_transition("TX", "IDLE", "txComplete") self.assertEqual(dfs_tran_to_name(pta.dfs(0), False), [["init"]]) self.assertEqual(dfs_tran_to_name(pta.dfs(1), False), []) self.assertEqual(dfs_tran_to_name(pta.dfs(2), False), [["init", "send", "txComplete"]]) self.assertEqual(dfs_tran_to_name(pta.dfs(3), False), [])
def test_dfs_objects(self): pta = PTA(["IDLE", "TX"]) pta.add_transition("UNINITIALIZED", "IDLE", "init") pta.add_transition("IDLE", "TX", "send") pta.add_transition("TX", "IDLE", "txComplete") traces = list(pta.dfs(2)) self.assertEqual(len(traces), 1) trace = traces[0] self.assertEqual(len(trace), 3) self.assertEqual(trace[0][0].name, "init") self.assertEqual(trace[1][0].name, "send") self.assertEqual(trace[2][0].name, "txComplete") self.assertEqual(pta.get_transition_id(trace[0][0]), 0) self.assertEqual(pta.get_transition_id(trace[1][0]), 1) self.assertEqual(pta.get_transition_id(trace[2][0]), 2)
def test_dfs_with_sleep(self): pta = PTA(["IDLE", "TX"]) pta.add_transition("UNINITIALIZED", "IDLE", "init") pta.add_transition("IDLE", "TX", "send") pta.add_transition("TX", "IDLE", "txComplete") traces = list(pta.dfs(2, sleep=10)) self.assertEqual(len(traces), 1) trace = traces[0] self.assertEqual(len(trace), 6) self.assertIsNone(trace[0][0]) self.assertEqual(trace[1][0].name, "init") self.assertIsNone(trace[2][0]) self.assertEqual(trace[3][0].name, "send") self.assertIsNone(trace[4][0]) self.assertEqual(trace[5][0].name, "txComplete") self.assertEqual(pta.get_transition_id(trace[1][0]), 0) self.assertEqual(pta.get_transition_id(trace[3][0]), 1) self.assertEqual(pta.get_transition_id(trace[5][0]), 2)
def test_simulation_set_param(self): pta = PTA(parameters=["txpower", "length"]) pta.add_state("IDLE", power=5) pta.add_state("TX", power=100) pta.add_transition( "UNINITIALIZED", "IDLE", "init", energy=500000, duration=50000, set_param={"txpower": 10}, ) trace = [["init"]] expected_energy = 500000 expected_duration = 50000 result = pta.simulate(trace) self.assertAlmostEqual(result.energy, expected_energy * 1e-12, places=12) self.assertAlmostEqual(result.duration, expected_duration * 1e-6, places=6) self.assertEqual(result.end_state.name, "IDLE") self.assertEqual(result.parameters, {"txpower": 10, "length": None})
def test_simulation_param_function(self): pta = PTA(parameters=["length", "txpower"]) pta.add_state("IDLE", power=5) pta.add_state( "TX", power=AnalyticFunction( 100, "regression_arg(0) + regression_arg(1) * parameter(txpower)", ["length", "txpower"], regression_args=[1000, 2], ), ) pta.add_transition("UNINITIALIZED", "IDLE", "init", energy=500000, duration=50000) pta.add_transition( "IDLE", "IDLE", "setTxPower", energy=10000, duration=120, param_update_function=lambda param, arg: { **param, "txpower": arg[0] }, ) pta.add_transition( "IDLE", "TX", "send", energy=AnalyticFunction( 3, "regression_arg(0) + regression_arg(1) * function_arg(1)", ["length", "txpower"], regression_args=[3, 5], num_args=2, ), duration=10, param_update_function=lambda param, arg: { **param, "length": arg[1] }, ) pta.add_transition( "TX", "IDLE", "txComplete", is_interrupt=True, timeout=AnalyticFunction( 2000, "regression_arg(0) + regression_arg(1) * parameter(length)", ["length", "txpower"], regression_args=[500, 16], ), ) trace = [["init"], ["setTxPower", 10], ["send", "foo", 3]] expected_energy = (500000 + 10000 + (3 + 5 * 3) + (1000 + 2 * 10) * (500 + 16 * 3)) expected_duration = 50000 + 120 + 10 + (500 + 16 * 3) result = pta.simulate(trace) self.assertAlmostEqual(result.energy, expected_energy * 1e-12, places=12) self.assertAlmostEqual(result.duration, expected_duration * 1e-6, places=6) self.assertEqual(result.end_state.name, "IDLE") self.assertEqual(result.parameters, {"txpower": 10, "length": 3})
def test_simulation_arg_function(self): pta = PTA(parameters=["txpower", "length"]) pta.add_state("IDLE", power=5) pta.add_state("TX", power=100) pta.add_transition("UNINITIALIZED", "IDLE", "init", energy=500000, duration=50000) pta.add_transition( "IDLE", "IDLE", "setTxPower", energy=10000, duration=120, param_update_function=lambda param, arg: { **param, "txpower": arg[0] }, ) pta.add_transition( "IDLE", "TX", "send", energy=AnalyticFunction( 3, "regression_arg(0) + regression_arg(1) * function_arg(1)", ["txpower", "length"], regression_args=[3.0, 5], num_args=2, ), duration=AnalyticFunction( 10, "regression_arg(0) + regression_arg(1) * function_arg(1)", ["txpower", "length"], regression_args=[48, 8], num_args=2, ), ) pta.add_transition("TX", "IDLE", "txComplete", timeout=2000, is_interrupt=True) trace = [["init"], ["setTxPower", 10], ["send", "foo", 3]] expected_energy = 500000 + 10000 + (3 + 5 * 3) + (2000 * 100) expected_duration = 50000 + 120 + (48 + 8 * 3) + 2000 result = pta.simulate(trace) self.assertAlmostEqual(result.energy, expected_energy * 1e-12, places=12) self.assertAlmostEqual(result.duration, expected_duration * 1e-6, places=6) self.assertEqual(result.end_state.name, "IDLE") self.assertEqual(result.parameters, {"txpower": 10, "length": None}) pta = PTA(parameters=["txpower", "length"]) pta.add_state("IDLE", power=5) pta.add_state("TX", power=100) pta.add_transition("UNINITIALIZED", "IDLE", "init", energy=500000, duration=50000) pta.add_transition( "IDLE", "IDLE", "setTxPower", energy=10000, duration=120, param_update_function=lambda param, arg: { **param, "txpower": arg[0] }, ) pta.add_transition( "IDLE", "TX", "send", energy=AnalyticFunction( 3, "regression_arg(0) + regression_arg(1) * function_arg(1)", ["txpower", "length"], regression_args=[3, 5], num_args=2, ), duration=AnalyticFunction( 10, "regression_arg(0) + regression_arg(1) * function_arg(1)", ["txpower", "length"], regression_args=[48, 8], num_args=2, ), ) pta.add_transition("TX", "IDLE", "txComplete", timeout=2000, is_interrupt=True) trace = [["init"], ["setTxPower", 10], ["send", "foobar", 6]] expected_energy = 500000 + 10000 + (3 + 5 * 6) + (2000 * 100) expected_duration = 50000 + 120 + (48 + 8 * 6) + 2000 result = pta.simulate(trace) self.assertAlmostEqual(result.energy, expected_energy * 1e-12, places=12) self.assertAlmostEqual(result.duration, expected_duration * 1e-6, places=6) self.assertEqual(result.end_state.name, "IDLE") self.assertEqual(result.parameters, {"txpower": 10, "length": None})