|
| 1 | +/* SPDX-License-Identifier: MIT */ |
| 2 | + |
| 3 | +#ifndef ARBITER_COMPOSE_H_ |
| 4 | +#define ARBITER_COMPOSE_H_ |
| 5 | + |
| 6 | +/** |
| 7 | + * @defgroup arbiter_compose Multi-Model Composition (REQ-ARCH-037) |
| 8 | + * @ingroup arbiter |
| 9 | + * @{ |
| 10 | + * @brief Share facts across multiple models via a common fact bus. |
| 11 | + * |
| 12 | + * The fact bus holds a shared array of fact values. Each model is |
| 13 | + * "attached" with a mapping table that translates bus fact indices |
| 14 | + * to the model's own fact indices. ARBITER_compose_sync() pushes |
| 15 | + * bus values into all attached contexts; ARBITER_compose_eval_all() |
| 16 | + * evaluates every attached model in attachment order. |
| 17 | + * |
| 18 | + * All storage is pre-allocated — no malloc. |
| 19 | + */ |
| 20 | + |
| 21 | +#include <stdint.h> |
| 22 | +#include <stdbool.h> |
| 23 | +#include <arbiter/arbiter.h> |
| 24 | + |
| 25 | +#ifdef __cplusplus |
| 26 | +extern "C" { |
| 27 | +#endif |
| 28 | + |
| 29 | +#ifndef CONFIG_ARBITER_MAX_MODELS |
| 30 | +#define CONFIG_ARBITER_MAX_MODELS 4 |
| 31 | +#endif |
| 32 | + |
| 33 | +#ifndef CONFIG_ARBITER_COMPOSE_MAX_BUS_FACTS |
| 34 | +#define CONFIG_ARBITER_COMPOSE_MAX_BUS_FACTS 64 |
| 35 | +#endif |
| 36 | + |
| 37 | +/** Maps one bus fact to a model fact. */ |
| 38 | +struct ARBITER_fact_mapping { |
| 39 | + uint16_t bus_fact_id; /**< Index into the bus fact array. */ |
| 40 | + uint16_t model_fact_id; /**< Index into the model's own fact array. */ |
| 41 | +}; |
| 42 | + |
| 43 | +/** Per-attachment record (bus ↔ model link). */ |
| 44 | +struct ARBITER_compose_attachment { |
| 45 | + struct ARBITER_ctx *ctx; /**< Model context. */ |
| 46 | + const struct ARBITER_fact_mapping *mapping; /**< Mapping table. */ |
| 47 | + uint16_t map_count; /**< Number of mappings. */ |
| 48 | + bool active; /**< Slot occupied? */ |
| 49 | +}; |
| 50 | + |
| 51 | +/** Shared fact bus. */ |
| 52 | +struct ARBITER_fact_bus { |
| 53 | + struct ARBITER_fact_value *facts; /**< Shared fact array. */ |
| 54 | + uint16_t max_facts; /**< Capacity of facts[]. */ |
| 55 | + |
| 56 | + struct ARBITER_compose_attachment |
| 57 | + attachments[CONFIG_ARBITER_MAX_MODELS]; /**< Attached models. */ |
| 58 | + uint16_t attachment_count; /**< Active attachments. */ |
| 59 | +}; |
| 60 | + |
| 61 | +/** |
| 62 | + * @brief Initialize a fact bus with a pre-allocated fact array. |
| 63 | + * |
| 64 | + * @param bus Bus to initialize. |
| 65 | + * @param facts Pre-allocated array of fact values. |
| 66 | + * @param max_facts Capacity of facts[]. |
| 67 | + * @return ARBITER_OK on success, ARBITER_EINVAL if bus or facts is NULL. |
| 68 | + */ |
| 69 | +int ARBITER_compose_init(struct ARBITER_fact_bus *bus, |
| 70 | + struct ARBITER_fact_value *facts, |
| 71 | + uint16_t max_facts); |
| 72 | + |
| 73 | +/** |
| 74 | + * @brief Attach a model context to the bus. |
| 75 | + * |
| 76 | + * @param bus Initialized fact bus. |
| 77 | + * @param ctx Model context to attach. |
| 78 | + * @param mapping Array of bus-to-model fact mappings. |
| 79 | + * @param map_count Number of entries in mapping[]. |
| 80 | + * @return ARBITER_OK on success, ARBITER_EOVERFLOW if no slots available. |
| 81 | + */ |
| 82 | +int ARBITER_compose_attach(struct ARBITER_fact_bus *bus, |
| 83 | + struct ARBITER_ctx *ctx, |
| 84 | + const struct ARBITER_fact_mapping *mapping, |
| 85 | + uint16_t map_count); |
| 86 | + |
| 87 | +/** |
| 88 | + * @brief Detach a model context from the bus. |
| 89 | + * |
| 90 | + * @param bus Bus. |
| 91 | + * @param ctx Context to detach. |
| 92 | + * @return ARBITER_OK on success, ARBITER_EINVAL if not found. |
| 93 | + */ |
| 94 | +int ARBITER_compose_detach(struct ARBITER_fact_bus *bus, |
| 95 | + struct ARBITER_ctx *ctx); |
| 96 | + |
| 97 | +/** |
| 98 | + * @brief Set a fact value on the bus. |
| 99 | + * |
| 100 | + * @param bus Initialized fact bus. |
| 101 | + * @param fact_id Bus fact index. |
| 102 | + * @param value Value to set. |
| 103 | + * @return ARBITER_OK or ARBITER_ERANGE. |
| 104 | + */ |
| 105 | +int ARBITER_compose_set_fact(struct ARBITER_fact_bus *bus, |
| 106 | + uint16_t fact_id, int32_t value); |
| 107 | + |
| 108 | +/** |
| 109 | + * @brief Push bus facts into all attached model contexts. |
| 110 | + * |
| 111 | + * For each attachment, iterates the mapping table and copies the bus |
| 112 | + * fact values into the model context's fact_values[]. |
| 113 | + * |
| 114 | + * @param bus Initialized fact bus. |
| 115 | + * @return ARBITER_OK on success. |
| 116 | + */ |
| 117 | +int ARBITER_compose_sync(struct ARBITER_fact_bus *bus); |
| 118 | + |
| 119 | +/** |
| 120 | + * @brief Evaluate all attached models in attachment order. |
| 121 | + * |
| 122 | + * For each attached model, takes a snapshot, evaluates, and stores |
| 123 | + * the result. Caller must provide arrays of at least |
| 124 | + * bus->attachment_count entries. |
| 125 | + * |
| 126 | + * @param bus Initialized fact bus. |
| 127 | + * @param results Pre-allocated array for results (one per attached model). |
| 128 | + * @param traces Pre-allocated array for traces (one per model, NULL entries OK). |
| 129 | + * @return ARBITER_OK on success. |
| 130 | + */ |
| 131 | +int ARBITER_compose_eval_all(struct ARBITER_fact_bus *bus, |
| 132 | + struct ARBITER_result *results, |
| 133 | + struct ARBITER_trace *traces); |
| 134 | + |
| 135 | +/** @} */ |
| 136 | + |
| 137 | +#ifdef __cplusplus |
| 138 | +} |
| 139 | +#endif |
| 140 | + |
| 141 | +#endif /* ARBITER_COMPOSE_H_ */ |
0 commit comments