Esta página fue traducida por una máquina y aún no ha sido revisada por una persona. El inglés es la referencia; las correcciones son bienvenidas en GitHub. GitHub
Cookwala / SDK y escenarios / 089
One allergy becomes two constraints for a grocer and one for a planner
The allergy facet derives to allergen_block and labeling_required. A grocer may receive both, a planner only the block, a delivery service neither. The answer names the facet id, never the allergen or the child.
Leer primero: /docs/HOUSEHOLD-CONTEXT/ /trust/
Pasos
The allergy facet alone, for a grocer: a block and a labelling requirement.
deriveConstraintsFor a planner: the block only; labelling is not in the planner's allow-list.
deriveConstraintsFor a delivery service: nothing; the facet is withheld as not allowed for the role.
deriveConstraintsderivedFrom carries the facet id. The allergen itself goes in the DerivedConstraint's value (examples/household/constraints-planner-allergen.json); the subject never does.
Resultado
You know how an allergy reaches a shop as a block and a label and reaches a planner as a block only; Core rule 6.7 then refuses any recipe that contains a blocked allergen. Next: 090 reads the public registry.
Línea de comandos
cookwala constraints examples/household/context.json --role grocer
jq '.entries[] | select(.id == "cw.facet.household.health.allergies") | .derivesTo' vocab/facets.jsonSalida grabada
Estado: ok
[1] deriveConstraints: {"constraints": [{"type": "allergen_block", "derivedFrom": ["cw.facet.household.health.allergies"]}, {"type": "labeling_required", "derivedFrom": ["cw.facet.household.health.allergies"]}], "disclosed": [], "withheld": []}
[2] deriveConstraints: {"constraints": [{"type": "allergen_block", "derivedFrom": ["cw.facet.household.health.allergies"]}], "disclosed": [], "withheld": []}
[3] deriveConstraints: {"constraints": [], "disclosed": [], "withheld": [{"facet": "cw.facet.household.health.allergies", "reason": "not_allowed_for_role"}]}Código
Ejecutar: python scenarios/out/089-one-allergy-two-constraints/python.py
# Scenario 089: One allergy becomes two constraints for a grocer and one for a planner
# The allergy facet derives to allergen_block and labeling_required. A grocer may receive both, a planner only the block, a delivery service neither. The answer names the facet id, never the allergen or the child.
# Run a hub first: python hub/cookwala_hub.py --recipes examples
import json, os
from cookwala.client import CookwalaClient, CookwalaProblem
def load(path):
with open(path, encoding="utf-8") as f:
return json.load(f)
def pick(obj, path):
for part in path.replace("]", "").replace("[", ".").split("."):
obj = obj[int(part)] if part.isdigit() else (len(obj) if part == "length" else obj.get(part))
return obj
c = CookwalaClient(os.environ.get("COOKWALA_HUB", "http://localhost:7878"))
# Step 1: The allergy facet alone, for a grocer: a block and a labelling requirement.
grocer = c.derive_constraints([{"facet": "cw.facet.household.health.allergies"}], "grocer")
assert pick(grocer, "constraints.length") == 2, pick(grocer, "constraints.length")
print("ok", "constraints.length", "=", 2)
assert pick(grocer, "constraints[0].type") == "allergen_block", pick(grocer, "constraints[0].type")
print("ok", "constraints[0].type", "=", "allergen_block")
assert pick(grocer, "constraints[1].type") == "labeling_required", pick(grocer, "constraints[1].type")
print("ok", "constraints[1].type", "=", "labeling_required")
assert pick(grocer, "withheld.length") == 0, pick(grocer, "withheld.length")
print("ok", "withheld.length", "=", 0)
# Step 2: For a planner: the block only; labelling is not in the planner's allow-list.
planner = c.derive_constraints([{"facet": "cw.facet.household.health.allergies"}], "planner")
assert pick(planner, "constraints.length") == 1, pick(planner, "constraints.length")
print("ok", "constraints.length", "=", 1)
assert pick(planner, "constraints[0].type") == "allergen_block", pick(planner, "constraints[0].type")
print("ok", "constraints[0].type", "=", "allergen_block")
# Step 3: For a delivery service: nothing; the facet is withheld as not allowed for the role.
delivery = c.derive_constraints([{"facet": "cw.facet.household.health.allergies"}], "delivery")
assert pick(delivery, "constraints.length") == 0, pick(delivery, "constraints.length")
print("ok", "constraints.length", "=", 0)
assert pick(delivery, "withheld.length") == 1, pick(delivery, "withheld.length")
print("ok", "withheld.length", "=", 1)
assert pick(delivery, "withheld[0].reason") == "not_allowed_for_role", pick(delivery, "withheld[0].reason")
print("ok", "withheld[0].reason", "=", "not_allowed_for_role")
# Step 4: derivedFrom carries the facet id. The allergen itself goes in the DerivedConstraint's value (examples/household/constraints-planner-allergen.json); the subject never does.
assert pick(grocer, "constraints[0].derivedFrom") == ["cw.facet.household.health.allergies"], pick(grocer, "constraints[0].derivedFrom")
print("ok", "constraints[0].derivedFrom", "=", ["cw.facet.household.health.allergies"])
assert pick(planner, "constraints[0].derivedFrom") == ["cw.facet.household.health.allergies"], pick(planner, "constraints[0].derivedFrom")
print("ok", "constraints[0].derivedFrom", "=", ["cw.facet.household.health.allergies"])
print("scenario complete")