def test_deprecated_leaf_system_port_declarations(self): """Checks that the bindings without a name= argument still work.""" dut = LeafSystem() # Input port. with catch_drake_warnings(expected_count=1): input_port = dut.DeclareInputPort(type=PortDataType.kVectorValued, size=1) # Vector output port. def _vector_calc(context, output): output.get_mutable_value()[:] = context.get_time() with catch_drake_warnings(expected_count=1): vector_output_port = dut.DeclareVectorOutputPort( BasicVector(1), _vector_calc) # Abstract output port. def _tuple_calc(context, output): output.set_value(("time", context.get_time())) with catch_drake_warnings(expected_count=1): abstract_output_port = dut.DeclareAbstractOutputPort( lambda: AbstractValue.Make(("string", 0.0)), _tuple_calc) # Check that the return values were sane. context = dut.CreateDefaultContext() input_port.get_index() vector_output_port.Eval(context) abstract_output_port.Eval(context)
def test_cache_entry(self): """Checks the existence of CacheEntry-related bindings.""" # Cover DeclareCacheEntry. dummy = LeafSystem() allocate_abstract_int = AbstractValue.Make(0).Clone cache_entry = dummy.DeclareCacheEntry( description="scratch", value_producer=ValueProducer(allocate=allocate_abstract_int, calc=ValueProducer.NoopCalc), prerequisites_of_calc={dummy.nothing_ticket()}) self.assertIsInstance(cache_entry, CacheEntry) # Cover CacheEntry and get_cache_entry. self.assertIsInstance(cache_entry.prerequisites(), set) cache_index = cache_entry.cache_index() self.assertIsInstance(cache_index, CacheIndex) self.assertIsInstance(cache_entry.ticket(), DependencyTicket) self.assertIs(dummy.get_cache_entry(cache_index), cache_entry) # Cover CacheEntryValue. context = dummy.CreateDefaultContext() cache_entry_value = cache_entry.get_mutable_cache_entry_value(context) self.assertIsInstance(cache_entry_value, CacheEntryValue) data = cache_entry_value.GetMutableValueOrThrow() self.assertIsInstance(data, int)
def test_state_output_port_declarations(self): """Checks that DeclareStateOutputPort is bound.""" dut = LeafSystem() xc_index = dut.DeclareContinuousState(2) xc_port = dut.DeclareStateOutputPort(name="xc", state_index=xc_index) self.assertEqual(xc_port.size(), 2) xd_index = dut.DeclareDiscreteState(3) xd_port = dut.DeclareStateOutputPort(name="xd", state_index=xd_index) self.assertEqual(xd_port.size(), 3) xa_index = dut.DeclareAbstractState(AbstractValue.Make(1)) xa_port = dut.DeclareStateOutputPort(name="xa", state_index=xa_index) self.assertEqual(xa_port.get_name(), "xa")
def test_leaf_system_per_item_tickets(self): dut = LeafSystem() dut.DeclareAbstractParameter(AbstractValue.Make(1)) dut.DeclareAbstractState(AbstractValue.Make(1)) dut.DeclareDiscreteState(1) dut.DeclareVectorInputPort("u0", BasicVector(1)) dut.DeclareNumericParameter(BasicVector(1)) for func, arg in [ (dut.abstract_parameter_ticket, AbstractParameterIndex(0)), (dut.abstract_state_ticket, AbstractStateIndex(0)), (dut.cache_entry_ticket, CacheIndex(0)), (dut.discrete_state_ticket, DiscreteStateIndex(0)), (dut.input_port_ticket, InputPortIndex(0)), (dut.numeric_parameter_ticket, NumericParameterIndex(0)), ]: self.assertIsInstance(func(arg), DependencyTicket, func)