Example #1
0
 def setUp(self):
   super(EntityTypeFieldTest).setUp()
   self.test_entity_type_field = EntityTypeField(
       namespace_name='',
       standard_field_name='supply_air_flowrate_sensor',
       is_optional=False,
       increment='_1_12')
Example #2
0
class EntityTypeFieldTest(absltest.TestCase):
  # Testing EntityTypeField objects defined in the global namespace

  def setUp(self):
    super(EntityTypeFieldTest).setUp()
    self.test_entity_type_field = EntityTypeField(
        namespace_name='',
        standard_field_name='supply_air_flowrate_sensor',
        is_optional=False,
        increment='_1_12')

  def testGetIncrement(self):
    expected_output = '_1_12'
    function_output = self.test_entity_type_field.GetIncrement()

    self.assertEqual(function_output, expected_output)

  def testIsOptional(self):
    self.assertFalse(self.test_entity_type_field.IsOptional())

  def testStandardizeField(self):
    expected_output = StandardField(
        namespace_name='', standard_field_name='supply_air_flowrate_sensor')

    function_output = StandardizeField(self.test_entity_type_field)

    self.assertEqual(function_output, expected_output)
Example #3
0
    def testGetRequiredFieldsForTypeName(self):
        expected_output = [
            EntityTypeField('', 'exhaust_air_damper_command', False),
            EntityTypeField('', 'exhaust_air_damper_status', False)
        ]

        function_output = self.ontology.GetFieldsForTypeName(
            namespace='HVAC', entity_type_name='DMP_EDM', required_only=True)

        self.assertEqual(function_output, expected_output)
Example #4
0
  def testGetFieldList(self):
    expected_output = [
        EntityTypeField('', 'manufacturer_label', True),
        EntityTypeField('', 'model_label', True),
        EntityTypeField('', 'exhaust_air_damper_command', False),
        EntityTypeField('', 'exhaust_air_damper_status', False)
    ]

    function_output = self.test_match.GetFieldList()

    self.assertEqual(function_output, expected_output)
Example #5
0
    def testGetAllFieldsForTypeName(self):
        expected_output = [
            EntityTypeField('', 'exhaust_air_damper_command', False),
            EntityTypeField('', 'exhaust_air_damper_status', False),
            EntityTypeField('', 'manufacturer_label', True),
            EntityTypeField('', 'model_label', True)
        ]

        function_output = self.ontology.GetFieldsForTypeName('HVAC', 'DMP_EDM')

        self.assertEqual(function_output, expected_output)
Example #6
0
  def testEqualityWithEntityTypeField(self):
    test_entity_type_field = EntityTypeField(
        namespace_name='',
        standard_field_name='supply_air_flowrate_sensor',
        is_optional=False,
        increment='_1_12')

    self.assertEqual(test_entity_type_field, self.test_standard_field)
Example #7
0
    def testGetFieldList(self):
        expected_output = [
            EntityTypeField(namespace_name='',
                            standard_field_name='zone_use_label',
                            is_optional=True,
                            increment='')
        ]

        function_output = self.test_match.GetFieldList()

        self.assertEqual(function_output, expected_output)
  def GetFieldsForTypeName(
      self,
      namespace: str,
      entity_type_name: str,
      required_only: bool = False) -> List[EntityTypeField]:
    """Gets a list of fields for a given typename within a namespace.

    Args:
      namespace: the name of the namespace as a string.
      entity_type_name: the name of the entity type as a string.
      required_only: when true will return only required fields for a given
        type.

    Returns:
      result_fields: a list of EntityTypeField objects.

    Raises:
      Exception: when inherited fields are not expanded.
    """
    entity_type = self.universe.entity_type_universe.GetEntityType(
        namespace, entity_type_name)
    if entity_type is None:
      if not namespace:
        raise ValueError(
            f'\n{entity_type_name} is not defined in global namespace.')
      else:
        raise ValueError(
            f'\n{entity_type_name} is not defined in namespace: {namespace}.')
    if not entity_type.inherited_fields_expanded:
      raise Exception(
          'Inherited fields must be expanded to query fields.\n' +
          'Run NamespaceValidator on your ConfigUniverse to expand fields.')
    # Entity_type_lib.FieldParts NamedTuple to EntityTypeField object.
    entity_type_fields = []
    for qualified_field in entity_type.GetAllFields().values():
      new_entity_type_field = EntityTypeField(qualified_field.field.namespace,
                                              qualified_field.field.field,
                                              qualified_field.optional,
                                              qualified_field.field.increment)
      entity_type_fields.append(new_entity_type_field)

    if required_only:
      entity_type_fields = [
          field for field in entity_type_fields if not field.IsOptional()
      ]
    entity_type_fields_sorted = sorted(
        entity_type_fields,
        key=lambda x: x.GetStandardFieldName(),
        reverse=False)

    return entity_type_fields_sorted
