diff options
| author | 2026-11-07 05:06:01 +0000 | |
|---|---|---|
| committer | 2026-11-07 05:38:49 +0000 | |
| commit | 04f8b646c1c8a2435931bea9d1acffdde138f81e (patch) | |
| tree | 892354a7b485c3ac53e51a7d3e3388254ac877b7 /src | |
| download | coursework-04f8b646c1c8a2435931bea9d1acffdde138f81e.tar.gz | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/dynamic-array.h | 56 | ||||
| -rw-r--r-- | src/expert-system.c | 21 | ||||
| -rw-r--r-- | src/inference-engine.c | 244 | ||||
| -rw-r--r-- | src/inference-engine.h | 10 | ||||
| -rw-r--r-- | src/knowledge-base.c | 64 | ||||
| -rw-r--r-- | src/knowledge-base.h | 171 | ||||
| -rw-r--r-- | src/utils.h | 14 |
7 files changed, 580 insertions, 0 deletions
diff --git a/src/dynamic-array.h b/src/dynamic-array.h new file mode 100644 index 0000000..212e9f4 --- /dev/null +++ b/src/dynamic-array.h | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | #ifndef INCLUDED_DYNAMIC_ARRAY_H | ||
| 2 | #define INCLUDED_DYNAMIC_ARRAY_H | ||
| 3 | |||
| 4 | #include "utils.h" | ||
| 5 | |||
| 6 | #ifndef DYNAMIC_ARRAY_INIT_CAP | ||
| 7 | #define DYNAMIC_ARRAY_INIT_CAP 256 | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #define DYNAMIC_ARRAY_ENSURE_CAPACITY(DYNAMIC_ARRAY, COUNT) \ | ||
| 11 | ({ \ | ||
| 12 | if ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) { \ | ||
| 13 | if ((DYNAMIC_ARRAY).capacity == 0) \ | ||
| 14 | (DYNAMIC_ARRAY).capacity = DYNAMIC_ARRAY_INIT_CAP; \ | ||
| 15 | while ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) \ | ||
| 16 | (DYNAMIC_ARRAY).capacity *= 2; \ | ||
| 17 | (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ | ||
| 18 | (DYNAMIC_ARRAY).capacity * \ | ||
| 19 | sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 20 | ASSERT((DYNAMIC_ARRAY).items); \ | ||
| 21 | } \ | ||
| 22 | }) | ||
| 23 | |||
| 24 | #define DYNAMIC_ARRAY_SHRINK(DYNAMIC_ARRAY) \ | ||
| 25 | ({ \ | ||
| 26 | (DYNAMIC_ARRAY).capacity = (DYNAMIC_ARRAY).count; \ | ||
| 27 | (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ | ||
| 28 | (DYNAMIC_ARRAY).capacity * \ | ||
| 29 | sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 30 | ASSERT((DYNAMIC_ARRAY).items); \ | ||
| 31 | \ | ||
| 32 | }) | ||
| 33 | |||
| 34 | #define DYNAMIC_ARRAY_APPEND(DYNAMIC_ARRAY, ITEM) \ | ||
| 35 | ({ \ | ||
| 36 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), 1); \ | ||
| 37 | (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count++] = (ITEM); \ | ||
| 38 | }) | ||
| 39 | |||
| 40 | #define DYNAMIC_ARRAY_APPEND_MANY(DYNAMIC_ARRAY, NEW_ITEMS, NEW_ITEMS_COUNT) \ | ||
| 41 | ({ \ | ||
| 42 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), (NEW_ITEMS_COUNT)); \ | ||
| 43 | memcpy ((DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count, (NEW_ITEMS), \ | ||
| 44 | (NEW_ITEMS_COUNT)*sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 45 | (DYNAMIC_ARRAY).count += (NEW_ITEMS_COUNT); \ | ||
| 46 | }) | ||
| 47 | |||
| 48 | #define DYNAMIC_ARRAY_RESET(DYNAMIC_ARRAY) ({ (DYNAMIC_ARRAY).count = 0; }) | ||
| 49 | |||
| 50 | #define DYNAMIC_ARRAY_FREE(DYNAMIC_ARRAY) FREE((DYNAMIC_ARRAY).items) | ||
| 51 | |||
| 52 | #define DYNAMIC_ARRAY_FOREACH(ITEM, DYNAMIC_ARRAY) for ( \ | ||
| 53 | typeof ((DYNAMIC_ARRAY).items) ITEM = (DYNAMIC_ARRAY).items; \ | ||
| 54 | ITEM < (DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count; ++ITEM) | ||
| 55 | |||
| 56 | #endif | ||
diff --git a/src/expert-system.c b/src/expert-system.c new file mode 100644 index 0000000..3462c44 --- /dev/null +++ b/src/expert-system.c | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdbool.h> | ||
| 5 | |||
| 6 | #include "knowledge-base.h" | ||
| 7 | #include "inference-engine.h" | ||
| 8 | |||
| 9 | int | ||
| 10 | main (void) | ||
| 11 | { | ||
| 12 | // TODO(cmmm): auto-rebuild | ||
| 13 | print_knowledge_base (); | ||
| 14 | putchar ('\n'); | ||
| 15 | build_decision_tree (); | ||
| 16 | write_decision_tree_in_dot_format (); | ||
| 17 | putchar ('\n'); | ||
| 18 | question_user (); | ||
| 19 | |||
| 20 | return EXIT_SUCCESS; | ||
| 21 | } | ||
diff --git a/src/inference-engine.c b/src/inference-engine.c new file mode 100644 index 0000000..db4f33a --- /dev/null +++ b/src/inference-engine.c | |||
| @@ -0,0 +1,244 @@ | |||
| 1 | #include "inference-engine.h" | ||
| 2 | #include <string.h> | ||
| 3 | #include "utils.h" | ||
| 4 | #include "dynamic-array.h" | ||
| 5 | |||
| 6 | typedef struct decision_tree_node { | ||
| 7 | union { | ||
| 8 | enum question question; | ||
| 9 | enum decision decision; | ||
| 10 | }; | ||
| 11 | uint32_t branch_yes; | ||
| 12 | uint32_t branch_no; | ||
| 13 | } *DECISION_TREE_NODE; | ||
| 14 | |||
| 15 | struct decision_tree { | ||
| 16 | struct decision_tree_node *items; | ||
| 17 | uint32_t count; | ||
| 18 | uint32_t capacity; | ||
| 19 | }; | ||
| 20 | |||
| 21 | #define MAX_DECISION_TREE_NODE_COUNT ((1 << QUESTION_COUNT) - 1) | ||
| 22 | |||
| 23 | static struct decision_tree decision_tree = {0}; | ||
| 24 | |||
| 25 | static int answers_known [QUESTION_COUNT] = {0}; | ||
| 26 | static int question_queue [QUESTION_COUNT] = {0}; | ||
| 27 | |||
| 28 | static inline bool | ||
| 29 | is_answer_known (enum decision decision, enum question question) { | ||
| 30 | switch (get_decision_predicate (decision, question)) { | ||
| 31 | case ANSWER_YES: | ||
| 32 | case ANSWER_NO: | ||
| 33 | return true; | ||
| 34 | default: | ||
| 35 | return false; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | static void | ||
| 40 | build_decision_tree_impl (enum decision *begin, | ||
| 41 | enum decision *end, int order, | ||
| 42 | DECISION_TREE_NODE node) | ||
| 43 | { | ||
| 44 | if (begin == end) | ||
| 45 | return; | ||
| 46 | |||
| 47 | if (begin + 1 == end) { | ||
| 48 | // printf ("INSERT DECISION %X\n", *begin); | ||
| 49 | node->decision = *begin; | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | |||
| 53 | enum question question; | ||
| 54 | for (; order < QUESTION_COUNT; ++order) { | ||
| 55 | question = question_queue[order]; | ||
| 56 | |||
| 57 | enum decision *decision; | ||
| 58 | for (decision = begin; decision < end; ++decision) { | ||
| 59 | if (!is_answer_known (*decision, question)) | ||
| 60 | break; | ||
| 61 | } if (decision == end) break; | ||
| 62 | } ASSERT (order != QUESTION_COUNT && "NOT ENOUGH KNOWLEDGE"); | ||
| 63 | |||
| 64 | // printf ("INSERT QUESTION %X\n", question); | ||
| 65 | node->question = question; | ||
| 66 | |||
| 67 | enum decision *pivot = begin; | ||
| 68 | for (enum decision *decision = begin; decision < end; ++decision) { | ||
| 69 | if (get_decision_predicate (*decision, question) == ANSWER_NO) { | ||
| 70 | pivot = decision; | ||
| 71 | break; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | for (enum decision *head = pivot + 1; head < end; ++head) { | ||
| 76 | if (get_decision_predicate (*head, question) == ANSWER_YES) { | ||
| 77 | enum decision tmp = *pivot; | ||
| 78 | *pivot++ = *head; | ||
| 79 | *head = tmp; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | // for (enum decision *decision = begin; decision < end; ++decision) { | ||
| 84 | // printf ("%s%X\n", decision == pivot ? "->" : " ", *decision + 1); | ||
| 85 | // } | ||
| 86 | |||
| 87 | DYNAMIC_ARRAY_APPEND (decision_tree, (struct decision_tree_node) {0}); | ||
| 88 | // printf ("BRANCH YES TO %X\n", decision_tree.count - 1); | ||
| 89 | node->branch_yes = decision_tree.count - 1; | ||
| 90 | build_decision_tree_impl (begin, pivot, order + 1, | ||
| 91 | &decision_tree.items[decision_tree.count - 1]); | ||
| 92 | DYNAMIC_ARRAY_APPEND (decision_tree, (struct decision_tree_node) {0}); | ||
| 93 | // printf ("BRANCH NO TO %X\n", decision_tree.count - 1); | ||
| 94 | node->branch_no = decision_tree.count - 1; | ||
| 95 | build_decision_tree_impl (pivot, end, order + 1, | ||
| 96 | &decision_tree.items[decision_tree.count - 1]); | ||
| 97 | } | ||
| 98 | |||
| 99 | void | ||
| 100 | build_decision_tree (void) | ||
| 101 | { | ||
| 102 | // compute answers_known[question] and question_order[order] | ||
| 103 | for (int question = 0; question < QUESTION_COUNT; ++question) { | ||
| 104 | for (int decision = 0; decision < DECISION_COUNT; ++decision) | ||
| 105 | answers_known[question] += is_answer_known (decision, question); | ||
| 106 | |||
| 107 | int order = 0; | ||
| 108 | while (order < question && | ||
| 109 | answers_known[question] <= | ||
| 110 | answers_known[question_queue[order]]) { | ||
| 111 | order += 1; | ||
| 112 | } | ||
| 113 | |||
| 114 | memmove (&question_queue[order + 1], | ||
| 115 | &question_queue[order + 0], | ||
| 116 | (question - order)*sizeof (*question_queue)); | ||
| 117 | question_queue[order] = question; | ||
| 118 | } | ||
| 119 | |||
| 120 | // initialize context and run recursive function on it | ||
| 121 | enum decision decisions[DECISION_COUNT]; | ||
| 122 | for (int decision = 0; decision < DECISION_COUNT; ++decision) | ||
| 123 | decisions[decision] = DECISION_COUNT - 1 - decision; | ||
| 124 | DYNAMIC_ARRAY_ENSURE_CAPACITY (decision_tree, MAX_DECISION_TREE_NODE_COUNT); | ||
| 125 | DYNAMIC_ARRAY_APPEND (decision_tree, (struct decision_tree_node) {0}); | ||
| 126 | build_decision_tree_impl (decisions, decisions + DECISION_COUNT, 0, | ||
| 127 | &decision_tree.items[0]); | ||
| 128 | DYNAMIC_ARRAY_SHRINK (decision_tree); | ||
| 129 | } | ||
| 130 | |||
| 131 | static void | ||
| 132 | write_decision_tree_in_dot_format_impl (FILE *file, DECISION_TREE_NODE node) | ||
| 133 | { | ||
| 134 | uint32_t node_id = node - decision_tree.items; | ||
| 135 | |||
| 136 | if (node->branch_yes && node->branch_no) { | ||
| 137 | fprintf (file, "\tn%u [label=\"%u: %s\"]\n", | ||
| 138 | node_id, node_id + 1, question_labels[node->question]); | ||
| 139 | |||
| 140 | write_decision_tree_in_dot_format_impl ( | ||
| 141 | file, &decision_tree.items[node->branch_yes]); | ||
| 142 | uint32_t branch_yes_id = &decision_tree.items[node->branch_yes] - decision_tree.items; | ||
| 143 | fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", | ||
| 144 | node_id, branch_yes_id, question_branch_labels[node->question].answer_yes); | ||
| 145 | |||
| 146 | write_decision_tree_in_dot_format_impl (file, &decision_tree.items[node->branch_no]); | ||
| 147 | uint32_t branch_no_id = &decision_tree.items[node->branch_no] - decision_tree.items; | ||
| 148 | fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", node_id, branch_no_id, question_branch_labels[node->question].answer_no); | ||
| 149 | } else { | ||
| 150 | fprintf (file, "\tn%u [label=\"%u: %s\"]\n", node_id, node_id + 1, decision_labels[node->decision]); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | struct decision_tree_node_stack { | ||
| 155 | struct decision_tree_node **items; | ||
| 156 | uint32_t count; | ||
| 157 | uint32_t capacity; | ||
| 158 | }; | ||
| 159 | |||
| 160 | void | ||
| 161 | write_decision_tree_in_dot_format (void) | ||
| 162 | { | ||
| 163 | FILE *file = fopen ("decision-tree.dot", "w"); | ||
| 164 | if (file == NULL) { | ||
| 165 | fprintf (stderr, "Error opening file!\n"); | ||
| 166 | return; | ||
| 167 | } | ||
| 168 | |||
| 169 | fprintf (file, "digraph {\n"); | ||
| 170 | fprintf (file, "\trankdir = \"LR\";\n"); | ||
| 171 | // write_decision_tree_in_dot_format_impl (file, &decision_tree.items[0]); | ||
| 172 | |||
| 173 | uint32_t node_visual_id = 0; | ||
| 174 | struct decision_tree_node_stack stack1 = {0}; | ||
| 175 | struct decision_tree_node_stack stack2 = {0}; | ||
| 176 | struct decision_tree_node_stack *front_stack = &stack1; | ||
| 177 | struct decision_tree_node_stack *back_stack = &stack2; | ||
| 178 | DYNAMIC_ARRAY_APPEND (*front_stack, &decision_tree.items[0]); | ||
| 179 | while (front_stack->count) { | ||
| 180 | DYNAMIC_ARRAY_RESET (*back_stack); | ||
| 181 | DYNAMIC_ARRAY_FOREACH (n, *front_stack) { | ||
| 182 | DECISION_TREE_NODE node = *n; | ||
| 183 | uint32_t node_id = node - decision_tree.items; | ||
| 184 | |||
| 185 | if (node->branch_yes && node->branch_no) { | ||
| 186 | fprintf (file, "\tn%u [label=\"%u: %s\"]\n", node_id, ++node_visual_id, question_labels[node->question]); | ||
| 187 | |||
| 188 | DYNAMIC_ARRAY_APPEND (*back_stack, &decision_tree.items[node->branch_yes]); | ||
| 189 | uint32_t branch_yes_id = &decision_tree.items[node->branch_yes] - decision_tree.items; | ||
| 190 | fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", node_id, branch_yes_id, question_branch_labels[node->question].answer_yes); | ||
| 191 | |||
| 192 | DYNAMIC_ARRAY_APPEND (*back_stack, &decision_tree.items[node->branch_no]); | ||
| 193 | uint32_t branch_no_id = &decision_tree.items[node->branch_no] - decision_tree.items; | ||
| 194 | fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", node_id, branch_no_id, question_branch_labels[node->question].answer_no); | ||
| 195 | } else { | ||
| 196 | fprintf (file, "\tn%u [label=\"%u: %s\"]\n", node_id, ++node_visual_id, decision_labels[node->decision]); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | struct decision_tree_node_stack *tmp = front_stack; | ||
| 201 | front_stack = back_stack; | ||
| 202 | back_stack = tmp; | ||
| 203 | } | ||
| 204 | fprintf (file, "}\n"); | ||
| 205 | |||
| 206 | fclose (file); | ||
| 207 | |||
| 208 | puts ("decision-tree.dot:0:0: Decision tree is ready for viewing."); | ||
| 209 | } | ||
| 210 | |||
| 211 | void | ||
| 212 | question_user (void) | ||
| 213 | { | ||
| 214 | char *line = NULL; | ||
| 215 | size_t line_size = 0; | ||
| 216 | struct decision_tree_node *node = &decision_tree.items[0]; | ||
| 217 | while (node->branch_yes && node->branch_no) { | ||
| 218 | enum question question = node->question; | ||
| 219 | printf ("%s\n", question_labels[question]); | ||
| 220 | printf (" [1] %s\n", question_branch_labels[question].answer_yes); | ||
| 221 | printf (" [2] %s\n", question_branch_labels[question].answer_no); | ||
| 222 | |||
| 223 | enum answer answer = 0; | ||
| 224 | for (;;) { | ||
| 225 | printf ("> "); | ||
| 226 | getline (&line, &line_size, stdin); | ||
| 227 | sscanf (line, "%d", &answer); | ||
| 228 | if (answer < 1 || answer > 2) | ||
| 229 | continue; | ||
| 230 | |||
| 231 | switch (answer) { | ||
| 232 | case ANSWER_YES: | ||
| 233 | node = &decision_tree.items[node->branch_yes]; | ||
| 234 | break; | ||
| 235 | case ANSWER_NO: | ||
| 236 | node = &decision_tree.items[node->branch_no]; | ||
| 237 | break; | ||
| 238 | default: break; | ||
| 239 | } break; | ||
| 240 | } | ||
| 241 | } | ||
| 242 | free (line); | ||
| 243 | printf ("Рекомендация: %s\n", decision_labels[node->decision]); | ||
| 244 | } | ||
diff --git a/src/inference-engine.h b/src/inference-engine.h new file mode 100644 index 0000000..13344f7 --- /dev/null +++ b/src/inference-engine.h | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | #ifndef INCLUDED_INFERENCE_ENGINE_H | ||
| 2 | #define INCLUDED_INFERENCE_ENGINE_H | ||
| 3 | |||
| 4 | #include "knowledge-base.h" | ||
| 5 | |||
| 6 | void build_decision_tree (void); | ||
| 7 | void write_decision_tree_in_dot_format (void); | ||
| 8 | void question_user (void); | ||
| 9 | |||
| 10 | #endif | ||
diff --git a/src/knowledge-base.c b/src/knowledge-base.c new file mode 100644 index 0000000..58a9d07 --- /dev/null +++ b/src/knowledge-base.c | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | #include "knowledge-base.h" | ||
| 2 | #include <stdio.h> | ||
| 3 | |||
| 4 | #define QUESTION(symbol, label, yes, no) \ | ||
| 5 | [QUESTION_##symbol] = label, | ||
| 6 | char *question_labels[QUESTION_COUNT] = { | ||
| 7 | QUESTIONS | ||
| 8 | }; | ||
| 9 | #undef QUESTION | ||
| 10 | |||
| 11 | #define QUESTION(symbol, label, yes, no) \ | ||
| 12 | [QUESTION_##symbol] = { yes, no }, | ||
| 13 | struct question_branch_labels question_branch_labels[QUESTION_COUNT] = { | ||
| 14 | QUESTIONS | ||
| 15 | }; | ||
| 16 | #undef QUESTION | ||
| 17 | |||
| 18 | #define DECISION(symbol, label, ...) [DECISION_##symbol] = label, | ||
| 19 | char *decision_labels[DECISION_COUNT] = { | ||
| 20 | DECISIONS | ||
| 21 | }; | ||
| 22 | #undef DECISION | ||
| 23 | |||
| 24 | #define DECISION(symbol, label, ...) [DECISION_##symbol] = { __VA_ARGS__ }, | ||
| 25 | struct decision_predicates decision_predicates[DECISION_COUNT] = { | ||
| 26 | DECISIONS | ||
| 27 | }; | ||
| 28 | #undef DECISION | ||
| 29 | |||
| 30 | #define QUESTION(symbol, label, yes, no) \ | ||
| 31 | if (QUESTION_##symbol == question) \ | ||
| 32 | return decision_predicates[decision].symbol; | ||
| 33 | enum answer | ||
| 34 | get_decision_predicate (enum decision decision, enum question question) | ||
| 35 | { | ||
| 36 | QUESTIONS | ||
| 37 | |||
| 38 | return ANSWER_MALFORMED; | ||
| 39 | } | ||
| 40 | #undef QUESTION | ||
| 41 | |||
| 42 | void | ||
| 43 | print_knowledge_base (void) | ||
| 44 | { | ||
| 45 | puts ("QUESTIONS:"); | ||
| 46 | for (int i = 0; i < QUESTION_COUNT; ++i) | ||
| 47 | printf ("%2X", i + 1); | ||
| 48 | putchar ('\n'); | ||
| 49 | for (int i = 0; i < QUESTION_COUNT; ++i) { | ||
| 50 | printf ("%0*b: %s\n", 2*QUESTION_COUNT, | ||
| 51 | (1 << ANSWER_COUNT) - 1 << (QUESTION_COUNT - 1 - i)*ANSWER_COUNT, question_labels[i]); | ||
| 52 | } putchar ('\n'); | ||
| 53 | |||
| 54 | puts ("DECISIONS:"); | ||
| 55 | for (int i = 0; i < QUESTION_COUNT; ++i) | ||
| 56 | printf ("%2X", i + 1); | ||
| 57 | putchar ('\n'); | ||
| 58 | for (int i = 0; i < DECISION_COUNT; ++i) { | ||
| 59 | for (int j = 0; j < QUESTION_COUNT; ++j) { | ||
| 60 | printf ("%0*b", ANSWER_COUNT, get_decision_predicate (i, j)); | ||
| 61 | } | ||
| 62 | printf (": %s\n", decision_labels[i]); | ||
| 63 | } | ||
| 64 | } | ||
diff --git a/src/knowledge-base.h b/src/knowledge-base.h new file mode 100644 index 0000000..1d31a9a --- /dev/null +++ b/src/knowledge-base.h | |||
| @@ -0,0 +1,171 @@ | |||
| 1 | #ifndef INCLUDED_KNOWLEDGE_BASE_H | ||
| 2 | #define INCLUDED_KNOWLEDGE_BASE_H | ||
| 3 | |||
| 4 | #include <stdlib.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdbool.h> | ||
| 7 | #include <stdint.h> | ||
| 8 | |||
| 9 | #define QUESTIONS \ | ||
| 10 | QUESTION(advanced, "Вы опытный пользователь?", "Опытный", "Неопытный" ) \ | ||
| 11 | QUESTION(confidentiality, "Вы нуждаетесь в максимальной конфиденциальности?", "Нуждаюсь", "Не нуждаюсь" ) \ | ||
| 12 | QUESTION(usb, "Вы хотите установить систему на переносной накопитель?", "Хочу", "Не хочу" ) \ | ||
| 13 | QUESTION(control, "Вы хотите управлять сборкой пакетов?", "Хочу", "Не хочу" ) \ | ||
| 14 | QUESTION(hacking, "Вы собираетесь использовать данную систему для взлома?", "Собираюсь", "Не собираюсь") \ | ||
| 15 | QUESTION(multilang, "Вам нужна поддержка нескольких языков?", "Нужна", "Не нужна" ) \ | ||
| 16 | QUESTION(declarative, "Вы хотите настраивать систему декларативно?", "Хочу", "Не хочу" ) \ | ||
| 17 | QUESTION(stability, "Вас интересуют фиксированные или вращающиеся выпуски изменении пакетов?", "Фиксированные", "Вращающиеся" ) \ | ||
| 18 | QUESTION(lightweight, "Вам важно чтобы дистрибутив весил мало?", "Важно", "Не важно" ) \ | ||
| 19 | QUESTION(scheme, "Вы хотите описывать настройки на схеме GNU Guile?", "Хочу", "Не хочу" ) \ | ||
| 20 | QUESTION(games, "Вы собираетесь играть в компьютерные игры?", "Собираюсь", "Не собираюсь") | ||
| 21 | |||
| 22 | #define DECISIONS \ | ||
| 23 | DECISION(TAILS, "Tails", \ | ||
| 24 | .advanced = ANSWER_YES, \ | ||
| 25 | .confidentiality = ANSWER_YES, \ | ||
| 26 | .usb = ANSWER_YES, \ | ||
| 27 | .multilang = ANSWER_YES, \ | ||
| 28 | ) \ | ||
| 29 | DECISION(WHONIX, "Whonix", \ | ||
| 30 | .advanced = ANSWER_YES, \ | ||
| 31 | .confidentiality = ANSWER_YES, \ | ||
| 32 | .usb = ANSWER_YES, \ | ||
| 33 | .multilang = ANSWER_NO, \ | ||
| 34 | ) \ | ||
| 35 | DECISION(QUBES_OS, "Qubes OS", \ | ||
| 36 | .advanced = ANSWER_YES, \ | ||
| 37 | .confidentiality = ANSWER_YES, \ | ||
| 38 | .usb = ANSWER_NO, \ | ||
| 39 | ) \ | ||
| 40 | DECISION(GNU_GUIX, "GNU Guix", \ | ||
| 41 | .advanced = ANSWER_YES, \ | ||
| 42 | .confidentiality = ANSWER_NO, \ | ||
| 43 | .control = ANSWER_YES, \ | ||
| 44 | .declarative = ANSWER_YES, \ | ||
| 45 | .scheme = ANSWER_YES, \ | ||
| 46 | ) \ | ||
| 47 | DECISION(NIX_OS, "Nix OS", \ | ||
| 48 | .advanced = ANSWER_YES, \ | ||
| 49 | .confidentiality = ANSWER_NO, \ | ||
| 50 | .control = ANSWER_YES, \ | ||
| 51 | .declarative = ANSWER_YES, \ | ||
| 52 | .scheme = ANSWER_NO, \ | ||
| 53 | ) \ | ||
| 54 | DECISION(GENTOO, "Gentoo", \ | ||
| 55 | .advanced = ANSWER_YES, \ | ||
| 56 | .confidentiality = ANSWER_NO, \ | ||
| 57 | .control = ANSWER_YES, \ | ||
| 58 | .declarative = ANSWER_NO, \ | ||
| 59 | ) \ | ||
| 60 | DECISION(DEBIAN, "Debian", \ | ||
| 61 | .advanced = ANSWER_YES, \ | ||
| 62 | .confidentiality = ANSWER_NO, \ | ||
| 63 | .control = ANSWER_NO, \ | ||
| 64 | .stability = ANSWER_YES, \ | ||
| 65 | .lightweight = ANSWER_YES, \ | ||
| 66 | ) \ | ||
| 67 | DECISION(ROCKY_LINUX, "Rocky Linux", \ | ||
| 68 | .advanced = ANSWER_YES, \ | ||
| 69 | .confidentiality = ANSWER_NO, \ | ||
| 70 | .control = ANSWER_NO, \ | ||
| 71 | .stability = ANSWER_YES, \ | ||
| 72 | .lightweight = ANSWER_NO, \ | ||
| 73 | ) \ | ||
| 74 | DECISION(VOID_LINUX, "Void Linux", \ | ||
| 75 | .advanced = ANSWER_YES, \ | ||
| 76 | .confidentiality = ANSWER_NO, \ | ||
| 77 | .control = ANSWER_NO, \ | ||
| 78 | .stability = ANSWER_NO, \ | ||
| 79 | .lightweight = ANSWER_YES, \ | ||
| 80 | ) \ | ||
| 81 | DECISION(ARCH_LINUX, "Arch Linux", \ | ||
| 82 | .advanced = ANSWER_YES, \ | ||
| 83 | .confidentiality = ANSWER_NO, \ | ||
| 84 | .control = ANSWER_NO, \ | ||
| 85 | .stability = ANSWER_NO, \ | ||
| 86 | .lightweight = ANSWER_NO, \ | ||
| 87 | ) \ | ||
| 88 | DECISION(KALI_LINUX, "Kali Linux", \ | ||
| 89 | .advanced = ANSWER_NO, \ | ||
| 90 | .usb = ANSWER_YES, \ | ||
| 91 | .hacking = ANSWER_YES, \ | ||
| 92 | ) \ | ||
| 93 | DECISION(PUPPY_LINUX, "Puppy Linux", \ | ||
| 94 | .advanced = ANSWER_NO, \ | ||
| 95 | .usb = ANSWER_YES, \ | ||
| 96 | .hacking = ANSWER_NO, \ | ||
| 97 | ) \ | ||
| 98 | DECISION(POP_OS, "Pop! OS", \ | ||
| 99 | .advanced = ANSWER_NO, \ | ||
| 100 | .usb = ANSWER_NO, \ | ||
| 101 | .stability = ANSWER_YES, \ | ||
| 102 | .lightweight = ANSWER_NO, \ | ||
| 103 | .games = ANSWER_YES, \ | ||
| 104 | ) \ | ||
| 105 | DECISION(FEDORA, "Fedora", \ | ||
| 106 | .advanced = ANSWER_NO, \ | ||
| 107 | .usb = ANSWER_NO, \ | ||
| 108 | .stability = ANSWER_YES, \ | ||
| 109 | .lightweight = ANSWER_NO, \ | ||
| 110 | .games = ANSWER_NO, \ | ||
| 111 | ) \ | ||
| 112 | DECISION(MAJARO, "Majaro", \ | ||
| 113 | .advanced = ANSWER_NO, \ | ||
| 114 | .usb = ANSWER_NO, \ | ||
| 115 | .stability = ANSWER_NO, \ | ||
| 116 | ) | ||
| 117 | |||
| 118 | enum answer { | ||
| 119 | ANSWER_UNKNOWN, | ||
| 120 | ANSWER_YES, | ||
| 121 | ANSWER_NO, | ||
| 122 | ANSWER_MALFORMED, | ||
| 123 | }; | ||
| 124 | |||
| 125 | #define ANSWER_COUNT 2 | ||
| 126 | |||
| 127 | #define QUESTION(symbol, label, yes, no) \ | ||
| 128 | QUESTION_##symbol, | ||
| 129 | enum question { | ||
| 130 | QUESTIONS | ||
| 131 | QUESTION_COUNT | ||
| 132 | }; | ||
| 133 | #undef QUESTION | ||
| 134 | |||
| 135 | #define QUESTION(symbol, label, yes, no) \ | ||
| 136 | PREDICATE_Y_##symbol = 1 << __COUNTER__, \ | ||
| 137 | PREDICATE_N_##symbol = 1 << __COUNTER__, | ||
| 138 | enum predicate { | ||
| 139 | QUESTIONS | ||
| 140 | }; | ||
| 141 | #undef QUESTION | ||
| 142 | |||
| 143 | #define DECISION(symbol, label, ...) DECISION_##symbol, | ||
| 144 | enum decision { | ||
| 145 | DECISIONS | ||
| 146 | DECISION_COUNT | ||
| 147 | }; | ||
| 148 | #undef DECISION | ||
| 149 | |||
| 150 | struct question_branch_labels { | ||
| 151 | char *answer_yes; | ||
| 152 | char *answer_no; | ||
| 153 | }; | ||
| 154 | |||
| 155 | #define QUESTION(symbol, label, yes, no) \ | ||
| 156 | enum answer symbol : ANSWER_COUNT; | ||
| 157 | struct decision_predicates { | ||
| 158 | QUESTIONS; | ||
| 159 | }; | ||
| 160 | #undef QUESTION | ||
| 161 | |||
| 162 | extern char *question_labels[QUESTION_COUNT]; | ||
| 163 | extern struct question_branch_labels question_branch_labels[QUESTION_COUNT]; | ||
| 164 | extern char *decision_labels[DECISION_COUNT]; | ||
| 165 | extern struct decision_predicates decision_predicates[DECISION_COUNT]; | ||
| 166 | |||
| 167 | extern void print_decision_predicates (struct decision_predicates); | ||
| 168 | extern void print_knowledge_base (void); | ||
| 169 | extern enum answer get_decision_predicate (enum decision, enum question); | ||
| 170 | |||
| 171 | #endif | ||
diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..d9ae7dc --- /dev/null +++ b/src/utils.h | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | #ifndef INCLUDED_UTILS_H | ||
| 2 | #define INCLUDED_UTILS_H | ||
| 3 | |||
| 4 | #include <assert.h> | ||
| 5 | #include <stdlib.h> | ||
| 6 | |||
| 7 | #define MALLOC malloc | ||
| 8 | #define REALLOC realloc | ||
| 9 | #define FREE free | ||
| 10 | #define ASSERT assert | ||
| 11 | |||
| 12 | #define PRINT_EXP(EXP, FMT) printf (#EXP" = "FMT"\n", EXP) | ||
| 13 | |||
| 14 | #endif | ||
