I have a wine defined like this:
(deftemplate wine
(slot name)
(slot color)
(slot certainty (type NUMBER) (default 0)))
And the list dof wines defined like this:
(deffacts wines
(wine (name "Chardonnay") (color white))
(wine (name "Merlot") (color red))
(wine (name "Cabernet sauvignon") (color red)))
Now, in case a rule gets triggered, I'd like to increase certainty value for items in a list which have a color slot set to "red".
Any ideas how to accomplish this?
I'm new to CLIPS so I'm sure there is a better way but the following rules do what you want:
(defrule inc-wines-with-color
(increase-all-with color ?color ?amount)
(wine (name ?name) (color ?color))
=>
(assert (increase-certainty ?name ?amount)))
(defrule retract-inc-all-with
?f <- (increase-all-with $?)
=>
(retract ?f))
(defrule increase-wine-certainty
(not (increase-all-with $?))
?ic <-(increase-certainty ?name ?amount)
?wine <- (wine (name ?name) (certainty ?c))
=>
(printout t "Incrementing " ?name " from " ?c " to " (+ ?amount ?c) crlf)
(modify ?wine (certainty (+ ?amount ?c)))
(retract ?ic))
Here are the results of running it:
CLIPS> (reset)
CLIPS> (assert (increase-all-with color red 0.2))
<Fact-4>
CLIPS> (run)
Incrementing Merlot from 0 to 0.2
Incrementing Cabernet sauvignon from 0 to 0.2
CLIPS> (facts)
f-0 (initial-fact)
f-1 (wine (name "Chardonnay") (color white) (certainty 0))
f-7 (wine (name "Merlot") (color red) (certainty 0.2))
f-8 (wine (name "Cabernet sauvignon") (color red) (certainty 0.2))
For a total of 4 facts.
Note: You may need to set your conflict resolution strategy to LEX or MEA to guarantee proper ordering of the rules.
Related
I have compiled CLIPS 6.4 into a shared library (compiled as C++) so that I can use in a C++ application.
I want to now write a simple test C++ application that allows me to:
Start up the CLIPS engine
Load a CLIPS program (see animal.clp)
Assert a fact from the C++ program to the CLIPS engine and receive responses back from CLIPS in my C++ program
Safely terminate the CLIPS engine and cleanup when nothing on the agenda (all rules fired) - i.e. program completed
Testapp.cc
int main(
int argc,
char *argv[])
{
mainEnv = CreateEnvironment();
// load CLIPS ruleset (program - e.g. animal.clp)
// while( !agenda.empty() ) {
// Receive "question" from CLIPS engine
// Send "Answer" to CLIPS engine
// Cleanly terminate CLIPS engine
return 0;
}
animal.clp
;;;======================================================
;;; Animal Identification Expert System
;;;
;;; A simple expert system which attempts to identify
;;; an animal based on its characteristics.
;;; The knowledge base in this example is a
;;; collection of facts which represent backward
;;; chaining rules. CLIPS forward chaining rules are
;;; then used to simulate a backward chaining inference
;;; engine.
;;;
;;; CLIPS Version 6.4 Example
;;;
;;; To execute, merely load, reset, and run.
;;; Answer questions yes or no.
;;;======================================================
(defmodule MAIN (export ?ALL))
(defmodule VALIDATE (import MAIN ?ALL))
(defmodule CHAIN (import MAIN ?ALL))
(defmodule ASK (import MAIN ?ALL))
;;;*************************
;;;* DEFGLOBAL DEFINITIONS *
;;;*************************
(defglobal MAIN
?*rule-index* = 1
?*validate* = TRUE)
;;;***************************
;;;* DEFFUNCTION DEFINITIONS *
;;;***************************
(deffunction generate-rule-name ()
(bind ?name (sym-cat rule- ?*rule-index*))
(bind ?*rule-index* (+ ?*rule-index* 1))
(return ?name))
;;;***************************
;;;* DEFTEMPLATE DEFINITIONS *
;;;***************************
(deftemplate MAIN::rule
(slot name (default-dynamic (generate-rule-name)))
(slot validate (default no))
(multislot if)
(multislot then)
(multislot processed))
(deftemplate MAIN::question
(multislot valid-answers)
(slot variable)
(slot query))
(deftemplate MAIN::answer
(slot variable)
(slot prefix (default ""))
(slot postfix (default "")))
(deftemplate MAIN::goal
(slot variable))
(deftemplate MAIN::variable
(slot name)
(slot value))
(deftemplate MAIN::activity)
(deftemplate MAIN::legalanswers
(multislot values))
;;;**************************
;;;* INFERENCE ENGINE RULES *
;;;**************************
(defrule MAIN::startup
=>
(if ?*validate*
then
(focus VALIDATE CHAIN ASK)
else
(focus CHAIN ASK)))
(defrule MAIN::continue
(declare (salience -10))
?f <- (activity)
=>
(retract ?f)
(focus CHAIN ASK))
(defrule MAIN::goal-satified ""
(goal (variable ?goal))
(variable (name ?goal) (value ?value))
(answer (prefix ?prefix) (postfix ?postfix) (variable ?goal))
=>
(println ?prefix ?value ?postfix))
;;; ##################
;;; CHAIN MODULE RULES
;;; ##################
(defrule CHAIN::propagate-goal ""
(logical (goal (variable ?goal))
(rule (if ?variable $?)
(then ?goal ? ?value)))
=>
(assert (goal (variable ?variable))))
(defrule CHAIN::modify-rule-match-is ""
(variable (name ?variable) (value ?value))
?f <- (rule (if ?variable is ?value and $?rest)
(processed $?p))
=>
(modify ?f (if ?rest)
(processed ?p ?variable is ?value and)))
(defrule CHAIN::rule-satisfied-is ""
(variable (name ?variable) (value ?value))
?f <- (rule (if ?variable is ?value)
(then ?goal ? ?goal-value)
(processed $?p))
=>
(modify ?f (if)
(processed ?p ?variable is ?value #)))
(defrule CHAIN::apply-rule ""
(logical (rule (if)
(then ?goal ? ?goal-value)))
=>
(assert (variable (name ?goal) (value ?goal-value))))
;;; ################
;;; ASK MODULE RULES
;;; ################
(defrule ASK::ask-question-no-legalvalues ""
(not (legalanswers))
?f1 <- (goal (variable ?variable))
(question (variable ?variable) (query ?text))
(not (variable (name ?variable)))
=>
(assert (activity))
(retract ?f1)
(print ?text " ")
(assert (variable (name ?variable) (value (read)))))
(defrule ASK::ask-question-legalvalues ""
(legalanswers (values $?answers))
?f1 <- (goal (variable ?variable))
(question (variable ?variable) (query ?text))
(not (variable (name ?variable)))
=>
(assert (activity))
(retract ?f1)
(print ?text " ")
(print ?answers " ")
(bind ?reply (read))
(if (lexemep ?reply)
then
(bind ?reply (lowcase ?reply)))
(if (member$ ?reply ?answers)
then (assert (variable (name ?variable) (value ?reply)))
else (assert (goal (variable ?variable)))))
;;; #####################
;;; VALIDATE MODULE RULES
;;; #####################
(defrule VALIDATE::copy-rule
(declare (salience 10))
?f <- (rule (validate no))
=>
(duplicate ?f (validate yes))
(modify ?f (validate done)))
(defrule VALIDATE::next-condition
(declare (salience -10))
?f <- (rule (name ?name) (validate yes)
(if ?a ?c ?v and $?rest))
=>
(modify ?f (if ?rest)))
(defrule VALIDATE::validation-complete
(declare (salience -10))
?f <- (rule (validate yes) (if ? ? ?))
=>
(retract ?f))
;;; *******************
;;; Validation - Syntax
;;; *******************
(defrule VALIDATE::and-connector
?f <- (rule (name ?name) (validate yes)
(if ?a ?c ?v ?connector&~and $?))
=>
(retract ?f)
(println "In rule " ?name ", if conditions must be connected using and:" crlf
" " ?a " " ?c " " ?v " *" ?connector "*"))
(defrule VALIDATE::and-requires-additional-condition
?f <- (rule (name ?name) (validate yes)
(if ?a ?c ?v and))
=>
(retract ?f)
(println "In rule " ?name ", an additional condition should follow the final and:" crlf
" " ?a " " ?c " " ?v " and <missing condition>"))
(defrule VALIDATE::incorrect-number-of-then-terms
?f <- (rule (name ?name) (validate yes)
(then $?terms&:(<> (length$ ?terms) 3)))
=>
(retract ?f)
(println "In rule " ?name ", then portion should be of the form <variable> is <value>:" crlf
" " (implode$ ?terms)))
(defrule VALIDATE::incorrect-number-of-if-terms
?f <- (rule (name ?name) (validate yes)
(if $?terms&:(< (length$ ?terms) 3)))
=>
(retract ?f)
(println "In rule " ?name ", if portion contains an incomplete condition:" crlf
" " (implode$ ?terms)))
(defrule VALIDATE::incorrect-then-term-syntax
?f <- (rule (name ?name) (validate yes)
(then ?a ?c&~is ?v))
=>
(retract ?f)
(println "In rule " ?name ", then portion should be of the form <variable> is <value>:" crlf
" " ?a " " ?c " " ?v " "))
(defrule VALIDATE::incorrect-if-term-syntax
?f <- (rule (name ?name) (validate yes)
(if ?a ?c&~is ?v $?))
=>
(retract ?f)
(println "In rule " ?name ", if portion comparator should be \"is\"" crlf
" " ?a " " ?c " " ?v " "))
(defrule VALIDATE::illegal-variable-value
?f <- (rule (name ?name) (validate yes)
(if ?a ?c ?v $?))
(question (variable ?a) (valid-answers))
(legalanswers (values $?values))
(test (not (member$ ?v ?values)))
=>
(retract ?f)
(println "In rule " ?name ", the value " ?v " is not legal for variable " ?a ":" crlf
" " ?a " " ?c " " ?v))
(defrule VALIDATE::reachable
(rule (name ?name) (validate yes)
(if ?a ?c ?v $?))
(not (question (variable ?a)))
(not (rule (then ?a $?)))
=>
(println "In rule " ?name " no question or rule could be found "
"that can supply a value for the variable " ?a ":" crlf
" " ?a " " ?c " " ?v))
(defrule VALIDATE::used "TBD lower salience"
?f <- (rule (name ?name) (validate yes)
(then ?a is ?v))
(not (goal (variable ?a)))
(not (rule (if ?a ? ?v $?)))
=>
(retract ?f)
(println "In rule " ?name " the conclusion for variable " ?a
" is neither referenced by any rules nor the primary goal" crlf
" " ?a " is " ?v))
(defrule VALIDATE::variable-in-both-if-and-then
?f <- (rule (name ?name) (validate yes)
(if ?a $?)
(then ?a is ?v))
=>
(retract ?f)
(println "In rule " ?name " the variable " ?a
" is used in both the if and then sections"))
(defrule VALIDATE::question-variable-unreferenced
(question (variable ?a) (query ?q))
(not (rule (validate done) (if $? ?a is ?v $?)))
=>
(println "The question \"" ?q "\", assigns a value to the variable " ?a
" which is not referenced by any rules"))
;;;***************************
;;;* DEFFACTS KNOWLEDGE BASE *
;;;***************************
(deffacts MAIN::knowledge-base
(goal (variable type.animal))
(legalanswers (values yes no))
(rule (if backbone is yes)
(then superphylum is backbone))
(rule (if backbone is no)
(then superphylum is jellyback))
(question (variable backbone)
(query "Does your animal have a backbone?"))
(rule (if superphylum is backbone and
warm.blooded is yes)
(then phylum is warm))
(rule (if superphylum is backbone and
warm.blooded is no)
(then phylum is cold))
(question (variable warm.blooded)
(query "Is the animal warm blooded?"))
(rule (if superphylum is jellyback and
live.prime.in.soil is yes)
(then phylum is soil))
(rule (if superphylum is jellyback and
live.prime.in.soil is no)
(then phylum is elsewhere))
(question (variable live.prime.in.soil)
(query "Does your animal live primarily in soil?"))
(rule (if phylum is warm and
has.breasts is yes)
(then class is breasts))
(rule (if phylum is warm and
has.breasts is no)
(then type.animal is bird/penguin))
(question (variable has.breasts)
(query "Normally, does the female of your animal nurse its young with milk?"))
(rule (if phylum is cold and
always.in.water is yes)
(then class is water))
(rule (if phylum is cold and
always.in.water is no)
(then class is dry))
(question (variable always.in.water)
(query "Is your animal always in water?"))
(rule (if phylum is soil and
flat.bodied is yes)
(then type.animal is flatworm))
(rule (if phylum is soil and
flat.bodied is no)
(then type.animal is worm/leech))
(question (variable flat.bodied)
(query "Does your animal have a flat body?"))
(rule (if phylum is elsewhere and
body.in.segments is yes)
(then class is segments))
(rule (if phylum is elsewhere and
body.in.segments is no)
(then class is unified))
(question (variable body.in.segments)
(query "Is the animals body in segments?"))
(rule (if class is breasts and
can.eat.meat is yes)
(then order is meat))
(rule (if class is breasts and
can.eat.meat is no)
(then order is vegy))
(question (variable can.eat.meat)
(query "Does your animal eat red meat?"))
(rule (if class is water and
boney is yes)
(then type.animal is fish))
(rule (if class is water and
boney is no)
(then type.animal is shark/ray))
(question (variable boney)
(query "Does your animal have a boney skeleton?"))
(rule (if class is dry and
scaly is yes)
(then order is scales))
(rule (if class is dry and
scaly is no)
(then order is soft))
(question (variable scaly)
(query "Is your animal covered with scaled skin?"))
(rule (if class is segments and
shell is yes)
(then order is shell))
(rule (if class is segments and
shell is no)
(then type.animal is centipede/millipede/insect))
(question (variable shell)
(query "Does your animal have a shell?"))
(rule (if class is unified and
digest.cells is yes)
(then order is cells))
(rule (if class is unified and
digest.cells is no)
(then order is stomach))
(question (variable digest.cells)
(query "Does your animal use many cells to digest its food instead of a stomach?"))
(rule (if order is meat and
fly is yes)
(then type.animal is bat))
(rule (if order is meat and
fly is no)
(then family is nowings))
(question (variable fly)
(query "Can your animal fly?"))
(rule (if order is vegy and
hooves is yes)
(then family is hooves))
(rule (if order is vegy and
hooves is no)
(then family is feet))
(question (variable hooves)
(query "Does your animal have hooves?"))
(rule (if order is scales and
rounded.shell is yes)
(then type.animal is turtle))
(rule (if order is scales and
rounded.shell is no)
(then family is noshell))
(question (variable rounded.shell)
(query "Does the animal have a rounded shell?"))
(rule (if order is soft and
jump is yes)
(then type.animal is frog))
(rule (if order is soft and
jump is no)
(then type.animal is salamander))
(question (variable jump)
(query "Does your animal jump?"))
(rule (if order is shell and
tail is yes)
(then type.animal is lobster))
(rule (if order is shell and
tail is no)
(then type.animal is crab))
(question (variable tail)
(query "Does your animal have a tail?"))
(rule (if order is cells and
stationary is yes)
(then family is stationary))
(rule (if order is cells and
stationary is no)
(then type.animal is jellyfish))
(question (variable stationary)
(query "Is your animal attached permanently to an object?"))
(rule (if order is stomach and
multicelled is yes)
(then family is multicelled))
(rule (if order is stomach and
multicelled is no)
(then type.animal is protozoa))
(question (variable multicelled)
(query "Is your animal made up of more than one cell?"))
(rule (if family is nowings and
opposing.thumb is yes)
(then genus is thumb))
(rule (if family is nowings and
opposing.thumb is no)
(then genus is nothumb))
(question (variable opposing.thumb)
(query "Does your animal have an opposing thumb?"))
(rule (if family is hooves and
two.toes is yes)
(then genus is twotoes))
(rule (if family is hooves and
two.toes is no)
(then genus is onetoe))
(question (variable two.toes)
(query "Does your animal stand on two toes/hooves per foot?"))
(rule (if family is feet and
live.in.water is yes)
(then genus is water))
(rule (if family is feet and
live.in.water is no)
(then genus is dry))
(question (variable live.in.water)
(query "Does your animal live in water?"))
(rule (if family is noshell and
limbs is yes)
(then type.animal is crocodile/alligator))
(rule (if family is noshell and
limbs is no)
(then type.animal is snake))
(question (variable limbs)
(query "Does your animal have limbs?"))
(rule (if family is stationary and
spikes is yes)
(then type.animal is sea.anemone))
(rule (if family is stationary and
spikes is no)
(then type.animal is coral/sponge))
(question (variable spikes)
(query "Does your animal normally have spikes radiating from its body?"))
(rule (if family is multicelled and
spiral.shell is yes)
(then type.animal is snail))
(rule (if family is multicelled and
spiral.shell is no)
(then genus is noshell))
(question (variable spiral.shell)
(query "Does your animal have a spiral-shaped shell?"))
(rule (if genus is thumb and
prehensile.tail is yes)
(then type.animal is monkey))
(rule (if genus is thumb and
prehensile.tail is no)
(then species is notail))
(question (variable prehensile.tail)
(query "Does your animal have a prehensile tail?"))
(rule (if genus is nothumb and
over.400 is yes)
(then species is 400))
(rule (if genus is nothumb and
over.400 is no)
(then species is under400))
(question (variable over.400)
(query "Does an adult normally weigh over 400 pounds?"))
(rule (if genus is twotoes and
horns is yes)
(then species is horns))
(rule (if genus is twotoes and
horns is no)
(then species is nohorns))
(question (variable horns)
(query "Does your animal have horns?"))
(rule (if genus is onetoe and
plating is yes)
(then type.animal is rhinoceros))
(rule (if genus is onetoe and
plating is no)
(then type.animal is horse/zebra))
(question (variable plating)
(query "Is your animal covered with a protective plating?"))
(rule (if genus is water and
hunted is yes)
(then type.animal is whale))
(rule (if genus is water and
hunted is no)
(then type.animal is dolphin/porpoise))
(question (variable hunted)
(query "Is your animal, unfortunately, commercially hunted?"))
(rule (if genus is dry and
front.teeth is yes)
(then species is teeth))
(rule (if genus is dry and
front.teeth is no)
(then species is noteeth))
(question (variable front.teeth)
(query "Does your animal have large front teeth?"))
(rule (if genus is noshell and
bivalve is yes)
(then type.animal is clam/oyster))
(rule (if genus is noshell and
bivalve is no)
(then type.animal is squid/octopus))
(question (variable bivalve)
(query "Is your animal protected by two half-shells?"))
(rule (if species is notail and
nearly.hairless is yes)
(then type.animal is man))
(rule (if species is notail and
nearly.hairless is no)
(then subspecies is hair))
(question (variable nearly.hairless)
(query "Is your animal nearly hairless?"))
(rule (if species is 400 and
land.based is yes)
(then type.animal is bear/tiger/lion))
(rule (if species is 400 and
land.based is no)
(then type.animal is walrus))
(question (variable land.based)
(query "Is your animal land based?"))
(rule (if species is under400 and
thintail is yes)
(then type.animal is cat))
(rule (if species is under400 and
thintail is no)
(then type.animal is coyote/wolf/fox/dog))
(question (variable thintail)
(query "Does your animal have a thin tail?"))
(rule (if species is nohorns and
lives.in.desert is yes)
(then type.animal is camel))
(rule (if species is nohorns and
lives.in.desert is no and
semi.aquatic is no)
(then type.animal is giraffe))
(rule (if species is nohorns and
lives.in.desert is no and
semi.aquatic is yes)
(then type.animal is hippopotamus))
(question (variable lives.in.desert)
(query "Does your animal normally live in the desert?"))
(question (variable semi.aquatic)
(query "Is your animal semi-aquatic?"))
(rule (if species is teeth and
large.ears is yes)
(then type.animal is rabbit))
(rule (if species is teeth and
large.ears is no)
(then type.animal is rat/mouse/squirrel/beaver/porcupine))
(question (variable large.ears)
(query "Does your animal have large ears?"))
(rule (if species is noteeth and
pouch is yes)
(then type.animal is "kangaroo/koala bear"))
(rule (if species is noteeth and
pouch is no)
(then type.animal is mole/shrew/elephant))
(question (variable pouch)
(query "Does your animal have a pouch?"))
(rule (if subspecies is hair and
long.powerful.arms is yes)
(then type.animal is orangutan/gorilla/chimpanzie))
(rule (if subspecies is hair and
long.powerful.arms is no)
(then type.animal is baboon))
(question (variable long.powerful.arms)
(query "Does your animal have long, powerful arms?"))
(rule (if species is horns and
fleece is yes)
(then type.animal is sheep/goat))
(rule (if species is horns and
fleece is no)
(then subsubspecies is nofleece))
(question (variable fleece)
(query "Does your animal have fleece?"))
(rule (if subsubspecies is nofleece and
domesticated is yes)
(then type.animal is cow))
(rule (if subsubspecies is nofleece and
domesticated is no)
(then type.animal is deer/moose/antelope))
(question (variable domesticated)
(query "Is your animal domesticated?"))
(answer (prefix "I think your animal is a ") (variable type.animal) (postfix ".")))
How can I interact with CLIPS from C++ in the simple way described? Specifically, what functions do I need to call/implement in order to implement the functionality required in Test.cc?
The CLIPS Advanced Programming Guide is here: http://clipsrules.sourceforge.net/documentation/v640/apg.pdf
You can use the Load function (section 3.2.2) to load a file. There is an example of its use in section 3.6.1.
You can use the GetNextActivation function (section 12.7.1) to determine if the agenda has any activations.
The simplest way to create facts is using the AssertString function (section 3.3.1). Sections 3.6.2, 4.5.4, and 5.3 have an example use of this function. You can also use the FactBuilder functions described in section 7.1 (with an example in section 7.6.1).
If the results of your program running are represented by facts, you can use the fact query functions via the Eval function to retrieve those values from your program. Section 4.5.4 has an example.
The code is asking a y/n question to the user and making a change - simple. The statement seems to accept only integer and float types and I need only two answers, so I used 1 and 0, and excluded the rest, BUT it reads only numbers, so only the numbers are excluded, not characters.
(defrule rule01
=>
(printout t "Question (yes=1/no=0)?" crlf)
(bind ?x (read))
(if (!= ?x 1)
then
(if (= ?x 0)
then
(assert (rule01 no))
else (printout t "Use ONLY 0 OR 1 for your answers!" crlf))
else (assert (rule01 yes))))
Currently, when you try to type in a character, it returns the following:
CLIPS> (run)
Question (yes=1/no=0)?
g
[ARGACCES5] Function <> expected argument #1 to be of type integer or float
[PRCCODE4] Execution halted during the actions of defrule rule01.
How can I put in an exception for characters?
Use eq and neq in place of = and <>.
CLIPS (6.31 6/12/19)
CLIPS>
(defrule rule01
=>
(printout t "Question (yes=1/no=0)?" crlf)
(bind ?x (read))
(if (neq ?x 1)
then
(if (eq ?x 0)
then
(assert (rule01 no))
else (printout t "Use ONLY 0 OR 1 for your answers!" crlf))
else (assert (rule01 yes))))
CLIPS> (reset)
CLIPS> (run)
Question (yes=1/no=0)?
g
Use ONLY 0 OR 1 for your answers!
CLIPS>
I am new to clips and I have not idea how to iterate through lists correctly and to add them together and put the sum on the same position.
I have two lists defined as facts and I want to create a list that will have the sums of the lists of the elements on the same position.
Ex:
(deffacts lists
(list 1 2 3 4)
(list 1 2 3 4)
)
And the result should be : (listSum 2 4 6 8)
Any help or advice is welcomed. Thank you!
Here's a function that will merge two lists:
CLIPS (6.31 6/12/19)
CLIPS>
(deffunction add-merge (?m1 ?m2)
;; Get the lengths of the multifields
(bind ?l1 (length$ ?m1))
(bind ?l2 (length$ ?m2))
;; Swap values if the first multifield is not the largest
(if (> ?l2 ?l1)
then
(bind ?tmp ?l1)
(bind ?l1 ?l2)
(bind ?l2 ?tmp)
(bind ?tmp ?m1)
(bind ?m1 ?m2)
(bind ?m2 ?tmp))
;; Merge the values
(bind ?rv (create$))
(loop-for-count (?i ?l2)
(bind ?rv (create$ ?rv (+ (nth$ ?i ?m1) (nth$ ?i ?m2)))))
(loop-for-count (?i (+ ?l2 1) ?l1)
(bind ?rv (create$ ?rv (nth$ ?i ?m1))))
?rv)
CLIPS> (add-merge (create$ 1 2 3 4) (create$ 5 6 7 8))
(6 8 10 12)
CLIPS>
With the default settings in CLIPS, you can't have duplicate facts, so if you want two lists with the same values you need to create a slot that will contain a unique value for each list:
CLIPS>
(deftemplate list
(slot id (default-dynamic (gensym*)))
(multislot values))
CLIPS>
(deffacts lists
(list (id l1) (values 1 2 3 4))
(list (id l2) (values 1 2 3 4)))
CLIPS>
Then you can create a rule which will merge two lists:
CLIPS>
(defrule merge
(sum-lists ?l1 ?l2)
(list (id ?l1) (values $?v1))
(list (id ?l2) (values $?v2))
=>
(assert (list (values (add-merge ?v1 ?v2)))))
CLIPS> (reset)
CLIPS> (assert (sum-lists l1 l2))
<Fact-3>
CLIPS> (run)
CLIPS> (facts)
f-0 (initial-fact)
f-1 (list (id l1) (values 1 2 3 4))
f-2 (list (id l2) (values 1 2 3 4))
f-3 (sum-lists l1 l2)
f-4 (list (id gen1) (values 2 4 6 8))
For a total of 5 facts.
CLIPS>
I am making a CLIPS exercise where I order a list in descending order. The thing is that I want to print the list only one time and only once it has been completely ordered, does anybody know how can I check that? So far I have this, and it properly orders the list. Thanks in advance :)
(deffacts hechos-iniciales
(lista 5 7 3 1 8 4 2 6)
)
(defrule ordena-lista
?indice<-(lista $?ini ?num1 ?num2 $?fin)
(test (< ?num1 ?num2))
=>
(assert (lista $?ini ?num2 ?num1 $?fin))
(retract ?indice)
)
You can either create a lower salience rule to print the result or have the rule printing the list check that there are no unsorted lists.
CLIPS>
(deffacts hechos-iniciales
(lista 5 7 3 1 8 4 2 6))
CLIPS>
(defrule ordena-lista
?indice<-(lista $?ini ?num1 ?num2 $?fin)
(test (< ?num1 ?num2))
=>
(assert (lista $?ini ?num2 ?num1 $?fin))
(retract ?indice))
CLIPS>
(defrule print-lista-1
(declare (salience -10))
(lista $?values)
=>
(printout t "print1: " (str-implode ?values) crlf))
CLIPS>
(defrule print-lista-2
(lista $?values)
(not (lista $?ini ?num1 ?num2&:(< ?num1 ?num2) $?fin))
=>
(printout t "print2: " (str-implode ?values) crlf))
CLIPS> (reset)
CLIPS> (run)
print2: 8 7 6 5 4 3 2 1
print1: 8 7 6 5 4 3 2 1
CLIPS>
There are so-called strict formats, like pdb - where the meaning of the symbol is defined by the colomn number of the symbol. For example here is a specification of the above mentioned pdb format.
Is there a way I can apply face colour basing on the column range?
One can normally add a regexp to be highlighted, for example for the current session in the following way:
(font-lock-add-keywords nil '(("\\[\\(.+?\\)\\]" . font-lock-keyword-face)))
So is there a way to specify that face at columns, say 7-11 - should be, say - red?
Edit:
So the answer is:
(font-lock-add-keywords nil '(("^.\\{2\\}\\(.\\{2\\}\\)" 1 font-lock-warning-face)))
Define a regexp that will select the appropriate column.
For example to select the 3rd column (assuming the columns contains only letter for the clarity of the example) you can do something like "\(?:[a-z]+ \)\{2\}\([a-z]+\)" and then match on the first group.
Of course you can create such a string by using format and passing it some arguments for more flexibility.
(font-lock-add-keywords nil '(("\\(?:[a-z]+ \\)\\{2\\}\\([a-z]+\\)" 1 font-lock-warning-face)))
As an other direction, the following code was found in whitespace-mode which highlights the characters over a certain number:
;; Show "long" lines
(list
(let ((line-column (or whitespace-line-column fill-column)))
(format
"^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
whitespace-tab-width
(1- whitespace-tab-width)
(/ line-column whitespace-tab-width)
(let ((rem (% line-column whitespace-tab-width)))
(if (zerop rem)
""
(format ".\\{%d\\}" rem)))))
(if (memq 'lines whitespace-active-style)
0 ; whole line
2) ; line tail
whitespace-line t)
The following applyies the coloring initially to the text from 7th to 11th column:
(defun bk-pdb-color-ATOM-initially()
(interactive)
(save-excursion
(goto-char (point-min))
(while (search-forward-regexp "^ATOM \\(.\\{5\\}\\).\\(.\\{4\\}\\).\\(.\\{3\\}\\).\\(.\\{5\\}\\).\\{4\\}\\(.\\{24\\}\\)" nil t)
(progn
(overlay-put
(make-overlay (match-beginning 1) (match-end 1))
'face '(:foreground "blue"))
(overlay-put
(make-overlay (match-beginning 2) (match-end 2))
'face '(:foreground "red"))
(overlay-put
(make-overlay (match-beginning 3) (match-end 3))
'face '(:foreground "purple1"))
(overlay-put
(make-overlay (match-beginning 4) (match-end 4))
'face '(:foreground "orange"))
(overlay-put
(make-overlay (match-beginning 5) (match-end 5))
'face '(:foreground "green"))
t nil)
)))