Example #9
0
 def setUp(self):
   super().setUp()
   self.universe = create_simplified_universe()
   nv.NamespaceValidator(self.universe.GetEntityTypeNamespaces())
   test_entity_type = self.universe.entity_type_universe.GetEntityType(
       namespace_name='HVAC', typename='DMP_EDM')
   test_field_list = [
       EntityTypeField(optwrapper.field.namespace, optwrapper.field.field[0:],
                       optwrapper.optional, optwrapper.field.increment)
       for optwrapper in test_entity_type.GetAllFields().values()
   ]
   self.test_match = Match(
       field_list=test_field_list,
       entity_type=test_entity_type,
       match_score=1.0)
  def _PopulateMatrix(self, match: Match):
    """Creates a matrix defining field relationships between an entity and type.

    Args:
      match: A instance of Match class

    Returns:
      final_matrix: a matrix concrete fields matching to canonical fields
      all_fields: a list of fields for a concrete entity and canonical type
    """
    final_matrix = []
    concrete_field_set = set(match.GetFieldList())

    canonical_field_dict = {}
    for qualified_field in match.GetEntityType().GetAllFields().values():
      new_entity_type_field = EntityTypeField(
          qualified_field.field.namespace,
          qualified_field.field.field,
          qualified_field.optional,
          qualified_field.field.increment
      )
      new_standard_field = StandardizeField(new_entity_type_field)
      canonical_field_dict[new_standard_field] = new_entity_type_field

    standard_canonical_field_set = set(canonical_field_dict.keys())

    only_concrete = concrete_field_set.difference(standard_canonical_field_set)
    for field in only_concrete:
      if not self.IsFieldValid(field):
        final_matrix.append(
            [colored(str(field) + ' (undefined)', 'red'), '', ''])
      else:
        final_matrix.append([str(field), '', ''])

    intersection = standard_canonical_field_set.intersection(concrete_field_set)
    for field in intersection:
      final_matrix.append(
          [str(field), str(field), canonical_field_dict[field].IsOptional()])

    only_canonical = standard_canonical_field_set.difference(concrete_field_set)
    for field in only_canonical:
      final_matrix.append(
          ['', str(field), canonical_field_dict[field].IsOptional()])

    all_fields = list(intersection) + list(only_concrete) + list(only_canonical)

    return final_matrix, all_fields
Example #11
0
 def setUp(self):
   super(ModelTest).setUp()
   self.yaml = RecursiveDirWalk(test_constants.ONTOLOGY_ROOT)
   self.config = presubmit_validate_types_lib.SeparateConfigFiles(self.yaml)
   self.universe = presubmit_validate_types_lib.BuildUniverse(self.config)
   nv.NamespaceValidator(self.universe.GetEntityTypeNamespaces())
   test_entity_type = self.universe.entity_type_universe.GetEntityType(
       namespace_name='HVAC', typename='ZONE_HVAC')
   test_field_list = [
       EntityTypeField(optwrapper.field.namespace, optwrapper.field.field[0:],
                       optwrapper.optional, optwrapper.field.increment)
       for optwrapper in test_entity_type.GetAllFields().values()
   ]
   self.test_match = Match(
       field_list=test_field_list,
       entity_type=test_entity_type,
       match_type='EXACT')
  def _CreateMatch(self, field_list: List[StandardField],
                   entity_type: EntityType) -> Match:
    """Creates an instance of Match class.

    calls _CalculateMatchScore() on field_list and the set of fields belonging
    to entity_type. The scoring function outputs aan integer in [0, 100]
    signifying the closeness of the match, and an instance of the Match class is
    created with field_list, entity_type, and match_score as arguments.

    Args:
      field_list: A list of EntityTypeField objects for a concrete entity.
      entity_type: An EntityType object.

    Returns:
      An instance of Match class.
    """
    canonical_field_set = set()
    for qualified_field in entity_type.GetAllFields().values():
      new_entity_type_field = EntityTypeField(
          qualified_field.field.namespace,
          qualified_field.field.field,
          qualified_field.optional,
          qualified_field.field.increment
      )
      canonical_field_set.add(new_entity_type_field)

    match_score = self._CalculateMatchScore(
        concrete_fields=frozenset(field_list),
        canonical_fields=frozenset(canonical_field_set)
    )
    new_match = Match(
        field_list,
        entity_type,
        match_score
    )
    return new_match