A Drools KIE SERVER client library
Project description
Python Drools SDK
This is a python package that allow you to interact with the REST API exposed by your KIE Server instance powered by Drools.
Installation
First and foremost, start by installing the package by running the following command :
pip install git+https://github.com/PapiHack/python-drools-sdk.git
Or more simply with :
pip install python-drools-sdk
Usage
Example of usage :
from python_drools_sdk.commands.insert_elements_command import InsertElementsCommand
from python_drools_sdk.commands.insert_object_command import InsertObjectCommand
from python_drools_sdk.kie.drools import Drools
from python_drools_sdk.utils import helpers
from python_drools_sdk.exceptions.drools_exception import DroolsException
# A dataclass for the example
class Person:
def __init__(self, **kwargs) -> None:
self.name = kwargs.get('name', 'Papi')
self.age = kwargs.get('age', 46)
self.id = kwargs.get('id', None)
self.isMajor = kwargs.get('isMajor', None)
def __str__(self) -> str:
return 'Person [id => {}, name => {}, age => {}, isMajor => {}]'.format(self.id, self.name, self.age, self.isMajor)
# Set configuration variables like your KIE_SERVER credentials, ROOT KIE_SERVER URL and so on
Drools.KIE_SERVER_CONTAINER_PACKAGE = 'you_kie_container_package' # Example: com.myspace.sample_project
Drools.KIE_SERVER_USERNAME = 'your_kie_server_username'
Drools.KIE_SERVER_PASSWORD = 'your_kie_server_password'
Drools.KIE_SERVER_ROOT_URL = 'your_kie_server_url'
Drools.KIE_SERVER_CONTAINER_ID = 'your_kie_container_id' # Example : Sample_Project_1.0.0-SNAPSHOT
# If you defined a KIE_SESSION in your drools workbench project, you can specify it like the following line
Drools.KIE_SESSION_NAME = 'your_kie_session_name'
# Creation of data objects
person = Person(id=1)
P1 = Person(name='Fatou', id=2, age=15)
P2 = Person(name='Daba', id=3, age=17)
P3 = Person(name='Nabou', id=4, age=28)
persons = [P1, P2, P3]
# Create an InsertObjectCommand in order to fire rules on a specific object
# The 'object' parameter represent the object to sent, 'out_identifier' should be a unique key
# that is going to used for extracting the associated object after having response from drools kie server
insert_command = InsertObjectCommand(object=person, out_identifier="person_papi").initialize()
# Create an InsertElementsCommand in order to fire rules on a list of object
# The 'objects' parameter represent the list of object to sent, 'out_identifier' should be a unique key
# that is going to used for extracting the associated objects'list after having response from drools kie server
insert_elements_command = InsertElementsCommand(objects=persons, out_identifier='persons').initialize()
# Add commands before excuting them
Drools.add_command(insert_command)
Drools.add_command(insert_elements_command)
# Execute all commands
try:
response = Drools.execute_commands()
except DroolsException as de:
print(de)
# Getting data by using the previous 'out_identifier' key
drools_person_response = response['person_papi']
# Getting the list of persons after drools rules execution on them by using its respective key
drools_persons_list_response = response['persons']
# I also make some helpers that will allow you to convert a json (dict) to a specific object and vice versa
# Here the 'json2object' function take as a first parameter the json/dict to be converted and as a final parameter,
# the destination type (here, the 'Person' class that we defined earlier)
person_papi_object = helpers.json2object(drools_person_response, Person)
print(person_papi_object)
# A function named 'object2json' is also defined and allow you like its name says, to convert an object to json/dict
Contributing
Feel free to make a PR or report an issue.
Oh, one more thing, please do not forget to put a description when you make your PR 🙂
Contributors
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python-drools-sdk-0.0.3.tar.gz.
File metadata
- Download URL: python-drools-sdk-0.0.3.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb84a9e38f4cb91f5ba735e41067bf6bd02c2b1fa14e2ec8ac5aadc7288f4e1c
|
|
| MD5 |
8130ce6919634fe0e5fd7341b6951926
|
|
| BLAKE2b-256 |
e9a13bc33cb24f1f9f49b30c4264d44999eb91aba838046eb93e05328cff27c8
|
File details
Details for the file python_drools_sdk-0.0.3-py3-none-any.whl.
File metadata
- Download URL: python_drools_sdk-0.0.3-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
663fd968296ff8d0366125fb7d537ec259dec551df203593f33d169bb5d9799e
|
|
| MD5 |
f83927db751e892ae71fcf479d784bdd
|
|
| BLAKE2b-256 |
26070f59b620b4fa6599d7c2af0b006e28da1d4532ab2118391b2d7f337f8611
|