demand.mojo (2085B)
1 from std.collections import List, Optional 2 3 from hyf_core.domain.product import ProductRef 4 5 6 @fieldwise_init 7 struct Condition(Copyable, Movable): 8 var kind: String 9 var strength: String 10 var value: Optional[String] 11 12 13 @fieldwise_init 14 struct DemandLine(Copyable, Movable): 15 var line_id: String 16 var product: ProductRef 17 var quantity_state: String 18 var quantity_value: Int 19 var quantity_scale: Int 20 var unit: String 21 var conditions: List[Condition] 22 23 24 def condition( 25 kind: String, strength: String, value: Optional[String] 26 ) raises -> Condition: 27 if kind.strip() == "": 28 raise Error("condition kind must not be empty") 29 var strengths = ["mandatory", "preferred", "excluded", "permitted"] 30 var known = False 31 for candidate in strengths: 32 if candidate == strength: 33 known = True 34 if not known: 35 raise Error("unknown condition strength: " + strength) 36 if strength != "excluded" and not value: 37 raise Error("non-excluded condition requires a value") 38 return Condition( 39 kind=String(kind), strength=String(strength), value=value.copy() 40 ) 41 42 43 def demand_line( 44 line_id: String, 45 product: ProductRef, 46 quantity_state: String, 47 quantity_value: Int, 48 quantity_scale: Int, 49 conditions: List[Condition], 50 ) raises -> DemandLine: 51 if line_id.strip() == "": 52 raise Error("demand line requires an id") 53 if quantity_state != "known" and quantity_state != "unknown": 54 raise Error("demand quantity state must be 'known' or 'unknown'") 55 var copied = List[Condition]() 56 for entry in conditions: 57 copied.append(entry.copy()) 58 return DemandLine( 59 line_id=String(line_id), 60 product=product.copy(), 61 quantity_state=String(quantity_state), 62 quantity_value=quantity_value, 63 quantity_scale=quantity_scale, 64 unit="kg", 65 conditions=copied^, 66 ) 67 68 69 def condition_is_exclusion(value: Condition) -> Bool: 70 return value.strength == "excluded" 71 72 73 def missing_information_is_prohibition() -> Bool: 74 return False