Fix IDS to not allow subclasses and allow restrictions in the class name

This commit is contained in:
Dion Moult
2022-05-10 21:40:35 +10:00
parent 08fc62d4d9
commit 4326a74c64
2 changed files with 24 additions and 5 deletions
+8 -5
View File
@@ -498,9 +498,8 @@ class entity(facet):
def __call__(self, inst, logger=None): def __call__(self, inst, logger=None):
"""Validate an entity. """Validate an entity.
When a simple value is provided for the name, subclasses are also Subclasses are not considered to pass the requirements. PredefinedType
treated as valid. PredefinedType checks support userdefined types for checks support userdefined types for both element and type elements.
both element and type elements.
:param inst: IFC entity element :param inst: IFC entity element
:type inst: IFC entity :type inst: IFC entity
@@ -509,16 +508,20 @@ class entity(facet):
:return: result of the validation as bool and message :return: result of the validation as bool and message
:rtype: facet_evaluation(bool, str) :rtype: facet_evaluation(bool, str)
""" """
if isinstance(self.name, str):
is_class = inst.is_a().lower() == self.name.lower()
else:
is_class = inst.is_a() == self.name
if self.predefinedType: if self.predefinedType:
predefined_type = ifcopenshell.util.element.get_predefined_type(inst) predefined_type = ifcopenshell.util.element.get_predefined_type(inst)
self.message = "an entity name '%(name)s' of predefined type '%(predefinedType)s'" self.message = "an entity name '%(name)s' of predefined type '%(predefinedType)s'"
return facet_evaluation( return facet_evaluation(
inst.is_a(self.name) and predefined_type == self.predefinedType, is_class and predefined_type == self.predefinedType,
self.message % {"name": inst.is_a(), "predefinedType": predefined_type}, self.message % {"name": inst.is_a(), "predefinedType": predefined_type},
) )
else: else:
self.message = "an entity name '%(name)s'" self.message = "an entity name '%(name)s'"
return facet_evaluation(inst.is_a(self.name), self.message % {"name": inst.is_a()}) return facet_evaluation(is_class, self.message % {"name": inst.is_a()})
class attribute(facet): class attribute(facet):
+16
View File
@@ -280,6 +280,11 @@ class TestIdsAuthoring(unittest.TestCase):
assert bool(facet(ifc.createIfcWall(PredefinedType="SOLIDWALL"))) is True assert bool(facet(ifc.createIfcWall(PredefinedType="SOLIDWALL"))) is True
assert bool(facet(ifc.createIfcSlab())) is False assert bool(facet(ifc.createIfcSlab())) is False
facet = ids.entity.create(name="IFCWALL")
assert bool(facet(ifc.createIfcWall())) is True
assert bool(facet(ifc.createIfcWall(PredefinedType="SOLIDWALL"))) is True
assert bool(facet(ifc.createIfcSlab())) is False
facet = ids.entity.create(name="IfcWall", predefinedType="SOLIDWALL") facet = ids.entity.create(name="IfcWall", predefinedType="SOLIDWALL")
assert bool(facet(ifc.createIfcWall())) is False assert bool(facet(ifc.createIfcWall())) is False
assert bool(facet(ifc.createIfcWall(PredefinedType="SOLIDWALL"))) is True assert bool(facet(ifc.createIfcWall(PredefinedType="SOLIDWALL"))) is True
@@ -291,6 +296,17 @@ class TestIdsAuthoring(unittest.TestCase):
facet = ids.entity.create(name="IfcWallType", predefinedType="WALDO") facet = ids.entity.create(name="IfcWallType", predefinedType="WALDO")
assert bool(facet(ifc.createIfcWallType(PredefinedType="USERDEFINED", ElementType="WALDO"))) is True assert bool(facet(ifc.createIfcWallType(PredefinedType="USERDEFINED", ElementType="WALDO"))) is True
restriction = ids.restriction.create(options=["IfcWall", "IfcSlab"], type="enumeration", base="string")
facet = ids.entity.create(name=restriction)
assert bool(facet(ifc.createIfcWall())) is True
assert bool(facet(ifc.createIfcSlab())) is True
assert bool(facet(ifc.createIfcBeam())) is False
restriction = ids.restriction.create(options="Ifc.*Type", type="pattern", base="string")
facet = ids.entity.create(name=restriction)
assert bool(facet(ifc.createIfcWall())) is False
assert bool(facet(ifc.createIfcWallType())) is True
def test_attribute_create(self): def test_attribute_create(self):
attribute = ids.attribute.create(name="Name", value="Value") attribute = ids.attribute.create(name="Name", value="Value")
assert attribute.name == "Name" assert attribute.name == "Name"