# gcpdev/xpshacl architecture

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

- Interactive diagram: https://gitdiagram.com/gcpdev/xpshacl
- Repository: https://github.com/gcpdev/xpshacl

## Overview

##### Main Components
- Justification Tree Builder – Builds logical justification trees (module: justification_tree_builder.py) when violations occur.
- Violation Knowledge Graph (KG) – A persistent cache that stores violations’ signatures along with their explanations (module: violation_kg.py and violation_signature related modules).
- Explanation Generator – Uses a retrieval-augmented generation approach and large language models to produce natural language explanations (module: explanation_generator.py). It interacts with external LLM APIs (or local models via Ollama, as mentioned in the README).
- Main Entry Point – The main.py under the src/ directory ties these modules together by handling inputs and orchestrating the overall validation-explanation workflow.

##### Relationships and Data Flow
– “No Violation” leading directly to a Success Report.
– “Violation Details” flowing to both the Justification Tree Builder and the Context Retriever.
– “Violation Signature” being sent to the Violation KG for caching.
- How the Violation KG:
– Immediately returns an explanation if there’s a cache hit.
– Triggers the Explanation Generator if there’s a cache miss.
- That both the output of the Justification Tree Builder and the Context Retriever flow into the Explanation Generator.

##### Architectural Patterns and Design Principles
- Modularity, with each component (e.g., validation, justification tree construction, context retrieval, caching, and explanation generation) encapsulated in its own Python module.
- Use of a caching mechanism through the Violation KG that improves performance and ensures consistency.
- Integration of external services (different LLM APIs and possibly local LLMs via Ollama) which means that the Explanation Generator must support multiple LLM integrations.

##### Relevant Technologies and Frameworks
- Python (3.7+), as the core language.
- rdflib for RDF graph manipulation (especially in the synthetic data generator).
- SHACL for data validation.
- LLM APIs (e.g., OpenAI, Google Gemini, Anthropic’s Claude) as well as optional local processing via Ollama.
- Unit testing framework for the tests/ directory code.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Inputs
    subgraph "Inputs"
        A["RDF Data & SHACL Shapes"]:::input
    end

    %% xpSHACL System
    subgraph "xpSHACL System"
        G["Orchestration (main.py)"]:::system
        B["Extended SHACL Validator"]:::system
        C["Justification Tree Builder"]:::system
        D["Context Retriever"]:::system
        E["Violation Knowledge Graph"]:::system
        E2["Violation Signature Module"]:::system
        E3["Violation Signature Factory"]:::system
        F["Explanation Generator"]:::system
        I["External LLMs"]:::external
    end

    %% Outputs
    subgraph "Outputs"
        H["Success/Explanation Output"]:::output
    end

    %% Data Flow
    G -->|"initiates"| A
    A -->|"provides_data"| B
    G -->|"orchestrates_validation"| B
    B -->|"NoViolation"| H
    B -->|"ViolationDetails"| C
    B -->|"ViolationDetails"| D
    B -->|"ViolationSignature"| E
    E -->|"includes"| E2
    E -->|"includes"| E3
    E -->|"cache_miss_triggers"| F
    C -->|"JustificationTree"| F
    D -->|"RetrievedContext"| F
    F -->|"GeneratedExplanation"| H
    F -->|"LLMIntegration"| I

    %% Click Events
    click A "https://github.com/gcpdev/xpshacl/tree/main/data/"
    click B "https://github.com/gcpdev/xpshacl/blob/main/src/extended_shacl_validator.py"
    click C "https://github.com/gcpdev/xpshacl/blob/main/src/justification_tree_builder.py"
    click D "https://github.com/gcpdev/xpshacl/blob/main/src/context_retriever.py"
    click E "https://github.com/gcpdev/xpshacl/blob/main/src/violation_kg.py"
    click E2 "https://github.com/gcpdev/xpshacl/blob/main/src/violation_signature.py"
    click E3 "https://github.com/gcpdev/xpshacl/blob/main/src/violation_signature_factory.py"
    click F "https://github.com/gcpdev/xpshacl/blob/main/src/explanation_generator.py"
    click G "https://github.com/gcpdev/xpshacl/blob/main/src/main.py"

    %% Style Definitions
    classDef input fill:#a8dadc,stroke:#457b9d,stroke-width:2px;
    classDef system fill:#f1faee,stroke:#1d3557,stroke-width:2px;
    classDef output fill:#ffb703,stroke:#fb8500,stroke-width:2px;
    classDef external fill:#e63946,stroke:#a4161a,stroke-width:2px;
```
