Skip to content

Commit

Permalink
Merge pull request #440 from SynBioDex/437-visitor-return-values
Browse files Browse the repository at this point in the history
Fix #437: visitor return values
  • Loading branch information
tcmitchell authored Oct 16, 2023
2 parents 1632e86 + c2a85e6 commit b19e64e
Show file tree
Hide file tree
Showing 25 changed files with 62 additions and 36 deletions.
2 changes: 1 addition & 1 deletion sbol3/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_attachment(self)
return visitor.visit_attachment(self)


def build_attachment(identity: str,
Expand Down
4 changes: 2 additions & 2 deletions sbol3/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_collection(self)
return visitor.visit_collection(self)


class Experiment(Collection):
Expand Down Expand Up @@ -86,7 +86,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_experiment(self)
return visitor.visit_experiment(self)


Document.register_builder(SBOL_COLLECTION, Collection)
Expand Down
2 changes: 1 addition & 1 deletion sbol3/combderiv.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_combinatorial_derivation(self)
return visitor.visit_combinatorial_derivation(self)


def build_combinatorial_derivation(identity: str,
Expand Down
2 changes: 1 addition & 1 deletion sbol3/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_component(self)
return visitor.visit_component(self)


def build_component(identity: str, *, type_uri: str = SBOL_COMPONENT) -> SBOLObject:
Expand Down
2 changes: 1 addition & 1 deletion sbol3/compref.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_component_reference(self)
return visitor.visit_component_reference(self)


def build_component_reference(identity: str, *,
Expand Down
2 changes: 1 addition & 1 deletion sbol3/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_constraint(self)
return visitor.visit_constraint(self)


def build_constraint(identity: str, type_uri: str = SBOL_CONSTRAINT) -> SBOLObject:
Expand Down
2 changes: 1 addition & 1 deletion sbol3/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_document(self)
return visitor.visit_document(self)

def traverse(self, func: Callable[[Identified], None]):
"""Enable a traversal of the entire object hierarchy contained
Expand Down
2 changes: 1 addition & 1 deletion sbol3/expdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_experimental_data(self)
return visitor.visit_experimental_data(self)


Document.register_builder(SBOL_EXPERIMENTAL_DATA, ExperimentalData)
2 changes: 1 addition & 1 deletion sbol3/extdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_externally_defined(self)
return visitor.visit_externally_defined(self)


def build_externally_defined(identity: str,
Expand Down
2 changes: 1 addition & 1 deletion sbol3/implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_implementation(self)
return visitor.visit_implementation(self)


Document.register_builder(SBOL_IMPLEMENTATION, Implementation)
2 changes: 1 addition & 1 deletion sbol3/interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_interaction(self)
return visitor.visit_interaction(self)


def build_interaction(identity: str, *, type_uri: str = SBOL_INTERACTION) -> SBOLObject:
Expand Down
2 changes: 1 addition & 1 deletion sbol3/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_interface(self)
return visitor.visit_interface(self)


def build_interface(identity: str, *, type_uri: str = SBOL_INTERFACE) -> SBOLObject:
Expand Down
2 changes: 1 addition & 1 deletion sbol3/localsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_local_sub_component(self)
return visitor.visit_local_sub_component(self)


def build_local_subcomponent(identity: str,
Expand Down
6 changes: 3 additions & 3 deletions sbol3/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_range(self)
return visitor.visit_range(self)


def build_range(identity: str, type_uri: str = SBOL_RANGE):
Expand Down Expand Up @@ -138,7 +138,7 @@ def accept(self, visitor: Any) -> Any:
:return: Whatever `visitor.visit_cut` returns
:rtype: Any
"""
visitor.visit_cut(self)
return visitor.visit_cut(self)


def build_cut(identity: str, type_uri: str = SBOL_CUT):
Expand Down Expand Up @@ -184,7 +184,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_entire_sequence(self)
return visitor.visit_entire_sequence(self)


def build_entire_sequence(identity: str, type_uri: str = SBOL_ENTIRE_SEQUENCE):
Expand Down
2 changes: 1 addition & 1 deletion sbol3/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_model(self)
return visitor.visit_model(self)


def build_model(identity: str, type_uri: str = SBOL_MODEL):
Expand Down
6 changes: 3 additions & 3 deletions sbol3/om_compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_unit_multiplication(self)
return visitor.visit_unit_multiplication(self)


def build_unit_multiplication(identity: str,
Expand Down Expand Up @@ -145,7 +145,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_unit_division(self)
return visitor.visit_unit_division(self)


def build_unit_division(identity: str,
Expand Down Expand Up @@ -209,7 +209,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_unit_exponentiation(self)
return visitor.visit_unit_exponentiation(self)


def build_unit_exponentiation(identity: str,
Expand Down
4 changes: 2 additions & 2 deletions sbol3/om_prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_si_prefix(self)
return visitor.visit_si_prefix(self)


def build_si_prefix(identity: str, *, type_uri: str = OM_SI_PREFIX) -> SBOLObject:
Expand Down Expand Up @@ -161,7 +161,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_binary_prefix(self)
return visitor.visit_binary_prefix(self)


def build_binary_prefix(identity: str,
Expand Down
6 changes: 3 additions & 3 deletions sbol3/om_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_measure(self)
return visitor.visit_measure(self)


def build_measure(identity: str, *, type_uri: str = OM_MEASURE) -> SBOLObject:
Expand Down Expand Up @@ -149,7 +149,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_singular_unit(self)
return visitor.visit_singular_unit(self)


def build_singular_unit(identity: str,
Expand Down Expand Up @@ -213,7 +213,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_prefixed_unit(self)
return visitor.visit_prefixed_unit(self)


def build_prefixed_unit(identity: str,
Expand Down
2 changes: 1 addition & 1 deletion sbol3/participation.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_participation(self)
return visitor.visit_participation(self)


def build_participation(identity: str,
Expand Down
10 changes: 5 additions & 5 deletions sbol3/provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_usage(self)
return visitor.visit_usage(self)


def build_usage(identity: str, *, type_uri: str = PROV_USAGE) -> SBOLObject:
Expand Down Expand Up @@ -93,7 +93,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_agent(self)
return visitor.visit_agent(self)


Document.register_builder(PROV_AGENT, Agent)
Expand Down Expand Up @@ -129,7 +129,7 @@ def accept(self, visitor: Any) -> Any:
:return: Whatever `visitor.visit_plan` returns
:rtype: Any
"""
visitor.visit_plan(self)
return visitor.visit_plan(self)


Document.register_builder(PROV_PLAN, Plan)
Expand Down Expand Up @@ -174,7 +174,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_association(self)
return visitor.visit_association(self)


def build_association(identity: str,
Expand Down Expand Up @@ -256,7 +256,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_activity(self)
return visitor.visit_activity(self)


Document.register_builder(PROV_ACTIVITY, Activity)
2 changes: 1 addition & 1 deletion sbol3/seqfeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_sequence_feature(self)
return visitor.visit_sequence_feature(self)


def build_sequence_feature(identity: str,
Expand Down
2 changes: 1 addition & 1 deletion sbol3/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_sequence(self)
return visitor.visit_sequence(self)


Document.register_builder(SBOL_SEQUENCE, Sequence)
2 changes: 1 addition & 1 deletion sbol3/subcomponent.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_sub_component(self)
return visitor.visit_sub_component(self)


def build_subcomponent(identity: str, type_uri: str = SBOL_SUBCOMPONENT) -> Identified:
Expand Down
2 changes: 1 addition & 1 deletion sbol3/varcomp.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def accept(self, visitor: Any) -> Any:
:rtype: Any
"""
visitor.visit_variable_feature(self)
return visitor.visit_variable_feature(self)


def build_variable_feature(identity: str, type_uri: str = SBOL_VARIABLE_FEATURE):
Expand Down
26 changes: 26 additions & 0 deletions test/test_visitors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest
from pathlib import Path

import sbol3


class TestVisitors(unittest.TestCase):

def test_visitor_pattern(self):
"""Test that the visitor pattern is implemented and can use return values from visit functions"""
class SumVisitor:
def visit_document(self, doc: sbol3.Document):
return sum(v.accept(self) for v in doc.objects)

@staticmethod
def visit_component(_: sbol3.Component):
return 1

doc = sbol3.Document()
doc.read(Path(__file__).parent / 'resources' / 'circuit.nt')
# File containing five Components and no other TopLevel objects
self.assertEqual(doc.accept(SumVisitor()), 5)


if __name__ == '__main__':
unittest.main()

0 comments on commit b19e64e

Please sign in to comment.