def test_compu_method_tab_verb(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.TAB_VERB.DEFAULT_VALUE "Verbal conversion with default value" TAB_VERB "%12.0" "" COMPU_TAB_REF CM.TAB_VERB.DEFAULT_VALUE.REF /end COMPU_METHOD /begin COMPU_VTAB CM.TAB_VERB.DEFAULT_VALUE.REF "List of text strings and relation to impl value" TAB_VERB 3 1 "SawTooth" 2 "Square" 3 "Sinus" DEFAULT_VALUE "unknown signal type" /end COMPU_VTAB /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() compu = functions.CompuMethod(session, module.compu_method[0]) assert compu(1) == "SawTooth" assert compu.inv("Sinus") == 3 assert compu(10) == "unknown signal type"
def test_compu_method_tab_nointerp_no_default(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.TAB_NOINTP.NO_DEFAULT_VALUE "" TAB_NOINTP "%8.4" "U/ min " COMPU_TAB_REF CM.TAB_NOINTP.NO_DEFAULT_VALUE.REF /end COMPU_METHOD /begin COMPU_TAB CM.TAB_NOINTP.NO_DEFAULT_VALUE.REF "" TAB_NOINTP 12 -3 98 -1 99 0 100 2 102 4 104 5 105 6 106 7 107 8 108 9 109 10 110 13 111 /end COMPU_TAB /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() compu = functions.CompuMethod(session, module.compu_method[0]) assert compu(-3) == 98 assert compu(8) == 108 assert compu.inv(108) == 8 assert compu(1) is None
def test_compu_method_tab_nointerp_both_defaults(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.TAB_NOINTP.DEFAULT_VALUE "" TAB_NOINTP "%8.4" "U/ min " COMPU_TAB_REF CM.TAB_NOINTP.DEFAULT_VALUE.REF /end COMPU_METHOD /begin COMPU_TAB CM.TAB_NOINTP.DEFAULT_VALUE.REF "" TAB_NOINTP 12 -3 98 -1 99 0 100 2 102 4 104 5 105 6 106 7 107 8 108 9 109 10 110 13 111 DEFAULT_VALUE "value out of range" DEFAULT_VALUE_NUMERIC 300.56 /* DEFAULT_VALUE_NUME RIC should be used here as the normal output is numeric */ /end COMPU_TAB /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() with pytest.raises(exceptions.StructuralError): compu = functions.CompuMethod(session, module.compu_method[0])
def import_a2l(self, file_name, debug=False): """ Parameters ---------- file_name: str Name of the A2L to be imported. If you don't specify an extension ``.a2l`` is added. Returns ------- SQLAlchemy session object. Note ---- ``AML`` and ``IF_DATA`` sections are currently not processed. """ from os import unlink from pya2l.a2l_listener import ParserWrapper, A2LListener, cut_a2ml parser = ParserWrapper('a2l', 'a2lFile', A2LListener, debug=debug) self._set_path_components(file_name) try: unlink(self._dbfn) except Exception: pass data = open(self._a2lfn).read() data, a2ml = cut_a2ml(data) self.session = parser.parseFromString(data, dbname=self._dbfn) return self.session
def import_a2l(self, file_name, debug=False, in_memory=False, remove_existing=False): """Import `.a2l` file to `.a2ldb` database. Parameters ---------- file_name: str Name of the A2L to be imported. If you don't specify an extension ``.a2l`` is added. debug: bool Additional debugging output. in_memory: bool Create non-persistent in-memory database. remove_existing: bool ** DANGER ZONE **: Remove existing database. Returns ------- SQLAlchemy session object. Raises ------ OSError If database already exists. Note ---- ``AML`` and ``IF_DATA`` sections are currently not processed. """ from os import unlink from pya2l.a2l_listener import ParserWrapper, A2LListener, cut_a2ml self.in_memory = in_memory parser = ParserWrapper('a2l', 'a2lFile', A2LListener, debug=debug) self._set_path_components(file_name) if not in_memory: if remove_existing: try: unlink(self._dbfn) except Exception: pass elif path.exists(self._dbfn): raise OSError("file '{}' already exists.".format(self._dbfn)) data = open(self._a2lfn).read() data, a2ml = cut_a2ml(data) self.session = parser.parseFromString(data, dbname=self._dbfn) return self.session
def test_compu_method_invalid(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.TAB_VERB.DEFAULT_VALUE "Verbal conversion with default value" FOO_BAR "%12.0" "" /end COMPU_METHOD /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() compu = functions.CompuMethod(session, module.compu_method[0])
def test_compu_method_linear_no_coeffs(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.LINEAR.MUL_2 "Linear function with parameter set for phys = f(int) = 2*int + 0" LINEAR "%3.1" "m/s" /end COMPU_METHOD /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() with pytest.raises(exceptions.StructuralError): compu = functions.CompuMethod(session, module.compu_method[0])
def test_compu_method_rat_func_no_coeffs(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.RAT_FUNC.DIV_81_9175 "rational function with parameter set for impl = f(phys) = phys * 81.9175" RAT_FUNC "%8.4" "grad C" /end COMPU_METHOD /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() with pytest.raises(exceptions.StructuralError): compu = functions.CompuMethod(session, module.compu_method[0])
def test_compu_method_tab_verb_no_vtab(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.TAB_VERB.DEFAULT_VALUE "Verbal conversion with default value" TAB_VERB "%12.0" "" COMPU_TAB_REF CM.TAB_VERB.DEFAULT_VALUE.REF /end COMPU_METHOD /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() with pytest.raises(exceptions.StructuralError): compu = functions.CompuMethod(session, module.compu_method[0])
def test_compu_method_identical(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.IDENTICAL "conversion that delivers always phys = int" IDENTICAL "%3.0" "hours" /end COMPU_METHOD /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() compu = functions.CompuMethod(session, module.compu_method[0]) xs = np.arange(-10, 11) assert np.array_equal(compu(xs), xs) assert np.array_equal(compu.inv(xs), xs)
def test_compu_method_linear(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.LINEAR.MUL_2 "Linear function with parameter set for phys = f(int) = 2*int + 0" LINEAR "%3.1" "m/s" COEFFS_LINEAR 2 0 /end COMPU_METHOD /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() compu = functions.CompuMethod(session, module.compu_method[0]) xs = np.arange(-10, 11) assert np.array_equal(compu(xs), xs * 2.0) assert np.array_equal(compu.inv(xs * 2.0), xs)
def test_compu_method_rat_func_identical(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.RAT_FUNC.IDENT "rational function with parameter set for int = f(phys) = phys" RAT_FUNC "%3.1" "m/s" COEFFS 0 1 0 0 0 1 /end COMPU_METHOD /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() compu = functions.CompuMethod(session, module.compu_method[0]) xs = np.arange(-10, 11) assert np.array_equal(compu(xs), xs) assert np.array_equal(compu.inv(xs), xs)
def test_compu_method_formula_without_inv(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.FORM.X_PLUS_4 "" FORM "%6.1" "rpm" /begin FORMULA "X1+4" /end FORMULA /end COMPU_METHOD /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() compu = functions.CompuMethod(session, module.compu_method[0]) assert compu(6) == 10
def test_compu_method_tab_interp_default(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.TAB_INTP.DEFAULT_VALUE "" TAB_INTP "%8.4" "U/ min " COMPU_TAB_REF CM.TAB_INTP.DEFAULT_VALUE.REF /end COMPU_METHOD /begin COMPU_TAB CM.TAB_INTP.DEFAULT_VALUE.REF "" TAB_INTP 12 -3 98 -1 99 0 100 2 102 4 104 5 105 6 106 7 107 8 108 9 109 10 110 13 111 DEFAULT_VALUE_NUMERIC 300.56 /* DEFAULT_VALUE_NUME RIC should be used here as the normal output is numeric */ /end COMPU_TAB /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() compu = functions.CompuMethod(session, module.compu_method[0]) xs = np.arange(-3, 14) ys = np.array( [98., 98.5, 99., 100., 101., 102., 103., 104., 105., 106., 107., 108., 109., 110., 110.33333333333333, 110.66666666666667, 111.] ) assert np.array_equal(compu(xs), ys) assert compu(-3) == 98 assert compu(8) == 108 assert compu(14) == 300.56 assert compu(-4) == 300.56
def test_compu_method_rat_func_linear(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.RAT_FUNC.DIV_81_9175 "rational function with parameter set for impl = f(phys) = phys * 81.9175" RAT_FUNC "%8.4" "grad C" COEFFS 0 81.9175 0 0 0 1 /end COMPU_METHOD /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() compu = functions.CompuMethod(session, module.compu_method[0]) xs = np.arange(-10, 11) ys = np.array([-819.1750000000001, -737.2575, -655.34, -573.4225, -491.505, -409.58750000000003, -327.67, -245.7525, -163.835, -81.9175, 0., 81.9175, 163.835, 245.7525, 327.67, 409.58750000000003, 491.505, 573.4225, 655.34, 737.2575, 819.1750000000001 ]) assert np.array_equal(compu(xs), ys) assert np.array_equal(compu.inv(ys), xs)
def test_compu_method_tab_verb_ranges(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin COMPU_METHOD CM.VTAB_RANGE.DEFAULT_VALUE "verbal range with default value" TAB_VERB "%4.2" "" COMPU_TAB_REF CM.VTAB_RANGE.DEFAULT_VALUE.REF /end COMPU_METHOD /begin COMPU_VTAB_RANGE CM.VTAB_RANGE.DEFAULT_VALUE.REF "" 11 0 1 "Zero_to_one" 2 3 "two_to_three" 4 7 "four_to_seven" 14 17 "fourteen_to_seventeen" 18 99 "eigteen_to_ninetynine" 100 100 "hundred" 101 101 "hundredone" 102 102 "hundredtwo" 103 103 "hundredthree" 104 104 "hundredfour" 105 105 "hundredfive" DEFAULT_VALUE "out of range value" /end COMPU_VTAB_RANGE /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() compu = functions.CompuMethod(session, module.compu_method[0]) assert compu(0) == "Zero_to_one" assert compu(6) == "four_to_seven" assert compu(45) == "eigteen_to_ninetynine" assert compu(100) == "hundred" assert compu(105) == "hundredfive" assert compu(-1) == "out of range value" assert compu(106) == "out of range value" assert compu(10) == "out of range value"
def test_compu_method_formula_with_sysc(): parser = ParserWrapper('a2l', 'module', A2LListener) DATA = """ /begin MODULE testModule "" /begin MOD_PAR "" SYSTEM_CONSTANT "System_Constant_1" "42" SYSTEM_CONSTANT "System_Constant_2" "Textual constant" /end MOD_PAR /begin COMPU_METHOD CM.FORM.X_PLUS_SYSC "" FORM "%6.1" "rpm" /begin FORMULA "X1 + sysc(System_Constant_1)" /end FORMULA /end COMPU_METHOD /end MODULE """ session = parser.parseFromString(DATA) module = session.query(model.Module).first() compu = functions.CompuMethod(session, module.compu_method[0]) assert compu(23) == 65