Package: python-drools-sdk
Python Drools SDKThis is apythonpackage that allow you to interact with the REST API exposed by yourKIE Serverinstance powered byDrools.InstallationFirst and foremost, start by installing the package by running the following command :pip install git+https://github.com/PapiHack/python-drools-sdk.gitOr more simply with :pip install python-drools-sdkUsageExample of usage :frompython_drools_sdk.commands.insert_elements_commandimportInsertElementsCommandfrompython_drools_sdk.commands.insert_object_commandimportInsertObjectCommandfrompython_drools_sdk.kie.droolsimportDroolsfrompython_drools_sdk.utilsimporthelpersfrompython_drools_sdk.exceptions.drools_exceptionimportDroolsException# A dataclass for the exampleclassPerson: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 onDrools.KIE_SERVER_CONTAINER_PACKAGE='you_kie_container_package'# Example: com.myspace.sample_projectDrools.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 lineDrools.KIE_SESSION_NAME='your_kie_session_name'# Creation of data objectsperson=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 serverinsert_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 serverinsert_elements_command=InsertElementsCommand(objects=persons,out_identifier='persons').initialize()# Add commands before excuting themDrools.add_command(insert_command)Drools.add_command(insert_elements_command)# Execute all commandstry:response=Drools.execute_commands()exceptDroolsExceptionasde:print(de)# Getting data by using the previous 'out_identifier' keydrools_person_response=response['person_papi']# Getting the list of persons after drools rules execution on them by using its respective keydrools_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/dictContributingFeel 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 🙂ContributorsM.B.C.M