Skip to content

Commit 3da01a2

Browse files
committed
bugfixes
1 parent c08d5dd commit 3da01a2

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

psymple/ported_objects.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ def compile(self, prefix_names=False):
627627
# We create a compiled output port
628628
assg = ParameterAssignment(destination.symbol, source.symbol)
629629
compiled.output_ports[destination.name] = CompiledOutputPort(
630-
source, assg
630+
destination, assg
631631
)
632632
else:
633633
raise WiringError(
@@ -703,8 +703,22 @@ def compile(self, prefix_names=False):
703703
compiled.internal_variable_assignments[name] = port.assignment
704704

705705
compiled.sub_symbol_identifications()
706+
707+
# Align the dictionary keys with the names of the symbols
708+
# whose assignments the dictionary is storing.
709+
# This has to happen after all the wiring compilation,
710+
# because the wires refer to the child + port name within the child,
711+
# so the child name cannot be part of the dictionary key while
712+
# the wiring is compiled.
713+
compiled.remap_dict_keys()
714+
706715
if prefix_names:
716+
# After this, all variables/parameters appearing everywhere
717+
# are prefixed by the name of the ported object.
718+
# This, however, does not apply to the dictionary keys,
719+
# see above for the reasoning
707720
compiled.sub_prefixed_symbols()
721+
708722
return compiled
709723

710724

@@ -756,6 +770,19 @@ def sub_prefixed_symbols(self):
756770
new_symbol = sym.Symbol(HIERARCHY_SEPARATOR.join([self.name, name]))
757771
self.sub_everywhere(old_symbol, new_symbol)
758772

773+
def remap_dict_keys(self):
774+
# Remap dictionary keys to add prefix
775+
for containers in [
776+
self.input_ports,
777+
self.output_ports,
778+
self.variable_ports,
779+
self.internal_variable_assignments,
780+
self.internal_parameter_assignments
781+
]:
782+
re_keyed = {content.name : content for name, content in containers.items()}
783+
containers.clear()
784+
containers.update(re_keyed)
785+
759786
def sub_everywhere(self, old_symbol, new_symbol):
760787
assert isinstance(old_symbol, sym.Symbol)
761788
assert isinstance(new_symbol, sym.Symbol)

tests/test_ported.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,24 @@ def test_output_forwarding(self):
383383
2 * sym.Symbol("rabbits"),
384384
)
385385

386+
def test_output_nesting(self):
387+
# TODO: Add checks
388+
A_func = FunctionalPortedObject("func")
389+
A_func.add_input_port(InputPort("X", default_value = 5))
390+
A_func.add_assignment(ParameterAssignment("Y", "X"))
391+
392+
A = CompositePortedObject("A")
393+
A.add_child(A_func)
394+
A.add_output_port(OutputPort("Y"))
395+
A.add_directed_wire("func.Y", "Y")
396+
397+
B = CompositePortedObject("B")
398+
B.add_output_port(OutputPort("BY"))
399+
B.add_child(A)
400+
B.add_directed_wire("A.Y", "BY")
401+
402+
compiled = B.compile()
403+
386404
def test_parameters(self):
387405
fpo = FunctionalPortedObject("double")
388406
fpo.add_input_port(InputPort("old"))
@@ -423,6 +441,30 @@ def test_parameters(self):
423441
sym.Symbol("double.new") * sym.Symbol("rabbits"),
424442
)
425443

444+
def test_key_remapping(self):
445+
# TODO: Add checks
446+
func_1 = FunctionalPortedObject("func_1")
447+
func_1.add_input_port(InputPort("X", default_value = 5))
448+
func_1.add_assignment(ParameterAssignment("Y", "X"))
449+
450+
A = CompositePortedObject("A")
451+
A.add_child(func_1)
452+
A.add_input_port(InputPort("X", default_value = 3))
453+
A.add_output_port(OutputPort("Y"))
454+
A.add_directed_wire("X", "func_1.X")
455+
A.add_directed_wire("func_1.Y", "Y")
456+
457+
B = CompositePortedObject("B")
458+
B.add_child(A)
459+
B.add_input_port(InputPort("X", default_value = 1))
460+
B.add_output_port(OutputPort("Y"))
461+
B.add_directed_wire("X", "A.X")
462+
B.add_directed_wire("A.Y", "Y")
463+
464+
C = CompositePortedObject("C")
465+
C.add_child(B)
466+
467+
compiled = C.compile()
426468

427469
class TestSimulation(unittest.TestCase):
428470
def test_no_params(self):

0 commit comments

Comments
 (0)