|
22 | 22 | DISJUNCT-CONDITION := CONDITION SPACE OR SPACE CONDITIONS; |
23 | 23 | CONJUNCT-CONDITION := CONDITION SPACE AND SPACE CONDITIONS; |
24 | 24 | CONDITION := NEIGHBOURS-CONDITION | PROPERTY-CONDITION; |
25 | | - NEIGHBOURS-CONDITION := QUANTIFIER SPACE NEIGHBOURS SPACE IS SPACE PROPERTY-CONDITION | QUANTIFIER SPACE NEIGHBOURS IS EXPRESSION | QUALIFIER SPACE NEIGHBOURS-CONDITION; |
| 25 | + WITHIN-CONDITION := NEIGHBOURS-CONDITION SPACE WITHIN SPACE NUMERIC-EXPRESSION; |
| 26 | + NEIGHBOURS-CONDITION := WITHIN-CONDITION | QUANTIFIER SPACE NEIGHBOURS SPACE IS SPACE PROPERTY-CONDITION | QUANTIFIER SPACE NEIGHBOURS IS EXPRESSION | QUALIFIER SPACE NEIGHBOURS-CONDITION; |
26 | 27 | PROPERTY-CONDITION := PROPERTY SPACE QUALIFIER SPACE EXPRESSION; |
27 | 28 | EXPRESSION := SIMPLE-EXPRESSION | RANGE-EXPRESSION | NUMERIC-EXPRESSION | DISJUNCT-EXPRESSION | VALUE; |
28 | 29 | SIMPLE-EXPRESSION := QUALIFIER SPACE EXPRESSION | VALUE; |
|
46 | 47 | NONE := 'no'; |
47 | 48 | ALL := 'all' |
48 | 49 | BETWEEN := 'between'; |
| 50 | + WITHIN := 'within'; |
49 | 51 | IN := 'in'; |
50 | 52 | MORE := 'more'; |
51 | 53 | LESS := 'less' | 'fewer'; |
|
178 | 180 | :SYMBOL (list (keyword (second (second tree))) 'cell) |
179 | 181 | (generate (second tree)))) |
180 | 182 |
|
181 | | -;; (defn generate-neighbours-condition |
182 | | -;; "Generate code for a condition which refers to neighbours." |
183 | | -;; ([tree] |
184 | | -;; (let [q (second tree)] |
185 | | -;; (if (number? q) |
186 | | -;; (generate-neighbours-condition '= q |
187 | | -;; ([comp1 quantity property value remainder comp2 distance] |
188 | | -;; [(list comp1 |
189 | | -;; (list 'count |
190 | | -;; (list 'get-neighbours-with-property-value 'world |
191 | | -;; '(cell :x) '(cell :y) distance |
192 | | -;; (keyword property) (keyword-or-numeric value) comp2)) |
193 | | -;; quantity) |
194 | | -;; remainder]) |
195 | | -;; ([comp1 quantity property value remainder comp2] |
196 | | -;; (gen-neighbours-condition comp1 quantity property value remainder comp2 1))) |
| 183 | +(defn generate-neighbours-condition |
| 184 | + "Generate code for a condition which refers to neighbours." |
| 185 | + ([tree] |
| 186 | + (generate-neighbours-condition tree (first (second tree)))) |
| 187 | + ([tree quantifier-type] |
| 188 | + (let [quantifier (second (second tree)) |
| 189 | + pc (generate (nth tree 4))] |
| 190 | + (case quantifier-type |
| 191 | + :NUMBER (generate-neighbours-condition '= (read-string quantifier) pc 1) |
| 192 | + :SOME (generate-neighbours-condition '> 0 pc 1) |
| 193 | + :QUANTIFIER |
| 194 | + (let [comparative (generate (simplify (second quantifier))) |
| 195 | + value (simplify (nth quantifier 5))] |
| 196 | + (generate-neighbours-condition comparative value pc 1))))) |
| 197 | + ([comp1 quantity property-condition distance] |
| 198 | + (list comp1 |
| 199 | + (list 'count (list 'remove false (list 'map (list 'fn ['cell] property-condition) '(get-neighbours cell world distance)))) quantity)) |
| 200 | + ([comp1 quantity property-condition] |
| 201 | + (generate-neighbours-condition comp1 quantity property-condition 1))) |
| 202 | + |
| 203 | +;; (def s1 "if 3 neighbours have state equal to forest then state should be forest") |
| 204 | +;; (def s2 "if some neighbours have state equal to forest then state should be forest") |
| 205 | +;; (def s3 "if more than 3 neighbours have state equal to forest then state should be forest") |
| 206 | +;; (def s4 "if fewer than 3 neighbours have state equal to forest then state should be forest") |
| 207 | +;; (def s5 "if all neighbours have state equal to forest then state should be forest") |
| 208 | +;; (def s6 "if more than 3 neighbours within 2 have state equal to forest then state should be forest") |
| 209 | + |
| 210 | +;; (nth (simplify (parse-rule s1)) 2) |
| 211 | +;; (second (nth (simplify (parse-rule s1)) 2)) |
| 212 | +;; (nth (simplify (parse-rule s2)) 2) |
| 213 | +;; (map simplify (nth (simplify (parse-rule s2)) 2)) |
| 214 | +;; ;; (second (nth (simplify (parse-rule s2)) 2)) |
| 215 | +;; ;; (nth (simplify (parse-rule s3)) 2) |
| 216 | +;; (second (nth (simplify (parse-rule s3)) 2)) |
| 217 | +;; (map simplify (second (nth (simplify (parse-rule s3)) 2))) |
| 218 | +;; ;; (nth (simplify (parse-rule s4)) 2) |
| 219 | +;; ;; (second (nth (simplify (parse-rule s4)) 2)) |
| 220 | +;; ;; (nth (simplify (parse-rule s5)) 2) |
| 221 | +;; ;; (second (nth (simplify (parse-rule s5)) 2)) |
| 222 | +;; ;; (nth (simplify (parse-rule s6)) 2) |
| 223 | +;; ;; (second (nth (simplify (parse-rule s6)) 2)) |
| 224 | + |
| 225 | +;; ;; (generate (nth (nth (simplify (parse-rule s5)) 2) 4)) |
| 226 | +;; ;; (generate (nth (simplify (parse-rule s2)) 2)) |
| 227 | +;; ;; (generate (nth (simplify (parse-rule s1)) 2)) |
| 228 | + |
| 229 | + |
| 230 | +;; (generate-neighbours-condition '= 3 '(= (:state cell) :forest) 1) |
| 231 | +;; (generate-neighbours-condition (nth (simplify (parse-rule s3)) 2)) |
| 232 | +;; (generate-neighbours-condition (nth (simplify (parse-rule s2)) 2)) |
| 233 | +;; (generate-neighbours-condition (nth (simplify (parse-rule s1)) 2)) |
197 | 234 |
|
198 | 235 |
|
199 | 236 | (defn generate |
|
209 | 246 | :CONDITIONS (generate-conditions tree) |
210 | 247 | :CONJUNCT-CONDITION (generate-conjunct-condition tree) |
211 | 248 | :DISJUNCT-CONDITION (generate-disjunct-condition tree) |
212 | | - :PROPERTY-CONDITION (generate-property-condition tree) |
213 | 249 | :DISJUNCT-EXPRESSION (generate (nth tree 2)) |
214 | 250 | :DISJUNCT-VALUE (generate-disjunct-value tree) |
215 | 251 | :EQUIVALENCE '= |
|
220 | 256 | = 'not= |
221 | 257 | > '< |
222 | 258 | < '>) |
223 | | -;; :NEIGHBOURS-CONDITION (generate-neighbours-condition tree) |
| 259 | + :NEIGHBOURS-CONDITION (generate-neighbours-condition tree) |
224 | 260 | :NUMERIC-EXPRESSION (generate-numeric-expression tree) |
225 | 261 | :NUMBER (read-string (second tree)) |
226 | 262 | :PROPERTY (list (generate (second tree)) 'cell) ;; dubious - may not be right |
| 263 | + :PROPERTY-CONDITION (generate-property-condition tree) |
227 | 264 | :QUALIFIER (generate (second tree)) |
228 | 265 | :RULE (generate-rule tree) |
229 | 266 | :SIMPLE-ACTION (generate-simple-action tree) |
|
271 | 308 | :CONDITION (simplify-second-of-two tree) |
272 | 309 | :CONDITIONS (simplify-second-of-two tree) |
273 | 310 | :EXPRESSION (simplify-second-of-two tree) |
274 | | - :QUANTIFIER (simplify-second-of-two tree) |
| 311 | +;; :QUANTIFIER (simplify-second-of-two tree) |
275 | 312 | :NOT nil |
276 | 313 | :PROPERTY (simplify-second-of-two tree) |
277 | 314 | :SPACE nil |
|
0 commit comments