Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# ~100 ms startup overhead for cached models by importing only when
# compiling.
from pymoca import parser
# Load folders
tree = None
for folder in [model_folder] + compiler_options['library_folders']:
for root, dir, files in os.walk(folder, followlinks=True):
for item in fnmatch.filter(files, "*.mo"):
logger.info("Parsing {}".format(item))
with open(os.path.join(root, item), 'r') as f:
if tree is None:
tree = parser.parse(f.read())
else:
tree.extend(parser.parse(f.read()))
# Compile
logger.info("Generating CasADi model")
model = generator.generate(tree, model_name, compiler_options)
if compiler_options['check_balanced']:
model.check_balanced()
model.simplify(compiler_options)
if compiler_options['verbose']:
model.check_balanced()
model._post_checks()
return model
def _compile_model(model_folder: str, model_name: str, compiler_options: Dict[str, str]):
# Importing the antlr4 (generated modules) is rather slow. Avoid for this
# ~100 ms startup overhead for cached models by importing only when
# compiling.
from pymoca import parser
# Load folders
tree = None
for folder in [model_folder] + compiler_options['library_folders']:
for root, dir, files in os.walk(folder, followlinks=True):
for item in fnmatch.filter(files, "*.mo"):
logger.info("Parsing {}".format(item))
with open(os.path.join(root, item), 'r') as f:
if tree is None:
tree = parser.parse(f.read())
else:
tree.extend(parser.parse(f.read()))
# Compile
logger.info("Generating CasADi model")
model = generator.generate(tree, model_name, compiler_options)
if compiler_options['check_balanced']:
model.check_balanced()
model.simplify(compiler_options)
if compiler_options['verbose']:
model.check_balanced()
model._post_checks()