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)
def test_diagram_api(self): def make_diagram(): builder = DiagramBuilder() adder1 = builder.AddNamedSystem("adder1", Adder(2, 2)) adder2 = builder.AddNamedSystem("adder2", Adder(1, 2)) builder.Connect(adder1.get_output_port(), adder2.get_input_port()) builder.ExportInput(adder1.get_input_port(0), "in0") builder.ExportInput(adder1.get_input_port(1), "in1") builder.ExportOutput(adder2.get_output_port(), "out") self.assertEqual(len(builder.connection_map()), 1) diagram = builder.Build() return adder1, adder2, diagram adder1, adder2, diagram = make_diagram() connections = diagram.connection_map() self.assertIn((adder2, InputPortIndex(0)), connections) self.assertEqual(connections[(adder2, InputPortIndex(0))], (adder1, OutputPortIndex(0))) del adder1, adder2, diagram # To test keep-alive logic gc.collect() self.assertEqual(list(connections.keys())[0][0].get_name(), "adder2") adder1, adder2, diagram = make_diagram() in0_locators = diagram.GetInputPortLocators( port_index=InputPortIndex(0)) in1_locators = diagram.GetInputPortLocators( port_index=InputPortIndex(1)) self.assertEqual(in0_locators, [(adder1, InputPortIndex(0))]) self.assertEqual(in1_locators, [(adder1, InputPortIndex(1))]) del adder1, adder2, diagram # To test keep-alive logic gc.collect() self.assertEqual(in0_locators[0][0].get_name(), "adder1") adder1, adder2, diagram = make_diagram() out_locators = diagram.get_output_port_locator( port_index=OutputPortIndex(0)) self.assertEqual(out_locators, (adder2, OutputPortIndex(0))) del adder1, adder2, diagram # To test keep-alive logic gc.collect() self.assertEqual(out_locators[0].get_name(), "adder2")