- Overview
- Features
- Project Structure
- Pre-requisites
- Setup & Run
- API Endpoints
- Prolog Query Endpoints
- Docker & Deployment
A CLI and webβbased tool that leverages ruleβbased Prolog inference combined with machine learning to estimate how much an EV charging session will add to your electricity bill. It provides:
- A
/estimateendpoint to calculate energy (kWh) and estimated cost (β‘) based on battery capacity, vehicle age, charging duration, company and period. - Metadata endpoints to fetch available companies, periods, and detailed tariff tables.
- A modern Next.js frontend with React & TypeScript for interactive user input and result display.
- Energy Prediction: Uses Prolog rules (
energy_required,charging_rate_value) for kWh estimation. - Cost Estimation: Combines energy estimate with dynamic tariffs from the database via the
/estimateendpoint. - Metadata Lookup: Single
/lookupendpoint returns lists of companies and tariff periods. - Detailed Tariffs:
/companies-with-tariffsreturns structured tariff info by company and period. - Prolog Integration: Dynamic seeding of Prolog facts (battery_capacity, vehicle_age, charging_duration, tariffs) from PostgreSQL.
- Frontend: Next.js + TypeScript + Tailwind CSS interactive form and modal components.
- Dockerized: Full-stack setup with Docker Compose for consistent deployment.
βββ backend
β βββ app
β β βββ routers
β β βββ rules
β β βββ engine
β β β βββ prolog_engine.py
β β βββ models.py
β β βββ database.py
β β βββ config.py
β β βββ main.py
β βββ requirements.txt
β βββ Dockerfile
βββ databaseDefinition
β βββ a_init_cars.sql
β βββ b_init_energy_facts.sql
| βββ c_init_energy_tarifss.sql
β βββ view_definitions.sql
βββ frontend
β βββ components
| βββ models
β βββ pages
β βββ public
β βββ services
β βββ styles
β βββ utils
β βββ package.json
β βββ tsconfig.json
βββ utils
β βββ start_clean.sh
βββ docker-compose.yml
βββ .gitignore
βββ README.md
- Docker Desktop (for containerized deployment)
- Node.js & npm (for local frontend dev)
- Python 3.9+ & pip (for local backend dev)
./utils/start_clean.sh # Mac M1+ friendly
# or:
docker-compose up --build # builds and runs frontend, backend, DBcd frontend
env: npm install
npm run dev
# Access at http://localhost:3000cd backend
python -m venv venv && source venv/bin/activate
pip install -r requirements.txt
uvicorn app.main:app --reload
# Runs at http://localhost:8000- Execute SQL scripts in
databaseDefinition/to create tables and views. - Ensure
tariffstable is populated with current rates.
Calculates energy and cost in one call.
POST /estimate
Body (JSON):
{
"battery_capacity": 75.0,
"vehicle_age": 5.0,
"charging_duration": 2.0,
"company": "CNFL",
"period": "Punta"
}Response:
{
"energy_kwh": 40.0,
"estimated_cost": 6278.40
}Fetch lists of valid companies and periods.
GET /lookup
Response:
{
"companies": ["CNFL","ICE"],
"tariffs": ["Punta","Valle","Nocturno"]
}Returns detailed tariff structure per company.
GET /companies-with-tariffs
Response:
[
{
"companyName": "CNFL",
"tariffs": [
{ "periodName": "Punta", "costKwh": 156.96 },
{ "periodName": "Valle", "costKwh": 64.35 },
{ "periodName": "Nocturno", "costKwh": 26.94 }
]
},
{
"companyName": "ICE",
"tariffs": [
{ "periodName": "Punta", "costKwh": 152.53 },
{ "periodName": "Valle", "costKwh": 104.80 },
{ "periodName": "Nocturno", "costKwh": 76.45 }
]
}
]- GET
/query?rule=&args=β raw Prolog query (useful for debugging).
E.g./query?rule=energy_required. - POST
/query/add_factβ dynamically assert a new Fact.
Use docker-compose.yml to orchestrate:
- frontend (Next.js)
- backend (FastAPI + Prolog)
- postgres
docker-compose up --build\```