Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FCLE - Fuzzy Control Language Engine

An engine for reasoning about Fuzzy Logic according to the IEC 61131-7 standard. FCLE is designed for dynamic pricing and other applications that require fuzzy logic control, enabling seamless integration and flexible rule management across various industries.

Table of Contents

Introduction

FCLE (Fuzzy Control Language Engine) is a versatile Fuzzy Logic Engine designed for evaluating function blocks according to the IEC 61131-7 standard (Fuzzy Control Language). While its primary application is dynamic pricing, FCLE is a generic engine that can be utilized for any fuzzy logic-based control algorithm. It provides flexibility and adaptability for various industries and use cases, enabling users to easily integrate and modify rule bases and input configurations for customized control strategies.

Features

  • IEC 61131-7 Compliance: Adheres to a subset of the IEC 61131-7 standard for Fuzzy Control Language, allowing for standardized evaluation of function blocks.
  • Comprehensive RESTful API: Offers a robust API for configuring rule bases, input values, and dynamically triggering the evaluation of fuzzy logic rules.
  • Easy Installation and Setup: Designed for straightforward installation with minimal configuration, enabling quick deployment.
  • Integration Ready: Seamlessly integrates with existing GUIs and external systems, suitable for a wide range of industrial applications beyond dynamic pricing.
  • Flexible Rule Base Management: Supports uploading, modifying, and retrieving rule bases in a standardized format, enhancing adaptability for various control strategies.
  • Dynamic Input Handling: Capable of processing and evaluating input values from multiple sources, ensuring robust and responsive control algorithms.

Quick Start

1. Clone the Repository

git clone https://github.com/ifak-prototypes/fuzzy_control_language_engine.git
cd fuzzy_control_language_engine

2. Set Up the Environment

Automated Installation

For Linux-based systems (including WSL on Windows), run:

bash ./bin/install.sh

This script will:

  • Create a Python virtual environment.
  • Upgrade pip to the latest version.
  • Install the package in editable mode.
  • Install all required dependencies.

Manual Installation

If you encounter issues with the automated script, you can install manually:

python -m venv --copies venv
source venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -e .
pip install -r requirements.txt

Note: The requirements.txt may not always contain the latest versions of dependencies.

3. Run the Service

Start the FastAPI server:

fcle

The service will be accessible at http://0.0.0.0:8000. You can interact with it via:

  • Web Browser: Navigate to http://localhost:8000/docs for interactive API documentation.
  • Command-Line Tools: Use tools like curl or httpie to make HTTP requests.
  • External Devices: Access via any device on the same network using the host's IP address and port 8000.

To stop the server, press Ctrl+C.

Installation

Ensure you have Python 3.10 or higher installed. The installation steps are covered in the Quick Start section.

Configuration

Before running the service, ensure the data directory contains:

  • rulebase.fcl: The initial rule base file.
  • service_conf.yaml: Service configuration file.

You can modify these files or use the API endpoints to update them dynamically.

Note: Integration with Keycloak or other authentication mechanisms is not supported out of the box. Modifications to service.py are required for such features.

Usage

Once the service is running, you can interact with it using HTTP requests. The service provides endpoints to evaluate fuzzy logic rules, manage rule bases, and update service configurations.

API Documentation

Interactive API documentation is available at:

These interfaces provide detailed information about each endpoint, expected parameters, and response formats. The OpenAPI specification is also available at http://localhost:8000/redoc.

Example API Calls

Below are detailed examples of how to interact with the FCLE API using curl. Replace localhost with your server's address if it's running elsewhere.

1. Evaluate Endpoint

Endpoint: POST /evaluate

Evaluates the fuzzy logic rules based on provided input values.

Request

curl -X POST "http://localhost:8000/evaluate" \
     -H "Content-Type: application/json" \
     -d '{"input_values": {"factory_load": 75, "market_demand": 60, "production_cost": 50}}'

Note: Make sure that those 3 input values are defined in the rulebase. If necessary parameters are missing, you will get an error.

Headers:

  • Content-Type: application/json

Body:

  • input_values: JSON object containing input variables.

Response

