def check_event(event): global last_time, tl_oracle, property, last_res event_dict = json.loads(event) if not tl_oracle: if model == 'dense' and 'time' in event_dict: tl_oracle = reelay.dense_timed_monitor(pattern=property.PROPERTY) else: tl_oracle = reelay.discrete_timed_monitor( pattern=property.PROPERTY) if 'time' in event_dict: if last_time is None: last_time = event_dict['time'] event_dict['time'] = 0 else: event_dict['time'] = event_dict['time'] - last_time abs_msg = property.abstract_message(event_dict) res = tl_oracle.update(abs_msg) if model == 'discrete' and res: last_res = res['value'] if model == 'dense' and res: last_res = True for d in res: if not d['value']: last_res = False break return last_res
def test_discrete_categ_condensing(): my_monitor = discrete_timed_monitor(pattern=r"""forall[sensor]. {sensor_id: *sensor, action: send_data} implies once[:4]{sensor_id: *sensor, action: calibrated} """, semantics="boolean", condense=True) input_sequence = [ dict(sensor_id="1", action="calibrated"), dict(sensor_id="2", action="calibrated"), dict(sensor_id="1", action="send_data"), dict(sensor_id="1", action="send_data"), dict(sensor_id="1", action="send_data"), dict(sensor_id="1", action="calibrated"), dict(sensor_id="2", action="send_data"), dict(sensor_id="1", action="send_data"), dict(sensor_id="1", action="send_data"), dict(sensor_id="2", action="send_data"), ] result = [] for x in input_sequence: y = my_monitor.update(x) result.append(y) expected = [ { 'time': 0, 'value': True }, {}, # 1 {}, # 2 {}, # 3 {}, # 4 {}, # 5 { 'time': 6, 'value': False }, { 'time': 7, 'value': True }, {}, # 8 { 'time': 9, 'value': False } ] approx_expected = [pytest.approx(x, 0.001) for x in expected] assert result == approx_expected
def test_discrete_robust(): my_monitor = discrete_timed_monitor( pattern=r"{speed > 13.0} since[:3] {lights_on: true}", semantics="robustness", y_name="rval", condense=False) input_sequence = [ dict(speed=3.3, lights_on=False), dict(speed=6.3, lights_on=False), dict(speed=9.3, lights_on=True), dict(speed=13.3, lights_on=False), dict(speed=13.4, lights_on=False), dict(speed=13.3, lights_on=False), dict(speed=13.2, lights_on=False), dict(speed=13.2, lights_on=False), ] result = [] for x in input_sequence: y = my_monitor.update(x) result.append(y) expected = [{ 'rval': -float('inf') }, { 'rval': -float('inf') }, { 'rval': float('inf') }, { 'rval': 0.3 }, { 'rval': 0.3 }, { 'rval': 0.3 }, { 'rval': -float('inf') }, { 'rval': -float('inf') }] approx_expected = [pytest.approx(x, 0.001) for x in expected] assert result == approx_expected
def test_discrete_prop(): my_monitor = discrete_timed_monitor( pattern=r"{speed > 13.0} since[:3] {lights_on}", semantics="boolean", y_name="verdict", condense=False) input_sequence = [ dict(speed=3.3, lights_on=False), dict(speed=6.3, lights_on=False), dict(speed=9.3, lights_on=True), dict(speed=13.3, lights_on=False), dict(speed=13.4, lights_on=False), dict(speed=13.3, lights_on=False), dict(speed=13.2, lights_on=False), dict(speed=13.2, lights_on=False), ] result = [] for x in input_sequence: y = my_monitor.update(x) result.append(y) expected = [{ 'verdict': False }, { 'verdict': False }, { 'verdict': True }, { 'verdict': True }, { 'verdict': True }, { 'verdict': True }, { 'verdict': False }, { 'verdict': False }] assert result == expected
] faulty_sys_behavior = [ dict(door_open=False, dow_suppressed=False, door_open_warning=False), dict(door_open=True, dow_suppressed=False, door_open_warning=False), dict(door_open=True, dow_suppressed=False, door_open_warning=False), dict(door_open=True, dow_suppressed=False, door_open_warning=False), dict(door_open=True, dow_suppressed=False, door_open_warning=False), dict(door_open=True, dow_suppressed=False, door_open_warning=False), dict(door_open=True, dow_suppressed=False, door_open_warning=True), dict(door_open=True, dow_suppressed=True, door_open_warning=False), dict(door_open=True, dow_suppressed=True, door_open_warning=True), ] my_monitor_1 = reelay.discrete_timed_monitor( pattern= r"(historically[0:5]{door_open} and not {dow_suppressed}) -> {door_open_warning}", condense=False) my_monitor_2 = reelay.discrete_timed_monitor( pattern=r"{door_open_warning} -> historically[0:5]{door_open}", condense=False) my_monitor_3 = reelay.discrete_timed_monitor( pattern=r"{door_open_warning} -> not {dow_suppressed}", condense=False) my_monitor_4 = reelay.discrete_timed_monitor( pattern= r"{door_open_warning} -> not(pre({door_open} since {door_open_warning}))", condense=False) for x in faulty_sys_behavior: # Change to correct_sys_behavior