+1 vote
285 views
in Test Automation by Radovan Živković (18 points)
edited by Radovan Živković
Zdravo, kako ste? :)

I have been trying to parametrize some of the variables defined in the model's initialization script for the purpose of testing.

I have found set_model_init_code exists, while getter method is missing in the Typhoon HIL Control Center 2020.3 API.

How should I then go about executing the following operation sequence?
- Get current value of the variable in the model initialization script
- Update the value
- Set variable value
- Compile the model and execute tests

Best,
Radovan

2 Answers

+2 votes
by Dusan Kostic (258 points)
selected by Radovan Živković
 
Best answer

Zdravo Radovane,

Hvala na pitanju, dobro je.

U pravu si, izgleda da ne postoji funkcija koja bi vratila init code. Da bi ga isparsirali iz .tse fajla, možemo upotrebiti ovako neki pajton kod:

import re
# Read the file
file_path = "YOUR MODEL.tse"
with open(file_path, "r") as file:
    content = file.read()
# Use regex to find code between CODE model_init and ENDCODE
match = re.search(r"CODE model_init(.*?)ENDCODE", content, re.DOTALL)
if match:
    extracted_code = match.group(1).strip()  # Extract and remove extra spaces    print("Extracted Code:\n", extracted_code)
else:
    print("Code block not found")

Međutim, s obzirom da je string koji ovaj kod vraća praktično pajton kod, nisam siguran koliko je uputno programski ga menjati.

Ono što bih ti preporučio je da baciš pogled na ovaj FAQ. U njemu su navedeni primeri kako možemo koristiti eksterne fajlove za inicijalizaciju varijabli. Nadam se da će ti biti od pomoći.

Pozdrav

+3 votes
by Ivan Morar (109 points)

Hi Radovan,

There is a way to do this by using mdl.set_ns_var() and mdl.get_ns_var() functions.

Let say your variable is named temp_var. To read it you can call:

With mdl.get_ns_var("temp_var") you will get value of that variable.

Problem occurs when you want to change it. If you just call mdl.set_ns_var("temp_var", 10) your variable will still be set on the previous value after compile. Reason for this is that during compilation model init is called and it will overwrite changed variable.

What you could do is to leave model init empty and than you will not face this problem, however if you try to compile it through schematic, you will get an error because there is no variable defined.
Better approach is to check first if variable already exists, in model init of course. Something like this:
if "temp_var" not in mdl.get_ns_vars():
    
temp_var = 1
In this case you will be always able to load compile model through schematic, or through API. If you dont set ns varable, temp_var will be set to 1, but if you set with set_ns_var to, lets say 10, it will be 10.
Drawback of this is that you will not be able to read initial value, at least without compiling the model.
by Radovan Živković (18 points)
+1
Hi Ivan,

Your suggestion is correct, and it will work.

I have already developed file editing method, like suggested in the first method, and will past here the base values acquisition method in entirety for any future users:

https://gist.github.com/dvozicnivod/cf4b850ed1943e781b5c7416b2fd5c17

Best,
Radovan
...