Evaluate Rules¶

Takes 2 json files - parse tree dictionary and variable dictionary - and a rule string. Returns a truth value.

In [ ]:
from birdy import WPSClient
import os
from wps_tools.testing import get_target_url
from importlib.resources import files

# Ensure we are in the working directory with access to the data
while os.path.basename(os.getcwd()) != "sandpiper":
    os.chdir('../')
In [2]:
# NBVAL_IGNORE_OUTPUT
url = get_target_url("sandpiper")
print(f"Using sandpiper on {url}")
Using sandpiper on https://docker-dev03.pcic.uvic.ca/twitcher/ows/proxy/sandpiper/wps
In [3]:
sandpiper = WPSClient(url)

The list of available processes can be diplayed using the ? command (ex. bird?). Furthermore, help for individual processes can be diplayed using the same command (ex. bird.process?).

In [4]:
# NBVAL_IGNORE_OUTPUT
sandpiper.evaluate_rule?
Signature:
sandpiper.evaluate_rule(
    rules,
    parse_tree=None,
    variables=None,
    loglevel='INFO',
    output_formats=None,
)
Docstring:
Evaluate parse trees to determine truth value of a rule

Parameters
----------
rules : string
    Rule expressions
parse_tree : ComplexData:mimetype:`application/json`
    File path to dictionary used for rule getter function
variables : ComplexData:mimetype:`application/json`
    File path to dictionary used for variables
loglevel : {'CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET'}string
    Logging level

Returns
-------
json : ComplexData:mimetype:`application/json`
    JSON file
File:      ~/code/birds/sandpiper/</tmp/sandpiper-venv/lib/python3.8/site-packages/birdy/client/base.py-2>
Type:      method

For the process, pass the rule and the file paths for the 2 json files

In [ ]:
# Evaluate truth value of expression 1
rules = ['rule_snow', 'rule_rain', 'rule_hybrid']
parse_tree = str((files("tests") / "data/parse_tree.json").resolve()),
variables = str((files("tests") / "data/collected_variables.json").resolve()),  
output = sandpiper.evaluate_rule(
    rules=rules,
    parse_tree=parse_tree,
    variables=variables
)
# Access the json contents as a dictionary by setting asobj=True
truth_values = output.get(asobj=True)[0]
In [6]:
# Evaluate truth value of one expression
rule = 'rule_hybrid'  
output = sandpiper.evaluate_rule(
    rules=rule,
    parse_tree=parse_tree,
    variables=variables
)
hybrid_truth_values = output.get(asobj=True)[0]

Now that we've run the process, we check that the output is what we expect.

In [7]:
# Test expected output
expected_output = {
    'rule_snow': False, 'rule_rain': True, 'rule_hybrid': True
}
assert truth_values == expected_output
assert hybrid_truth_values["rule_hybrid"] == expected_output["rule_hybrid"]