2016-07-13 5 views
1

Ich versuche, eine Regel genau einmal für jedes eindeutige Paar unterschiedlicher Instanzen auszulösen.Eindeutige Reihenfolge der Instance-Bindung

Ein triviales Beispiel folgt:

(defclass USER_THING (is-a USER)) 

(definstances KNOWN_THINGS 
    (thing-a of USER_THING) 
    (thing-b of USER_THING) 
    (thing-c of USER_THING)) 

(defrule match-things 
    ?thing0 <- (object (is-a USER_THING)) 
    ?thing1 <- (object (is-a USER_THING)) 
=> 
    (printout t "-------" crlf) 
    (printout t "thing0 " (instance-name ?thing0) crlf) 
    (printout t "thing1 " (instance-name ?thing1) crlf)) 

Klar, würden wir das kartesische Produkt von KNOWN_THINGS mit sich selbst erwarten, das ist genau das, was wir bekommen:

 
CLIPS> (reset) 
CLIPS> (run) 
------- 
thing0 [thing-c] 
thing1 [thing-c] 
------- 
thing0 [thing-c] 
thing1 [thing-b] 
------- 
thing0 [thing-c] 
thing1 [thing-a] 
------- 
thing0 [thing-a] 
thing1 [thing-c] 
------- 
thing0 [thing-b] 
thing1 [thing-c] 
------- 
thing0 [thing-b] 
thing1 [thing-b] 
------- 
thing0 [thing-b] 
thing1 [thing-a] 
------- 
thing0 [thing-a] 
thing1 [thing-b] 
------- 
thing0 [thing-a] 
thing1 [thing-a] 

Während meine gewünschte Ausgabe ist mehr verwandt mit:

 
CLIPS> (reset) 
CLIPS> (run) 
------- 
thing0 [thing-a] 
thing1 [thing-b] 
------- 
thing0 [thing-a] 
thing1 [thing-c] 
------- 
thing0 [thing-b] 
thing1 [thing-c] 

Ich habe Erfahrung mit Apache Jena forward Dies ist keine richtige CLIPS Regel

(defrule match-things 
    ?thing0 <- (object (is-a USER_THING)) 
    ?thing1 <- (object (is-a USER_THING)) 
    (> (str-compare (instance-name ?thing0) (instance-name ?thing1)) 0) 
=> 
    (printout t "-------" crlf) 
    (printout t "thing0 " (instance-name ?thing0) crlf) 
    (printout t "thing1 " (instance-name ?thing1) crlf)) 

: -chaining Deduktionssystems, wobei ich würde einfach eine Regel-Klausel hinzufügen, eine willkürliche Ordnung auf den Instanznamen zu erzwingen. Was kann ich tun, um meine gewünschte Wirkung zu erzielen? Ich kann den Instanzen Informationen hinzufügen (z. B. eine beliebige numerische oder Zeichenfolgen-ID), um dies zu erleichtern.

Antwort

1

das Testbedingungselement Verwendung:

(defrule match-things 
    ?thing0 <- (object (is-a USER_THING)) 
    ?thing1 <- (object (is-a USER_THING)) 
    (test (> (str-compare (instance-name ?thing0) (instance-name ?thing1)) 0)) 
=> 
    (printout t "-------" crlf) 
    (printout t "thing0 " (instance-name ?thing0) crlf) 
    (printout t "thing1 " (instance-name ?thing1) crlf)) 
Verwandte Themen