clips-bindings 0.1.2+clips-6.4.2

Safe, idiomatic, custom-written Rust bindings for the CLIPS 6.4.2 embedding API
Documentation
//! Construct lookup, undefinition, and metadata.

use clips_bindings::*;

#[test]
fn finds_and_iterates_constructs_with_typed_handles() {
    let mut environment = Environment::new().unwrap();
    for construct in [
        "(deftemplate sample)",
        "(deffacts starting (sample))",
        "(defrule react (sample) =>)",
        "(defglobal ?*answer* = 42)",
        "(deffunction identity (?value) ?value)",
        "(defgeneric combine)",
        "(defclass ITEM (is-a USER))",
        "(definstances examples (item of ITEM))",
    ] {
        environment.build(construct).unwrap();
    }

    assert_eq!(
        environment
            .find_deftemplate("sample")
            .unwrap()
            .unwrap()
            .name(),
        "sample"
    );
    assert_eq!(
        environment
            .find_deffacts("starting")
            .unwrap()
            .unwrap()
            .name(),
        "starting"
    );
    assert_eq!(
        environment.find_defrule("react").unwrap().unwrap().name(),
        "react"
    );
    assert_eq!(
        environment
            .find_defglobal("answer")
            .unwrap()
            .unwrap()
            .name(),
        "answer"
    );
    assert_eq!(
        environment
            .find_deffunction("identity")
            .unwrap()
            .unwrap()
            .name(),
        "identity"
    );
    assert_eq!(
        environment
            .find_defgeneric("combine")
            .unwrap()
            .unwrap()
            .name(),
        "combine"
    );
    assert_eq!(
        environment.find_defclass("ITEM").unwrap().unwrap().name(),
        "ITEM"
    );
    assert_eq!(
        environment
            .find_definstances("examples")
            .unwrap()
            .unwrap()
            .name(),
        "examples"
    );
    assert!(
        environment
            .defmodules()
            .any(|module| module.name() == "MAIN")
    );
}

#[test]
fn inspects_and_safely_undefines_constructs() {
    let mut environment = Environment::new().unwrap();
    environment
        .build("(deftemplate disposable (slot value))")
        .unwrap();
    {
        let template = environment.find_deftemplate("disposable").unwrap().unwrap();
        assert_eq!(template.module_name(), "MAIN");
        assert!(template.pretty_print().unwrap().contains("disposable"));
        assert!(template.is_deletable());
    }
    assert!(environment.undefine_deftemplate("disposable").unwrap());
    assert!(
        environment
            .find_deftemplate("disposable")
            .unwrap()
            .is_none()
    );
}

#[test]
fn inspects_template_slot_constraints() {
    let mut environment = Environment::new().unwrap();
    environment
        .build("(deftemplate constrained (slot count (type INTEGER) (range 1 10) (default 3)) (multislot tags))")
        .unwrap();
    let template = environment
        .find_deftemplate("constrained")
        .unwrap()
        .unwrap();
    assert_eq!(template.slot_names().unwrap(), ["count", "tags"]);
    let count = template.slot("count").unwrap().unwrap();
    assert!(count.is_single_slot());
    assert_eq!(count.default_type(), SlotDefaultType::Static);
    assert_eq!(count.default_value().unwrap().as_i64(), Some(3));
    let tags = template.slot("tags").unwrap().unwrap();
    assert!(tags.is_multislot());
}

#[test]
fn inspects_generic_methods_and_global_values() {
    let mut environment = Environment::new().unwrap();
    environment.build("(defgeneric twice)").unwrap();
    environment
        .build("(defmethod twice ((?value INTEGER)) (* ?value 2))")
        .unwrap();
    environment.build("(defglobal ?*counter* = 1)").unwrap();

    {
        let generic = environment.find_defgeneric("twice").unwrap().unwrap();
        let method = generic.methods().next().unwrap();
        assert!(method.pretty_print().unwrap().contains("defmethod"));
        assert!(method.is_deletable());
    }
    assert_eq!(
        environment
            .defglobal_value("counter")
            .unwrap()
            .unwrap()
            .as_i64(),
        Some(1)
    );
    environment.set_defglobal_integer("counter", 42).unwrap();
    assert_eq!(
        environment
            .defglobal_value("counter")
            .unwrap()
            .unwrap()
            .as_i64(),
        Some(42)
    );
}

#[test]
fn inspects_cool_classes_and_message_handlers() {
    let mut environment = Environment::new().unwrap();
    environment
        .build("(defclass DEVICE (is-a USER) (slot state))")
        .unwrap();
    environment
        .build("(defmessage-handler DEVICE ping primary () 42)")
        .unwrap();
    let class = environment.find_defclass("DEVICE").unwrap().unwrap();
    assert!(!class.is_abstract());
    assert!(class.slots(false).contains(&"state".to_owned()));
    let handler = class
        .find_message_handler("ping", HandlerType::Primary)
        .unwrap()
        .unwrap();
    assert_eq!(handler.name(), "ping");
    assert_eq!(handler.handler_type(), HandlerType::Primary);
}

