# statnett/cimsparql architecture

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

- Interactive diagram: https://gitdiagram.com/statnett/cimsparql
- Repository: https://github.com/statnett/cimsparql

## Overview

##### Main Components
- Client Configuration & Environment: The ServiceConfig setup and the ability to alter behavior using environment variables (e.g., SPARQL_REST_API) is central to how the package tailors connections to the external triple stores.
- SPARQL Query Engine:
– The src/cimsparql/sparql directory contains numerous SPARQL query templates. This component is responsible for maintaining these query definitions.
– The utility methods (like template_to_query) support dynamic query construction.
- Data Model & Query Execution:
– The modules under src/cimsparql (such as model.py, graphdb.py, data_models.py, value_mapper.py, and type_mapper.py) encapsulate how SPARQL queries are executed against the triple stores and how responses are converted into usable pandas dataframes.
– A “model” component that abstracts the interaction, as seen in the get_single_client_model usage example.
- Utility & Support Modules:
– Other helper modules (e.g., constants.py, utils.py, retry_cb.py) and the pre-configured packages under pkg_data (for configuration files like namespaces, native store template) streamline supporting operations.
- Testing & Documentation:
– The tests folder indicates extensive testing (unit tests covering various modules) and shows sample data files for test cases.
– The docs folder includes Sphinx documentation which provides further architectural details to developers.

##### Relationships and Data Flow
- Client Code → Model Layer → GraphDB (or other triple store) Connector → REST/SPARQL Query Engine.
- How the SPARQL query templates (located in src/cimsparql/sparql) feed into the model layer.
- Conversion step wherein raw SPARQL results are passed through utility modules (e.g., value_mapper.py, type_mapper.py) and then converted into pandas dataframes.
- A feedback loop or error handling mechanism using the retry_cb.py module for robust query execution.

##### Architectural Patterns & Design Principles
- Separation of Concerns: The package modularly separates query templating, client configuration, graph database interaction, and data transformation.
- Reusability and Extensibility: By exposing predefined queries and supporting custom templates, the design encourages extensibility. The client model abstracts away the complexities of interacting with different triple stores.
- Integration with External Services: Represent the external service integration (REST API calls) distinctly from the internal processing logic.

##### Including Relevant Technologies & Libraries
- Dependency on pandas for the final data structure conversion.
- References to external configuration files (e.g., pyproject.toml and CI/CD files in .github) which support continuous integration and testing.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Client/Application"]:::client
    B["ServiceConfig & Environment"]:::config
    C["Model Interface (model.py)"]:::internal
    D["SPARQL Query Templates (src/cimsparql/sparql)"]:::internal
    E["Templates Utility (templates.py)"]:::internal
    F["Triple Store Connector (graphdb.py)"]:::connector
    G["Data Transformation (data_models.py, type_mapper.py, value_mapper.py)"]:::internal
    H["Utilities (constants.py, utils.py, retry_cb.py)"]:::utility
    I["GraphDB"]:::external
    J["BlazeGraph"]:::external
    K["RDF4J"]:::external
    L["Tests"]:::support
    M["Documentation"]:::support

    %% Data Flow
    A -->|"configures"| B
    A -->|"calls"| C
    B -->|"injects"| C
    C -->|"uses"| D
    D -->|"assists"| E
    C -->|"queries"| F
    F -->|"calls"| I
    F -->|"calls"| J
    F -->|"calls"| K
    I -->|"returns"| G
    J -->|"returns"| G
    K -->|"returns"| G
    G -->|"transforms"| C
    C -->|"handles"| H

    %% Click Events
    click B "https://github.com/statnett/cimsparql/tree/master/src/pkg_data"
    click B "https://github.com/statnett/cimsparql/blob/master/pyproject.toml"
    click C "https://github.com/statnett/cimsparql/blob/master/src/cimsparql/model.py"
    click D "https://github.com/statnett/cimsparql/tree/master/src/cimsparql/sparql"
    click E "https://github.com/statnett/cimsparql/blob/master/src/cimsparql/templates.py"
    click F "https://github.com/statnett/cimsparql/blob/master/src/cimsparql/graphdb.py"
    click G "https://github.com/statnett/cimsparql/blob/master/src/cimsparql/data_models.py"
    click G "https://github.com/statnett/cimsparql/blob/master/src/cimsparql/type_mapper.py"
    click G "https://github.com/statnett/cimsparql/blob/master/src/cimsparql/value_mapper.py"
    click H "https://github.com/statnett/cimsparql/blob/master/src/cimsparql/constants.py"
    click H "https://github.com/statnett/cimsparql/blob/master/src/cimsparql/utils.py"
    click H "https://github.com/statnett/cimsparql/blob/master/src/cimsparql/retry_cb.py"
    click L "https://github.com/statnett/cimsparql/tree/master/tests"
    click M "https://github.com/statnett/cimsparql/tree/master/docs"

    %% Styles
    classDef client fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef config fill:#ffff99,stroke:#000,stroke-width:2px;
    classDef internal fill:#ccffcc,stroke:#000,stroke-width:2px;
    classDef connector fill:#ffb6c1,stroke:#000,stroke-width:2px;
    classDef utility fill:#ffdab9,stroke:#000,stroke-width:2px;
    classDef external fill:#d3d3d3,stroke:#000,stroke-width:2px;
    classDef support fill:#dda0dd,stroke:#000,stroke-width:2px;
```
