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.
- FCLE - Fuzzy Control Language Engine
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.
- 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.
git clone https://github.com/ifak-prototypes/fuzzy_control_language_engine.git
cd fuzzy_control_language_engineFor Linux-based systems (including WSL on Windows), run:
bash ./bin/install.shThis script will:
- Create a Python virtual environment.
- Upgrade
pipto the latest version. - Install the package in editable mode.
- Install all required dependencies.
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.txtNote: The requirements.txt may not always contain the latest versions of dependencies.
Start the FastAPI server:
fcleThe service will be accessible at http://0.0.0.0:8000. You can interact with it via:
- Web Browser: Navigate to
http://localhost:8000/docsfor interactive API documentation. - Command-Line Tools: Use tools like
curlorhttpieto 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.
Ensure you have Python 3.10 or higher installed. The installation steps are covered in the Quick Start section.
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.
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.
Interactive API documentation is available at:
- Swagger UI: http://localhost:8000/docs
- Redoc: http://localhost:8000/redoc
These interfaces provide detailed information about each endpoint, expected parameters, and response formats. The OpenAPI specification is also available at http://localhost:8000/redoc.
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.
Endpoint: POST /evaluate
Evaluates the fuzzy logic rules based on provided input values.
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.
{
"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.
Endpoint: POST /set_rulebase
Uploads a new rule base in FCL (Fuzzy Control Language) format.
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 yourrulebase.fclfile.
{
"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.
Endpoint: GET /get_rulebase
Retrieves the current rule base in use.
curl -X GET "http://localhost:8000/get_rulebase"{
"status": "success",
"timestamp": "2024-09-02T12:34:56.789012+00:00",
"rulebase": "<base64_encoded_rulebase>",
"message": "Rule base successfully retrieved."
}Explanation:
-
The
rulebasefield contains the Base64-encoded FCL content. -
Decode using:
echo "<base64_encoded_rulebase>" | base64 -d > retrieved_rulebase.fcl
Endpoint: POST /set_service_conf
Updates the service configuration.
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 yourservice_conf.yamlfile.
{
"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.
Endpoint: GET /get_service_conf
Retrieves the current service configuration.
curl -X GET "http://localhost:8000/get_service_conf"{
"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_conffield contains the Base64-encoded YAML content. -
Decode using:
echo "<base64_encoded_service_conf>" | base64 -d > retrieved_service_conf.yaml
To run the test suite:
bash ./bin/test.shOr manually:
source venv/bin/activate
python -m unittest discover testNote: You can also take the test cases as examples and for getting more insigths on how to use the API.
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch:
git checkout -b feature/your-feature-name. - Commit your changes:
git commit -am 'Add some feature'. - Push to the branch:
git push origin feature/your-feature-name. - Open a pull request.
This project is licensed under the MIT License. See the LICENSE.txt file for more details.
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.