#[test]
fn inspects_class_slot_descriptors_and_inheritance() {
    let mut environment = Environment::new().unwrap();
    environment
        .build(
            "(defclass VEHICLE (is-a USER) \
             (slot wheels (type INTEGER) (range 2 18) (default 4) (access read-only) \
              (visibility public)) \
             (multislot owners (cardinality 0 3)) \
             (slot colour (allowed-values red green blue) (default red)))",
        )
        .unwrap();
    environment
        .build("(defclass CAR (is-a VEHICLE) (slot doors (type INTEGER)))")
        .unwrap();

    let car = environment.find_defclass("CAR").unwrap().unwrap();
    let vehicle = environment.find_defclass("VEHICLE").unwrap().unwrap();
    assert!(vehicle.is_superclass_of(&car));
    assert!(car.is_subclass_of(&vehicle));
    assert!(!car.is_superclass_of(&vehicle));
    assert_eq!(
        car.precedence_list(),
        vec![
            "CAR".to_owned(),
            "VEHICLE".to_owned(),
            "USER".to_owned(),
            "OBJECT".to_owned()
        ]
    );

    assert!(car.has_slot("wheels", true).unwrap());
    assert!(!car.has_slot("wheels", false).unwrap());
    assert!(car.slot("nosuchslot").unwrap().is_none());

    let wheels = car.slot("wheels").unwrap().unwrap();
    assert_eq!(wheels.sources(), vec!["VEHICLE".to_owned()]);
    assert_eq!(wheels.allowed_types(), vec!["INTEGER".to_owned()]);
    assert_eq!(wheels.range(), Some(("2".to_owned(), "18".to_owned())));
    assert_eq!(wheels.default_value(), Some("4".to_owned()));
    // `default_type` does not follow inheritance, so it is asked of the
    // class the slot is defined on.
    assert_eq!(wheels.default_type(&environment), SlotDefaultType::None);
    assert_eq!(
        vehicle
            .slot("wheels")
            .unwrap()
            .unwrap()
            .default_type(&environment),
        SlotDefaultType::Static
    );
    assert!(!wheels.is_writable());
    // `is_public` shares `default_type`'s non-inheriting resolution.
    assert!(!wheels.is_public());
    assert!(vehicle.slot("wheels").unwrap().unwrap().is_public());
    // CLIPS reports facets as abbreviations: single, static, inherit,
    // read-only, shared, reactive, exclusive, public, read, no source.
    assert_eq!(
        wheels.facets(),
        [
            "SGL", "STC", "INH", "R", "SHR", "RCT", "EXC", "PUB", "R", "NIL"
        ]
        .map(str::to_owned)
        .to_vec()
    );

    let owners = car.slot("owners").unwrap().unwrap();
    assert_eq!(owners.cardinality(), Some(("0".to_owned(), "3".to_owned())));

    let colour = car.slot("colour").unwrap().unwrap();
    assert_eq!(
        colour.allowed_values(),
        vec!["red".to_owned(), "green".to_owned(), "blue".to_owned()]
    );
    assert!(colour.is_writable());
    assert!(colour.is_initable());
}

#[test]
fn inspects_method_restrictions_globals_arity_and_implied_templates() {
    let mut environment = Environment::new().unwrap();
    environment
        .build("(defmethod foo ((?a NUMBER SYMBOL) (?b (= 1 1)) $?c) 0)")
        .unwrap();

    let generic = environment.find_defgeneric("foo").unwrap().unwrap();
    let method = generic.methods().next().unwrap();
    let restrictions = method.restrictions().unwrap();
    assert_eq!(restrictions.min_arguments, 2);
    assert_eq!(restrictions.max_arguments, None);
    assert_eq!(restrictions.parameters.len(), 3);
    assert_eq!(
        restrictions.parameters[0].allowed_classes,
        vec!["NUMBER".to_owned(), "SYMBOL".to_owned()]
    );
    assert!(!restrictions.parameters[0].has_query);
    assert!(restrictions.parameters[1].has_query);
    assert!(restrictions.parameters[2].allowed_classes.is_empty());

    // Only the generic value setter can store a multifield in a global.
    environment.build("(defglobal ?*items* = 0)").unwrap();
    let items = environment.string_to_multifield("a b c").unwrap();
    assert!(
        environment
            .set_defglobal_value("items", items.value())
            .unwrap()
    );
    assert_eq!(
        environment
            .defglobal_value("items")
            .unwrap()
            .unwrap()
            .as_multifield()
            .unwrap()
            .len(),
        3
    );
    drop(items);
    assert!(environment.reset_globals());
    assert!(environment.set_reset_globals(false));
    assert!(!environment.reset_globals());

    environment
        .build("(deffunction add (?a ?b) (+ ?a ?b))")
        .unwrap();
    environment
        .build("(deffunction total ($?values) (length$ ?values))")
        .unwrap();
    assert_eq!(
        environment
            .find_deffunction("add")
            .unwrap()
            .unwrap()
            .arity()
            .unwrap(),
        DeffunctionArity {
            min_arguments: 2,
            max_arguments: Some(2)
        }
    );
    assert_eq!(
        environment
            .find_deffunction("total")
            .unwrap()
            .unwrap()
            .arity()
            .unwrap(),
        DeffunctionArity {
            min_arguments: 0,
            max_arguments: None
        }
    );

    // An ordered fact creates an implied deftemplate, which has no source.
    environment.assert_string("(ordered a b)").unwrap();
    environment
        .build("(deftemplate written (slot value))")
        .unwrap();
    assert!(
        environment
            .find_deftemplate("ordered")
            .unwrap()
            .unwrap()
            .is_implied()
    );
    assert!(
        !environment
            .find_deftemplate("written")
            .unwrap()
            .unwrap()
            .is_implied()
    );
}