2017-03-17 3 views
0

Je lis dans this répondre SO qu'il estCLIPS accès à une propriété d'une propriété

better to explicitly retrieve the slot value by matching it rather than using the slot accessor as this will cause the condition to be reevaluated whenever the slot value changes

Et si je veux accéder à la propriété d'un bien immobilier? Par exemple,

donné deux instances a et b des classes A et B, respectivement.

a a une propriété appelée ref_to_b qui est une référence à b. b a une propriété appelée some_prop_of_b.

Comment puis-je correspondre à ce qui suit:

a avec ref_to_b égal à b et some_prop_of_b égal à "some_string".

J'ai essayé mais nous avons eu une erreur:

(defrule my_rule "comment me" 
    (object (is-a A) 
     (ref_to_b ?ref_to_b)) 
    (?ref_to_b 
     (some_prop_of_b "some_string")) 
=> 
) 

Répondre

1

Placez le nom de l'instance de l'instance référencée dans la fente de ref_to_b puis utilisez la fente de nom pour correspondre à la référence:

CLIPS> 
(defclass A (is-a USER) (slot ref_to_b)) 
CLIPS> 
(defclass B (is-a USER) (slot some_prop_of_b)) 
CLIPS> 
(make-instance [b1] of B (some_prop_of_b "some_string")) 
[b1] 
CLIPS> 
(make-instance [b2] of B (some_prop_of_b "not_some_string")) 
[b2] 
CLIPS> 
(make-instance [a] of A (ref_to_b [b2])) 
[a] 
CLIPS> 
(defrule my_rule 
    (object (is-a A) (ref_to_b ?name_b)) 
    (object (name ?name_b) (some_prop_of_b "some_string")) 
    =>) 
CLIPS> (agenda) 
CLIPS> (send [a] put-ref_to_b [b1]) 
[b1] 
CLIPS> (agenda) 
0  my_rule: [a],[b1] 
For a total of 1 activation. 
CLIPS> 
+0

Ah la nom du logement. C'était la clé. – stackoverflowwww