git @ Cat's Eye Technologies Animals / 89b8cd8
Import of Animals version 1.1 revision 2010.0429. catseye 11 years ago
6 changed file(s) with 92 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
11 <html>
22 <head>
33 <title>Module animals</title>
4 <link rel="stylesheet" type="text/css" href="stylesheet.css">
4 <link rel="stylesheet" type="text/css" href="stylesheet.css" title="EDoc">
55 </head>
66 <body bgcolor="white">
7 <div class="navbar"><a name="#navbar_top"></a><table width="100%" border="0" cellspacing="0" cellpadding="2" summary="navigation bar"><tr><td><a href="overview-summary.html" target="overviewFrame">Overview</a></td><td><a href="http://www.erlang.org/"><img src="erlang.png" align="right" border="0" alt="erlang logo"></a></td></tr></table></div>
8 <hr>
9
710 <h1>Module animals</h1>
8 The classic 'expert system' demonstration game of Animals.<ul><li><a href="#description">Description</a></li><li><a href="#index">Function Index</a></li><li><a href="#functions">Function Details</a></li></ul><h2><a name="description">Description</a></h2>The classic 'expert system' demonstration game of Animals.
11 <ul class="index"><li><a href="#description">Description</a></li><li><a href="#index">Function Index</a></li><li><a href="#functions">Function Details</a></li></ul>The classic 'expert system' demonstration game of Animals.
12
13
14 <h2><a name="description">Description</a></h2><p>The classic 'expert system' demonstration game of Animals.</p>
915
1016 This game stores a 'knowledge tree' about the animals it knows
1117 in a nested tuple structure. This is mainly to demonstrate how
1420 system, such as Mnesia.
1521
1622 <h2><a name="index">Function Index</a></h2>
17 <table width="100%" border="1"><tr><td valign="top"><a href="#start-0">start/0</a></td><td>Plays a game of Animals.</td></tr>
23 <table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#start-0">start/0</a></td><td>Plays a game of Animals.</td></tr>
1824 </table>
1925
2026 <h2><a name="functions">Function Details</a></h2>
2127
22 <h3><a name="start-0">start/0</a></h3>
23 <p><tt>start() -> ok</tt></p>
24 <p>Plays a game of Animals.</p>
28 <h3 class="function"><a name="start-0">start/0</a></h3>
29 <div class="spec">
30 <p><tt>start() -&gt; ok</tt></p>
31 </div><p>Plays a game of Animals.</p>
32 <hr>
33
34 <div class="navbar"><a name="#navbar_bottom"></a><table width="100%" border="0" cellspacing="0" cellpadding="2" summary="navigation bar"><tr><td><a href="overview-summary.html" target="overviewFrame">Overview</a></td><td><a href="http://www.erlang.org/"><img src="erlang.png" align="right" border="0" alt="erlang logo"></a></td></tr></table></div>
35 <p><i>Generated by EDoc, Apr 29 2010, 14:56:32.</i></p>
2536 </body>
26 </html>
37 </html>
00 {application,animals,
11 [{description,"The game of Animals, a demonstration of an 'expert system'"},
2 {vsn,"2002.0807"},
2 {vsn,"2010.0429"},
33 {modules,[animals]},
44 {registered,[]},
55 {env,[]},
6 {applications,[kernel,stdlib,ce]}]}.
6 {applications,[kernel,stdlib]}]}.
77
Binary diff not shown
0 {question,"Does it fly",{animal,"robin"},{animal,"horse"}}.
00 #!/bin/sh
1 erl -noshell -run animals -run init stop
1 ERL_LIBS=`dirname $0`/../../ erl -noshell -run animals -run init stop
00 %%% BEGIN animals.erl %%%
11 %%%
22 %%% animals - Classic 'expert system' game of Animals, in Erlang
3 %%% Copyright (c)2002 Cat's Eye Technologies. All rights reserved.
3 %%% Copyright (c)2002-2010 Cat's Eye Technologies. All rights reserved.
44 %%%
55 %%% Redistribution and use in source and binary forms, with or without
66 %%% modification, are permitted provided that the following conditions
1818 %%% contributors may be used to endorse or promote products derived
1919 %%% from this software without specific prior written permission.
2020 %%%
21 %%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22 %%% CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23 %%% INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 %%% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 %%% DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
26 %%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27 %%% OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 %%% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
29 %%% OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 %%% ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 %%% OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 %%% OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 %%% POSSIBILITY OF SUCH DAMAGE.
21 %%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 %%% ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 %%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 %%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 %%% COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26 %%% INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 %%% (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 %%% SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 %%% HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 %%% STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 %%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32 %%% OF THE POSSIBILITY OF SUCH DAMAGE.
3433
3534 %% @doc The classic 'expert system' demonstration game of Animals.
3635 %%
4342 %% @end
4443
4544 -module(animals).
46 -vsn('$Id: animals.erl 5 2004-09-20 02:22:25Z catseye $').
47 -author('cpressey@catseye.mine.nu').
48 -copyright('Copyright (c)2002-2004 Cat`s Eye Technologies. All rights reserved.').
45 -vsn('$Id: animals.erl 531 2010-04-29 20:05:21Z cpressey $').
46 -author('cpressey@catseye.tc').
47 -copyright('Copyright (c)2002-2010 Cat`s Eye Technologies. All rights reserved.').
4948
5049 -export([start/0]).
5150
114113 end
115114 end.
116115
117 %%% Utilities
116 %%% High-level Utilities
118117
119118 %% @spec load() -> tree()
120119 %% @doc Loads the animal knowledge tree from <code>animals.dat</code>.
132131 %% @doc Saves the animal knowledge tree to <code>animals.dat</code>.
133132
134133 save(Animals) ->
135 case ce_file:dump(filename:join([code:priv_dir(animals), "animals.dat"]),
134 case file_dump(filename:join([code:priv_dir(animals), "animals.dat"]),
136135 [Animals]) of
137136 {ok, [Animals]} ->
138137 ok;
166165 %% to the given noun.
167166
168167 indefart(Noun) ->
169 case hd(ce_string:uc(Noun)) of
168 case hd(uc(Noun)) of
170169 N when N == $A; N == $E; N == $I; N == $O; N == $U ->
171170 "an " ++ Noun;
172171 _ ->
179178 get_y_or_n(Prompt) ->
180179 io:fwrite("~s", [Prompt]),
181180 Response = io:get_line(''),
182 case string:strip(ce_string:uc(Response)) of
181 case string:strip(uc(Response)) of
183182 "Y" ++ _Remainder ->
184183 true;
185184 "N" ++ _Remainder ->
194193
195194 get_animal_name() ->
196195 io:fwrite("What was the name of the animal you were thinking of? "),
197 case ce_string:chomp(io:get_line('')) of
196 case chomp(io:get_line('')) of
198197 "" ->
199198 io:fwrite("Sorry, I didn't quite catch that.~n"),
200199 get_animal_name();
201200 AnimalName ->
202 ce_string:lc(AnimalName)
201 lc(AnimalName)
203202 end.
204203
205204 get_question() ->
206 case ce_string:chomp(io:get_line('> ')) of
205 case chomp(io:get_line('> ')) of
207206 "" ->
208207 io:fwrite("Sorry, I didn't quite catch that.~n"),
209208 get_question();
211210 strip_question_marks([to_upper(hd(Question)) | tl(Question)])
212211 end.
213212
213 %%% Low-level Utilities
214
214215 strip_question_marks(String) ->
215216 lists:reverse(strip_question_marks0(lists:reverse(String))).
216217
219220 strip_question_marks0(Else) ->
220221 Else.
221222
223 %% @spec chomp(string()) -> string()
224 %% @doc Removes all newlines from the end of a string.
225 %% Should work on both Unix and MS-DOS newlines.
226
227 chomp([]) -> [];
228 chomp(List) when is_list(List) ->
229 lists:reverse(chomp0(lists:reverse(List))).
230 chomp0([]) -> [];
231 chomp0([H | T]) when H == 10; H == 13 -> chomp0(T);
232 chomp0([_ | _]=L) -> L.
233
234 %% @spec uc(string()) -> string()
235 %% @doc Translates a string to uppercase. Also flattens the list.
236
237 uc(L) -> uc(L, []).
238 uc([], A) -> A;
239 uc([H|T], A) -> uc(T, A ++ [uc(H)]);
240 uc(L, _) -> to_upper(L).
241
222242 to_upper(X) when X >= $a, X =< $z -> X + ($A - $a);
223243 to_upper(X) -> X.
224244
245 %% @spec lc(string()) -> string()
246 %% @doc Translates a string to lowercase. Also flattens the list.
247
248 lc(L) -> lc(L, []).
249 lc([], A) -> A;
250 lc([H|T], A) -> lc(T, A ++ [lc(H)]);
251 lc(L, _) -> to_lower(L).
252
253 to_lower(X) when X >= $A, X =< $Z -> X + ($a - $A);
254 to_lower(X) -> X.
255
256 %% @spec file_dump(filename(), [term()]) -> {ok, [term()]} | {error, Reason}
257 %% @doc Writes all terms to a file. Complements file:consult/1.
258
259 file_dump(Filename, List) ->
260 case file:open(Filename, [write]) of
261 {ok, Device} ->
262 lists:foreach(fun(Term) ->
263 io:fwrite(Device, "~p.~n", [Term])
264 end, List),
265 file:close(Device),
266 {ok, List};
267 Other ->
268 Other
269 end.
270
225271 %%% END of animals.erl %%%