From 98f15798692af9b18cc23cd8ab38de9fc11ce0d9 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Tue, 15 Jul 2014 09:22:24 +0100 Subject: [PATCH] Fixed the 'Keyword cannnot be cast to Number' bug. --- src/mw_parser/core.clj | 22 ++++++++++++---------- test/mw_parser/core_test.clj | 33 ++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/mw_parser/core.clj b/src/mw_parser/core.clj index ab402d1..21da195 100644 --- a/src/mw_parser/core.clj +++ b/src/mw_parser/core.clj @@ -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 diff --git a/test/mw_parser/core_test.clj b/test/mw_parser/core_test.clj index c70d736..1b3316b 100644 --- a/test/mw_parser/core_test.clj +++ b/test/mw_parser/core_test.clj @@ -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") + + ))