Skip to content

Commit 29503d8

Browse files
Nikhil-Singhal-06fred-labs
authored andcommitted
feature/random-functions (IntelLabs#217)
* add random int and random list * format * add unittest * generic method * format
1 parent f83978c commit 29503d8

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

scenario_execution/scenario_execution/external_methods/random.py

+10
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,13 @@ def seed(seed_value: int = 0):
2222

2323
def get_float(min_val: dict, max_val: float):
2424
return rd.uniform(min_val, max_val) # nosec B311
25+
26+
27+
def get_int(min_val: int, max_val: int):
28+
return rd.randint(min_val, max_val) # nosec B311
29+
30+
31+
def get_random_list_element(elements_list: list):
32+
if not elements_list:
33+
return None # Return None if the list is empty
34+
return rd.choice(elements_list) # nosec B311

scenario_execution/scenario_execution/lib_osc/helpers.osc

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ action run_process:
3131
struct random:
3232
def seed(seed_value: int = 0) is external scenario_execution.external_methods.random.seed()
3333
def get_float(min_val: float, max_val: float) -> float is external scenario_execution.external_methods.random.get_float()
34-
35-
34+
def get_int(min_val: int, max_val: int) -> int is external scenario_execution.external_methods.random.get_int()
35+
def get_random_string(elements_list: list of string) -> string is external scenario_execution.external_methods.random.get_random_list_element()
3636

3737
#########################
3838
# Common modifiers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions
13+
# and limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
import unittest
18+
import py_trees
19+
from datetime import datetime
20+
from scenario_execution.scenario_execution_base import ScenarioExecution
21+
from scenario_execution.model.osc2_parser import OpenScenario2Parser
22+
from scenario_execution.model.model_to_py_tree import create_py_tree
23+
from .common import DebugLogger
24+
from antlr4.InputStream import InputStream
25+
26+
27+
class TestExternalMethodsRandom(unittest.TestCase):
28+
# pylint: disable=missing-function-docstring
29+
30+
def setUp(self) -> None:
31+
self.logger = DebugLogger("")
32+
self.parser = OpenScenario2Parser(self.logger)
33+
self.tree = py_trees.composites.Sequence(name="", memory=True)
34+
self.scenario_execution = ScenarioExecution(debug=False,
35+
log_model=False,
36+
live_tree=False,
37+
scenario_file='test',
38+
output_dir='', logger=self.logger)
39+
self.tree = py_trees.composites.Sequence(name="", memory=True)
40+
41+
def execute(self, scenario_content):
42+
parsed_tree = self.parser.parse_input_stream(InputStream(scenario_content))
43+
model = self.parser.create_internal_model(parsed_tree, self.tree, "test.osc", False)
44+
self.tree = create_py_tree(model, self.tree, self.parser.logger, False)
45+
self.scenario_execution.tree = self.tree
46+
self.scenario_execution.run()
47+
48+
def test_get_random_int(self):
49+
scenario_content = """
50+
import osc.helpers
51+
52+
scenario test_success:
53+
do serial:
54+
wait elapsed(random.get_int(0, 5))
55+
emit end
56+
"""
57+
parsed_tree = self.parser.parse_input_stream(InputStream(scenario_content))
58+
model = self.parser.create_internal_model(parsed_tree, self.tree, "test.osc", False)
59+
self.tree = create_py_tree(model, self.tree, self.parser.logger, False)
60+
self.scenario_execution.tree = self.tree
61+
62+
start_time = datetime.now()
63+
self.scenario_execution.run()
64+
end_time = datetime.now()
65+
self.assertTrue(self.scenario_execution.process_results())
66+
67+
delta = end_time - start_time
68+
self.assertLess(delta.total_seconds(), 5.)
69+
70+
def test_get_random_string(self):
71+
scenario_content = """
72+
import osc.helpers
73+
74+
scenario test_success:
75+
do serial:
76+
log(random.get_random_string(["test", "test-scenario", "scenario-test"]))
77+
emit end
78+
"""
79+
self.execute(scenario_content)
80+
self.assertTrue(self.scenario_execution.process_results())
81+
valid_strings = ["test", "test-scenario", "scenario-test"]
82+
log_messages = self.logger.logs_info
83+
for log_message in log_messages:
84+
if log_message in valid_strings:
85+
self.assertIn(log_message, valid_strings)

0 commit comments

Comments
 (0)