Is there a way to allow only unique values for slots in CLIPS

96 views Asked by At
(deftemplate Employee
   (slot employee-id)
   (slot employee-name)
   (slot Role)
   (slot Designation))

(deffacts Employees
   (employee (employee-name X) (employee-id 1) (Role CS) (Designation TA))
   (employee (employee-name Y) (employee-id 2) (Role CS) (Designation SA))
   (employee (employee-name Z) (employee-id 3) (Role CIS) (Designation TA))
   (employee (employee-name W) (employee-id 4) (Role SE) (Designation SA))
   (employee (employee-name Q) (employee-id 5) (Role CIS) (Designation TA))
   (employee (employee-name U) (employee-id 1) (Role CS) (Designation TA)))

In above example, I have entered employee id twice. How can I define employee id to be unique?

1

There are 1 answers

3
Gary Riley On
CLIPS> (defglobal ?*id* = 0)
CLIPS> 
(deffunction next-id ()
   (bind ?*id* (+ ?*id* 1)))
CLIPS> 
(deftemplate employee
   (slot employee-id (default-dynamic (next-id)))
   (slot employee-name)
   (slot Role)
   (slot Designation))
CLIPS> 
(deffacts Employees
   (employee (employee-name X) (Role CS) (Designation TA))
   (employee (employee-name Y) (Role CS) (Designation SA))
   (employee (employee-name Z) (Role CIS) (Designation TA))
   (employee (employee-name W) (Role SE) (Designation SA))
   (employee (employee-name Q) (Role CIS) (Designation TA))
   (employee (employee-name U) (Role CS) (Designation TA)))
CLIPS> (reset)
CLIPS> (facts)
f-0     (initial-fact)
f-1     (employee (employee-id 1) (employee-name X) (Role CS) (Designation TA))
f-2     (employee (employee-id 2) (employee-name Y) (Role CS) (Designation SA))
f-3     (employee (employee-id 3) (employee-name Z) (Role CIS) (Designation TA))
f-4     (employee (employee-id 4) (employee-name W) (Role SE) (Designation SA))
f-5     (employee (employee-id 5) (employee-name Q) (Role CIS) (Designation TA))
f-6     (employee (employee-id 6) (employee-name U) (Role CS) (Designation TA))
For a total of 7 facts.
CLIPS>