Some work on flow, mainly code tidy-up

This commit is contained in:
Simon Brooke 2023-07-11 09:15:56 +01:00
parent ca3861b505
commit fb39f1ee9c
36 changed files with 5191 additions and 3995 deletions

View file

@ -1,11 +1,10 @@
(ns mw-parser.declarative-test
(:use clojure.pprint
mw-engine.core
mw-engine.world
mw-engine.utils
mw-parser.utils)
(:require [clojure.test :refer :all]
[mw-parser.declarative :refer :all]))
(:require [clojure.test :refer [deftest is testing]]
[mw-engine.core :refer [transform-world]]
[mw-engine.utils :refer [get-cell]]
[mw-engine.world :refer [make-world]]
[mw-parser.declarative :refer [compile-rule parse-rule]]
[mw-parser.utils :refer [rule?]]))
(deftest rules-tests
(testing "Rule parser - does not test whether generated functions actually work, just that something is generated!"
@ -18,8 +17,7 @@
(is (rule? (parse-rule "if deer is more than 1 and wolves is more than 1 then deer should be deer - wolves")))
(is (rule? (parse-rule "if state is forest and fertility is between 55 and 75 then state should be climax")))
(is (rule? (parse-rule "if fertility is between 55 and 75 then state should be climax")))
(is (rule? (parse-rule "if altitude is less than 100 and state is forest then state should be climax and deer should be 3")))
))
(is (rule? (parse-rule "if altitude is less than 100 and state is forest then state should be climax and deer should be 3")))))
(deftest neighbours-rules-tests
(testing "Rules which relate to neighbours - hard!"

View file

@ -0,0 +1,66 @@
(ns mw-parser.flow-test
(:require ;; [clojure.pprint :as pprint]
[clojure.test :refer [deftest is testing]] ;; [mw-engine.core :refer [transform-world]]
[mw-parser.flow :refer [parse-flow simplify-flow]]))
(deftest parse-flow-tests
(testing "flow-grammar"
(let [rule "flow 1 food from house having food more than 10 to house within 2 with least food"
expected '(:FLOW-RULE
(:SIMPLE-EXPRESSION (:NUMBER "1"))
(:SYMBOL "food")
(:FROM "from")
(:SOURCE
(:PROPERTY-CONDITION
(:SYMBOL "state")
(:QUALIFIER (:EQUIVALENCE (:IS "is")))
(:EXPRESSION (:VALUE [:SYMBOL "house"])))
(:WITH "having")
(:PROPERTY-CONDITION
(:SYMBOL "food")
(:QUALIFIER (:COMPARATIVE-QUALIFIER (:MORE "more") (:THAN "than")))
(:NUMBER "10")))
(:TO-HOW (:TO "to"))
(:DESTINATION
(:PROPERTY-CONDITION
(:SYMBOL "state")
(:QUALIFIER (:EQUIVALENCE (:IS "is")))
(:EXPRESSION (:VALUE [:SYMBOL "house"])))
(:WITHIN "within")
(:VALUE (:NUMBER "2"))
(:WITH "with")
(:FLOW-CONDITIONS
(:DETERMINER-CONDITION (:LEAST "least") (:SYMBOL "food")))))
actual (simplify-flow (parse-flow rule))]
(is (= actual expected) rule))
(let [rule "flow 10% food from house having food more than 10 to each house within 2 with food less than 4"
expected '(:FLOW-RULE
(:PERCENTAGE (:NUMBER "10") "%")
(:SYMBOL "food")
(:FROM "from")
(:SOURCE
(:PROPERTY-CONDITION
(:SYMBOL "state")
(:QUALIFIER (:EQUIVALENCE (:IS "is")))
(:EXPRESSION (:VALUE [:SYMBOL "house"])))
(:WITH "having")
(:PROPERTY-CONDITION
(:SYMBOL "food")
(:QUALIFIER (:COMPARATIVE-QUALIFIER (:MORE "more") (:THAN "than")))
(:NUMBER "10")))
(:TO-HOW (:TO-EACH (:TO "to") (:EACH "each")))
(:DESTINATION
(:PROPERTY-CONDITION
(:SYMBOL "state")
(:QUALIFIER (:EQUIVALENCE (:IS "is")))
(:EXPRESSION (:VALUE [:SYMBOL "house"])))
(:WITHIN "within")
(:VALUE (:NUMBER "2"))
(:WITH "with")
(:FLOW-CONDITIONS
(:PROPERTY-CONDITION
(:SYMBOL "food")
(:QUALIFIER (:COMPARATIVE-QUALIFIER (:LESS "less") (:THAN "than")))
(:NUMBER "4")))))
actual (simplify-flow (parse-flow rule))]
(is (= actual expected) rule))))

View file

@ -0,0 +1,30 @@
(ns mw-parser.utils-test
(:require [clojure.test :refer [deftest is testing]]
[mw-parser.utils :refer [assert-type rule? search-tree
suitable-fragment? TODO]]))
(deftest fragment-tests
(testing "Functions finding and identifying rule fragments"
(let [rule '(:RULE
(:IF "if")
(:PROPERTY-CONDITION
(:SYMBOL "state")
(:QUALIFIER (:EQUIVALENCE (:IS "is")))
(:SYMBOL "forest"))
(:ACTIONS
(:SIMPLE-ACTION
(:SYMBOL "state")
(:BECOMES "should be")
(:SYMBOL "climax"))))
not-rule [:FROBOZ :foo :bar :ban]]
(is (rule? rule))
(is (not (rule? not-rule)))
(is (= nil (assert-type rule :RULE)))
(is (thrown-with-msg?
Exception #"Expected a :RULE fragment" (assert-type not-rule :RULE)))
(is (= '(:EQUIVALENCE (:IS "is")) (search-tree rule :EQUIVALENCE)))
(is (= nil (search-tree rule :EQUIVOCATION)))
(is (suitable-fragment? '(:EQUIVALENCE (:IS "is")) :EQUIVALENCE))
(is (not (suitable-fragment? :EQUIVALENCE :EQUIVALENCE)))
(is (not (suitable-fragment? '(:EQUIVALENCE (:IS "is")) :QUALIFIER)))
(is (= (TODO "Froboz") "Froboz")))))