This article shows how to execute a  Decision Model and Notation (DMN) model deployed as an Azure Function from a simple Python script. You will use a DMN model deployed to a serverless environment(Azure) and then call its execution endpoint from any Python environment.

We going to use the Standard Loan Origination DMN sample project for this use case. You can simply load your DMN file into FlexRule Designer and then its ready to deploy. You can refer to the guide on deploying a DMN model as an Azure Function if you have not completed that step yet.

How to get the execute URL

When you deploy a DMN model as an Azure Function from FlexRule Designer, Azure exposes a REST API endpoint that represents your decision service. 

Execute URL for Python

Building the Python client

The deployed DMN service behaves like a regular REST API. Then you can write a simple Python client using requests to send the inputs and read the response.

Define the endpoint and payload

Use the execute URL from FlexRule Designer as its includes the  code query parameters. Then build an inputs array with each array element followed by name, value pair required by your DMN model . In this example we have 3 input nodes which are “Bureau data”, “Applicant data” and “Requested product”.

import requests
import json
url = "https://DMNLoanOrigination-gbeulb8.azurewebsites.net/api/Execute/DMNLoanOrigination/1/DRD for Decide routing decision point/1?code=aOUtxj80u6b5KQFbXyM_ANO8L3p1SElJiW8m3hT1Cb0dAzFu_8pJhw==&formatter=Object"
payload = {
    "inputs": [
        {
            "name": "Bureau data",
            "value": {
                "Bankrupt": False,
                "CreditScore": 200
            }
        },
        {
            "name": "Applicant data",
            "value": {
                "Age": 32,
                "MaritalStatus": "S",
                "EmploymentStatus": "UNEMPLOYED",
                "ExistingCustomer": False,
                "Monthly": {
                    "Income": 651000,
                    "Repayments": 10000,
                    "Expenses": 36000
                }
            }
        },
        {
            "name": "Requested product",
            "value": {
                "ProductType": "STANDARD LOAN",
                "Rate": 2.5,
                "Term": 25,
                "Amount": 10000
            }
        }
    ]
}
headers = {
    "Content-Type": "application/json"
}

Sending the request and handling the response

Next, inside the same script, send the POST request and inspect the response. The service response contains an outputs array with your decision results.

try:
    response = requests.post(url, json=payload, headers=headers)
    try:
        print(json.dumps(response.json(), indent=2))
    except ValueError:
        print(response.text)
except requests.exceptions.RequestException as e:
    print("Request failed:", e)

Let's run this python script from the command line.

Response Details

After successfully executing the python script, we got the above JSON response for the loan origination model according to the given input data. It contains an outputs array with the output parameters defined inside the model. In this case its a parameter called “Routing” and its value is “REFER”.

Summary

This article explains how to call a DMN model, deployed as an Azure Function, from a simple Python script using HTTP POST and JSON payloads. It walks through understanding the execute URL, structuring the inputs array to match the input parameters, sending the request with requests, and reading the response.