[][src]Function phreak_engine::jointest::get_join_tests_from_condition

pub fn get_join_tests_from_condition(
    condition: &Condition,
    earlier: &[Condition]
) -> Vec<JoinNodeTest>

Compare the current condition against the earlier conditions and find variable fields to match.

Each condition can contain variables. In order for a chain of conditions to match, each condition must match the same token for the same variable. If condition has a variable field x, each condition in earlier that has a variable x must also bind the same token for that variable x. In the join tests, we store the information which earlier condition uses a variable that is used in condition, by counting the number of times steps between the current condition and the earlier condition.

Note that we only need to find one earlier match per variable in the current condition, since all conditions in earlier already hold the same constraint to their earlier conditions.

Tests are returned sorted by distance of the earlier condition to the current condition, closest first, so we can fail-fast and don't need to iterate the token tree further than necessary.

use phreak_engine::jointest::*;

let mut earlier: Vec<Condition> = Vec::new();

let condition =
    ConditionBuilder::new(String::from("car"))
        .variable_field1(String::from("z")).const_field2(String::from("is")).const_field3(String::from("green"))
        .build();
let tests = get_join_tests_from_condition(&condition, &earlier);
earlier.push(condition);

assert_eq!(tests, []); // no earlier conditions can match

let condition =
    ConditionBuilder::new(String::from("car"))
        .variable_field1(String::from("z")).const_field2(String::from("is")).const_field3(String::from("big"))
        .build();
let tests = get_join_tests_from_condition(&condition, &earlier);
earlier.push(condition);

assert_eq!(tests, [JoinNodeTest{ f1: JoinTestField::Field1,
                                 condition_number_of_arg2 : 1,
                                 f2: JoinTestField::Field1,
}]); // The z in both conditions is the same variable, and must match for both facts to be related