|
| 1 | +"""Definitions for GemdQuery objects, and their sub-objects.""" |
| 2 | +from typing import List, Type |
| 3 | + |
| 4 | +from gemd.enumeration.base_enumeration import BaseEnumeration |
| 5 | + |
| 6 | +from citrine._serialization.serializable import Serializable |
| 7 | +from citrine._serialization.polymorphic_serializable import PolymorphicSerializable |
| 8 | +from citrine._serialization import properties |
| 9 | +from citrine.gemd_queries.filter import PropertyFilterType |
| 10 | + |
| 11 | +__all__ = ['MaterialClassification', 'TextSearchType', |
| 12 | + 'AndOperator', 'OrOperator', |
| 13 | + 'PropertiesCriteria', 'NameCriteria', |
| 14 | + 'MaterialRunClassificationCriteria', 'MaterialTemplatesCriteria' |
| 15 | + ] |
| 16 | + |
| 17 | + |
| 18 | +class MaterialClassification(BaseEnumeration): |
| 19 | + """A classification based on where in a Material History you find a Material.""" |
| 20 | + |
| 21 | + ATOMIC_INGREDIENT = "atomic_ingredient" |
| 22 | + INTERMEDIATE_INGREDIENT = "intermediate_ingredient" |
| 23 | + TERMINAL_MATERIAL = "terminal_material" |
| 24 | + |
| 25 | + |
| 26 | +class TextSearchType(BaseEnumeration): |
| 27 | + """The style of text search to run.""" |
| 28 | + |
| 29 | + EXACT = "exact" |
| 30 | + PREFIX = "prefix" |
| 31 | + SUFFIX = "suffix" |
| 32 | + SUBSTRING = "substring" |
| 33 | + |
| 34 | + |
| 35 | +class Criteria(PolymorphicSerializable): |
| 36 | + """Abstract concept of a criteria to apply when searching for materials.""" |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def get_type(cls, data) -> Type[Serializable]: |
| 40 | + """Return the subtype.""" |
| 41 | + classes: List[Type[Criteria]] = [ |
| 42 | + AndOperator, OrOperator, |
| 43 | + PropertiesCriteria, NameCriteria, MaterialRunClassificationCriteria, |
| 44 | + MaterialTemplatesCriteria |
| 45 | + ] |
| 46 | + return {klass.typ: klass for klass in classes}[data['type']] |
| 47 | + |
| 48 | + |
| 49 | +class AndOperator(Serializable['AndOperator'], Criteria): |
| 50 | + """ |
| 51 | + Combine multiple criteria, requiring EACH to be true for a match. |
| 52 | +
|
| 53 | + Parameters |
| 54 | + ---------- |
| 55 | + criteria: Criteria |
| 56 | + List of conditions all responses must satisfy (i.e., joined with an AND). |
| 57 | +
|
| 58 | + """ |
| 59 | + |
| 60 | + criteria = properties.List(properties.Object(Criteria), "criteria") |
| 61 | + typ = properties.String('type', default="and_operator", deserializable=False) |
| 62 | + |
| 63 | + |
| 64 | +class OrOperator(Serializable['OrOperator'], Criteria): |
| 65 | + """ |
| 66 | + Combine multiple criteria, requiring ANY to be true for a match. |
| 67 | +
|
| 68 | + Parameters |
| 69 | + ---------- |
| 70 | + criteria: Criteria |
| 71 | + List of conditions, at least one of which must match (i.e., joined with an OR). |
| 72 | +
|
| 73 | + """ |
| 74 | + |
| 75 | + criteria = properties.List(properties.Object(Criteria), "criteria") |
| 76 | + typ = properties.String('type', default="or_operator", deserializable=False) |
| 77 | + |
| 78 | + |
| 79 | +class PropertiesCriteria(Serializable['PropertiesCriteria'], Criteria): |
| 80 | + """ |
| 81 | + Look for materials with a particular Property and optionally Value types & ranges. |
| 82 | +
|
| 83 | + Parameters |
| 84 | + ---------- |
| 85 | + property_templates_filter: Set[UUID] |
| 86 | + The citrine IDs of the property templates matches must reference. |
| 87 | + value_type_filter: Optional[PropertyFilterType] |
| 88 | + The value range matches must conform to. |
| 89 | +
|
| 90 | + """ |
| 91 | + |
| 92 | + property_templates_filter = properties.Set(properties.UUID, "property_templates_filter") |
| 93 | + value_type_filter = properties.Optional( |
| 94 | + properties.Object(PropertyFilterType), "value_type_filter" |
| 95 | + ) |
| 96 | + typ = properties.String('type', default="properties_criteria", deserializable=False) |
| 97 | + |
| 98 | + |
| 99 | +class NameCriteria(Serializable['NameCriteria'], Criteria): |
| 100 | + """ |
| 101 | + Look for materials with particular names. |
| 102 | +
|
| 103 | + Parameters |
| 104 | + ---------- |
| 105 | + name: str |
| 106 | + The name the returned objects must have. |
| 107 | + search_type: TextSearchType |
| 108 | + What kind of string match to use (exact, substring, ...). |
| 109 | +
|
| 110 | + """ |
| 111 | + |
| 112 | + name = properties.String('name') |
| 113 | + search_type = properties.Enumeration(TextSearchType, 'search_type') |
| 114 | + typ = properties.String('type', default="name_criteria", deserializable=False) |
| 115 | + |
| 116 | + |
| 117 | +class MaterialRunClassificationCriteria( |
| 118 | + Serializable['MaterialRunClassificationCriteria'], |
| 119 | + Criteria |
| 120 | +): |
| 121 | + """ |
| 122 | + Look for materials with particular classification, defined by MaterialClassification. |
| 123 | +
|
| 124 | + Parameters |
| 125 | + ---------- |
| 126 | + classifications: Set[MaterialClassification] |
| 127 | + The classification, based on where in a material history an object appears. |
| 128 | +
|
| 129 | + """ |
| 130 | + |
| 131 | + classifications = properties.Set( |
| 132 | + properties.Enumeration(MaterialClassification), 'classifications' |
| 133 | + ) |
| 134 | + typ = properties.String( |
| 135 | + 'type', |
| 136 | + default="material_run_classification_criteria", |
| 137 | + deserializable=False |
| 138 | + ) |
| 139 | + |
| 140 | + |
| 141 | +class MaterialTemplatesCriteria(Serializable['MaterialTemplatesCriteria'], Criteria): |
| 142 | + """ |
| 143 | + Look for materials with particular Material Templates and tags. |
| 144 | +
|
| 145 | + This has a similar behavior to the old [[MaterialRunByTemplate]] Row definition |
| 146 | +
|
| 147 | + Parameters |
| 148 | + ---------- |
| 149 | + material_templates_identifiers: Set[UUID] |
| 150 | + Which material templates to filter by. |
| 151 | + tag_filters: Set[str] |
| 152 | + Which tags to filter by. |
| 153 | +
|
| 154 | + """ |
| 155 | + |
| 156 | + material_templates_identifiers = properties.Set( |
| 157 | + properties.UUID, |
| 158 | + "material_templates_identifiers" |
| 159 | + ) |
| 160 | + tag_filters = properties.Set(properties.String, 'tag_filters') |
| 161 | + typ = properties.String('type', default="material_template_criteria", deserializable=False) |
0 commit comments