Skip to content

Commit

Permalink
First Pass
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-Morgan committed Dec 11, 2024
1 parent 118fa07 commit fb78e96
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/specklepy/objects/graph_traversal/default_traversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from specklepy.objects.base import Base
from specklepy.objects.graph_traversal.traversal import GraphTraversal, TraversalRule

DISPLAY_VALUE_PROPERTY_ALIASES = {"displayValue", "@displayValue"}
ELEMENTS_PROPERTY_ALIASES = {"elements", "@elements"}


def has_display_value(x: Base):
return any(hasattr(x, alias) for alias in DISPLAY_VALUE_PROPERTY_ALIASES)


def create_default_traversal_function() -> GraphTraversal:
"""
Traversal func for traversing the root object of a Speckle Model
"""

convertible_rule = TraversalRule(
[lambda b: b.speckle_type != "Base", has_display_value],
lambda _: ELEMENTS_PROPERTY_ALIASES,
)

default_rule = TraversalRule(
[lambda _: True],
lambda o: o.get_member_names(), # NOTE: Unlike the C# implementation, this does not ignore Obsolete members
False,
)

return GraphTraversal([convertible_rule, default_rule])
15 changes: 14 additions & 1 deletion src/specklepy/objects/graph_traversal/traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@


class ITraversalRule(Protocol):

@property
def should_return(self) -> bool:
pass

Check warning on line 13 in src/specklepy/objects/graph_traversal/traversal.py

View check run for this annotation

Codecov / codecov/patch

src/specklepy/objects/graph_traversal/traversal.py#L13

Added line #L13 was not covered by tests

def get_members_to_traverse(self, o: Base) -> Set[str]:
"""Get the members to traverse."""
pass
Expand Down Expand Up @@ -50,10 +55,13 @@ def traverse(self, root: Base) -> Iterator[TraversalContext]:

while len(stack) > 0:
head = stack.pop()
yield head

current = head.current
active_rule = self._get_active_rule_or_default_rule(current)

if active_rule.should_return:
yield head

members_to_traverse = active_rule.get_members_to_traverse(current)
for child_prop in members_to_traverse:
try:
Expand Down Expand Up @@ -114,6 +122,11 @@ def _get_active_rule(self, o: Base) -> Optional[ITraversalRule]:
class TraversalRule:
_conditions: Collection[Callable[[Base], bool]]
_members_to_traverse: Callable[[Base], Iterable[str]]
_should_return_to_output: bool = True

@property
def should_return(self) -> bool:
return self._should_return_to_output

def get_members_to_traverse(self, o: Base) -> Set[str]:
return set(self._members_to_traverse(o))
Expand Down

0 comments on commit fb78e96

Please sign in to comment.