1 (load fish.dat)
  2 (load fish2.dat)
  3 (load fish3.dat)
  4 (load fish4.dat)
  5 (reset)
  6 (focus FISH)
  7 (defrule FISH::getdate
  8    =>
  9  ( printout t "Enter the Date" crlf "What is the Month (as an integer):" crlf )
 10  (bind ?month (read) )
 11  (printout t "What is the Day (as an integer): " crlf)
 12  (bind ?day (read) )
 13  (bind ?day2 (+ ?day  10000))
 14  (bind ?month2 (+ (* ?month  100) 10000))
 15  (assert (date (categ today) (month ?month) (day ?day) (dateint (+ ?month2 ?day2))))
 16 )
 17 
 18 (defrule FISH::whatsonthemenu
 19  (fish (species ?sp) (open_season ?open) (close_season ?close)
 20        (habitat $?hb) (seasonal_availability ?sa) (size_min ?min) (limit ?lm)
 21          (fishing_type ?tp) (fishing_location ?loc) (best_trip $?dn) (desireability ?ds ))
 22  (date (dateint ?today))
 23  (test ( openseason ?open ?close ?today))
 24   =>
 25   (assert (menu (species ?sp) (habitat $?hb) (seasonal_availability ?sa) (size_min ?min) (limit ?lm)
 26          (fishing_type ?tp) (fishing_location ?loc) (best_trip $?dn) (desireability ?ds ) (bonus OFF)))
 27   (printout t ?sp " is open now. They are currently at " ?hb  " and best fished during the " ?sa  " season. " crlf )
 28   (printout t "We can fish for them by " ?tp " fishing " ?loc ". " crlf   )
 29   (printout t "You can have " ?lm  " fish and it is best to fish for them during the " $?dn " trip(s). " crlf )
 30   (printout t "On a scale of 1-5 they are desireable: " ?ds crlf crlf )
 31 )
 32 
 33 (defrule FISH::testrule
 34  (menu (seasonal_availability $sa3))
 35  =>
 36  (printout t  "==> " $sa3 crlf)
 37 )
 38 
 39 (defrule FISH::goodseason
 40  ?f1 <- (menu (species ?sp) (habitat $?hb) (seasonal_availability ?sa) (size_min ?min) (limit ?lm)
 41         (fishing_type ?tp) (fishing_location ?loc) (best_trip $?dn) (desireability ?ds )(bonus OFF))
 42  (date (dateint ?today))
 43  (season ( description ?sa )(start_date ?open) (end_date ?close))
 44  (test ( openseason ?open ?close ?today))
 45  =>
 46 ;  (printout t "YOOO!! " ?sp " is open now. They are currently at " ?hb  " and best fished during the " ?sa  " season. " crlf )
 47 ;  (printout t "We can fish for them by " ?tp " fishing " ?loc ". " crlf   )
 48 ;  (printout t "You can have " ?lm  " fish and it is best to fish for them during the " $?dn " trip(s). " crlf )
 49 ;  (printout t "On a scale of 1-5 they are desireable: " ?ds crlf crlf )
 50 ; (printout t "What is ?ds => " ?ds crlf crlf )
 51  (bind ?ds (+ ?ds 2)) ; GOOD SEASON MORE DESIRABLE BY 2
 52  (retract ?f1)
 53  (assert (menu (species ?sp) (habitat $?hb) (seasonal_availability ?sa) (size_min ?min) (limit ?lm)
 54                         (fishing_type ?tp) (fishing_location ?loc) (best_trip ?dn) (desireability ?ds ) (bonus ON) )
 55  )
 56   (printout t "Oh we are in Season - even better. This is " ?sa " SEASON"  crlf )
 57   (printout t ?sp " is open now. They are currently at " ?hb  " and best fished NOW!. " crlf )
 58   (printout t "We can fish for them by " ?tp " fishing " ?loc ". " crlf   )
 59   (printout t "You can have " ?lm  " fish and it is best to fish for them during the " $?dn " trip(s). " crlf )
 60   (printout t "and they are now at desireable level: " ?ds crlf crlf )
 61 )
 62 
 63 ;WEATHER MODULE
 64 (focus WEATHER)
 65 
 66 (defrule WEATHER::WAVEHEIGHT
 67    =>
 68    (printout t "What is the expected Wave Heights today =>" crlf )
 69    (bind ?height (read))
 70    (assert (wave_height ( height ?height )))
 71 )
 72 
 73 (defrule WEATHER::SWELL
 74    =>
 75    (printout t "What Swell Period in Seconds =>" crlf )
 76    (bind ?sec (read))
 77    (assert (swells( seconds ?sec )))
 78 )
 79 
 80 (defrule WEATHER::WIND
 81    =>
 82    (printout t "What is the expected wind speed in knots =>" crlf  )
 83    (bind ?speed (read))
 84    (printout t "From what direction is the wind from? (NORTH SOUTH EAST or WEST) =>" crlf  )
 85    (bind ?direction (read))
 86    (assert (wind_speed (speed ?speed)(dir ?direction)))
 87 )
 88 
 89 (defrule WEATHER::CANCEL
 90       (or (wave_height (height ?ht&:(>= ?ht 10)))
 91          (wind_speed (speed ?sp&:(>= ?sp 30)))
 92          (wind_speed (speed ?sp&:(>= ?sp 20)) (dir EAST))
 93          (swells (seconds ?sec&:(>= ?sec 6)))
 94       )
 95 =>
 96    (assert (acceptable_condition (locale cancel)))
 97    (printout t "Weather is too harsh to fish" crlf )
 98    (exit)
 99 )
