remotivelabs.topology.namespaces.filters
Filters for RemotiveTopology.
This module re-exports all filters from remotivelabs.broker.filters for backwards compatibility,
and adds SOME/IP-specific filters (SomeIPEventFilter, SomeIPRequestFilter).
This module provides filters to select subsets of frames (remotivelabs.broker.FrameInfo) and signals (remotivelabs.broker.SignalInfo)
based on various criteria.
All filters have an exclude flag to indicate whether matches should be excluded (True) or
included (False, default). Filters are callable and can be passed to Python built-in functions that
expect a predicate, such as filter(), any(), or all(). The snippet below shows examples
using filter():
Filter Strategy:
The
filter_recursive()function applies frame and signal filters with context-dependent behavior. Frame filtering determines eligibility, then signal filtering applies differently based on frame inclusion: included frames start with all signals (exclude-only), excluded frames require explicit signal inclusion. Exclusion filters always take priority over inclusion filters.
Examples:
from remotivelabs.broker import FrameInfo, SignalInfo
from remotivelabs.broker.filters import (
AllFramesFilter,
FrameFilter,
ReceiverFilter,
SenderFilter,
SignalFilter,
filter_recursive,
is_frame_filter,
is_signal_filter,
)
from remotivelabs.topology.namespaces.filters import SomeIPEventFilter, SomeIPRequestFilter
# assume frames and signals are populated elsewhere
frames: list[FrameInfo] = []
signals: list[SignalInfo] = []
# Example 1: Include all frames
all_frames_filter = AllFramesFilter()
filtered_frames = list(filter(all_frames_filter, frames))
# Example 2: Include all frames but exclude a specific frame
frame_exclude_filter = FrameFilter(frame_name="Frame1", exclude=True)
filtered_frames = list(filter(frame_exclude_filter, frames))
# Example 3: Filter frames sent by a specific ECU
sender_filter = SenderFilter(ecu_name="ECU1")
filtered_frames = list(filter(sender_filter, frames))
# Example 4: Filter frames received by a specific ECU, excluding one signal
receiver_filter = ReceiverFilter(ecu_name="ECU2")
signal_exclude_filter = SignalFilter(signal_name="SignalA", exclude=True)
filtered_frames = list(filter(receiver_filter, frames))
filtered_signals = list(filter(signal_exclude_filter, signals))
# Example 5: SOME/IP request filter
someip_request_filter = SomeIPRequestFilter(service_instance_name="ServiceA", method_name="RequestX")
filtered_frames = list(filter(someip_request_filter, frames))
filtered_signals = list(filter(someip_request_filter, signals))
# Example 6: SOME/IP event filter
someip_event_filter = SomeIPEventFilter(service_instance_name="ServiceB", event_name="EventY")
filtered_frames = list(filter(someip_event_filter, frames))
filtered_signals = list(filter(someip_event_filter, signals))
# Example 7: Chaining filters
frame_include = FrameFilter(frame_name="FrameA")
frame_exclude = FrameFilter(frame_name="FrameB", exclude=True)
filtered_frames = list(filter(frame_exclude, filter(frame_include, frames)))
# Example 8: Combining inclusion and exclusion with AllFramesFilter and FrameFilter
all_frames_filter = AllFramesFilter()
frame_exclude_filter = FrameFilter(frame_name="FrameC", exclude=True)
filtered_frames = list(filter(frame_exclude_filter, filter(all_frames_filter, frames)))
# Example 9: Recursive filtering of signals (SignalInfos) in frames (FrameInfos)
filtered_frames = [
filtered_frame
for frame in frames
if (filtered_frame := filter_recursive(frame, filters=[all_frames_filter, signal_exclude_filter])) is not None
]
# Example 10: Type checking filters
assert is_frame_filter(all_frames_filter) # works with frames
assert not is_signal_filter(all_frames_filter) # doesn't work with signals
assert not is_frame_filter(signal_exclude_filter) # doesn't work with frames
assert is_signal_filter(signal_exclude_filter) # works with signals
assert is_frame_filter(sender_filter) # works with frames
assert is_signal_filter(sender_filter) # works with signals
1""" 2# Filters for RemotiveTopology. 3 4<!-- start-include --> 5This module re-exports all filters from `remotivelabs.broker.filters` for backwards compatibility, 6and adds SOME/IP-specific filters (`SomeIPEventFilter`, `SomeIPRequestFilter`). 7 8This module provides filters to select subsets of frames (`remotivelabs.broker.FrameInfo`) and signals (`remotivelabs.broker.SignalInfo`) 9based on various criteria. 10 11All filters have an `exclude` flag to indicate whether matches should be excluded (`True`) or 12included (`False`, default). Filters are callable and can be passed to Python built-in functions that 13expect a predicate, such as `filter()`, `any()`, or `all()`. The snippet below shows examples 14using `filter()`: 15 16Filter Strategy: 17 18 The `filter_recursive()` function applies frame and signal filters with context-dependent behavior. 19 Frame filtering determines eligibility, then signal filtering applies differently based on frame 20 inclusion: included frames start with all signals (exclude-only), excluded frames require explicit 21 signal inclusion. Exclusion filters always take priority over inclusion filters. 22 23Examples: 24 25```python 26.. include:: ../_docs/snippets/filters.py 27``` 28<!-- end-include --> 29""" 30 31from __future__ import annotations 32 33from dataclasses import dataclass 34from typing import Literal, overload 35 36from remotivelabs.broker import FrameInfo, SignalInfo 37from remotivelabs.broker.filters import AllFramesFilter as AllFramesFilter # noqa: PLC0414 38from remotivelabs.broker.filters import E2eSignalsFilter as E2eSignalsFilter # noqa: PLC0414 39from remotivelabs.broker.filters import FilterLike as FilterLike # noqa: PLC0414 40from remotivelabs.broker.filters import FrameFilter as FrameFilter # noqa: PLC0414 41from remotivelabs.broker.filters import FrameFilterPredicate as FrameFilterPredicate # noqa: PLC0414 42from remotivelabs.broker.filters import FrameSignalFilter as FrameSignalFilter # noqa: PLC0414 43from remotivelabs.broker.filters import ReceiverFilter as ReceiverFilter # noqa: PLC0414 44from remotivelabs.broker.filters import SenderFilter as SenderFilter # noqa: PLC0414 45from remotivelabs.broker.filters import SignalFilter as SignalFilter # noqa: PLC0414 46from remotivelabs.broker.filters import SignalFilterPredicate as SignalFilterPredicate # noqa: PLC0414 47from remotivelabs.broker.filters import filter_recursive as filter_recursive # noqa: PLC0414 48from remotivelabs.broker.filters import is_filter as is_filter # noqa: PLC0414 49from remotivelabs.broker.filters import is_frame_filter as is_frame_filter # noqa: PLC0414 50from remotivelabs.broker.filters import is_signal_filter as is_signal_filter # noqa: PLC0414 51 52 53def _someip_match(sep: str, service: str | None, name: str | None, is_signal: bool, full_name: str) -> bool: 54 if sep not in full_name: 55 return False 56 service_part, rest = full_name.split(sep, 1) 57 if not rest or (service and service_part != service): 58 return False 59 if is_signal and name: 60 return rest.startswith(f"{name}.") 61 return name == rest if name else True 62 63 64@dataclass(frozen=True) 65class SomeIPRequestFilter: 66 """Filter that matches SOME/IP request frames and signals by service instance and method name. 67 68 Attributes: 69 service_instance_name: If set, only match frames belonging to this service instance. 70 method_name: If set, only match frames for this method. 71 exclude: If `True`, matching items are excluded instead of included. 72 """ 73 74 service_instance_name: str | None = None 75 method_name: str | None = None 76 exclude: bool = False 77 type: Literal["any"] = "any" 78 79 @overload 80 def __call__(self, info: FrameInfo) -> bool: ... 81 82 @overload 83 def __call__(self, info: SignalInfo) -> bool: ... 84 85 def __call__(self, info: FrameInfo | SignalInfo) -> bool: 86 name = getattr(info, "name", None) 87 if not isinstance(name, str): 88 return False 89 90 matches = _someip_match( 91 ".Request.", 92 self.service_instance_name, 93 self.method_name, 94 isinstance(info, SignalInfo), 95 name, 96 ) 97 98 return not matches if self.exclude else matches 99 100 101@dataclass(frozen=True) 102class SomeIPEventFilter: 103 """Filter that matches SOME/IP event frames and signals by service instance and event name. 104 105 Attributes: 106 service_instance_name: If set, only match frames belonging to this service instance. 107 event_name: If set, only match frames for this event. 108 exclude: If `True`, matching items are excluded instead of included. 109 """ 110 111 service_instance_name: str | None = None 112 event_name: str | None = None 113 exclude: bool = False 114 type: Literal["any"] = "any" 115 116 @overload 117 def __call__(self, info: FrameInfo) -> bool: ... 118 119 @overload 120 def __call__(self, info: SignalInfo) -> bool: ... 121 122 def __call__(self, info: FrameInfo | SignalInfo) -> bool: 123 name = getattr(info, "name", None) 124 if not isinstance(name, str): 125 return False 126 127 matches = _someip_match( 128 ".Event.", 129 self.service_instance_name, 130 self.event_name, 131 isinstance(info, SignalInfo), 132 name, 133 ) 134 135 return not matches if self.exclude else matches
65@dataclass(frozen=True) 66class SomeIPRequestFilter: 67 """Filter that matches SOME/IP request frames and signals by service instance and method name. 68 69 Attributes: 70 service_instance_name: If set, only match frames belonging to this service instance. 71 method_name: If set, only match frames for this method. 72 exclude: If `True`, matching items are excluded instead of included. 73 """ 74 75 service_instance_name: str | None = None 76 method_name: str | None = None 77 exclude: bool = False 78 type: Literal["any"] = "any" 79 80 @overload 81 def __call__(self, info: FrameInfo) -> bool: ... 82 83 @overload 84 def __call__(self, info: SignalInfo) -> bool: ... 85 86 def __call__(self, info: FrameInfo | SignalInfo) -> bool: 87 name = getattr(info, "name", None) 88 if not isinstance(name, str): 89 return False 90 91 matches = _someip_match( 92 ".Request.", 93 self.service_instance_name, 94 self.method_name, 95 isinstance(info, SignalInfo), 96 name, 97 ) 98 99 return not matches if self.exclude else matches
Filter that matches SOME/IP request frames and signals by service instance and method name.
Attributes:
- service_instance_name: If set, only match frames belonging to this service instance.
- method_name: If set, only match frames for this method.
- exclude: If
True, matching items are excluded instead of included.
102@dataclass(frozen=True) 103class SomeIPEventFilter: 104 """Filter that matches SOME/IP event frames and signals by service instance and event name. 105 106 Attributes: 107 service_instance_name: If set, only match frames belonging to this service instance. 108 event_name: If set, only match frames for this event. 109 exclude: If `True`, matching items are excluded instead of included. 110 """ 111 112 service_instance_name: str | None = None 113 event_name: str | None = None 114 exclude: bool = False 115 type: Literal["any"] = "any" 116 117 @overload 118 def __call__(self, info: FrameInfo) -> bool: ... 119 120 @overload 121 def __call__(self, info: SignalInfo) -> bool: ... 122 123 def __call__(self, info: FrameInfo | SignalInfo) -> bool: 124 name = getattr(info, "name", None) 125 if not isinstance(name, str): 126 return False 127 128 matches = _someip_match( 129 ".Event.", 130 self.service_instance_name, 131 self.event_name, 132 isinstance(info, SignalInfo), 133 name, 134 ) 135 136 return not matches if self.exclude else matches
Filter that matches SOME/IP event frames and signals by service instance and event name.
Attributes:
- service_instance_name: If set, only match frames belonging to this service instance.
- event_name: If set, only match frames for this event.
- exclude: If
True, matching items are excluded instead of included.