Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow standalone system simulation component #155

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/configs/standalone-system-sim-counter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- tickit.core.components.system_simulation.SystemSimulation:
name: internal_tickit
inputs: {}
components:
- examples.devices.counter.Counter:
name: counter
inputs: {}
- tickit.devices.sink.Sink:
name: counter_sink
inputs:
input: counter:value
Comment on lines +1 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- tickit.core.components.system_simulation.SystemSimulation:
name: internal_tickit
inputs: {}
components:
- examples.devices.counter.Counter:
name: counter
inputs: {}
- tickit.devices.sink.Sink:
name: counter_sink
inputs:
input: counter:value
- type: tickit.core.components.system_simulation.SystemSimulation
name: internal_tickit
inputs: {}
components:
- type: examples.devices.counter.Counter
name: counter
inputs: {}
- type: tickit.devices.sink.Sink
name: counter_sink
inputs:
input:
component: counter
port: value

12 changes: 12 additions & 0 deletions examples/configs/standalone-system-sim.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- tickit.core.components.system_simulation.SystemSimulation:
name: internal_tickit
inputs: {}
components:
- tickit.devices.source.Source:
name: internal_source
inputs: {}
value: 42
- tickit.devices.sink.Sink:
name: internal_sink
inputs:
input: internal_source:value
Comment on lines +1 to +12
Copy link
Contributor

@DiamondJoseph DiamondJoseph Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- tickit.core.components.system_simulation.SystemSimulation:
name: internal_tickit
inputs: {}
components:
- tickit.devices.source.Source:
name: internal_source
inputs: {}
value: 42
- tickit.devices.sink.Sink:
name: internal_sink
inputs:
input: internal_source:value
- type: tickit.core.components.system_simulation.SystemSimulation
name: internal_tickit
inputs: {}
components:
- type: tickit.devices.source.Source
name: internal_source
inputs: {}
value: 42
- type: tickit.devices.sink.Sink
name: internal_sink
inputs:
input:
component: internal_source
port: value

8 changes: 8 additions & 0 deletions examples/configs/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- tickit.devices.source.Source:
name: internal_source
inputs: {}
value: 42
- tickit.devices.sink.Sink:
name: internal_sink
inputs:
input: internal_source:value
Comment on lines +1 to +8
Copy link
Contributor

@DiamondJoseph DiamondJoseph Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- tickit.devices.source.Source:
name: internal_source
inputs: {}
value: 42
- tickit.devices.sink.Sink:
name: internal_sink
inputs:
input: internal_source:value
- type: tickit.devices.source.Source
name: internal_source
inputs: {}
value: 42
- type: tickit.devices.sink.Sink
name: internal_sink
inputs:
input:
component: internal_source
port: value

4 changes: 1 addition & 3 deletions src/tickit/core/components/system_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ async def stop_component(self) -> None:
class SystemSimulation(ComponentConfig):
"""Simulation of a nested set of components."""

name: ComponentID
inputs: Dict[PortID, ComponentPort]
components: List[ComponentConfig]
expose: Dict[PortID, ComponentPort]
expose: Dict[PortID, ComponentPort] = field(default_factory=dict)

def __call__(self) -> Component: # noqa: D102
return SystemSimulationComponent(
Expand Down
31 changes: 26 additions & 5 deletions src/tickit/core/management/schedulers/slave.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import logging
from typing import Awaitable, Callable, Dict, Optional, Set, Tuple, Type, Union

Expand Down Expand Up @@ -48,10 +49,22 @@ def __init__(
wiring = self.add_exposing_wiring(wiring, expose)
super().__init__(wiring, state_consumer, state_producer)

reads_from_external: bool = False
for component in self._wiring:
input_components = [
p.component for p in self._wiring[component].values() # type: ignore
]
if "external" in input_components:
reads_from_external = True
break

self.reads_from_external = reads_from_external
self.raise_interrupt = raise_interrupt
self.interrupts: Set[ComponentID] = set()
self.component_error: ComponentException

self.first_tick: asyncio.Event = asyncio.Event()

@staticmethod
def add_exposing_wiring(
wiring: Union[Wiring, InverseWiring],
Expand Down Expand Up @@ -127,17 +140,20 @@ async def on_tick(
wakeup_components = {
component for component, when in self.wakeups.items() if when <= time
}
root_components: Set[ComponentID] = {
*self.interrupts,
*wakeup_components,
ComponentID("external"),
}

root_components: Set[ComponentID] = {*self.interrupts, *wakeup_components}

if self.reads_from_external:
root_components.update([ComponentID("external")])

for component in wakeup_components:
del self.wakeups[component]
self.interrupts.clear()

self.input_changes = changes
self.output_changes = Changes(Map())

await asyncio.wait_for(self.first_tick.wait(), timeout=30)
await self.ticker(time, root_components)

_, call_at = self.get_first_wakeups()
Expand All @@ -146,6 +162,11 @@ async def on_tick(
async def run_forever(self) -> None:
"""Delegates to setup which instantiates the ticker and state interfaces."""
await self.setup()
await self.ticker(
SimTime(0),
self.ticker.components,
)
self.first_tick.set()

async def schedule_interrupt(self, source: ComponentID) -> None:
"""Schedules the interrupt of a component immediately.
Expand Down
3 changes: 3 additions & 0 deletions tests/core/management/schedulers/test_slave_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ async def test_slave_scheduler_run_forever_method(slave_scheduler: SlaveSchedule
async def test_slave_scheduler_on_tick_method(
slave_scheduler: SlaveScheduler, mock_ticker: Mock
):
await slave_scheduler.run_forever()

changes = Changes(Map({PortID("67"): 67}))
slave_scheduler.ticker = mock_ticker
output_changes, call_at = await slave_scheduler.on_tick(SimTime(8), changes)
Expand All @@ -190,6 +192,7 @@ async def test_slave_scheduler_on_tick_method(
async def test_slave_scheduler_on_tick_method_with_wakeups(
slave_scheduler: SlaveScheduler, mock_ticker: Mock
):
await slave_scheduler.run_forever()
changes = Changes(Map({PortID("67"): 67}))
slave_scheduler.ticker = mock_ticker
slave_scheduler.wakeups = {
Expand Down