Hi all,
I have implemented some code to run an external process. I let you know just in case you have idea to improve or if you're facing the same issue.
It's a process which need the data produced by liam (simulation as argument) but can't be written directly as common liam processes (because of complexity and because already done in an other way).
Adding it as an other process enable to benefit from the Liam frame.
In the process file I added:
class ExtProcess(Process):
'''these processes are not real Liam2 processes
The file containing the function should be in the path and
the function itself must be named "main".
'''
def __init__(self, name, arg):
super(ExtProcess, self).__init__()
self.name = name
self.args = arg
def run_guarded(self, simulation, const_dict):
context = EntityContext(self.entity, const_dict.copy())
self.run(simulation, context['period'])
def run(self, simulation, period):
module = importlib.import_module(
self.name)
if self.args is not None:
arg_bis = list(self.args)
for index, arg in enumerate(self.args):
if arg == 'period':
arg_bis[index] = period
elif arg == 'simulation':
arg_bis[index] = simulation
else:
arg_bis[index]= arg
arg_bis = tuple(arg_bis)
module.main(*arg_bis)
else:
module.main()
def expressions(self):
if isinstance(self.expr, Expr):
yield self.expr
And in simulation:
if ent_name == 'legislation':
proc2 = ExtProcess('of',['simulation',2009,'period'])
processes.append((proc2, 1))