# deivisito2911/expert_systems architecture

> Architecture diagram of the GitHub repository deivisito2911/expert_systems, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/deivisito2911/expert_systems
- Repository: https://github.com/deivisito2911/expert_systems

## Overview

##### Project Type and Purpose
- This is a standalone Python “Expert System” application. Its goal is to load a knowledge base (JSON files), apply rule‐based inference, and identify the phylum of species (or any other domain provided via the JSON schema). It provides a command‐line and simple GUI/HTML interface for querying, adding rules, and updating the knowledge base.

##### Architectural Overview
- Layered, modular monolith:
a. Presentation Layer: CLI and simple HTML/Tkinter front end
b. Application/Logic Layer: The expert‐general engine, rule management, and action handlers
c. Data Layer: JSON‐based knowledge base files

##### Main Components
- **main.py** (Orchestrator)
- Initializes the application, loads configuration, and launches the UI/menu.
- **Presentation Layer**
- `src/cli.py` – Generic CLI scaffolding
- `src/interfaz/menu.py`, `consultarbase.py`, `insertarbase.py`, `guardarbase.py` – Menu options and user prompts
- `src/interfaz/presentacion.html` – Optional HTML/Tkinter template (requires customtkinter & pillow)
- **Application/Logic Layer**
- `src/experto_general/entry.py` – Entry point into the inference engine
- `src/experto_general/engine.py` – Core forward‐chaining/backward‐chaining logic
- `src/experto_general/base.py` – Knowledge base abstraction layer
- `src/experto_general/property.py`, `response.py` – Data structures for facts and results
- `src/acciones.py` – Domain‐specific action handlers (e.g., adding new rules or facts)
- **Data Layer**
- `src/Base_De_Conocimiento/Phylum.json` (and copies) – JSON files describing rules and facts
- `src/example.json` – Sample schema for user customization

##### Relationships and Data Flows
- **Startup**: `main.py` → loads JSON (Data Layer) → initializes Engine & Base (Application Layer)
- **User Interaction**: Presentation Layer (menu/CLI) → calls Engine via `entry.py`
- **Inference**: Engine reads facts/rules from Base → applies logic → returns `response` objects
- **Persistence**: If user adds or modifies rules/facts, Presentation Layer → calls `acciones.py` → updates JSON files

##### Architectural Patterns and Principles
- **Layered Architecture**: Clear separation (Presentation / Business Logic / Data)
- **Modularity**: Each package (`experto_general`, `interfaz`, `Base_De_Conocimiento`) encapsulates a concern
- **Extensibility**: New domains beyond “Phylum” can be added by supplying a JSON that follows `example.json` schema

##### Technologies & Frameworks
- Language: Python 3.x
- CLI: built-in `argparse` or custom in `cli.py`
- GUI: `customtkinter` + `pillow` (optional) rendering of `presentacion.html`
- Data Serialization: JSON

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Orchestrator
    main["main.py\nOrchestrator"]:::orchestrator

    %% Presentation Layer
    subgraph "Presentation Layer"
        CLI["cli.py\nCLI scaffolding"]:::presentation
        menu["menu.py\nMenu entry points"]:::presentation
        consultar["consultarbase.py\nQuery KB"]:::presentation
        insertar["insertarbase.py\nInsert facts/rules"]:::presentation
        guardar["guardarbase.py\nSave KB"]:::presentation
        html["presentacion.html\nHTML/Tkinter template"]:::presentation
    end

    %% Application/Logic Layer
    subgraph "Application/Logic Layer"
        entry["entry.py\nInference engine entry"]:::application
        engine["engine.py\nCore inference logic"]:::application
        base["base.py\nKnowledge base abstraction"]:::application
        property["property.py\nFact data structures"]:::application
        response["response.py\nResponse structures"]:::application
        acciones["acciones.py\nDomain-specific actions"]:::application
    end

    %% Data Layer
    subgraph "Data Layer"
        phylum["Phylum.json\nKB file"]:::data
        phylumcopy["Phylumcopy.json\nKB copy"]:::data
        example["example.json\nDomain schema"]:::data
    end

    %% Connections: Startup
    main -->|"launchUI() / loadConfig"| CLI
    main -->|"launchUI() / loadConfig"| menu
    main -->|"launchUI() / loadConfig"| html
    main -->|"initializeEngine()"| entry

    %% User Interaction
    CLI -->|"userRequest()"| entry
    menu -->|"userRequest()"| entry
    consultar -->|"query()"| entry
    insertar -->|"insert()"| entry
    guardar -->|"save()"| entry
    html -->|"guiRequest()"| entry

    %% Inference Flow
    entry -->|"infer()"| engine
    engine -->|"loadKnowledge() / getFacts()"| base
    base -->|"readJSON()"| phylum
    base -->|"readJSON()"| phylumcopy
    base -->|"readJSON()"| example
    engine -->|"createResponse()"| response
    entry -->|"returnResponse()"| response

    %% Presentation output
    response -->|"display()"| CLI
    response -->|"display()"| menu
    response -->|"display()"| html

    %% Persistence
    menu -->|"invokeActions()"| acciones
    insertar -->|"invokeActions()"| acciones
    guardar -->|"invokeActions()"| acciones
    acciones -->|"saveJSON()"| base
    base -->|"writeJSON()"| phylum
    base -->|"writeJSON()"| phylumcopy
    base -->|"writeJSON()"| example

    %% Click Events
    click main "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/main.py"
    click CLI "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/cli.py"
    click menu "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/interfaz/menu.py"
    click consultar "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/interfaz/consultarbase.py"
    click insertar "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/interfaz/insertarbase.py"
    click guardar "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/interfaz/guardarbase.py"
    click html "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/interfaz/presentacion.html"
    click entry "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/experto_general/entry.py"
    click engine "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/experto_general/engine.py"
    click base "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/experto_general/base.py"
    click property "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/experto_general/property.py"
    click response "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/experto_general/response.py"
    click acciones "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/acciones.py"
    click phylum "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/Base_De_Conocimiento/Phylum.json"
    click phylumcopy "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/Base_De_Conocimiento/Phylumcopy.json"
    click example "https://github.com/deivisito2911/expert_systems/blob/Main-UDO/src/example.json"

    %% Styles
    classDef orchestrator fill:#e2c8f7,stroke:#000,color:#000;
    classDef presentation fill:#cdeaff,stroke:#000,color:#000;
    classDef application fill:#c8f7c5,stroke:#000,color:#000;
    classDef data fill:#fff5b1,stroke:#000,color:#000;
```
