I have number of key:value pairs such as Company:ABC, Role:Admin etc. I want to write a general rule such that I can search for facts with those pair values. For now, my rule below is just searching for the values in the fact but cannot be bound in such a way to compare company:ABC in rule fact to company:ABC in provided fact.
I have certain facts (dynamic) for employees which is as follows:
Format: Employee
FormatData:
[ Name: Bob,
Role: Admin,
Company: ABC]
And another fact (rule): (Here the rule changes dynamically. So the rule should be very general.)
Format: Rule
FormatData:
[ Format: Employee,
Field: Company
Value: ABC
]
My code for now:
(deftemplate rule
(multislot fact1)
(multislot fact2))
(deftemplate fact
(slot name)
(multislot field)
(multislot value))
(defrule reasoning
(rule (fact1 ?name1 ?field1 ?value1)
(fact2 ?name2 ?field2 ?value2))
(fact (name ?name1) (field $? ?field1 $?) (value $? ?value1 $?))
(fact (name ?name2) (field $? ?field2 $?) (value $? ?value2 $?))
=>
(assert (worked))
Provided facts:
(rule (fact1 'Employee' 'company' 'ABC')
(fact2 'Event' 'Place' 'USA'))
(fact (name 'Employee') (field 'Name' 'Company' 'Role') (value 'Bob' 'ABC' 'Admin'))
(fact (name 'Event') (field 'Place') (value 'USA'))
Expected output:
(worked)
But with this rule and templates even a fact with Employee name ABC and company bob gets the rule fired. I want it such that rule with company:ABC should be matched with only company:ABC in fact Employee. And also, I want the rule to be general and not restricted to Employee, event etc..
Use the length$ function to insure that the field and value retrieved from the multifield slots are from the same position.
CLIPS (6.31 4/1/19)
CLIPS>
(deftemplate rule
(multislot fact1)
(multislot fact2))
CLIPS>
(deftemplate fact
(slot name)
(multislot field)
(multislot value))
CLIPS>
(defrule reasoning
(rule (fact1 ?name1 ?field1 ?value1)
(fact2 ?name2 ?field2 ?value2))
(fact (name ?name1)
(field $?f1 ?field1 $?)
(value $?v1&:(= (length$ ?f1) (length$ ?v1)) ?value1 $?))
(fact (name ?name2)
(field $?f2 ?field2 $?)
(value $?v2&:(= (length$ ?f2) (length$ ?v2)) ?value2 $?))
=>
(assert (worked)))
CLIPS>
(deffacts initial
(rule (fact1 'Employee' 'Company' 'ABC')
(fact2 'Event' 'Place' 'USA'))
(fact (name 'Employee') (field 'Name' 'Company' 'Role') (value 'Bob' 'ABC' 'Admin'))
(fact (name 'Event') (field 'Place') (value 'USA'))
(fact (name 'Employee') (field 'Name' 'Company' 'Role') (value 'ABC' 'Bob' 'Admin')))
CLIPS> (reset)
CLIPS> (agenda)
0 reasoning: f-1,f-2,f-3
For a total of 1 activation.
CLIPS>