|
2 | 2 | # SPDX-License-Identifier: MIT |
3 | 3 |
|
4 | 4 | import importlib.metadata |
| 5 | +import logging |
5 | 6 | import typing |
6 | 7 | from typing import Annotated |
7 | 8 |
|
|
18 | 19 | MeasuredPropertyTypeEnum, |
19 | 20 | Property, |
20 | 21 | ) |
21 | | -from orchestrator.schema.property_value import ConstitutivePropertyValue, PropertyValue |
| 22 | +from orchestrator.schema.property_value import ( |
| 23 | + ConstitutivePropertyValue, |
| 24 | + PropertyValue, |
| 25 | + validate_point_against_properties, |
| 26 | +) |
22 | 27 | from orchestrator.schema.reference import ( |
23 | 28 | ExperimentReference, |
24 | 29 | check_parameterization_validity, |
@@ -615,6 +620,74 @@ def propertyValuesFromEntity(self, entity: "Entity", target=False) -> dict: |
615 | 620 |
|
616 | 621 | return identifierValueMap |
617 | 622 |
|
| 623 | + def validate_entity(self, entity: "Entity", strict_optional=False) -> bool: |
| 624 | + """Returns True if Experiment can be applied to entity, false otherwise |
| 625 | +
|
| 626 | + This method only checks constitutive properties. |
| 627 | + - All properties of the Entity that match the experiments required or optional properties must have |
| 628 | + values in the domain of that property |
| 629 | + - All required properties of the experiment must have a matching constitutive property |
| 630 | + - If strict_optional is True all properties of the Entity that are not required properties of the Experiment |
| 631 | + must match optional properties of the experiment. |
| 632 | + """ |
| 633 | + |
| 634 | + point = { |
| 635 | + v.property.identifier: v.value for v in entity.constitutive_property_values |
| 636 | + } |
| 637 | + if validate_point_against_properties( |
| 638 | + point, |
| 639 | + constitutive_properties=self.requiredConstitutiveProperties, |
| 640 | + ): |
| 641 | + return True |
| 642 | + |
| 643 | + # It's not an exact match - check if partial match |
| 644 | + if not validate_point_against_properties( |
| 645 | + point, |
| 646 | + constitutive_properties=self.requiredConstitutiveProperties, |
| 647 | + allow_partial_matches=True, |
| 648 | + ): |
| 649 | + # no partial match - missing required properties or has incorrect values for them |
| 650 | + logging.getLogger("experiment").warning( |
| 651 | + f"The entity is missing or has invalid values for required properties of " |
| 652 | + f" {self.identifier}" |
| 653 | + ) |
| 654 | + return False |
| 655 | + |
| 656 | + # It has the required properties with valid values but there are additional properties |
| 657 | + # See if these properties are optional propertiesof the experiment |
| 658 | + potential_optional_properties: set[str] = point.keys() - { |
| 659 | + cp.identifier for cp in self.requiredProperties |
| 660 | + } |
| 661 | + optional_properties = potential_optional_properties & { |
| 662 | + cp.identifier for cp in self.optionalProperties |
| 663 | + } |
| 664 | + # If strict_optional is on all the additional properties must be optional properties |
| 665 | + if ( |
| 666 | + len(optional_properties) != len(potential_optional_properties) |
| 667 | + and strict_optional |
| 668 | + ): |
| 669 | + logging.getLogger("experiment").warning( |
| 670 | + f"Strict property checking is on and the following entity " |
| 671 | + f"properties are not required or optional properties of {self.identifier}:" |
| 672 | + f"{potential_optional_properties-optional_properties} " |
| 673 | + ) |
| 674 | + return False |
| 675 | + |
| 676 | + is_valid = validate_point_against_properties( |
| 677 | + point={key: point[key] for key in optional_properties}, |
| 678 | + constitutive_properties=list(self.optionalProperties), |
| 679 | + allow_partial_matches=True, |
| 680 | + ) |
| 681 | + if not is_valid: |
| 682 | + logging.getLogger("experiment").warning( |
| 683 | + f"The entity has properties that match optional properties" |
| 684 | + f"of {self.identifier} - " |
| 685 | + f"{potential_optional_properties - optional_properties} - " |
| 686 | + f"but its values for those properties are not in the domain of the optional properties" |
| 687 | + ) |
| 688 | + |
| 689 | + return is_valid |
| 690 | + |
618 | 691 |
|
619 | 692 | class ParameterizedExperiment(Experiment): |
620 | 693 | """Represents an Experiment where default optional parameters have been overridden |
|
0 commit comments