100 ; I have ovewrlapping decisions and a single outcome => need a decision tree might be better
101 
102 (defrule WEATHER::HIGHWINDS
103    (wind_speed (speed ?sp&:(and (>= ?sp 20 ) (< ?sp 30 ))) (dir ~EAST))
104  =>
105    (assert (acceptable_condition (locale inshore) (fsh_type bottom)))
106 )
107 
108 (defrule WEATHER::MODERATEWINDS
109  (wind_speed (speed ?sp&:(and(>= ?sp 15) (< ?sp 20))) (dir WEST))
110  =>
111   (assert (acceptable_condition (locale offshore) (fsh_type bottom)))
112   (assert (acceptable_condition (locale offshore_federal_waters) (fsh_type bottom)))
113   (assert (acceptable_condition(locale inshore) (fsh_type bottom)))
114   (assert (acceptable_condition(locale inshore) (fsh_type game)))
115   (assert (acceptable_condition(locale inshore) (fsh_type jigging)))
116   (assert (acceptable_condition(locale inshore) (fsh_type wreck)))
117  )
118 
119 (defrule WEATHER::MODERATE_HARSH_WINDS
120  (wind_speed (speed ?sp&:(and(>= ?sp 15) (< ?sp 20))) (dir ~WEST))
121  =>
122   (assert (acceptable_condition(locale inshore) (fsh_type game)))
123   (assert (acceptable_condition(locale inshore) (fsh_type bottom)))
124 )
125 
126 (defrule WEATHER::CALMSEAS
127  (wind_speed (speed ?sp&:(< ?sp 15)))
128  =>
129   (assert (acceptable_condition (locale offshore_federal_waters) (fsh_type bottom)))
130   (assert (acceptable_condition (locale offshore_federal_waters) (fsh_type game)))
131   (assert (acceptable_condition (locale offshore_federal_waters) (fsh_type jigging)))
132   (assert (acceptable_condition (locale offshore_federal_waters) (fsh_type wreck)))
133   (assert (acceptable_condition (locale offshore) (fsh_type bottom)))
134   (assert (acceptable_condition (locale offshore) (fsh_type game)))
135   (assert (acceptable_condition (locale offshore) (fsh_type jigging)))
136   (assert (acceptable_condition (locale offshore) (fsh_type wreck)))
137   (assert (acceptable_condition (locale inshore) (fsh_type bottom)))
138   (assert (acceptable_condition (locale inshore) (fsh_type game)))
139   (assert (acceptable_condition (locale inshore) (fsh_type jigging)))
140   (assert (acceptable_condition (locale inshore) (fsh_type wreck)))
141   (assert (acceptable_condition (locale open_ocean) (fsh_type bottom)))
142   (assert (acceptable_condition (locale open_ocean) (fsh_type game)))
143   (assert (acceptable_condition (locale open_ocean) (fsh_type jigging)))
144   (assert (acceptable_condition (locale open_ocean) (fsh_type wreck)))
145 )
146 
147 (focus TRIP)
148 (defrule TRIP::all_possible_trips
149     ?f1 <- (menu (species ?spec) (habitat $?hab) (size_min ?min)
150        (limit ?lim) (fishing_type ?ft) (fishing_location ?loc) (best_trip $?) (desireability ?ds))
151     ?f2 <- (acceptable_condition (locale ?loc) (fsh_type ?ft))
152 
153     =>
154     (retract ?f1)
155     (assert (all_trips (location ?loc) (method ?ft) (species ?spec) (desireability ?ds) (check 1)))
156 ;   (format t "Trip: %-15s Method: %-15s  Species: %-18s Profit Points: %4d" ?loc ?ft ?spec ?ds)
157 ;   (printout t crlf )
158 )
159 
160 
161 ;(defrule TRIP::trips
162 ;  ?f1 <- (all_trips (location ?loc) (method ?ft) (species $?spec) (desireability ?ds) (check 1))
163 ;  ?f2 <- (spec_tmp (spec $?spec_tmp))
164 ;  ?f3 <- (sum ?sum)
165 ;  =>
166 ;   (modify ?f1 (check 0))
167 ;   (retract ?f2)
168 ;  (assert (spec_tmp (spec ?spec_tmp ?spec )))
169 ;  (retract ?f3) 
170 ;  (assert (sum =(+ ?sum ?ds ) ))
171 ;)
172 
173 (defrule TRIP::make_table
174    ?f1 <- (all_trips (location ?s_loc) (method ?s_ft) )
175    =>
176    (assert (table (location ?s_loc) (method ?s_ft) ))
177 )
178 
179 (defrule TRIP::eval_function_new
180  ?f1 <- (table (location ?s_loc) (method ?s_ft))
181  ?f2 <- (all_trips (location ?s_loc) (method ?s_ft) (species $?spec) (desireability ?ds) (check 1))
182  (not (exists (trips (location ?s_loc ) (method ?s_ft)))) ;restrict to new entries only
183  =>
184  (modify ?f2 (check -1));
185  (assert (trips (location ?s_loc ) (method ?s_ft) (species ?spec ) (desireability ?ds)))
186  )
187 
188 (defrule TRIP::eval_function_cont
189  ?f3 <- (trips (location ?s_loc ) (method ?s_ft) (species $?species ) (desireability ?sum)) ;current data in trips
190  ?f1 <- (table (location ?s_loc) (method ?s_ft))
191  ?f2 <- (all_trips (location ?s_loc) (method ?s_ft) (species $?spec) (desireability ?ds) (check 1))
192  =>
193  (modify ?f2 (check -1))
194  (retract ?f3)
195  (assert (trips (location ?s_loc ) (method ?s_ft) (species =(create$ ?species ?spec )) (desireability =(+ ?sum  ?ds)))) ; unify old data with new data
196  )
197 
198 (defrule TRIP::send_report
199    (not (exists (menu (species ?))))
200    (not (exists(all_trips (check 1 ))));all trips processed
201    (all_trips (check -1)); and at least one finished
202    ?f1 <- (print_report (value off))
203 =>
204    (modify ?f1 (value on))
205    (printout t "TURNING ON REPORT" crlf)
206 )
207 
208 (focus REPORT)
209 (deffacts initial_facts (report_phase header))
210 
211 (defrule REPORT::final_report
212    (report_phase body)
213    (trips (location ?s_loc) (method ?s_ft) (species $?species) (desireability ?ds))
214 =>
215    (format t "Trip: %-25s Method: %-6s  Species: %-25s" ?s_loc ?s_ft (implode$ ?species) )
216    (printout t crlf)
217    (format t "Profit Points: %4d" ?ds)
218    (printout t crlf )
219 )
220 
221 ( defrule MAXIE
222    (report_phase header)
223    (trips (location ?s_loc) (method ?s_ft) (species $?species) (desireability ?MAX))
224    (not (trips (desireability ?LOWER&:(< ?MAX ?LOWER))))
225 =>
226    (printout t "MAX Points is ==>" ?MAX   crlf )
227    (assert (report_phase body))
228 )
229 
230 ;(focus FISH WEATHER TRIP )
231 (get-focus-stack)
232 (clear-focus-stack)
233 (reset)
234 (focus FISH WEATHER TRIP REPORT)
235 (run)
236 
237