User guide - jan-klos/dmn_python GitHub Wiki
Basic user guide. Should you need any particular information, you can contact me: [email protected].
Installation
pip3 install dmn_python
Importing/exporting model from/to an XML file and visualizing it
Example of these operations can be found in tests/test_example_from_manual
.
Modyfing model
A user may want to change all elements names from model presented in Figure 4.5 by adding ’ v1’ to them. In GUI-based editors they would have to click and type many times, but using the DMN Python library it is the matter of one simple loop.
for elem in model.element_list:
elem.name = elem.name + ' v1'
Addition of an element is equally simple. If Determine Personal Credit Rating from the same model would need another input, Persons Age, referencing to Persons Data item definition, it could be made as follows:
age_input_data = InputDataElement(name = 'Persons Age', id = 'personsAge_id')
age_input_data.variable = InputElementVariable(name = 'Persons Age', id = 'personsAge_ii')
model.add_element(age_input_data)
model.create_requirement(from_elem = age_input_data, to_elem = model.get_element_by_name('Determine Personal Credit Rating'), type = 'information')
Creating model from scratch
Let’s say user wants to create the model presented in the image:
This is how to create these elements, requirements and one decision table. Note that this code is incomplete, it just shows basic operations:
from definitions.Model import Model
from definitions.Elements import *
from definitions.Logic import *
model = Model(description = 'Model basing on http://blog.luxmagi.com/2015/11/how-dmn-allows-business-rules-to-scale')
input1 = InputDataElement(id = 'personsEmplHis_id', name = 'Persons Employment History')
input2 = InputDataElement(id = 'personsAnnSal_id', name = 'Persons Annual Salary')
input3 = InputDataElement(id = 'personsDebtRec_id', name = 'Persons Debt Record')
ks1 = KnowledgeSourceElement(id = 'riskManPolicy_ks', name = 'Risk Management Policy')
decision1 = DecisionElement(id = 'detPersonalEmplRec_d', name = 'Determine Personal Employment Record')
decision2 = DecisionElement(id = 'detPersonalCreditRat_d', name = 'Determine Personal Credit Rating')
decision3 = DecisionElement(id = 'detPersonalLoanLimit_d', name = 'Determine Personal Loan Limit')
model.add_element_list([input1, input2, input3, ks1, decision1, decision2, decision3])
model.create_requirement(from_elem = input1, to_elem = decision1, type = 'information')
model.create_requirement(from_elem = input2, to_elem = decision2, type = 'information')
model.create_requirement(from_elem = decision1, to_elem = decision2, type = 'information')
model.create_requirement(from_elem = input3, to_elem = decision2, type = 'information')
model.create_requirement(from_elem = ks1, to_elem = decision2, type = 'authority')
model.create_requirement(from_elem = decision2, to_elem = decision3, type = 'information')
model.create_requirement(from_elem = input3, to_elem = decision3, type = 'information')
#decision table
input1 = DecisionTableInput(label = 'Personal Employment Record', allowed_values = 'Good, Average, Poor', expression = 'personalEmploymentRec')
input2 = DecisionTableInput(label = 'Persons Debt Record', expression = 'personalDebtRec')
output = DecisionTableOutput(name = 'Personal Credit Rating', allowed_values = 'AAA, AA, A, B, C')
rule1 = Rule(['Good', '<= Persons Annual Salary * 4'], 'AAA')
rule2 = Rule(['Good', '> Persons Annual Salary * 4'], 'AA')
rule3 = Rule(['Average', '<= Persons Annual Salary * 4'], 'A')
rule4 = Rule(['Average', '> Persons Annual Salary * 4'], 'B')
rule5 = Rule(['Poor', '-'], 'C')
decision_table = DecisionTable(id = 'personalCreditRat_dt', hit_policy = 'UNIQUE', preferred_orientation = 'Rule-As-Row', rule_list = [rule1, rule2, rule3, rule4, rule5], input_list = [input1, input2], output = output)
model.get_element_by_id('detPersonalCreditRat_d').decision_table = decision_table