i'd remove 2 of test conditional elements following rule improve readability of it.
(defrule compare-things (logical ?thing0 <- (object (is-a typed_thing) (type-results $? ?t0 $?))) (logical ?thing1 <- (object (is-a typed_thing) (type-results $? ?t1 $?))) (thing-comparable ?type) ?type0 <- (object (is-a typing) (qualified-type ?type ?model ?mode ?comp0)) ?type1 <- (object (is-a typing) (qualified-type ?type ?model ?mode ?comp1)) ; test exists restrict number of rule firings (test (> (str-compare (instance-name ?thing0) (instance-name ?thing1)) 0)) ; ideally, following 2 tests can removed (test (= (str-compare (instance-name ?type0) (instance-name ?t0)) 0)) (test (= (str-compare (instance-name ?type1) (instance-name ?t1)) 0)) => (make-instance of comparison (compares ?thing0 ?thing1) (score nil) ) (printout t "comparing: " (instance-name ?thing0) (instance-name ?thing1) crlf) )
the multislot field values ?t0
, ?t1
should correspond same instances ?type0
, ?type1
. if replace ?t0
, ?t1
?type0
, ?type1
(which intuitive first attempt), receive following error while loading rules:
defining defrule: compare-things [analysis2] pattern-address ?type0 used in ce #4 bound within pattern ce. [analysis2] pattern-address ?type1 used in ce #5 bound within pattern ce. error: (defrule main::compare-things (logical ?thing0 <- (object (is-a typed_thing) (type-results $? ?type0 $?))) (logical ?thing1 <- (object (is-a typed_thing) (type-results $? ?type1 $?))) (thing-comparable ?type) ?type0 <- (object (is-a typing) (qualified-type ?type ?model ?mode ?comp0)) ?type1 <- (object (is-a typing) (qualified-type ?type ?model ?mode ?comp1)) (test (> (str-compare (instance-name ?thing0) (instance-name ?thing1)) 0)) => (make-instance of comparison (compares ?thing0 ?thing1) (score nil)) (printout t "comparing: " (instance-name ?thing0) (instance-name ?thing1) crlf)) false
the following data being used stimulate rule in development:
(defclass typing (is-a user) (role concrete) (multislot qualified-type (access initialize-only) (type string) (cardinality 4 4)) (slot score (access initialize-only) (type float)) ) (defclass typed_thing (is-a user) (slot id (access initialize-only) (type integer)) (multislot type-results (access initialize-only) (type instance)) ; of typing ) (defclass comparison (is-a user) (multislot compares (access initialize-only) (type instance) ; of typed_thing (cardinality 2 2)) (slot score (access read-write) (type float)) ) ; these facts tag top-level types comparable (deffacts known_comparable_types (thing-comparable "cat-a") (thing-comparable "cat-c") ) (definstances known_things (thing0 of typed_thing (id 0) (type-results (make-instance of typing (qualified-type "cat-a" "x0" "y0" "z0")(score 0.9)) (make-instance of typing (qualified-type "cat-b" "x0" "y0" "z0")(score 0.9)))) (thing1 of typed_thing (id 1) (type-results (make-instance of typing (qualified-type "cat-a" "x0" "y0" "z1")(score 0.9)) (make-instance of typing (qualified-type "cat-a" "x1" "y1" "z0")(score 0.9)))) (thing2 of typed_thing (id 2) (type-results (make-instance of typing (qualified-type "cat-b" "x0" "y0" "z1")(score 0.9)))) )
which should produce following output (as does):
clips> (reset) clips> (run) comparing: [thing1][thing0]
working within limitation indicated error message, can rule compile modification:
(defrule compare-things (logical ?thing0 <- (object (is-a typed_thing) (type-results $? ?t0 $?))) (logical ?thing1 <- (object (is-a typed_thing) (type-results $? ?t1 $?))) (thing-comparable ?type) (object (is-a typing) (name =(instance-name ?t0)) (qualified-type ?type ?model ?mode ?comp0)) (object (is-a typing) (name =(instance-name ?t1)) (qualified-type ?type ?model ?mode ?comp1)) ; test exists restrict number of rule firings (test (> (str-compare (instance-name ?thing0) (instance-name ?thing1)) 0)) => (make-instance of comparison (compares ?thing0 ?thing1) (score nil) ) (printout t "comparing: " (instance-name ?thing0) (instance-name ?thing1) crlf) )
Comments
Post a Comment