Trying to get code quality up, but in the process I've broken something -
I think, the simplifier.
This commit is contained in:
parent
d2a73ba408
commit
3168c1b2fb
|
@ -1,13 +1,32 @@
|
||||||
;; parse multiple rules from a stream, possibly a file - although the real
|
(ns ^{:doc "parse multiple rules from a stream, possibly a file."
|
||||||
;; objective is to parse rules out of a block of text from a textarea
|
:author "Simon Brooke"}
|
||||||
|
mw-parser.bulk
|
||||||
(ns mw-parser.bulk
|
|
||||||
(:use mw-parser.declarative
|
(:use mw-parser.declarative
|
||||||
mw-engine.utils
|
mw-engine.utils
|
||||||
clojure.java.io
|
clojure.java.io
|
||||||
[clojure.string :only [split trim]])
|
[clojure.string :only [split trim]])
|
||||||
(:import (java.io BufferedReader StringReader)))
|
(:import (java.io BufferedReader StringReader)))
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;;
|
||||||
|
;; This program is free software; you can redistribute it and/or
|
||||||
|
;; modify it under the terms of the GNU General Public License
|
||||||
|
;; as published by the Free Software Foundation; either version 2
|
||||||
|
;; of the License, or (at your option) any later version.
|
||||||
|
;;
|
||||||
|
;; This program is distributed in the hope that it will be useful,
|
||||||
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
;; GNU General Public License for more details.
|
||||||
|
;;
|
||||||
|
;; You should have received a copy of the GNU General Public License
|
||||||
|
;; along with this program; if not, write to the Free Software
|
||||||
|
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||||
|
;; USA.
|
||||||
|
;;
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
|
||||||
(defn comment?
|
(defn comment?
|
||||||
"Is this `line` a comment?"
|
"Is this `line` a comment?"
|
||||||
[line]
|
[line]
|
||||||
|
|
|
@ -1,3 +1,30 @@
|
||||||
|
(ns ^{:doc "A very simple parser which parses production rules."
|
||||||
|
:author "Simon Brooke"}
|
||||||
|
mw-parser.core
|
||||||
|
(:use mw-engine.utils
|
||||||
|
[clojure.string :only [split trim triml]])
|
||||||
|
(:gen-class)
|
||||||
|
)
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;;
|
||||||
|
;; This program is free software; you can redistribute it and/or
|
||||||
|
;; modify it under the terms of the GNU General Public License
|
||||||
|
;; as published by the Free Software Foundation; either version 2
|
||||||
|
;; of the License, or (at your option) any later version.
|
||||||
|
;;
|
||||||
|
;; This program is distributed in the hope that it will be useful,
|
||||||
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
;; GNU General Public License for more details.
|
||||||
|
;;
|
||||||
|
;; You should have received a copy of the GNU General Public License
|
||||||
|
;; along with this program; if not, write to the Free Software
|
||||||
|
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||||
|
;; USA.
|
||||||
|
;;
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;;
|
||||||
;; A very simple parser which parses production rules of the following forms:
|
;; A very simple parser which parses production rules of the following forms:
|
||||||
;;
|
;;
|
||||||
;; * "if altitude is less than 100 and state is forest then state should be climax and deer should be 3"
|
;; * "if altitude is less than 100 and state is forest then state should be climax and deer should be 3"
|
||||||
|
@ -24,18 +51,14 @@
|
||||||
;;
|
;;
|
||||||
;; This is the parser that is actually used currently; but see also insta.clj,
|
;; This is the parser that is actually used currently; but see also insta.clj,
|
||||||
;; which is potentially a much better parser but does not quite work yet.
|
;; which is potentially a much better parser but does not quite work yet.
|
||||||
|
;;
|
||||||
(ns mw-parser.core
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
(:use mw-engine.utils
|
|
||||||
[clojure.string :only [split trim triml]])
|
|
||||||
(:gen-class)
|
|
||||||
)
|
|
||||||
|
|
||||||
(declare parse-conditions)
|
(declare parse-conditions)
|
||||||
(declare parse-not-condition)
|
(declare parse-not-condition)
|
||||||
(declare parse-simple-condition)
|
(declare parse-simple-condition)
|
||||||
|
|
||||||
;; a regular expression which matches string representation of numbers
|
;; a regular expression which matches string representation of positive numbers
|
||||||
(def re-number #"^[0-9.]*$")
|
(def re-number #"^[0-9.]*$")
|
||||||
|
|
||||||
;; error thrown when an attempt is made to set a reserved property
|
;; error thrown when an attempt is made to set a reserved property
|
||||||
|
|
|
@ -1,11 +1,31 @@
|
||||||
(ns mw-parser.declarative
|
(ns ^{:doc "A very simple parser which parses production rules."
|
||||||
(:use mw-engine.utils
|
:author "Simon Brooke"}
|
||||||
mw-parser.utils
|
mw-parser.declarative
|
||||||
[mw-parser.errors :as pe]
|
(:require [instaparse.core :as insta]
|
||||||
[mw-parser.generate :as pg]
|
[clojure.string :refer [split trim triml]]
|
||||||
[mw-parser.simplify :as ps]
|
[mw-parser.errors :as pe]
|
||||||
[clojure.string :only [split trim triml]])
|
[mw-parser.generate :as pg]
|
||||||
(:require [instaparse.core :as insta]))
|
[mw-parser.simplify :as ps]
|
||||||
|
[mw-parser.utils :refer [rule?]]))
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;;
|
||||||
|
;; This program is free software; you can redistribute it and/or
|
||||||
|
;; modify it under the terms of the GNU General Public License
|
||||||
|
;; as published by the Free Software Foundation; either version 2
|
||||||
|
;; of the License, or (at your option) any later version.
|
||||||
|
;;
|
||||||
|
;; This program is distributed in the hope that it will be useful,
|
||||||
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
;; GNU General Public License for more details.
|
||||||
|
;;
|
||||||
|
;; You should have received a copy of the GNU General Public License
|
||||||
|
;; along with this program; if not, write to the Free Software
|
||||||
|
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||||
|
;; USA.
|
||||||
|
;;
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
|
||||||
(def grammar
|
(def grammar
|
||||||
|
@ -93,3 +113,12 @@
|
||||||
(compile-rule rule-text false)))
|
(compile-rule rule-text false)))
|
||||||
|
|
||||||
|
|
||||||
|
(ps/simplify
|
||||||
|
(parse-rule
|
||||||
|
"if more than 2 neighbours have altitude equal to 11 then state should be beach"))
|
||||||
|
|
||||||
|
(pg/generate
|
||||||
|
(ps/simplify
|
||||||
|
(parse-rule
|
||||||
|
"if more than 2 neighbours have altitude equal to 11 then state should be beach")))
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,27 @@
|
||||||
(ns mw-parser.errors)
|
(ns ^{:doc "Display parse errors in a format which makes it easy for the user
|
||||||
|
to see where the error occurred."
|
||||||
|
:author "Simon Brooke"}
|
||||||
|
mw-parser.errors)
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;;
|
||||||
|
;; This program is free software; you can redistribute it and/or
|
||||||
|
;; modify it under the terms of the GNU General Public License
|
||||||
|
;; as published by the Free Software Foundation; either version 2
|
||||||
|
;; of the License, or (at your option) any later version.
|
||||||
|
;;
|
||||||
|
;; This program is distributed in the hope that it will be useful,
|
||||||
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
;; GNU General Public License for more details.
|
||||||
|
;;
|
||||||
|
;; You should have received a copy of the GNU General Public License
|
||||||
|
;; along with this program; if not, write to the Free Software
|
||||||
|
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||||
|
;; USA.
|
||||||
|
;;
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
|
||||||
;; error thrown when an attempt is made to set a reserved property
|
;; error thrown when an attempt is made to set a reserved property
|
||||||
(def reserved-properties-error
|
(def reserved-properties-error
|
||||||
|
|
|
@ -1,8 +1,29 @@
|
||||||
(ns mw-parser.generate
|
(ns ^{:doc "Generate Clojure source from simplified parse trees."
|
||||||
(:use mw-engine.utils
|
:author "Simon Brooke"}
|
||||||
mw-parser.utils
|
mw-parser.generate
|
||||||
|
(:require [mw-engine.utils :refer []]
|
||||||
|
[mw-parser.utils :refer [assert-type TODO]]
|
||||||
[mw-parser.errors :as pe]))
|
[mw-parser.errors :as pe]))
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;;
|
||||||
|
;; This program is free software; you can redistribute it and/or
|
||||||
|
;; modify it under the terms of the GNU General Public License
|
||||||
|
;; as published by the Free Software Foundation; either version 2
|
||||||
|
;; of the License, or (at your option) any later version.
|
||||||
|
;;
|
||||||
|
;; This program is distributed in the hope that it will be useful,
|
||||||
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
;; GNU General Public License for more details.
|
||||||
|
;;
|
||||||
|
;; You should have received a copy of the GNU General Public License
|
||||||
|
;; along with this program; if not, write to the Free Software
|
||||||
|
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||||
|
;; USA.
|
||||||
|
;;
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
|
||||||
(declare generate generate-action)
|
(declare generate generate-action)
|
||||||
|
|
||||||
|
@ -24,6 +45,8 @@
|
||||||
|
|
||||||
|
|
||||||
(defn generate-condition
|
(defn generate-condition
|
||||||
|
"From this `tree`, assumed to be a syntactically correct condition clause,
|
||||||
|
generate and return the appropriate clojure fragment."
|
||||||
[tree]
|
[tree]
|
||||||
(assert-type tree :CONDITION)
|
(assert-type tree :CONDITION)
|
||||||
(generate (second tree)))
|
(generate (second tree)))
|
||||||
|
@ -31,18 +54,24 @@
|
||||||
|
|
||||||
(defn generate-conjunct-condition
|
(defn generate-conjunct-condition
|
||||||
[tree]
|
[tree]
|
||||||
|
"From this `tree`, assumed to be a syntactically conjunct correct condition clause,
|
||||||
|
generate and return the appropriate clojure fragment."
|
||||||
(assert-type tree :CONJUNCT-CONDITION)
|
(assert-type tree :CONJUNCT-CONDITION)
|
||||||
(cons 'and (map generate (rest tree))))
|
(cons 'and (map generate (rest tree))))
|
||||||
|
|
||||||
|
|
||||||
(defn generate-disjunct-condition
|
(defn generate-disjunct-condition
|
||||||
|
"From this `tree`, assumed to be a syntactically correct disjunct condition clause,
|
||||||
|
generate and return the appropriate clojure fragment."
|
||||||
[tree]
|
[tree]
|
||||||
(assert-type tree :DISJUNCT-CONDITION)
|
(assert-type tree :DISJUNCT-CONDITION)
|
||||||
(cons 'or (map generate (rest tree))))
|
(cons 'or (map generate (rest tree))))
|
||||||
|
|
||||||
|
|
||||||
(defn generate-ranged-property-condition
|
(defn generate-ranged-property-condition
|
||||||
"Generate a property condition where the expression is a numeric range"
|
"From this `tree`, assumed to be a syntactically property condition clause for
|
||||||
|
this `property` where the `expression` is a numeric range, generate and return
|
||||||
|
the appropriate clojure fragment."
|
||||||
[tree property expression]
|
[tree property expression]
|
||||||
(assert-type tree :PROPERTY-CONDITION)
|
(assert-type tree :PROPERTY-CONDITION)
|
||||||
(assert-type (nth tree 3) :RANGE-EXPRESSION)
|
(assert-type (nth tree 3) :RANGE-EXPRESSION)
|
||||||
|
@ -55,7 +84,9 @@
|
||||||
|
|
||||||
|
|
||||||
(defn generate-disjunct-property-condition
|
(defn generate-disjunct-property-condition
|
||||||
"Generate a property condition where the expression is a disjunct expression.
|
"From this `tree`, assumed to be a syntactically property condition clause
|
||||||
|
where the expression is a a disjunction, generate and return
|
||||||
|
the appropriate clojure fragment.
|
||||||
TODO: this is definitely still wrong!"
|
TODO: this is definitely still wrong!"
|
||||||
([tree]
|
([tree]
|
||||||
(let [property (generate (second tree))
|
(let [property (generate (second tree))
|
||||||
|
@ -70,6 +101,8 @@
|
||||||
|
|
||||||
|
|
||||||
(defn generate-property-condition
|
(defn generate-property-condition
|
||||||
|
"From this `tree`, assumed to be a syntactically property condition clause,
|
||||||
|
generate and return the appropriate clojure fragment."
|
||||||
([tree]
|
([tree]
|
||||||
(assert-type tree :PROPERTY-CONDITION)
|
(assert-type tree :PROPERTY-CONDITION)
|
||||||
(if
|
(if
|
||||||
|
@ -100,6 +133,8 @@
|
||||||
|
|
||||||
|
|
||||||
(defn generate-qualifier
|
(defn generate-qualifier
|
||||||
|
"From this `tree`, assumed to be a syntactically correct qualifier,
|
||||||
|
generate and return the appropriate clojure fragment."
|
||||||
[tree]
|
[tree]
|
||||||
(if
|
(if
|
||||||
(= (count tree) 2)
|
(= (count tree) 2)
|
||||||
|
@ -109,6 +144,8 @@
|
||||||
|
|
||||||
|
|
||||||
(defn generate-simple-action
|
(defn generate-simple-action
|
||||||
|
"From this `tree`, assumed to be a syntactically correct simple action,
|
||||||
|
generate and return the appropriate clojure fragment."
|
||||||
([tree]
|
([tree]
|
||||||
(assert-type tree :SIMPLE-ACTION)
|
(assert-type tree :SIMPLE-ACTION)
|
||||||
(generate-simple-action tree []))
|
(generate-simple-action tree []))
|
||||||
|
@ -126,6 +163,8 @@
|
||||||
|
|
||||||
|
|
||||||
(defn generate-probable-action
|
(defn generate-probable-action
|
||||||
|
"From this `tree`, assumed to be a syntactically correct probable action,
|
||||||
|
generate and return the appropriate clojure fragment."
|
||||||
([tree]
|
([tree]
|
||||||
(assert-type tree :PROBABLE-ACTION)
|
(assert-type tree :PROBABLE-ACTION)
|
||||||
(generate-probable-action tree []))
|
(generate-probable-action tree []))
|
||||||
|
@ -142,6 +181,8 @@
|
||||||
|
|
||||||
|
|
||||||
(defn generate-action
|
(defn generate-action
|
||||||
|
"From this `tree`, assumed to be a syntactically correct action,
|
||||||
|
generate and return the appropriate clojure fragment."
|
||||||
[tree others]
|
[tree others]
|
||||||
(case (first tree)
|
(case (first tree)
|
||||||
:ACTIONS (generate-action (first tree) others)
|
:ACTIONS (generate-action (first tree) others)
|
||||||
|
@ -151,6 +192,8 @@
|
||||||
|
|
||||||
|
|
||||||
(defn generate-multiple-actions
|
(defn generate-multiple-actions
|
||||||
|
"From this `tree`, assumed to be one or more syntactically correct actions,
|
||||||
|
generate and return the appropriate clojure fragment."
|
||||||
[tree]
|
[tree]
|
||||||
(assert-type tree :ACTIONS)
|
(assert-type tree :ACTIONS)
|
||||||
(generate-action (first (rest tree)) (second (rest tree))))
|
(generate-action (first (rest tree)) (second (rest tree))))
|
||||||
|
@ -166,6 +209,8 @@
|
||||||
|
|
||||||
|
|
||||||
(defn generate-numeric-expression
|
(defn generate-numeric-expression
|
||||||
|
"From this `tree`, assumed to be a syntactically correct numeric expression,
|
||||||
|
generate and return the appropriate clojure fragment."
|
||||||
[tree]
|
[tree]
|
||||||
(assert-type tree :NUMERIC-EXPRESSION)
|
(assert-type tree :NUMERIC-EXPRESSION)
|
||||||
(case (count tree)
|
(case (count tree)
|
||||||
|
@ -182,6 +227,7 @@
|
||||||
([tree]
|
([tree]
|
||||||
(assert-type tree :NEIGHBOURS-CONDITION)
|
(assert-type tree :NEIGHBOURS-CONDITION)
|
||||||
(case (first (second tree))
|
(case (first (second tree))
|
||||||
|
:NUMBER (read-string (second (second tree)))
|
||||||
:QUANTIFIER (generate-neighbours-condition tree (first (second (second tree))))
|
:QUANTIFIER (generate-neighbours-condition tree (first (second (second tree))))
|
||||||
:QUALIFIER (cons (generate (second tree)) (rest (generate (nth tree 2))))))
|
:QUALIFIER (cons (generate (second tree)) (rest (generate (nth tree 2))))))
|
||||||
([tree quantifier-type]
|
([tree quantifier-type]
|
||||||
|
|
|
@ -1,92 +0,0 @@
|
||||||
(ns mw-parser.simplifier
|
|
||||||
(:use mw-engine.utils
|
|
||||||
mw-parser.parser))
|
|
||||||
|
|
||||||
(declare simplify)
|
|
||||||
|
|
||||||
(defn simplify-qualifier
|
|
||||||
"Given that this `tree` fragment represents a qualifier, what
|
|
||||||
qualifier is that?"
|
|
||||||
[tree]
|
|
||||||
(cond
|
|
||||||
(empty? tree) nil
|
|
||||||
(and (coll? tree)
|
|
||||||
(member? (first tree) '(:EQUIVALENCE :COMPARATIVE))) tree
|
|
||||||
(coll? (first tree)) (or (simplify-qualifier (first tree))
|
|
||||||
(simplify-qualifier (rest tree)))
|
|
||||||
(coll? tree) (simplify-qualifier (rest tree))
|
|
||||||
true tree))
|
|
||||||
|
|
||||||
(defn simplify-second-of-two
|
|
||||||
"There are a number of possible simplifications such that if the `tree` has
|
|
||||||
only two elements, the second is semantically sufficient."
|
|
||||||
[tree]
|
|
||||||
(if (= (count tree) 2) (simplify (nth tree 1)) tree))
|
|
||||||
|
|
||||||
|
|
||||||
(defn simplify-some
|
|
||||||
"'some' is the same as 'more than zero'"
|
|
||||||
[tree]
|
|
||||||
[:COMPARATIVE '> 0])
|
|
||||||
|
|
||||||
(defn simplify-none
|
|
||||||
"'none' is the same as 'zero'"
|
|
||||||
[tree]
|
|
||||||
[:COMPARATIVE '= 0])
|
|
||||||
|
|
||||||
(defn simplify-all
|
|
||||||
"'all' isn't actually the same as 'eight', because cells at the edges of the world have
|
|
||||||
fewer than eight neighbours; but it's a simplifying (ha!) assumption for now."
|
|
||||||
[tree]
|
|
||||||
[:COMPARATIVE '= 8])
|
|
||||||
|
|
||||||
(defn simplify-quantifier
|
|
||||||
"If this quantifier is a number, 'simplifiy' it into a comparative whose operator is '='
|
|
||||||
and whose quantity is that number. This is actually more complicated but makes generation easier."
|
|
||||||
[tree]
|
|
||||||
(if (number? (second tree)) [:COMPARATIVE '= (second tree)] (simplify (second tree))))
|
|
||||||
|
|
||||||
(defn simplify
|
|
||||||
"Simplify/canonicalise this `tree`. Opportunistically replace complex fragments with
|
|
||||||
semantically identical simpler fragments"
|
|
||||||
[tree]
|
|
||||||
(if
|
|
||||||
(coll? tree)
|
|
||||||
(case (first tree)
|
|
||||||
:SPACE nil
|
|
||||||
:QUALIFIER (simplify-qualifier tree)
|
|
||||||
:CONDITIONS (simplify-second-of-two tree)
|
|
||||||
:CONDITION (simplify-second-of-two tree)
|
|
||||||
:EXPRESSION (simplify-second-of-two tree)
|
|
||||||
:COMPARATIVE (simplify-second-of-two tree)
|
|
||||||
:QUANTIFIER (simplify-quantifier tree)
|
|
||||||
:VALUE (simplify-second-of-two tree)
|
|
||||||
:PROPERTY (simplify-second-of-two tree)
|
|
||||||
:ACTIONS (simplify-second-of-two tree)
|
|
||||||
:ACTION (simplify-second-of-two tree)
|
|
||||||
:ALL (simplify-all tree)
|
|
||||||
:SOME (simplify-some tree)
|
|
||||||
:NONE (simplify-none tree)
|
|
||||||
(remove nil? (map simplify tree)))
|
|
||||||
tree))
|
|
||||||
|
|
||||||
(simplify (parse-rule "if state is climax and 4 neighbours have state equal to fire then 3 chance in 5 state should be fire"))
|
|
||||||
(simplify (parse-rule "if state is climax and no neighbours have state equal to fire then 3 chance in 5 state should be fire"))
|
|
||||||
|
|
||||||
(simplify (parse-rule "if state is in grassland or pasture or heath and more than 4 neighbours have state equal to water then state should be village"))
|
|
||||||
|
|
||||||
(simplify (parse-rule "if 6 neighbours have state equal to water then state should be village"))
|
|
||||||
|
|
||||||
(simplify (parse-rule "if fertility is between 55 and 75 then state should be climax"))
|
|
||||||
|
|
||||||
(simplify (parse-rule "if state is forest then state should be climax"))
|
|
||||||
|
|
||||||
|
|
||||||
(simplify (parse-rule "if state is in grassland or pasture or heath and more than 4 neighbours have state equal to water then state should be village"))
|
|
||||||
(simplify (parse-rule "if altitude is less than 100 and state is forest then state should be climax and deer should be 3"))
|
|
||||||
(simplify (parse-rule "if altitude is 100 or fertility is 25 then state should be heath and fertility should be 24.3"))
|
|
||||||
(simplify (parse-rule "if altitude is 100 or fertility is 25 then state should be heath"))
|
|
||||||
|
|
||||||
(simplify (parse-rule "if deer is more than 2 and wolves is 0 and fertility is more than 20 then deer should be deer + 2"))
|
|
||||||
(simplify (parse-rule "if deer is more than 1 and wolves is more than 1 then deer should be deer - wolves"))
|
|
||||||
(simplify (parse-rule "if state is grassland and 4 neighbours have state equal to water then state should be village"))
|
|
|
@ -1,12 +1,33 @@
|
||||||
(ns mw-parser.simplify
|
(ns ^{:doc "Simplify a parse tree."
|
||||||
(:use mw-engine.utils
|
:author "Simon Brooke"}
|
||||||
mw-parser.utils))
|
mw-parser.simplify
|
||||||
|
(:require [mw-engine.utils :refer [member?]]))
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;;
|
||||||
|
;; This program is free software; you can redistribute it and/or
|
||||||
|
;; modify it under the terms of the GNU General Public License
|
||||||
|
;; as published by the Free Software Foundation; either version 2
|
||||||
|
;; of the License, or (at your option) any later version.
|
||||||
|
;;
|
||||||
|
;; This program is distributed in the hope that it will be useful,
|
||||||
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
;; GNU General Public License for more details.
|
||||||
|
;;
|
||||||
|
;; You should have received a copy of the GNU General Public License
|
||||||
|
;; along with this program; if not, write to the Free Software
|
||||||
|
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||||
|
;; USA.
|
||||||
|
;;
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
|
||||||
(declare simplify)
|
(declare simplify)
|
||||||
|
|
||||||
(defn simplify-qualifier
|
(defn simplify-qualifier
|
||||||
"Given that this `tree` fragment represents a qualifier, what
|
"Given that this `tree` fragment represents a qualifier, what
|
||||||
qualifier is that?"
|
qualifier is that?"
|
||||||
[tree]
|
[tree]
|
||||||
(cond
|
(cond
|
||||||
(empty? tree) nil
|
(empty? tree) nil
|
||||||
|
@ -19,9 +40,16 @@
|
||||||
|
|
||||||
(defn simplify-second-of-two
|
(defn simplify-second-of-two
|
||||||
"There are a number of possible simplifications such that if the `tree` has
|
"There are a number of possible simplifications such that if the `tree` has
|
||||||
only two elements, the second is semantically sufficient."
|
only two elements, the second is semantically sufficient."
|
||||||
[tree]
|
[tree]
|
||||||
(if (= (count tree) 2) (simplify (second tree)) tree))
|
(if (= (count tree) 2) (simplify (nth tree 1)) tree))
|
||||||
|
|
||||||
|
|
||||||
|
(defn simplify-quantifier
|
||||||
|
"If this quantifier is a number, 'simplifiy' it into a comparative whose operator is '='
|
||||||
|
and whose quantity is that number. This is actually more complicated but makes generation easier."
|
||||||
|
[tree]
|
||||||
|
(if (number? (second tree)) [:COMPARATIVE '= (second tree)] (simplify (second tree))))
|
||||||
|
|
||||||
|
|
||||||
(defn simplify
|
(defn simplify
|
||||||
|
@ -31,18 +59,24 @@
|
||||||
(if
|
(if
|
||||||
(coll? tree)
|
(coll? tree)
|
||||||
(case (first tree)
|
(case (first tree)
|
||||||
|
;; 'all' isn't actually the same as 'eight', because cells at the edges of the world have
|
||||||
|
;; fewer than eight neighbours; but it's a simplifying (ha!) assumption for now."
|
||||||
|
;; TODO: fix this so it actually works.
|
||||||
|
:ALL [:COMPARATIVE '= 8]
|
||||||
:ACTION (simplify-second-of-two tree)
|
:ACTION (simplify-second-of-two tree)
|
||||||
:ACTIONS (cons (first tree) (simplify (rest tree)))
|
:ACTIONS (simplify-second-of-two tree)
|
||||||
:CHANCE-IN nil
|
|
||||||
:COMPARATIVE (simplify-second-of-two tree)
|
:COMPARATIVE (simplify-second-of-two tree)
|
||||||
:CONDITION (simplify-second-of-two tree)
|
:CONDITION (simplify-second-of-two tree)
|
||||||
:CONDITIONS (simplify-second-of-two tree)
|
:CONDITIONS (simplify-second-of-two tree)
|
||||||
:EXPRESSION (simplify-second-of-two tree)
|
:EXPRESSION (simplify-second-of-two tree)
|
||||||
|
:NONE [:COMPARATIVE '= 0]
|
||||||
|
:NUMBER tree
|
||||||
:PROPERTY (simplify-second-of-two tree)
|
:PROPERTY (simplify-second-of-two tree)
|
||||||
:PROPERTY-CONDITION-OR-EXPRESSION (simplify-second-of-two tree)
|
:QUALIFIER (simplify-qualifier tree)
|
||||||
|
:QUANTIFIER (simplify-quantifier tree)
|
||||||
|
:SOME [:COMPARATIVE '> 0]
|
||||||
:SPACE nil
|
:SPACE nil
|
||||||
:THEN nil
|
|
||||||
:AND nil
|
|
||||||
:VALUE (simplify-second-of-two tree)
|
:VALUE (simplify-second-of-two tree)
|
||||||
(remove nil? (map simplify tree)))
|
(remove nil? (map simplify tree)))
|
||||||
tree))
|
tree))
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,25 @@
|
||||||
(ns mw-parser.utils)
|
(ns ^{:doc "Utilities used in more than one namespace within the parser."
|
||||||
|
:author "Simon Brooke"}
|
||||||
|
mw-parser.utils)
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;;
|
||||||
|
;; This program is free software; you can redistribute it and/or
|
||||||
|
;; modify it under the terms of the GNU General Public License
|
||||||
|
;; as published by the Free Software Foundation; either version 2
|
||||||
|
;; of the License, or (at your option) any later version.
|
||||||
|
;;
|
||||||
|
;; This program is distributed in the hope that it will be useful,
|
||||||
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
;; GNU General Public License for more details.
|
||||||
|
;;
|
||||||
|
;; You should have received a copy of the GNU General Public License
|
||||||
|
;; along with this program; if not, write to the Free Software
|
||||||
|
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||||
|
;; USA.
|
||||||
|
;;
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
|
||||||
(defn rule?
|
(defn rule?
|
||||||
|
|
|
@ -472,3 +472,15 @@
|
||||||
(is (nil? (apply afn (list {:x 0 :y 1} world)))
|
(is (nil? (apply afn (list {:x 0 :y 1} world)))
|
||||||
"Middle cell of the strip has only two high neighbours, so rule should not fire."))
|
"Middle cell of the strip has only two high neighbours, so rule should not fire."))
|
||||||
))
|
))
|
||||||
|
|
||||||
|
(deftest regression-tests
|
||||||
|
(testing "Rule in default set which failed on switchover to declarative rules"
|
||||||
|
(let [afn (compile-rule "if state is scrub then 1 chance in 5 state should be forest")
|
||||||
|
world (transform-world
|
||||||
|
(make-world 3 3)
|
||||||
|
(list (compile-rule "if x is 2 then altitude should be 11")
|
||||||
|
(compile-rule "if x is less than 2 then state should be scrub")))]
|
||||||
|
(is (= (:state (apply afn (list {:x 1 :y 1} world))) :forest)
|
||||||
|
"Centre cell is scrub, so rule should fire")
|
||||||
|
(is (= (:state (apply afn (list {:x 2 :y 1} world))) :beach)
|
||||||
|
"Middle cell of the strip is not scrub, so rule should not fire."))))
|
||||||
|
|
Loading…
Reference in a new issue