Skip to content

Commit

Permalink
Merge pull request #2411 from lf-lang/bank-of-feds-py
Browse files Browse the repository at this point in the history
Fix bank_index Parameter Override
  • Loading branch information
lhstrh authored Sep 27, 2024
2 parents 3d8cfd6 + 0911b10 commit 7141aca
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,22 @@ private static String generatePythonClassInstantiation(
CodeBuilder code = new CodeBuilder();
code.pr(PyUtil.reactorRef(instance) + " = _" + className + "(");
code.indent();
// Always add the bank_index
code.pr("_bank_index = " + PyUtil.bankIndex(instance) + ",");
boolean hasBankIndexParameter = false;
for (ParameterInstance param : instance.parameters) {
if (!param.getName().equals("bank_index")) {
code.pr(
"_"
+ param.getName()
+ "="
+ PythonParameterGenerator.generatePythonInitializer(param)
+ ",");
if (param.getName().equals("bank_index")) {
if (param.getOverride() != null) hasBankIndexParameter = true;
else continue; // Skip bank_index if it is not explicitly set
}
code.pr(
"_"
+ param.getName()
+ "="
+ PythonParameterGenerator.generatePythonInitializer(param)
+ ",");
}
if (!hasBankIndexParameter) {
// Only add bank_index if not explicitly set
code.pr("_bank_index = " + PyUtil.bankIndex(instance) + ",");
}
code.unindent();
code.pr(")");
Expand Down
28 changes: 28 additions & 0 deletions test/Python/src/federated/BankIndexFed.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
target Python

reactor FirstReactor(bank_index=0) {
output out

reaction(startup) -> out {=
print("bank_index: {:d}".format(self.bank_index))
out.set(self.bank_index)
=}
}

reactor SecondReactor {
input[4] inp

reaction(inp) {=
for i, port in enumerate(inp):
assert port.is_present
print("in[{:d}]: {:d}".format(i, port.value))
assert port.value == i
request_stop()
=}
}

federated reactor {
a = new[4] FirstReactor()
b = new SecondReactor()
a.out -> b.inp
}
12 changes: 12 additions & 0 deletions test/Python/src/federated/BankIndexOverride.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
target Python

reactor SingleReactor(bank_index=0) {
reaction(startup) {=
print("bank_index: {:d}".format(self.bank_index))
assert(self.bank_index == 2)
=}
}

main reactor {
a = new SingleReactor(bank_index=2)
}

0 comments on commit 7141aca

Please sign in to comment.