<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" href="../coverage.css"/> <title> mw_parser/flow.clj </title> </head> <body> <span class="covered" title="1 out of 1 forms covered"> 001 (ns ^{:doc "A very simple parser which parses flow rules." </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 002 :author "Simon Brooke"} </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 003 mw-parser.flow </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 004 (:require [clojure.string :refer [join]] </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 005 [mw-parser.declarative :refer [build-parser]] </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 006 [mw-parser.simplify :refer [simplify-second-of-two]])) </span><br/> <span class="blank" title="0 out of 0 forms covered"> 007 </span><br/> <span class="covered" title="1 out of 1 forms covered"> 008 (def flow-grammar </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 009 "Grammar for flow rules. </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 010 </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 011 My initial conception of this would be that production rules </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 012 (if-then rules) and flow rules (flow-from-to rules) would be </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 013 entirely separate, presented to the parser as separate text </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 014 files, and parsed and compiled by different chains of functions. </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 015 </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 016 This appears not to be necessary. Flow rules are easy to parse </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 017 with the same parser as production rules -- a lot of the grammar </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 018 is intentionally common -- and the rules are easily discriminated </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 019 at the compilation ('generate') stage. </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 020 </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 021 The basic rule I want to be able to compile at this stage is the 'mutual </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 022 aid' rule: </span><br/> <span class="blank" title="0 out of 0 forms covered"> 023 </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 024 `flow 1 food from house having food > 1 to house with least food within 2` </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 025 " </span><br/> <span class="covered" title="16 out of 16 forms covered"> 026 (join "\n" ["FLOW-RULE := FLOW SPACE QUANTITY SPACE PROPERTY SPACE FROM SPACE SOURCE SPACE TO-HOW SPACE DESTINATION;" </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 027 "PERCENTAGE := NUMBER #'%';" </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 028 "QUANTITY := PERCENTAGE | NUMBER | EXPRESSION | SOME;" </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 029 "SOURCE := STATE | STATE SPACE WITH SPACE CONDITIONS;" </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 030 "DESTINATION := STATE | STATE SPACE WITH SPACE FLOW-CONDITIONS | STATE SPACE WITHIN SPACE VALUE SPACE WITH SPACE FLOW-CONDITIONS;" </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 031 "DETERMINER := MOST | LEAST;" </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 032 "DETERMINER-CONDITION := DETERMINER SPACE PROPERTY | DETERMINER SPACE PROPERTY;" </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 033 "FLOW-CONDITIONS := DETERMINER-CONDITION | CONDITIONS" </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 034 "STATE := SYMBOL;" </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 035 "TO-HOW := TO | TO-EACH | TO-FIRST;" </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 036 "TO-EACH := TO SPACE EACH | TO SPACE ALL;" </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 037 "TO-FIRST := TO SPACE FIRST"])) </span><br/> <span class="blank" title="0 out of 0 forms covered"> 038 </span><br/> <span class="covered" title="1 out of 1 forms covered"> 039 (def parse-flow </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 040 "Parse the argument, assumed to be a string in the correct syntax, and return a parse tree." </span><br/> <span class="covered" title="6 out of 6 forms covered"> 041 (build-parser flow-grammar)) </span><br/> <span class="blank" title="0 out of 0 forms covered"> 042 </span><br/> <span class="covered" title="1 out of 1 forms covered"> 043 (defn simplify-flow </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 044 [tree] </span><br/> <span class="covered" title="4 out of 4 forms covered"> 045 (if (coll? tree) </span><br/> <span class="covered" title="7 out of 7 forms covered"> 046 (case (first tree) </span><br/> <span class="not-covered" title="0 out of 3 forms covered"> 047 :CONDITION (simplify-second-of-two tree) </span><br/> <span class="covered" title="3 out of 3 forms covered"> 048 :CONDITIONS (simplify-second-of-two tree) </span><br/> <span class="covered" title="3 out of 3 forms covered"> 049 :DETERMINER (simplify-second-of-two tree) </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 050 ;; :DETERMINER-CONDITION (simplify-determiner-condition tree) </span><br/> <span class="not-covered" title="0 out of 3 forms covered"> 051 :EXPRESSION (simplify-second-of-two tree) </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 052 :FLOW nil </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 053 ;; :FLOW-CONDITIONS (simplify-second-of-two tree) </span><br/> <span class="covered" title="3 out of 3 forms covered"> 054 :PROPERTY (simplify-second-of-two tree) </span><br/> <span class="not-covered" title="0 out of 3 forms covered"> 055 :PROPERTY-CONDITION-OR-EXPRESSION (simplify-second-of-two tree) </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 056 :SPACE nil </span><br/> <span class="covered" title="3 out of 3 forms covered"> 057 :QUANTITY (simplify-second-of-two tree) </span><br/> <span class="covered" title="4 out of 4 forms covered"> 058 :STATE (list :PROPERTY-CONDITION </span><br/> <span class="covered" title="4 out of 4 forms covered"> 059 (list :SYMBOL "state") </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 060 '(:QUALIFIER </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 061 (:EQUIVALENCE </span><br/> <span class="not-tracked" title="0 out of 0 forms covered"> 062 (:IS "is"))) </span><br/> <span class="covered" title="3 out of 3 forms covered"> 063 (list :EXPRESSION </span><br/> <span class="covered" title="6 out of 6 forms covered"> 064 (list :VALUE (second tree)))) </span><br/> <span class="covered" title="7 out of 7 forms covered"> 065 (remove nil? (map simplify-flow tree))) </span><br/> <span class="covered" title="1 out of 1 forms covered"> 066 tree)) </span><br/> <span class="blank" title="0 out of 0 forms covered"> 067 </span><br/> </body> </html>