def builder(): Meta_meta = Clabject(name="MetaMeta", parents=[], init_props={}) prop_a = create_clabject_prop(n="a", t=1, f="*", i_f=False, c=[is_str_constraint]) prob_b = create_clabject_prop(n="b", t=1, f="*", i_f=False, i_assoc=True) Meta_meta.define_props([prop_a, prob_b]) return Meta_meta
def test_invalid_src_clabject_state(MetaSrcClab, MetaTgtClab, clab_state_constr): # The following state constraint should be evaluated only on instance level state_constr_at_least_4_arr_members_prop = create_clabject_prop( n=clab_state_constr.name, t=2, f='*', c=[], i_sc=True, v=clab_state_constr) MetaSrcClab.define_props([state_constr_at_least_4_arr_members_prop]) TgtClab = MetaTgtClab(name="TgtClab", parents=[], init_props={}) inst_Tgt_Clab = TgtClab(name="inst_Tgt_Clab") SrcClab = MetaSrcClab(name="SrcClab", parents=[], init_props={ "int_arr": [123], "tgt_arr": [inst_Tgt_Clab] }) assert len(SrcClab.check_state_constraints()) == 0 inst_Src_Clab = SrcClab(name="inst_src_clab", declare_as_instance=True) print(inst_Src_Clab.check_state_constraints()) assert "AtLeastThreeArrMembers" in inst_Src_Clab.check_state_constraints( ).keys()
def model_snippet(): from pathlib import Path from multilevel_py.constraints import is_int_constraint, is_str_constraint from multilevel_py.core import create_clabject_prop, Clabject Breed = Clabject(name="Breed") yearReg = create_clabject_prop(n="yearReg", t=1, f=0, i_f=True, c=[is_int_constraint]) age = create_clabject_prop(n="age", t=2, f=0, i_f=True, c=[is_int_constraint]) Breed.define_props([yearReg, age]) Collie = Breed(name="Collie", init_props={"yearReg": 1888}) lassie = Collie(name="Lassie", init_props={"age": 7}, declare_as_instance=True) hidden_root = False viz_name = str(Path(__file__).stem) return Breed, viz_name, hidden_root
def define_contraction_phase(clabject, prop_value): prop_contraction_phase = create_clabject_prop(n="contraction_phase", t=0, f='*', i_f=True, c=[is_str_constraint], v=prop_value) clabject.define_props([prop_contraction_phase])
def MetaSrcClab(MetaTgtClab): _MetaSrcClab = Clabject(name="MetaSrcClab", parents=[], init_props={}) snd_order_inst_of_meta_tgt = prop_constraint_is_th_order_instance_of_clabject_set_functional( {MetaTgtClab}, order=2) int_arr_prop = create_clabject_prop("int_arr", t=1, f="*", c=[], coll_desc=(2, 4, is_int_constraint)) tgt_cls_arr_prop = create_clabject_prop( "tgt_arr", t=1, f="*", c=[], coll_desc=(1, 2, snd_order_inst_of_meta_tgt)) _MetaSrcClab.define_props([int_arr_prop, tgt_cls_arr_prop]) return _MetaSrcClab
def model_snippet(): from pathlib import Path from multilevel_py.constraints import is_int_constraint, is_str_constraint, is_date_constraint from multilevel_py.core import create_clabject_prop, Clabject from datetime import date Breed = Clabject(name="Breed") year_reg = create_clabject_prop(n="year_reg", t=1, f=1, i_f=False, c=[is_int_constraint]) coat_colour = create_clabject_prop(n="coat_colour", t=2, f='*', i_f=True, c=[is_str_constraint]) year_of_birth = create_clabject_prop(n="year_of_birth", t=3, f='*', i_f=True, c=[is_date_constraint]) Breed.define_props([year_reg, coat_colour, year_of_birth]) Collie = Breed(name="Collie", init_props={"year_reg": 1888}) SableRoughCollie = Collie(name="SableRoughCollie", init_props={"coat_colour": "sable-white"}) SableRoughCollie.year_reg = 1902 TriColourRoughCollie = Collie( name="TriColourRoughCollie", init_props={"coat_colour": "black-edged-in-tan"}) lassie = SableRoughCollie( name="Lassie", init_props={"year_of_birth": date(year=2002, month=2, day=20)}, declare_as_instance=True) hidden_root = False viz_name = str(Path(__file__).stem) return Breed, viz_name, hidden_root
def model_snippet(): from multilevel_py.constraints import prop_constraint_ml_instance_of_th_order_functional from multilevel_py.constraints import prop_constraint_optional_value_functional as optional from multilevel_py.constraints import is_str_constraint, is_date_constraint, \ EmptyValue, ClabjectStateConstraint from multilevel_py.core import create_clabject_prop, Clabject from datetime import date Breed = Clabject(name="Breed") coat_colour = create_clabject_prop(n="coat_colour", t=2, f=1, i_f=True, c=[is_str_constraint]) father = create_clabject_prop(n="father", t=3, f='*', i_f=True, i_assoc=True, c=[]) make_noise = create_clabject_prop(n="make_noise", t=1, f='*', i_f=False, i_m=True, c=[]) def eval_state(current_clab) -> str: res = "" if current_clab.father != EmptyValue and \ current_clab.father.instance_of() != current_clab.instance_of(): res = "If specified, father must be of the same breed as the current dog" return res clab_state_constr = ClabjectStateConstraint( name="FatherOfTheSameBreedType", eval_clabject_func=eval_state) father_sc = create_clabject_prop(n="father_sc", t=3, f='*', i_sc=True, c=[], d=clab_state_constr) Breed.define_props([make_noise, coat_colour, father, father_sc]) def make_noise(obj) -> str: return "Wuff - I'm a Collie" Collie = Breed(name="Collie", init_props={"make_noise": make_noise}) def make_noise(obj) -> str: return "Wuff I'm a Golden Retriever" GoldenRetriever = Breed(name="GoldenRetriever", speed_adjustments={"coat_colour": -1}, init_props={ "make_noise": make_noise, "coat_colour": 'light to dark golden' }) SableRoughCollie = Collie(name="SableRoughCollie", init_props={"coat_colour": "sable-white"}) def make_noise(obj) -> str: return "Wuff I'm called " + str(obj.__name__) sam = SableRoughCollie(name="Sam", init_props={ "father": EmptyValue, "make_noise": make_noise }, declare_as_instance=True) lassie = SableRoughCollie(name="Lassie", init_props={"father": sam}, declare_as_instance=True) derek = GoldenRetriever(name="Derek", speed_adjustments={ "father": -1, "father_sc": -1 }, init_props={"father": sam}, declare_as_instance=True) print(Collie.make_noise()) # > Wuff - I'm a Collie print(GoldenRetriever.make_noise()) # > Wuff I'm a Golden Retriever print(sam.make_noise()) # > Wuff I'm called Sam print(lassie.make_noise()) # > Wuff - I'm a Collie print(derek.make_noise()) # Wuff I'm a GoldenRetriever print(lassie.check_state_constraints()) # > {} print(derek.check_state_constraints() ) # > {'father_sc': 'If specified, father ...'} hidden_root = False viz_name = str(Path(__file__).stem) return Breed, viz_name, hidden_root
def model_snippet(sub_composite_hierarchy=False): from datetime import timedelta from math import inf from multilevel_py.constraints import prop_constraint_py_isinstance_functional, \ prop_constraint_ml_instance_of_th_order_functional, \ prop_constraint_is_th_order_instance_of_clabject_set_functional from multilevel_py.constraints import prop_constraint_collection_member_functional as as_member_constr from multilevel_py.constraints import is_str_constraint, is_timedelta_constraint from multilevel_py.core import Clabject, create_clabject_prop from multilevel_py.exceptions import ConstraintViolationException # Traditional Class (to be refined in the later development steps) class Exercise: def __init__(self, name: str): self.name = name TrainingPlanElement = Clabject(name="TraingPlanElement", init_props={}) prop_part_name = create_clabject_prop(n="part_name", t=3, f='*', c=[is_str_constraint]) prop_calc_plan_duration = create_clabject_prop(n="calc_plan_duration", t=1, f='*', i_m=True) TrainingPlanElement.define_props([prop_part_name, prop_calc_plan_duration]) def calc_plan_duration_recursive(obj) -> timedelta: planned_train_time = timedelta() # 0 for child_plan in obj.child_plans: planned_train_time += child_plan.calc_plan_duration() return planned_train_time CompositePlanElement = TrainingPlanElement( name="CompositePlanElement", init_props={"calc_plan_duration": calc_plan_duration_recursive}) def append_childs(obj, childs): for child in childs: # [child] to check member constraints violations = obj.check_prop_constraints(prop_name="child_plans", potential_value=[child], init_only=True) if violations: raise ConstraintViolationException( violated_constraints=violations) else: obj.child_plans.append(child) prop_child_plans = create_clabject_prop(n="child_plans", t=2, f='*', c=[], coll_desc=(1, inf, None), d=[]) prop_append_child = create_clabject_prop(n="append_childs", t=2, f='*', i_m=True, c=[], v=append_childs) CompositePlanElement.define_props([prop_child_plans, prop_append_child]) SimplePlanElement = TrainingPlanElement( name="SimplePlanElement", speed_adjustments={"calc_plan_duration": 1}) is_exercise = prop_constraint_py_isinstance_functional(Exercise, eval_on_init=True) prop_exercise = create_clabject_prop(n="exercise", t=2, f='*', c=[is_exercise]) prop_exercise_duration = create_clabject_prop(n="exercise_duration", t=2, f='*', c=[is_timedelta_constraint]) SimplePlanElement.define_props([prop_exercise, prop_exercise_duration]) # M1 Section def calc_train_duration(obj): train_duration = obj.exercise_duration + obj.rest_duration return train_duration ExerciseSetPlanElement = SimplePlanElement( name="ExerciseSetPlanElement", init_props={"calc_plan_duration": calc_train_duration}) prop_rest_duration = create_clabject_prop(n="rest_duration", t=1, f='*', c=[is_timedelta_constraint]) ExerciseSetPlanElement.define_props([prop_rest_duration]) def calc_train_duration(obj): return obj.exercise_duration ExerciseIntervalPlanElement = SimplePlanElement( name="ExerciseIntervalPlanElement", init_props={"calc_plan_duration": calc_train_duration}) if sub_composite_hierarchy: StressRestPatternPlanElement = CompositePlanElement( name="StressRestPatternPlanElement", init_props={}, speed_adjustments={ 'part_name': 1, 'child_plans': 1 }) stress_rest_child_constraint = prop_constraint_is_th_order_instance_of_clabject_set_functional( {SimplePlanElement}, order=2, eval_on_init=True) StressRestPatternPlanElement.add_prop_constraint( prop_name="child_plans", constraint=as_member_constr(stress_rest_child_constraint)) IntervalTrainingPlanElement = StressRestPatternPlanElement( name="IntervalTrainingPlanElement", init_props={}) interval_childs = prop_constraint_is_th_order_instance_of_clabject_set_functional( expected_values={ExerciseIntervalPlanElement}, order=1) IntervalTrainingPlanElement.add_prop_constraint( prop_name="child_plans", constraint=as_member_constr(interval_childs)) StationTrainingPlanElement = StressRestPatternPlanElement( name="StationTrainingPlanElement", init_props={}) station_childs = prop_constraint_is_th_order_instance_of_clabject_set_functional( expected_values={ExerciseSetPlanElement}, order=1) StationTrainingPlanElement.add_prop_constraint( prop_name="child_plans", constraint=as_member_constr(station_childs)) TrainingSectionPlanElement = CompositePlanElement( name="TrainingSectionPlanElement", init_props={}) section_childs = prop_constraint_is_th_order_instance_of_clabject_set_functional( expected_values={StressRestPatternPlanElement}, order=2) TrainingSectionPlanElement.add_prop_constraint( prop_name="child_plans", constraint=as_member_constr(section_childs)) return CompositePlanElement else: return TrainingPlanElement
def model_snippet(): from pathlib import Path from multilevel_py.constraints import is_str_constraint, is_float_constraint, \ prop_constraint_ml_instance_of_th_order_functional from multilevel_py.core import create_clabject_prop, Clabject from multilevel_py.constraints import ReInitPropConstr # DslRoot for illustration purposes - integrating three classification hierarchies DslRoot = Clabject(name="DSLRoot") # Mass Unit Hierarchy symbol_prop = create_clabject_prop(n='symbol', t=2, f='*', i_f=True, c=[is_str_constraint]) MassUnit = DslRoot(name="MassUnit") MassUnit.define_props([symbol_prop]) kilogram = MassUnit(declare_as_instance=True, name="Kilogram", speed_adjustments={'symbol': -1}, init_props={'symbol': 'kg'}) conversion_factor_prop = create_clabject_prop(n='conversion_factor', t=1, f='*', i_f=True, c=[is_float_constraint]) is_mass_unit_constr = prop_constraint_ml_instance_of_th_order_functional( MassUnit, instantiation_order=1) base_unit_prop = create_clabject_prop(n='base_unit', t=0, f='*', i_assoc=True, c=[is_mass_unit_constr], v=kilogram) DerivedMassUnit = MassUnit(name="DerivedMassUnit") DerivedMassUnit.define_props([conversion_factor_prop, base_unit_prop]) pound = DerivedMassUnit(name="Pound", declare_as_instance=True, init_props={ "symbol": "lb", "conversion_factor": 0.45359 }) # Weight Load Hierarchy planned_value_prop = create_clabject_prop(n='planned_value', t=1, f='*', i_f=False, c=[is_float_constraint]) actual_value_prop = create_clabject_prop(n='actual_value', t=2, f='*', i_f=True, c=[is_float_constraint]) mass_unit_prop = create_clabject_prop(n='mass_unit', t=0, f='*', i_f=False, i_assoc=True, v=MassUnit) WeightLoad = DslRoot(name="WeightLoad") WeightLoad.define_props( [planned_value_prop, actual_value_prop, mass_unit_prop]) is_fst_or_snd_order_mass_unit_instance = \ prop_constraint_ml_instance_of_th_order_functional( MassUnit, instantiation_order=(1, 2)) re_init_constr = ReInitPropConstr( del_constr=[], add_constr=[is_fst_or_snd_order_mass_unit_instance]) WeightLoad.require_re_init_on_next_step(prop_name="mass_unit", re_init_prop_constr=re_init_constr) ParameterisedWeightLoad = WeightLoad(name='ParameterisedWeightLoad', init_props={ 'planned_value': 180.0, 'mass_unit': pound }) realisedWeightLoad = ParameterisedWeightLoad( declare_as_instance=True, name="RealisedWeightLoad", init_props={"actual_value": 182.5}) # Deadlift Hierarchy weight_load_prop = create_clabject_prop(n='weight_load', t=0, f='*', i_f=False, i_assoc=True, v=WeightLoad) Deadlift = DslRoot(name="Deadlift") Deadlift.define_props([weight_load_prop]) is_weight_load_instance = prop_constraint_ml_instance_of_th_order_functional( WeightLoad, instantiation_order=1) re_init_constr = ReInitPropConstr(del_constr=[], add_constr=[is_weight_load_instance]) Deadlift.require_re_init_on_next_step(prop_name="weight_load", re_init_prop_constr=re_init_constr) ParameterisedDeadlift = Deadlift( name="ParameterisedDeadlift", init_props={'weight_load': ParameterisedWeightLoad}) is_param_weight_load_instance = prop_constraint_ml_instance_of_th_order_functional( ParameterisedWeightLoad, instantiation_order=1) re_init_constr = ReInitPropConstr( del_constr=[is_weight_load_instance], add_constr=[is_param_weight_load_instance]) ParameterisedDeadlift.require_re_init_on_next_step( prop_name="weight_load", re_init_prop_constr=re_init_constr) realisedDeadlift = ParameterisedDeadlift( declare_as_instance=True, name="RealisedDeadlift", init_props={"weight_load": realisedWeightLoad}) hidden_root = False viz_name = str(Path(__file__).stem) return DslRoot, viz_name, hidden_root
from multilevel_py.constraints import is_str_constraint from multilevel_py.clabject_prop import CollectionDescription from multilevel_py.core import create_clabject_prop, Clabject, MetaClabject Animal = Clabject(name="Animal") # Declare Properties to be instantiated down the classification hierarchy is_animal = create_clabject_prop(n="is_animal", t=0, f="*", v=True) species = create_clabject_prop(n="species", t=1, f='*', c=[is_str_constraint]) name = create_clabject_prop(n="name", t=2, f='*', c=[is_str_constraint]) traits = create_clabject_prop(n="traits", t=2, f='*', coll_desc=(1,3, is_str_constraint)) Animal.define_props([is_animal, species, name, traits]) def create_tom(animal_clabject): """Create a 'clabject' representing the cartoon character Tom Parameters ---------- animal_clabject : multilevel_py.core.MetaClabject the Animal clabject, i.e. the top of the classification hierarchy defined above Returns ------- multilevel_py.core.MetaClabject the tom clabject (declared as instance) """ Cat = animal_clabject(name="Cat", init_props={"species": "cat"}) return Cat(name="tom", init_props={"name": "tom", "traits": ["lazy", "food loving"]}, declare_as_instance=True) def create_jerry(animal_clabject): """Create a 'clabject' representing the cartoon character Jerry
def model_snippet(): from pathlib import Path from multilevel_py.constraints import is_str_constraint from multilevel_py.core import create_clabject_prop, Clabject Exercise = Clabject(name="Exercise") prop_exercise_name = create_clabject_prop(n="exercise_name", t=1, f='*', i_f=True, c=[is_str_constraint]) Exercise.define_props([prop_exercise_name]) KiteSurfing = Exercise(name="KiteSurfing", init_props={"exercise_name": "KiteSurfing"}) StrengthExercise = Exercise(name="StrengthExercise", speed_adjustments={"exercise_name": 1}) prop_motor_kind = create_clabject_prop(n="motor_kind", t=0, f='*', i_f=True, c=[is_str_constraint], v="strength") StrengthExercise.define_props([prop_motor_kind]) def define_contraction_form(clabject, prop_value): prop_contraction_form = create_clabject_prop(n="contraction_form", t=0, f='*', i_f=True, c=[is_str_constraint], v=prop_value) clabject.define_props([prop_contraction_form]) StaticStrengthExercise = StrengthExercise( name="StaticStrengthExercise", speed_adjustments={"exercise_name": 1}) define_contraction_form(StaticStrengthExercise, "static") DynamicStrengthExercise = StrengthExercise( name="DynamicStrengthExercise", speed_adjustments={"exercise_name": 1}) define_contraction_form(DynamicStrengthExercise, "dynamic") def define_contraction_phase(clabject, prop_value): prop_contraction_phase = create_clabject_prop(n="contraction_phase", t=0, f='*', i_f=True, c=[is_str_constraint], v=prop_value) clabject.define_props([prop_contraction_phase]) ConcentricStrengthExercise = DynamicStrengthExercise( name="ConcentricStrengthExercise", speed_adjustments={"exercise_name": 1}) define_contraction_phase(ConcentricStrengthExercise, "concentric") ExcentricStrengthExercise = DynamicStrengthExercise( name="ExcentricStrengthExercise", speed_adjustments={"exercise_name": 1}) define_contraction_phase(ExcentricStrengthExercise, "excentric") Deadlift = DynamicStrengthExercise( name="Deadlift", init_props={"exercise_name": "Deadlift"}) ExcentricBenchpress = ExcentricStrengthExercise( name="ExcentricBenchpress", init_props={"exercise_name": "ExcentricBenchpress"}) ConcentricDeadlift = ConcentricStrengthExercise( name="ConcentricDeadlift", init_props={"exercise_name": "ConcentricDeadlift"}) hidden_root = False viz_name = str(Path(__file__).stem) return Exercise, viz_name, hidden_root