Commit c31ed46b authored by Greenwood, Scott's avatar Greenwood, Scott
Browse files

update to switch to dictionary for variable extraction. updated creation of...

update to switch to dictionary for variable extraction. updated creation of model to handle new format and allow for definition of arrays for sources
parent 1dbb5bdd
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -84,15 +84,17 @@ def sources_line(data, project_path, base_path, model_class_path, key_path, unif
            temp_names = nested_structure_utils.generate_key_strings(temp_struct, key_path, f'sources_{var["name"]}')
            temp_struct_filled = nested_structure_utils.replace_struct_values(temp_struct, temp_names)
            
            array_modifier = '' if var['dimensions'] is None else f'[{var["dimensions"]}]'
            if uniform:
                temp_name = nested_structure_utils.get_first_value(temp_struct_filled)
                temp_s.append(f'each {var["name"]}={temp_name}'.replace("'",""))
                global_variables_interface.append('Modelica.Blocks.Interfaces.RealInput ' + temp_name + f'(start={var["value"]});')
                
                global_variables_interface.append('Modelica.Blocks.Interfaces.RealInput ' + temp_name + array_modifier + f'(start={var["value"]});')
            else:
                temp_s.append(f'{var["name"]}={temp_struct_filled}'.replace('[', '{').replace(']', '}').replace("'",""))

                for temp_name in temp_names:
                    global_variables_interface.append('Modelica.Blocks.Interfaces.RealInput ' + temp_name + f'(start={var["value"]});')
                    global_variables_interface.append('Modelica.Blocks.Interfaces.RealInput ' + temp_name + array_modifier + f'(start={var["value"]});')
                    
            temp = ','.join(temp_s)
    else:
+24 −48
Original line number Diff line number Diff line
@@ -10,54 +10,25 @@ Users may choose either license, at their discretion.
import re
import os

# def get_file_lines(file_path):
#     """
#     Reads all lines from a file and returns them as a list.

#     Parameters:
#     file_path (str): Path to the file to read.

#     Returns:
#     list: List of lines from the file.
#     """
#     with open(file_path, 'r') as struct:
#         lines = struct.readlines()  
#     return lines

# def convert_lines_to_string(lines):
#     """
#     Converts a list of lines into a single string, joining them with spaces.

#     Parameters:
#     lines (list): List of lines (strings).

#     Returns:
#     str: A single string with all lines joined by spaces.
#     """
#     output = ' '.join(lines)
#     # output = output.replace('\n',' ')
#     return output

# def extract_variable(input_string, dtype=None):
#     """
#     Extracts variable definitions from a given input string based on modelica type.

#     Parameters:
#     input_string (str): The input string to search for variables.
#     dtype (str or list): Type of variable to extract (e.g., 'parameter', 'input'). 
#                          If None, defaults to 'parameter|input|output'.

#     Returns:
#     list: A list of tuples containing variable details (dtype, variable name, etc.).
#     """
#     if dtype is None:
#         dtype = 'parameter|input|output'
#     elif type(dtype) == list:
#         dtype = '|'.join(dtype)
#     pattern = r'(?<!final\s)\b({})\s*([\w\.]+)\s*(\w+)\s*=\s*([\w\.]+)\s*(?:"([^"]*)")?'.format(dtype)
#     variables = re.findall(pattern, input_string)
    
#     return variables
def _remove_whitespace_between_strings(text, str1, str2):
    """
    Removes whitespace between two specified strings in a given text.

    Args:
        text: The input string.
        str1: The first string.
        str2: The second string.

    Returns:
        The modified string with whitespace removed between str1 and str2.
    """

    pattern = re.compile(rf"({re.escape(str1)})\s+({re.escape(str2)})")
    
    def replace_match(match):
        return match.group(1) + match.group(2)

    return pattern.sub(replace_match, text)

def _remove_comments(content):
    """
@@ -147,10 +118,13 @@ def extract_variables(file_path, type_prefixes=None, types=None, exclude_prefixe
    Returns:
        list: List of dictionaries containing variable details.
    """

    # Read the file content
    with open(file_path, 'r') as file:
        content = file.read()

    content = content.replace('\n',' ')
    
    # Remove all comments from the content
    content = _remove_comments(content)

@@ -177,6 +151,7 @@ def extract_variables(file_path, type_prefixes=None, types=None, exclude_prefixe

        # Find then remove annotation if present
        annotation = None
        definition = _remove_whitespace_between_strings(definition, 'annotation', '(') # Correct for spaces
        annotation_index = definition.find("annotation(")
        if annotation_index != -1:
            annotation = definition[annotation_index:]
@@ -361,7 +336,8 @@ def search_log_file_reverse(file_path, search_text):
if __name__ == "__main__":
    
    file_path = '../../../methods/modelica/TemplatesCSM/Templates/Structure.mo'
    parameters = get_variable_by_type(file_path)
    file_path = r'E:\ExaDigiT\AutoCSM\examples\modelica\GenericDatacenter\Systems\Datacenter\Systems\CoolingBlock\Systems\Cabinet\Sources\v0.mo'
    parameters = get_variable_by_type(file_path, 'input')
    print(parameters)

    
 No newline at end of file