From 04f8b646c1c8a2435931bea9d1acffdde138f81e Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sat, 7 Nov 2026 08:06:01 +0300 Subject: publish --- src/dynamic-array.h | 56 ++++++++++++ src/expert-system.c | 21 +++++ src/inference-engine.c | 244 +++++++++++++++++++++++++++++++++++++++++++++++++ src/inference-engine.h | 10 ++ src/knowledge-base.c | 64 +++++++++++++ src/knowledge-base.h | 171 ++++++++++++++++++++++++++++++++++ src/utils.h | 14 +++ 7 files changed, 580 insertions(+) create mode 100644 src/dynamic-array.h create mode 100644 src/expert-system.c create mode 100644 src/inference-engine.c create mode 100644 src/inference-engine.h create mode 100644 src/knowledge-base.c create mode 100644 src/knowledge-base.h create mode 100644 src/utils.h (limited to 'src') 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 @@ +#ifndef INCLUDED_DYNAMIC_ARRAY_H +#define INCLUDED_DYNAMIC_ARRAY_H + +#include "utils.h" + +#ifndef DYNAMIC_ARRAY_INIT_CAP +#define DYNAMIC_ARRAY_INIT_CAP 256 +#endif + +#define DYNAMIC_ARRAY_ENSURE_CAPACITY(DYNAMIC_ARRAY, COUNT) \ + ({ \ + if ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) { \ + if ((DYNAMIC_ARRAY).capacity == 0) \ + (DYNAMIC_ARRAY).capacity = DYNAMIC_ARRAY_INIT_CAP; \ + while ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) \ + (DYNAMIC_ARRAY).capacity *= 2; \ + (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ + (DYNAMIC_ARRAY).capacity * \ + sizeof (*(DYNAMIC_ARRAY).items)); \ + ASSERT((DYNAMIC_ARRAY).items); \ + } \ + }) + +#define DYNAMIC_ARRAY_SHRINK(DYNAMIC_ARRAY) \ + ({ \ + (DYNAMIC_ARRAY).capacity = (DYNAMIC_ARRAY).count; \ + (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ + (DYNAMIC_ARRAY).capacity * \ + sizeof (*(DYNAMIC_ARRAY).items)); \ + ASSERT((DYNAMIC_ARRAY).items); \ + \ + }) + +#define DYNAMIC_ARRAY_APPEND(DYNAMIC_ARRAY, ITEM) \ + ({ \ + DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), 1); \ + (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count++] = (ITEM); \ + }) + +#define DYNAMIC_ARRAY_APPEND_MANY(DYNAMIC_ARRAY, NEW_ITEMS, NEW_ITEMS_COUNT) \ + ({ \ + DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), (NEW_ITEMS_COUNT)); \ + memcpy ((DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count, (NEW_ITEMS), \ + (NEW_ITEMS_COUNT)*sizeof (*(DYNAMIC_ARRAY).items)); \ + (DYNAMIC_ARRAY).count += (NEW_ITEMS_COUNT); \ + }) + +#define DYNAMIC_ARRAY_RESET(DYNAMIC_ARRAY) ({ (DYNAMIC_ARRAY).count = 0; }) + +#define DYNAMIC_ARRAY_FREE(DYNAMIC_ARRAY) FREE((DYNAMIC_ARRAY).items) + +#define DYNAMIC_ARRAY_FOREACH(ITEM, DYNAMIC_ARRAY) for ( \ + typeof ((DYNAMIC_ARRAY).items) ITEM = (DYNAMIC_ARRAY).items; \ + ITEM < (DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count; ++ITEM) + +#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 @@ +#include +#include +#include +#include + +#include "knowledge-base.h" +#include "inference-engine.h" + +int +main (void) +{ + // TODO(cmmm): auto-rebuild + print_knowledge_base (); + putchar ('\n'); + build_decision_tree (); + write_decision_tree_in_dot_format (); + putchar ('\n'); + question_user (); + + return EXIT_SUCCESS; +} 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 @@ +#include "inference-engine.h" +#include +#include "utils.h" +#include "dynamic-array.h" + +typedef struct decision_tree_node { + union { + enum question question; + enum decision decision; + }; + uint32_t branch_yes; + uint32_t branch_no; +} *DECISION_TREE_NODE; + +struct decision_tree { + struct decision_tree_node *items; + uint32_t count; + uint32_t capacity; +}; + +#define MAX_DECISION_TREE_NODE_COUNT ((1 << QUESTION_COUNT) - 1) + +static struct decision_tree decision_tree = {0}; + +static int answers_known [QUESTION_COUNT] = {0}; +static int question_queue [QUESTION_COUNT] = {0}; + +static inline bool +is_answer_known (enum decision decision, enum question question) { + switch (get_decision_predicate (decision, question)) { + case ANSWER_YES: + case ANSWER_NO: + return true; + default: + return false; + } +} + +static void +build_decision_tree_impl (enum decision *begin, + enum decision *end, int order, + DECISION_TREE_NODE node) +{ + if (begin == end) + return; + + if (begin + 1 == end) { + // printf ("INSERT DECISION %X\n", *begin); + node->decision = *begin; + return; + } + + enum question question; + for (; order < QUESTION_COUNT; ++order) { + question = question_queue[order]; + + enum decision *decision; + for (decision = begin; decision < end; ++decision) { + if (!is_answer_known (*decision, question)) + break; + } if (decision == end) break; + } ASSERT (order != QUESTION_COUNT && "NOT ENOUGH KNOWLEDGE"); + + // printf ("INSERT QUESTION %X\n", question); + node->question = question; + + enum decision *pivot = begin; + for (enum decision *decision = begin; decision < end; ++decision) { + if (get_decision_predicate (*decision, question) == ANSWER_NO) { + pivot = decision; + break; + } + } + + for (enum decision *head = pivot + 1; head < end; ++head) { + if (get_decision_predicate (*head, question) == ANSWER_YES) { + enum decision tmp = *pivot; + *pivot++ = *head; + *head = tmp; + } + } + + // for (enum decision *decision = begin; decision < end; ++decision) { + // printf ("%s%X\n", decision == pivot ? "->" : " ", *decision + 1); + // } + + DYNAMIC_ARRAY_APPEND (decision_tree, (struct decision_tree_node) {0}); + // printf ("BRANCH YES TO %X\n", decision_tree.count - 1); + node->branch_yes = decision_tree.count - 1; + build_decision_tree_impl (begin, pivot, order + 1, + &decision_tree.items[decision_tree.count - 1]); + DYNAMIC_ARRAY_APPEND (decision_tree, (struct decision_tree_node) {0}); + // printf ("BRANCH NO TO %X\n", decision_tree.count - 1); + node->branch_no = decision_tree.count - 1; + build_decision_tree_impl (pivot, end, order + 1, + &decision_tree.items[decision_tree.count - 1]); +} + +void +build_decision_tree (void) +{ + // compute answers_known[question] and question_order[order] + for (int question = 0; question < QUESTION_COUNT; ++question) { + for (int decision = 0; decision < DECISION_COUNT; ++decision) + answers_known[question] += is_answer_known (decision, question); + + int order = 0; + while (order < question && + answers_known[question] <= + answers_known[question_queue[order]]) { + order += 1; + } + + memmove (&question_queue[order + 1], + &question_queue[order + 0], + (question - order)*sizeof (*question_queue)); + question_queue[order] = question; + } + + // initialize context and run recursive function on it + enum decision decisions[DECISION_COUNT]; + for (int decision = 0; decision < DECISION_COUNT; ++decision) + decisions[decision] = DECISION_COUNT - 1 - decision; + DYNAMIC_ARRAY_ENSURE_CAPACITY (decision_tree, MAX_DECISION_TREE_NODE_COUNT); + DYNAMIC_ARRAY_APPEND (decision_tree, (struct decision_tree_node) {0}); + build_decision_tree_impl (decisions, decisions + DECISION_COUNT, 0, + &decision_tree.items[0]); + DYNAMIC_ARRAY_SHRINK (decision_tree); +} + +static void +write_decision_tree_in_dot_format_impl (FILE *file, DECISION_TREE_NODE node) +{ + uint32_t node_id = node - decision_tree.items; + + if (node->branch_yes && node->branch_no) { + fprintf (file, "\tn%u [label=\"%u: %s\"]\n", + node_id, node_id + 1, question_labels[node->question]); + + write_decision_tree_in_dot_format_impl ( + file, &decision_tree.items[node->branch_yes]); + uint32_t branch_yes_id = &decision_tree.items[node->branch_yes] - decision_tree.items; + fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", + node_id, branch_yes_id, question_branch_labels[node->question].answer_yes); + + write_decision_tree_in_dot_format_impl (file, &decision_tree.items[node->branch_no]); + uint32_t branch_no_id = &decision_tree.items[node->branch_no] - decision_tree.items; + fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", node_id, branch_no_id, question_branch_labels[node->question].answer_no); + } else { + fprintf (file, "\tn%u [label=\"%u: %s\"]\n", node_id, node_id + 1, decision_labels[node->decision]); + } +} + +struct decision_tree_node_stack { + struct decision_tree_node **items; + uint32_t count; + uint32_t capacity; +}; + +void +write_decision_tree_in_dot_format (void) +{ + FILE *file = fopen ("decision-tree.dot", "w"); + if (file == NULL) { + fprintf (stderr, "Error opening file!\n"); + return; + } + + fprintf (file, "digraph {\n"); + fprintf (file, "\trankdir = \"LR\";\n"); + // write_decision_tree_in_dot_format_impl (file, &decision_tree.items[0]); + + uint32_t node_visual_id = 0; + struct decision_tree_node_stack stack1 = {0}; + struct decision_tree_node_stack stack2 = {0}; + struct decision_tree_node_stack *front_stack = &stack1; + struct decision_tree_node_stack *back_stack = &stack2; + DYNAMIC_ARRAY_APPEND (*front_stack, &decision_tree.items[0]); + while (front_stack->count) { + DYNAMIC_ARRAY_RESET (*back_stack); + DYNAMIC_ARRAY_FOREACH (n, *front_stack) { + DECISION_TREE_NODE node = *n; + uint32_t node_id = node - decision_tree.items; + + if (node->branch_yes && node->branch_no) { + fprintf (file, "\tn%u [label=\"%u: %s\"]\n", node_id, ++node_visual_id, question_labels[node->question]); + + DYNAMIC_ARRAY_APPEND (*back_stack, &decision_tree.items[node->branch_yes]); + uint32_t branch_yes_id = &decision_tree.items[node->branch_yes] - decision_tree.items; + fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", node_id, branch_yes_id, question_branch_labels[node->question].answer_yes); + + DYNAMIC_ARRAY_APPEND (*back_stack, &decision_tree.items[node->branch_no]); + uint32_t branch_no_id = &decision_tree.items[node->branch_no] - decision_tree.items; + fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", node_id, branch_no_id, question_branch_labels[node->question].answer_no); + } else { + fprintf (file, "\tn%u [label=\"%u: %s\"]\n", node_id, ++node_visual_id, decision_labels[node->decision]); + } + } + + struct decision_tree_node_stack *tmp = front_stack; + front_stack = back_stack; + back_stack = tmp; + } + fprintf (file, "}\n"); + + fclose (file); + + puts ("decision-tree.dot:0:0: Decision tree is ready for viewing."); +} + +void +question_user (void) +{ + char *line = NULL; + size_t line_size = 0; + struct decision_tree_node *node = &decision_tree.items[0]; + while (node->branch_yes && node->branch_no) { + enum question question = node->question; + printf ("%s\n", question_labels[question]); + printf (" [1] %s\n", question_branch_labels[question].answer_yes); + printf (" [2] %s\n", question_branch_labels[question].answer_no); + + enum answer answer = 0; + for (;;) { + printf ("> "); + getline (&line, &line_size, stdin); + sscanf (line, "%d", &answer); + if (answer < 1 || answer > 2) + continue; + + switch (answer) { + case ANSWER_YES: + node = &decision_tree.items[node->branch_yes]; + break; + case ANSWER_NO: + node = &decision_tree.items[node->branch_no]; + break; + default: break; + } break; + } + } + free (line); + printf ("Рекомендация: %s\n", decision_labels[node->decision]); +} 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 @@ +#ifndef INCLUDED_INFERENCE_ENGINE_H +#define INCLUDED_INFERENCE_ENGINE_H + +#include "knowledge-base.h" + +void build_decision_tree (void); +void write_decision_tree_in_dot_format (void); +void question_user (void); + +#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 @@ +#include "knowledge-base.h" +#include + +#define QUESTION(symbol, label, yes, no) \ +[QUESTION_##symbol] = label, +char *question_labels[QUESTION_COUNT] = { + QUESTIONS +}; +#undef QUESTION + +#define QUESTION(symbol, label, yes, no) \ +[QUESTION_##symbol] = { yes, no }, +struct question_branch_labels question_branch_labels[QUESTION_COUNT] = { + QUESTIONS +}; +#undef QUESTION + +#define DECISION(symbol, label, ...) [DECISION_##symbol] = label, +char *decision_labels[DECISION_COUNT] = { + DECISIONS +}; +#undef DECISION + +#define DECISION(symbol, label, ...) [DECISION_##symbol] = { __VA_ARGS__ }, +struct decision_predicates decision_predicates[DECISION_COUNT] = { + DECISIONS +}; +#undef DECISION + +#define QUESTION(symbol, label, yes, no) \ + if (QUESTION_##symbol == question) \ + return decision_predicates[decision].symbol; +enum answer +get_decision_predicate (enum decision decision, enum question question) +{ + QUESTIONS + + return ANSWER_MALFORMED; +} +#undef QUESTION + +void +print_knowledge_base (void) +{ + puts ("QUESTIONS:"); + for (int i = 0; i < QUESTION_COUNT; ++i) + printf ("%2X", i + 1); + putchar ('\n'); + for (int i = 0; i < QUESTION_COUNT; ++i) { + printf ("%0*b: %s\n", 2*QUESTION_COUNT, + (1 << ANSWER_COUNT) - 1 << (QUESTION_COUNT - 1 - i)*ANSWER_COUNT, question_labels[i]); + } putchar ('\n'); + + puts ("DECISIONS:"); + for (int i = 0; i < QUESTION_COUNT; ++i) + printf ("%2X", i + 1); + putchar ('\n'); + for (int i = 0; i < DECISION_COUNT; ++i) { + for (int j = 0; j < QUESTION_COUNT; ++j) { + printf ("%0*b", ANSWER_COUNT, get_decision_predicate (i, j)); + } + printf (": %s\n", decision_labels[i]); + } +} 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 @@ +#ifndef INCLUDED_KNOWLEDGE_BASE_H +#define INCLUDED_KNOWLEDGE_BASE_H + +#include +#include +#include +#include + +#define QUESTIONS \ + QUESTION(advanced, "Вы опытный пользователь?", "Опытный", "Неопытный" ) \ + QUESTION(confidentiality, "Вы нуждаетесь в максимальной конфиденциальности?", "Нуждаюсь", "Не нуждаюсь" ) \ + QUESTION(usb, "Вы хотите установить систему на переносной накопитель?", "Хочу", "Не хочу" ) \ + QUESTION(control, "Вы хотите управлять сборкой пакетов?", "Хочу", "Не хочу" ) \ + QUESTION(hacking, "Вы собираетесь использовать данную систему для взлома?", "Собираюсь", "Не собираюсь") \ + QUESTION(multilang, "Вам нужна поддержка нескольких языков?", "Нужна", "Не нужна" ) \ + QUESTION(declarative, "Вы хотите настраивать систему декларативно?", "Хочу", "Не хочу" ) \ + QUESTION(stability, "Вас интересуют фиксированные или вращающиеся выпуски изменении пакетов?", "Фиксированные", "Вращающиеся" ) \ + QUESTION(lightweight, "Вам важно чтобы дистрибутив весил мало?", "Важно", "Не важно" ) \ + QUESTION(scheme, "Вы хотите описывать настройки на схеме GNU Guile?", "Хочу", "Не хочу" ) \ + QUESTION(games, "Вы собираетесь играть в компьютерные игры?", "Собираюсь", "Не собираюсь") + +#define DECISIONS \ + DECISION(TAILS, "Tails", \ + .advanced = ANSWER_YES, \ + .confidentiality = ANSWER_YES, \ + .usb = ANSWER_YES, \ + .multilang = ANSWER_YES, \ + ) \ + DECISION(WHONIX, "Whonix", \ + .advanced = ANSWER_YES, \ + .confidentiality = ANSWER_YES, \ + .usb = ANSWER_YES, \ + .multilang = ANSWER_NO, \ + ) \ + DECISION(QUBES_OS, "Qubes OS", \ + .advanced = ANSWER_YES, \ + .confidentiality = ANSWER_YES, \ + .usb = ANSWER_NO, \ + ) \ + DECISION(GNU_GUIX, "GNU Guix", \ + .advanced = ANSWER_YES, \ + .confidentiality = ANSWER_NO, \ + .control = ANSWER_YES, \ + .declarative = ANSWER_YES, \ + .scheme = ANSWER_YES, \ + ) \ + DECISION(NIX_OS, "Nix OS", \ + .advanced = ANSWER_YES, \ + .confidentiality = ANSWER_NO, \ + .control = ANSWER_YES, \ + .declarative = ANSWER_YES, \ + .scheme = ANSWER_NO, \ + ) \ + DECISION(GENTOO, "Gentoo", \ + .advanced = ANSWER_YES, \ + .confidentiality = ANSWER_NO, \ + .control = ANSWER_YES, \ + .declarative = ANSWER_NO, \ + ) \ + DECISION(DEBIAN, "Debian", \ + .advanced = ANSWER_YES, \ + .confidentiality = ANSWER_NO, \ + .control = ANSWER_NO, \ + .stability = ANSWER_YES, \ + .lightweight = ANSWER_YES, \ + ) \ + DECISION(ROCKY_LINUX, "Rocky Linux", \ + .advanced = ANSWER_YES, \ + .confidentiality = ANSWER_NO, \ + .control = ANSWER_NO, \ + .stability = ANSWER_YES, \ + .lightweight = ANSWER_NO, \ + ) \ + DECISION(VOID_LINUX, "Void Linux", \ + .advanced = ANSWER_YES, \ + .confidentiality = ANSWER_NO, \ + .control = ANSWER_NO, \ + .stability = ANSWER_NO, \ + .lightweight = ANSWER_YES, \ + ) \ + DECISION(ARCH_LINUX, "Arch Linux", \ + .advanced = ANSWER_YES, \ + .confidentiality = ANSWER_NO, \ + .control = ANSWER_NO, \ + .stability = ANSWER_NO, \ + .lightweight = ANSWER_NO, \ + ) \ + DECISION(KALI_LINUX, "Kali Linux", \ + .advanced = ANSWER_NO, \ + .usb = ANSWER_YES, \ + .hacking = ANSWER_YES, \ + ) \ + DECISION(PUPPY_LINUX, "Puppy Linux", \ + .advanced = ANSWER_NO, \ + .usb = ANSWER_YES, \ + .hacking = ANSWER_NO, \ + ) \ + DECISION(POP_OS, "Pop! OS", \ + .advanced = ANSWER_NO, \ + .usb = ANSWER_NO, \ + .stability = ANSWER_YES, \ + .lightweight = ANSWER_NO, \ + .games = ANSWER_YES, \ + ) \ + DECISION(FEDORA, "Fedora", \ + .advanced = ANSWER_NO, \ + .usb = ANSWER_NO, \ + .stability = ANSWER_YES, \ + .lightweight = ANSWER_NO, \ + .games = ANSWER_NO, \ + ) \ + DECISION(MAJARO, "Majaro", \ + .advanced = ANSWER_NO, \ + .usb = ANSWER_NO, \ + .stability = ANSWER_NO, \ + ) + +enum answer { + ANSWER_UNKNOWN, + ANSWER_YES, + ANSWER_NO, + ANSWER_MALFORMED, +}; + +#define ANSWER_COUNT 2 + +#define QUESTION(symbol, label, yes, no) \ + QUESTION_##symbol, +enum question { + QUESTIONS + QUESTION_COUNT +}; +#undef QUESTION + +#define QUESTION(symbol, label, yes, no) \ + PREDICATE_Y_##symbol = 1 << __COUNTER__, \ + PREDICATE_N_##symbol = 1 << __COUNTER__, +enum predicate { + QUESTIONS +}; +#undef QUESTION + +#define DECISION(symbol, label, ...) DECISION_##symbol, +enum decision { + DECISIONS + DECISION_COUNT +}; +#undef DECISION + +struct question_branch_labels { + char *answer_yes; + char *answer_no; +}; + +#define QUESTION(symbol, label, yes, no) \ + enum answer symbol : ANSWER_COUNT; +struct decision_predicates { + QUESTIONS; +}; +#undef QUESTION + +extern char *question_labels[QUESTION_COUNT]; +extern struct question_branch_labels question_branch_labels[QUESTION_COUNT]; +extern char *decision_labels[DECISION_COUNT]; +extern struct decision_predicates decision_predicates[DECISION_COUNT]; + +extern void print_decision_predicates (struct decision_predicates); +extern void print_knowledge_base (void); +extern enum answer get_decision_predicate (enum decision, enum question); + +#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 @@ +#ifndef INCLUDED_UTILS_H +#define INCLUDED_UTILS_H + +#include +#include + +#define MALLOC malloc +#define REALLOC realloc +#define FREE free +#define ASSERT assert + +#define PRINT_EXP(EXP, FMT) printf (#EXP" = "FMT"\n", EXP) + +#endif -- cgit v1.3.1