def test_nested_logic_works(): eq_cond_1 = top.Equal('A1', 100) eq_cond_2 = top.Equal('A2', 100) wait_cond = top.WaitFor(100) or_cond = top.Or([eq_cond_1, eq_cond_2]) and_cond = top.And([or_cond, wait_cond]) state_0 = {'pressures': {'A1': 0, 'A2': 0, 'A3': 0}, 'time': 0} state_1 = {'pressures': {'A1': 100, 'A2': 200, 'A3': 300}, 'time': 100} assert eq_cond_1.satisfied() is False assert eq_cond_2.satisfied() is False assert wait_cond.satisfied() is False assert or_cond.satisfied() is False assert and_cond.satisfied() is False and_cond.reinitialize(state_0) assert eq_cond_1.satisfied() is False assert eq_cond_2.satisfied() is False assert wait_cond.satisfied() is False assert or_cond.satisfied() is False assert and_cond.satisfied() is False and_cond.update(state_1) assert eq_cond_1.satisfied() is True assert eq_cond_2.satisfied() is False assert wait_cond.satisfied() is True assert or_cond.satisfied() is True assert and_cond.satisfied() is True
def test_or_requires_only_one_satisfied(): eq_cond_1 = top.Equal('A1', 100) eq_cond_2 = top.Equal('A2', 100) or_cond = top.Or([eq_cond_1, eq_cond_2]) state = {'pressures': {'A1': 100, 'A2': 200}} assert eq_cond_1.satisfied() is False assert eq_cond_2.satisfied() is False assert or_cond.satisfied() is False or_cond.update(state) assert eq_cond_1.satisfied() is True assert eq_cond_2.satisfied() is False assert or_cond.satisfied() is True
def test_and_requires_all_satisfied(): eq_cond_1 = top.Equal('A1', 100) eq_cond_2 = top.Equal('A2', 100) and_cond = top.And([eq_cond_1, eq_cond_2]) state = {'pressures': {'A1': 100, 'A2': 200}} assert eq_cond_1.satisfied() is False assert eq_cond_2.satisfied() is False assert and_cond.satisfied() is False and_cond.update(state) assert eq_cond_1.satisfied() is True assert eq_cond_2.satisfied() is False assert and_cond.satisfied() is False
def test_equal_condition_with_eps(): cond = top.Equal('A1', 100, 1) state = {'pressures': {'A1': 101}} assert cond.satisfied() is False cond.update(state) assert cond.satisfied() is True
def test_or_updates_subconditions(): eq_cond = top.Equal('A1', 100) or_cond = top.Or([eq_cond]) state = {'pressures': {'A1': 100}} assert eq_cond.satisfied() is False assert or_cond.satisfied() is False or_cond.update(state) assert eq_cond.satisfied() is True assert or_cond.satisfied() is True
def test_string_representations(): immediate_cond = top.Immediate() waitFor_cond = top.WaitFor(1000000) equal_cond = top.Equal('A1', 100) less_cond = top.Less('A2', 100) greater_cond = top.Greater('A3', 100) lessEqual_cond = top.LessEqual('A4', 100) greaterEqual_cond = top.GreaterEqual('A5', 100) and_cond = top.And([less_cond, greater_cond]) or_cond = top.Or([lessEqual_cond, greaterEqual_cond]) assert str(immediate_cond) == 'Immediately' assert str(waitFor_cond) == 'Wait for 1 seconds' assert str(equal_cond) == 'A1 == 100' assert str(less_cond) == 'A2 < 100' assert str(greater_cond) == 'A3 > 100' assert str(lessEqual_cond) == 'A4 <= 100' assert str(greaterEqual_cond) == 'A5 >= 100' assert str(and_cond) == '(A2 < 100 and A3 > 100)' assert str(or_cond) == '(A4 <= 100 or A5 >= 100)'
def test_equal_condition_equality(): cond1 = top.Equal('p1', 100) cond2 = top.Equal('p1', 100) cond3 = top.Equal('p1', 100, 0) cond4 = top.Equal('p1', 200) cond5 = top.Equal('p2', 100) cond6 = top.Equal('p1', 100, 10) assert cond1 == cond2 assert cond1 == cond3 assert cond1 != cond4 assert cond1 != cond5 assert cond1 != cond6 assert cond1 != 100 assert cond1 is not None
def test_parse_steps_with_comparisons(): proclang = ''' main: 1. PRIMARY: set injector_valve to open 2. PRIMARY: [p1 < 100] set vent_valve to closed 3. PRIMARY: [p1 > 100] set vent_valve to open 4. PRIMARY: [p1 <= 100] set vent_valve to closed 5. SECONDARY: [p1 >= 100] set vent_valve to open 6. SECONDARY: [p1 == 100] set vent_valve to closed ''' suite = top.proclang.parse(proclang) expected_suite = top.ProcedureSuite([ top.Procedure('main', [ top.ProcedureStep( '1', top.StateChangeAction('injector_valve', 'open'), [ (top.Less('p1', 100), top.Transition('main', '2')) ], 'PRIMARY'), top.ProcedureStep('2', top.StateChangeAction( 'vent_valve', 'closed'), [(top.Greater( 'p1', 100), top.Transition('main', '3'))], 'PRIMARY'), top.ProcedureStep('3', top.StateChangeAction( 'vent_valve', 'open'), [(top.LessEqual( 'p1', 100), top.Transition('main', '4'))], 'PRIMARY'), top.ProcedureStep('4', top.StateChangeAction( 'vent_valve', 'closed'), [(top.GreaterEqual( 'p1', 100), top.Transition('main', '5'))], 'PRIMARY'), top.ProcedureStep('5', top.StateChangeAction( 'vent_valve', 'open'), [(top.Equal( 'p1', 100), top.Transition('main', '6'))], 'SECONDARY'), top.ProcedureStep('6', top.StateChangeAction( 'vent_valve', 'closed'), [], 'SECONDARY') ]) ]) assert suite == expected_suite