code: def test_doubly_nested_field_works(self):
      field1 = ProductionRuleField("S -> [NP, VP]", is_global_rule=True)
      field2 = ProductionRuleField("NP -> test", is_global_rule=True)
      field3 = ProductionRuleField("VP -> eat", is_global_rule=False)
      list_field = ListField(
          [
              ListField([field1, field2, field3]),
              ListField([field1, field2]),
          ]
      )
      list_field.index(self.vocab)
      padding_lengths = list_field.get_padding_lengths()
      tensors = list_field.as_tensor(padding_lengths)
      assert isinstance(tensors, list)
      assert len(tensors) == 2
      assert isinstance(tensors[0], list)
      assert len(tensors[0]) == 3
      assert isinstance(tensors[1], list)
      assert len(tensors[1]) == 3
  
      tensor_tuple = tensors[0][0]
      assert tensor_tuple[0] == "S -> [NP, VP]"
      assert tensor_tuple[1] is True
      assert_almost_equal(
          tensor_tuple[2].detach().cpu().numpy(), [self.s_rule_index]
      )
  
      tensor_tuple = tensors[0][1]
      assert tensor_tuple[0] == "NP -> test"
      assert tensor_tuple[1] is True
      assert_almost_equal(
          tensor_tuple[2].detach().cpu().numpy(), [self.np_index]
      )
  
      tensor_tuple = tensors[0][2]
      assert tensor_tuple[0] == "VP -> eat"
      assert tensor_tuple[1] is False
      assert tensor_tuple[2] is None
  
      tensor_tuple = tensors[1][0]
      assert tensor_tuple[0] == "S -> [NP, VP]"
      assert tensor_tuple[1] is True
      assert_almost_equal(
          tensor_tuple[2].detach().cpu().numpy(), [self.s_rule_index]
      )
  
      tensor_tuple = tensors[1][1]
      assert tensor_tuple[0] == "NP -> test"
      assert tensor_tuple[1] is True
      assert_almost_equal(
          tensor_tuple[2].detach().cpu().numpy(), [self.np_index]
      )
  
      # This item was just padding.
      tensor_tuple = tensors[1][2]
      assert tensor_tuple[0] == ""
      assert tensor_tuple[1] is False
      assert tensor_tuple[2] is None
  
file_path: magnitude/pymagnitude/third_party/allennlp/tests/data/fields/production_rule_field_test.py
imports: from __future__ import absolute_import
  from collections import defaultdict
  from numpy.testing import assert_almost_equal
  from allennlp.common.testing import AllenNlpTestCase
  from allennlp.data import Vocabulary
  from allennlp.data.fields import ListField, ProductionRuleField
label_desc: Use image node_id 7 for calling the TestProductionRuleField obj's underlying member method code with example usage: obj.test_doubly_nested_field_works() without return types