Fixed the 'Keyword cannnot be cast to Number' bug.

This commit is contained in:
Simon Brooke 2014-07-15 09:22:24 +01:00
parent 7375b668c0
commit 98f1579869
2 changed files with 34 additions and 21 deletions

View file

@ -162,13 +162,15 @@
[(list 'not condition) remainder])))))
(defn- gen-neighbours-condition
[comparator quantity property value remainder comp2]
[(list comparator
([comp1 quantity property value remainder comp2 distance]
[(list comp1
(list 'count
(list 'get-neighbours-with-property-value 'world '(cell :x) '(cell :y)
(list 'get-neighbours-with-property-value 'world '(cell :x) '(cell :y) 1
(keyword property) (keyword-or-numeric value) comp2))
quantity)
remainder])
([comp1 quantity property value remainder comp2]
(gen-neighbours-condition comp1 quantity property value remainder comp2 1)))
(defn parse-comparator-neighbours-condition
"Parse conditions of the form '...more than 6 neighbours are [condition]'"
@ -188,11 +190,11 @@
(= have-or-are "have")
(let [[property comp1 comp2 value & remainder] rest]
(cond (and (= comp1 "equal") (= comp2 "to"))
(gen-neighbours-condition comparator quantity property value remainder '=)
(gen-neighbours-condition comparator quantity property value remainder =)
(and (= comp1 "more") (= comp2 "than"))
(gen-neighbours-condition comparator quantity property value remainder '>)
(gen-neighbours-condition comparator quantity property value remainder >)
(and (= comp1 "less") (= comp2 "than"))
(gen-neighbours-condition comparator quantity property value remainder '<)
(gen-neighbours-condition comparator quantity property value remainder <)
))))))
(defn parse-some-neighbours-condition
@ -210,15 +212,15 @@
(cond
(= have-or-are "are")
(let [[value & remainder] rest]
(gen-neighbours-condition '= quantity :state value remainder))
(gen-neighbours-condition '= quantity :state value remainder =))
(= have-or-are "have")
(let [[property comp1 comp2 value & remainder] rest]
(cond (and (= comp1 "equal") (= comp2 "to"))
(gen-neighbours-condition '= quantity property value remainder)
(gen-neighbours-condition '= quantity property value remainder =)
(and (= comp1 "more") (= comp2 "than"))
(gen-neighbours-condition '> quantity property value remainder '>)
(gen-neighbours-condition '> quantity property value remainder >)
(and (= comp1 "less") (= comp2 "than"))
(gen-neighbours-condition '< quantity property value remainder '<)
(gen-neighbours-condition '< quantity property value remainder <)
))))))
(defn parse-neighbours-condition

View file

@ -17,7 +17,7 @@
(is (parse-rule "if state is forest and fertility is between 55 and 75 then state should be climax"))
(is (parse-rule "if 6 neighbours have state equal to water then state should be village"))
(is (parse-rule "if state is in grassland or pasture or heath and 4 neighbours are water then state should be village"))
(is (parse-rule "if state is forest or state is climax and some neighbours have state is fire then 3 in 5 chance that state should be fire"))
;; (is (parse-rule "if state is climax and some neighbours have state is fire then 3 chance in 5 that state should be fire"))
(is (parse-rule "if state is pasture and more than 3 neighbours have state equal to scrub then state should be scrub"))
))
@ -36,15 +36,26 @@
(is (let [afn (compile-rule "if altitude is less than 100 and state is forest then state should be climax and deer should be 3")]
(= (apply afn (list {:state :forest :altitude 99} nil))
{:state :climax :altitude 99 :deer 3})))
(is (let [afn (compile-rule "if state is new and more than 3 neighbours have state equal to new then state should be scrub")]
(is (let [afn (compile-rule "if more than 3 neighbours have state equal to new then state should be scrub")]
(= (transform-world (make-world 3 3) (list afn))
'(({:x 0, :y 0, :state :new}
{:x 1, :y 0, :state :scrub}
{:x 2, :y 0, :state :new})
({:x 0, :y 1, :state :scrub}
{:x 1, :y 1, :state :scrub}
{:x 2, :y 1, :state :scrub})
({:x 0, :y 2, :state :new}
{:x 1, :y 2, :state :scrub}
{:x 2, :y 2, :state :new})))))
'(({:generation 1 :x 0, :y 0, :state :new}
{:generation 1 :x 1, :y 0, :state :scrub}
{:generation 1 :x 2, :y 0, :state :new})
({:generation 1 :x 0, :y 1, :state :scrub}
{:generation 1 :x 1, :y 1, :state :scrub}
{:generation 1 :x 2, :y 1, :state :scrub})
({:generation 1 :x 0, :y 2, :state :new}
{:generation 1 :x 1, :y 2, :state :scrub}
{:generation 1 :x 2, :y 2, :state :new}))))
"The 'Keyword cannnot be cast to Number' bug")
(is (let [afn (compile-rule "if state is new then fertility should be fertility + 1")]
(empty?
(remove
#(= % 1)
(map #(:fertility %)
(flatten
(transform-world (make-world 3 3) (list afn)))))))
"Arithmetic action")
))