def monitor(): # data dataSet = { 'a': [(0, 100.0), (1, -1.0), (2, -2.0)], 'b': [(0, 20.0), (1, 2.0), (2, -10.0)] } for i in range(0, 7): bound = 10**i bound_str = str(bound) spec_python = rtamt.STLDiscreteTimeSpecification( language=rtamt.Language.PYTHON) spec_python.name = 'PythonMonitor' spec_python.declare_var('a', 'float') spec_python.declare_var('b', 'float') spec_python.declare_var('c', 'float') spec_python.spec = 'c = always[0:' + bound_str + '](a + b >= - 2)' spec_cpp = rtamt.STLDiscreteTimeSpecification( language=rtamt.Language.CPP) spec_cpp.name = 'CPPMonitor' spec_cpp.declare_var('a', 'float') spec_cpp.declare_var('b', 'float') spec_cpp.declare_var('c', 'float') spec_cpp.spec = 'c = always[0:' + bound_str + '](a + b >= - 2)' try: spec_python.parse() spec_cpp.parse() except rtamt.STLParseException as err: print('STL Parse Exception: {}'.format(err)) sys.exit() # # eval aTraj = dataSet['a'] bTraj = dataSet['b'] for i in range(len(dataSet['a'])): aData = aTraj[i] bData = bTraj[i] start_python = time.time() spec_python.update(aData[0], [('a', aData[1]), ('b', bData[1])]) end_python = time.time() start_cpp = time.time() spec_cpp.update(aData[0], [('a', aData[1]), ('b', bData[1])]) end_cpp = time.time() time_python = end_python - start_python time_cpp = end_cpp - start_cpp print('Bound {0} - Python time {1} - CPP time {2}'.format( bound, time_python, time_cpp))
def monitor(): # data dataSet = { 'a': [(0, 100.0), (1, -1.0), (2, -2.0)], 'b': [(0, 20.0), (1, 2.0), (2, -10.0)] } # # stl spec = rtamt.STLDiscreteTimeSpecification(1) spec.name = 'HandMadeMonitor' spec.declare_var('a', 'float') spec.declare_var('b', 'float') spec.declare_var('c', 'float') spec.spec = 'c = (a>=2) unless[1,2] (b>=-2)' try: spec.parse() spec.pastify() except rtamt.STLParseException as err: print('STL Parse Exception: {}'.format(err)) sys.exit() # # eval aTraj = dataSet['a'] bTraj = dataSet['b'] for i in range(len(dataSet['a'])): aData = aTraj[i] bData = bTraj[i] rob = spec.update(aData[0], [('a', aData[1]), ('b', bData[1])]) print('time=' + str(aData[0]) + ' rob=' + str(rob))
def test_example(self): spec = rtamt.STLDiscreteTimeSpecification(0) spec.name = 'STL Example specification' self.assertEqual(spec.name, 'STL Example specification', 'Spec name assertion') spec.declare_var('req', 'float') spec.declare_var('gnt', 'float') spec.declare_var('out', 'float') spec.spec = 'out = req' self.assertEqual(spec.spec, 'out = req', 'Spec assertion') self.assertEqual(spec.sampling_tolerance, 0.1, 'Spec sampling tolerance assertion') try: spec.parse() computed = spec.update(0, [['req', 2.2], ['gnt', 1]]) self.assertEqual(2.2, computed, 'First computation') computed = spec.update(1.2, [['req', 4.2], ['gnt', -3.7]]) self.assertEqual(4.2, computed, 'First computation') self.assertEqual(1, spec.sampling_violation_counter, 'Violation counter') except rtamt.STLParseException as err: print('STL Parse Exception: {}'.format(err))
def test_default_unit_default_sampling_unit_greater_tolerance_ok_samples(self): spec = rtamt.STLDiscreteTimeSpecification() spec.name = 'STL Example specification' self.assertEqual(spec.name, 'STL Example specification', 'Spec name assertion') spec.declare_var('req', 'float') spec.declare_var('gnt', 'float') spec.declare_var('out', 'float') spec.set_sampling_period(1, 's', 0.5) spec.spec = 'out = rise(req)' try: spec.parse(); spec.update(0, [['req', 2.2], ['gnt', 1]]) spec.update(1.11, [['req', 2.2], ['gnt', 1]]) spec.update(1.99, [['req', 2.2], ['gnt', 1]]) spec.update(3.38, [['req', 2.2], ['gnt', 1]]) spec.update(4.39, [['req', 2.2], ['gnt', 1]]) self.assertEqual(0, spec.sampling_violation_counter, 'Violation counter') except rtamt.STLParseException as err: print('STL Parse Exception: {}'.format(err))
def monitor(): # Load traces data = read_csv('example1.csv') spec = rtamt.STLDiscreteTimeSpecification() spec.name = 'Example 1' spec.declare_const('threshold', 'float', '3') spec.declare_const('T', 'float', '5') spec.declare_var('req', 'float') spec.declare_var('gnt', 'float') spec.declare_var('response', 'float') spec.declare_var('out', 'float') spec.set_var_io_type('req', 'input') spec.set_var_io_type('gnt', 'output') spec.add_sub_spec('response = eventually[0:T s](gnt >= threshold)') spec.spec = 'out = ((req >= threshold) implies response)' try: spec.parse() spec.pastify() except rtamt.STLParseException as err: print('STL Parse Exception: {}'.format(err)) sys.exit() for i in range(len(data[' gnt'])): out_rob = spec.update(i, [('req', data[' req'][i][1]), ('gnt', data[' gnt'][i][1])]) response_rob = spec.get_value('response') print( 'time: {0}, response robustness: {1}, out robustness: {2}'.format( i, response_rob, out_rob))
def monitor(): # data dataSet = { 'a': [(0, 100.0), (1, -1.0), (2, -2.0)], 'b': [(0, 20.0), (1, 2.0), (2, -10.0)] } # # stl spec = rtamt.STLDiscreteTimeSpecification(language=rtamt.Language.PYTHON) spec.name = 'HandMadeMonitor' spec.declare_var('a', 'float') spec.declare_var('b', 'float') spec.declare_const('c', 'float', '5.2') spec.declare_const('d', 'float', '2') spec.spec = 'a + b >= once[0, d s] c' try: spec.parse() except rtamt.STLParseException as err: print('STL Parse Exception: {}'.format(err)) sys.exit() # # eval aTraj = dataSet['a'] bTraj = dataSet['b'] for i in range(len(dataSet['a'])): aData = aTraj[i] bData = bTraj[i] rob = spec.update(aData[0], [('a', aData[1]), ('b', bData[1])]) print('time='+str(aData[0])+' rob='+str(rob))
def test_eventually_with_pastify(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('out', 'float') spec.spec = 'out = eventually(req)' spec.parse() self.assertRaises(rtamt.LTLPastifyException, spec.pastify)
def test_always_0_1_without_pastify(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('out', 'float') spec.spec = 'out = always[0,1](req)' spec.parse() self.assertRaises(rtamt.STLNotImplementedException, spec.update, 0, [('req', self.left1)])
def test_until_0_1_without_pastify(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('gnt', 'float') spec.declare_var('out', 'float') spec.spec = 'out = req until[0,1] gnt' spec.parse() self.assertRaises(rtamt.STLNotImplementedException, spec.update, 0, [('req', self.left1), ('gnt', self.right1)])
def test_constant_1(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('out', 'float') spec.spec = 'out = 5' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, 5], [1, 5], [2, 5], [3, 5], [4, 5]] self.assertListEqual(out, expected, "constant")
def test_always_with_pastify(self): spec = rtamt.STLDiscreteTimeSpecification() spec.declare_var('req', 'float') spec.declare_var('gnt', 'float') spec.declare_var('out', 'float') spec.spec = 'out = req until gnt' spec.parse() self.assertRaises(rtamt.LTLPastifyException, spec.pastify)
def test_next(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('out', 'float') spec.spec = 'out = next req' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, -1], [1, -2], [2, 5], [3, -1], [4, float("inf")]] self.assertListEqual(out, expected, "next")
def test_previous(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('out', 'float') spec.spec = 'out = prev req' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, float("inf")], [1, 100], [2, -1], [3, -2], [4, 5]] self.assertListEqual(out, expected, "previous")
def test_always(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('out', 'float') spec.spec = 'out = always req' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, -2], [1, -2], [2, -2], [3, -1], [4, -1]] self.assertListEqual(out, expected, "always")
def test_sqrt(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('out', 'float') spec.spec = 'out = sqrt(abs(req))' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, math.sqrt(100)], [1, math.sqrt(1)], [2, math.sqrt(2)], [3, math.sqrt(5)], [4, math.sqrt(1)]] self.assertListEqual(out, expected, "abs")
def test_abs(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('out', 'float') spec.spec = 'out = abs(req)' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, 100], [1, 1], [2, 2], [3, 5], [4, 1]] self.assertListEqual(out, expected, "abs")
def test_constant(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('out', 'float') spec.spec = 'out = 5' spec.parse(); out1 = spec.update(0, []) out2 = spec.update(1, []) self.assertEqual(out1, 5, "input 1") self.assertEqual(out2, 5, "input 2")
def test_historically_1_2(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('out', 'float') spec.spec = 'out = historically[1,2] req' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, float("inf")], [1, 100], [2, -1], [3, -2], [4, -2]] self.assertListEqual(out, expected, "historically[1,2]")
def test_pow(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('out', 'float') spec.spec = 'out = pow(2.2,req)' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, math.pow(2.2, 100)], [1, math.pow(2.2, -1)], [2, math.pow(2.2, -2)], [3, math.pow(2.2, 5)], [4, math.pow(2.2, -1)]] self.assertListEqual(out, expected, "pow")
def test_once_0_1(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('out', 'float') spec.spec = 'out = once[0,1] req' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, 100], [1, 100], [2, -1], [3, 5], [4, 5]] self.assertListEqual(out, expected, "once[0,1]")
def test_eventually(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('out', 'float') spec.spec = 'out = eventually req' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, 100], [1, 5], [2, 5], [3, 5], [4, -1]] self.assertListEqual(out, expected, "eventually")
def test_addition(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('gnt', 'float') spec.declare_var('out', 'float') spec.spec = 'out = req + gnt' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, 120], [1, -3], [2, 8], [3, 9], [4, -2]] self.assertListEqual(out, expected, "addition")
def test_iff(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('gnt', 'float') spec.declare_var('out', 'float') spec.spec = 'out = req iff gnt' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, -80], [1, -1], [2, -12], [3, -1], [4, 0]] self.assertListEqual(out, expected, "iff")
def test_division(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('gnt', 'float') spec.declare_var('out', 'float') spec.spec = 'out = req / gnt' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, 100/20], [1, -1/-2], [2, -2/10], [3, 5/4], [4, -1/-1]] self.assertListEqual(out, expected, "division")
def test_multiplication(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('gnt', 'float') spec.declare_var('out', 'float') spec.spec = 'out = req * gnt' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, 2000], [1, 2], [2, -20], [3, 20], [4, 1]] self.assertListEqual(out, expected, "multiplication")
def test_predicate_neq(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('gnt', 'float') spec.declare_var('out', 'float') spec.spec = 'out = req !== gnt' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, 80], [1, 1], [2, 12], [3, 1], [4, 0]] self.assertListEqual(out, expected, "neq")
def test_until_0_1(self): spec = rtamt.STLDiscreteTimeSpecification(); spec.declare_var('req', 'float') spec.declare_var('gnt', 'float') spec.declare_var('out', 'float') spec.spec = 'out = req until[0,1] gnt' spec.parse(); out = spec.evaluate(self.dataset) expected = [[0, 20], [1, -1], [2, 10], [3, 4], [4, -1]] self.assertListEqual(out, expected, "until")
def test_interval_style(self): spec_comma = rtamt.STLDiscreteTimeSpecification() spec_comma.name = 'STL Comma Style' spec_column = rtamt.STLDiscreteTimeSpecification() spec_column.name = 'STL Column Style' spec_comma.declare_var('req', 'float') spec_comma.declare_var('gnt', 'float') spec_comma.declare_var('out', 'float') spec_column.declare_var('req', 'float') spec_column.declare_var('gnt', 'float') spec_column.declare_var('out', 'float') spec_comma.spec = 'out = once[0,2] (req<=2 and gnt>=3)' spec_column.spec = 'out = once[0:2] (req<=2 and gnt>=3)' try: spec_comma.parse(); spec_column.parse(); computed_comma = spec_comma.update(0, [['req', 2.2], ['gnt', 1]]) computed_column = spec_column.update(0, [['req', 2.2], ['gnt', 1]]) self.assertEqual(computed_comma, computed_column, 'First computation') computed_comma = spec_comma.update(1, [['req', 1.2], ['gnt', 3.1]]) computed_column = spec_column.update(1, [['req', 1.2], ['gnt', 3.1]]) self.assertEqual(computed_comma, computed_column, 'Second computation') computed_comma = spec_comma.update(2, [['req', 3.3], ['gnt', 1.4]]) computed_column = spec_column.update(2, [['req', 3.3], ['gnt', 1.4]]) self.assertEqual(computed_comma, computed_column, 'Second computation') computed_comma = spec_comma.update(3, [['req', 4.3], ['gnt', 4.4]]) computed_column = spec_column.update(3, [['req', 4.3], ['gnt', 4.4]]) self.assertEqual(computed_comma, computed_column, 'Second computation') except rtamt.STLParseException as err: print('STL Parse Exception: {}'.format(err))
def test_output_vacuity(self): spec = rtamt.STLDiscreteTimeSpecification( semantics=rtamt.Semantics.OUTPUT_VACUITY) spec.declare_var('req', 'float') spec.declare_var('gnt', 'float') spec.declare_var('out', 'float') spec.set_var_io_type('req', 'output') spec.set_var_io_type('gnt', 'input') spec.spec = 'out = (req >= 3) implies (gnt >= 0)' spec.parse() out = spec.evaluate(self.dataset) expected = [[0, 0], [1, 4], [2, 5], [3, 0], [4, 4]] self.assertListEqual(out, expected, "output vacuity")
def test_input_robustness(self): spec = rtamt.STLDiscreteTimeSpecification( semantics=rtamt.Semantics.INPUT_ROBUSTNESS) spec.declare_var('req', 'float') spec.declare_var('gnt', 'float') spec.declare_var('out', 'float') spec.set_var_io_type('req', 'output') spec.set_var_io_type('gnt', 'input') spec.spec = 'out = (req >= 3) implies (gnt >= 0)' spec.parse() out = spec.evaluate(self.dataset) expected = [[0, 20], [1, float("inf")], [2, float("inf")], [3, 4], [4, float("inf")]] self.assertListEqual(out, expected, "input robustness")