{
  "status": "success",
  "timestamp": "2024-09-02T12:34:56.789012+00:00",
  "request_id": "abcd1234",
  "input_params": {
    "factory_load": 75,
    "market_demand": 60,
    "production_cost": 50
  },
  "output_values": {
    "pricing_factor": {
      "pricing_factor": 1.23
    }
  },
  "message": "Pricing factor successfully calculated."
}

Explanation:

  • Input Variables:
    • factory_load: Current load on the factory (0 to 100%, or more with extra shifts).
    • market_demand: Market demand level (0 to 100, 50 is normal demand).
    • production_cost: Cost of production (0 to 100, 50 is normal production cost).
  • Output Variables:
    • pricing_factor: Calculated pricing factor based on fuzzy logic rules.

2. Set Rulebase Endpoint

Endpoint: POST /set_rulebase

Uploads a new rule base in FCL (Fuzzy Control Language) format.

Request

curl -X POST "http://localhost:8000/set_rulebase" \
     -H "Content-Type: application/json" \
     -d '{"rulebase": "'$(base64 -w 0 path_to_your_rulebase.fcl)'"}'

Headers:

  • Content-Type: application/json

Body:

  • rulebase: Base64-encoded string of your rulebase.fcl file.

Response

{
  "status": "success",
  "timestamp": "2024-09-02T12:34:56.789012+00:00",
  "message": "Rule base successfully updated."
}

Explanation:

  • This endpoint replaces the existing rule base with the provided one.
  • Ensure your FCL file is correctly formatted and encoded.

3. Get Rulebase Endpoint

Endpoint: GET /get_rulebase

Retrieves the current rule base in use.

Request

curl -X GET "http://localhost:8000/get_rulebase"

Response

{
  "status": "success",
  "timestamp": "2024-09-02T12:34:56.789012+00:00",
  "rulebase": "<base64_encoded_rulebase>",
  "message": "Rule base successfully retrieved."
}

Explanation:

  • The rulebase field contains the Base64-encoded FCL content.

  • Decode using:

    echo "<base64_encoded_rulebase>" | base64 -d > retrieved_rulebase.fcl

4. Set Service Configuration Endpoint

Endpoint: POST /set_service_conf

Updates the service configuration.

Request

curl -X POST "http://localhost:8000/set_service_conf" \
     -H "Content-Type: application/json" \
     -d '{"service_conf": "'$(base64 -w 0 path_to_your_service_conf.yaml)'"}'

Headers:

  • Content-Type: application/json

Body:

  • service_conf: Base64-encoded string of your service_conf.yaml file.

Response

{
  "status": "success",
  "timestamp": "2024-09-02T12:34:56.789012+00:00",
  "message": "Service configuration successfully updated."
}

Explanation:

  • Updates the service configuration parameters.
  • Useful for changing server settings or variable endpoints.

5. Get Service Configuration Endpoint

Endpoint: GET /get_service_conf

Retrieves the current service configuration.

Request

curl -X GET "http://localhost:8000/get_service_conf"

Response

{
  "status": "success",
  "timestamp": "2024-09-02T12:34:56.789012+00:00",
  "service_conf": "<base64_encoded_service_conf>",
  "message": "Service configuration successfully retrieved."
}

Explanation:

  • The service_conf field contains the Base64-encoded YAML content.

  • Decode using:

    echo "<base64_encoded_service_conf>" | base64 -d > retrieved_service_conf.yaml

Testing

To run the test suite:

bash ./bin/test.sh

Or manually:

source venv/bin/activate
python -m unittest discover test

Note: You can also take the test cases as examples and for getting more insigths on how to use the API.

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch: git checkout -b feature/your-feature-name.
  3. Commit your changes: git commit -am 'Add some feature'.
  4. Push to the branch: git push origin feature/your-feature-name.
  5. Open a pull request.

License

This project is licensed under the MIT License. See the LICENSE.txt file for more details.

Credits

This project is maintained and developed by Institut für Automation und Kommunikation, Magdeburg. This work is supported by ITEA3 under the supervision of the German Federal Ministry of Education and Research (FKZ: 01IS21084 (InnoSale)).

Special thanks to our contributors and the community for their support.

About

Python-based fuzzy logic engine and REST service implementing a sub-set of IEC61131-7.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages