Search code examples
clips

How to retrieve a handle to facts inside a deffacts construct?


As described by the question, I would somehow try to get an handle to a fact within a deffacts construct. The problem arises because I don't want to redefine the same thing several times in WM (since set-fact-duplication is true) and because I use a structured deftemplate in which a field is a FACT_ADDRESS.enter image description here


Solution

  • You can't bind a fact address within a deffacts construct. What I would suggest instead is to use a symbolic link between the facts. In your case, if the name of the tourism-type, tourism-resort, and hotel facts is unique among facts of each type, you could use that slot as the symbolic link:

    (deftemplate tourism-type
       (slot name)
       (slot score))
    
    (deftemplate hotel
       (slot name)
       (slot tr)
       (slot stars)
       (slot price-per-night))
    
    (deftemplate tourism-resort
       (slot name)
       (slot region)
       (multislot type))
    
    (deffacts the-tourism-type-list
       (tourism-type (name culturale) (score 3))
       (tourism-type (name enogastronomico) (score 4)))
    
    (deffacts the-tourism-resort-list
       (tourism-resort
          (name Venezia)
          (region Veneto)
          (type culturale enogastronomico)))
    
    (deffacts the-hotels-list
       (hotel
          (name hotel1)
          (tr Venezia)
          (stars 3)
          (price-per-night 100)))
    

    In your rules, you can then use the symbolic link to retrieve the linked fact:

    (defrule food-and-wine-hotels
       (hotel (name ?hotel)
              (tr ?tr-name))
       (tourism-resort
          (name ?tr-name)
          (type $? enogastronomico $?))
       =>
       (printout t ?hotel crlf))