You could write the patterns pretty much the same way.
You just have to call functions (wrap, replace, deref - see
below) providing the extra layer.
-W

;
; A wrapper template contains a single slot pointing to
; the actual fact. This could be changed arbitrarily; not
; only in its slot values due to a simple update but also
; its template may change, due to retract/assert. All the
; references to the wrapper may remain.

(deftemplate Wrap (slot ref))

; All wrapped facts have an additional slot pointing to
; its wrapper.
;
(deftemplate Links   (slot wrapper)(slot pred)(slot succ))
(deftemplate String  extends Links (slot str))
(deftemplate Charseq extends Links (slot char)(slot rep))

(deffunction repeat (?c ?n)
  (bind ?str "")
  (while (> ?n 0) do
    (bind ?str (str-cat ?str ?c))
    (-- ?n))
  (return ?str)
)

;
; All asserted facts must be wrapped by calling wrap. Use
; the return value as a slot value linking other facts to
; this fact.
;
(deffunction wrap (?fact)
  (bind ?wrapper (assert (Wrap (ref ?fact))))
  (modify ?fact (wrapper ?wrapper))
  (return ?wrapper)
)

;
; To retract and re-assert a wrapped fact, call replace
; with the newly asserted fact. It retracts the old
; wrapped fact and inserts it into the wrapper.
;
(deffunction replace (?wrapper ?fact)
  (retract ?wrapper.ref)
  (modify ?wrapper (ref ?fact))
)

;
; This is the additional dereferencing operation
; required to access the wrapped fact, given a pointer
; to its wrapper.
;
(deffunction deref (?w)
  (return ?w.ref)
)

(reset)

; Create some wrapped facts - note the wrap call.
;
(bind ?s1 (wrap (assert (String (str "The quick brown fox")))))
(bind ?c1 (wrap (assert (Charseq (char " ")(rep 5)))))
(bind ?s2 (wrap (assert (String (str "jumps over the lazy dog")))))
(bind ?c2 (wrap (assert (Charseq (char "!")(rep 10)))))

; Modify the link fields with pointers to Wrap facts.
; Note the deref call.
;
(modify (deref ?s1) (succ ?c1))
(modify (deref ?c1) (pred ?s1)(succ ?s2))
(modify (deref ?s2) (pred ?c1)(succ ?c2))
(modify (deref ?c2) (pred ?s2))

(facts)

; Substitute all short character repetitions by a String fact.
; Note the replace call.
;
(defrule Expand
  ?charseq <- (Charseq {rep <= 5))
=>
  (bind ?str (repeat ?charseq.char ?charseq.rep))
  (printout t ?charseq.char "x" ?charseq.rep "=" ?str crlf)
  (replace ?charseq.wrapper
           (assert (String (pred ?charseq.pred)(succ ?charseq.succ)(str
?str))))
)

(run)
(facts)


On Fri, Sep 12, 2008 at 2:09 AM, Hal Hildebrand
<[EMAIL PROTECTED]>wrote:

> How would you write the rules relying on the referred to facts?
>
> Just curious to find out, as this would be a useful technique...
>
>
> On Sep 11, 2008, at 6:00 PM, [EMAIL PROTECTED] wrote:
>
>
>> Thank you Wolfgang, this is a clever idea. I will think about it
>> some more.
>>
>>
>>
>> On Sep 11, 2008, at 10:24 AM, Wolfgang Laun wrote:
>>
>> Perhaps a wrapper fact with a single slot containing a reference of
>> the actual fact could server as a stable point of reference. That
>> means that the many references in other "real" facts would remain
>> stable, but, of course, dereferencing becomes more complicated.
>> Only the pointer in the wrapper would have to be updated.
>>
>>
>>
>>
>> --------------------------------------------------------------------
>> To unsubscribe, send the words 'unsubscribe jess-users
>> [EMAIL PROTECTED]'
>> in the BODY of a message to [EMAIL PROTECTED], NOT to the list
>> (use your own address!) List problems? Notify [EMAIL PROTECTED]
>> .
>> --------------------------------------------------------------------
>>
>>
>
>
> --------------------------------------------------------------------
> To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
> in the BODY of a message to [EMAIL PROTECTED], NOT to the list
> (use your own address!) List problems? Notify [EMAIL PROTECTED]
> --------------------------------------------------------------------
>
>

Reply via email to